6
6
// #include <fcntl.h>
7
7
// #include <fuse.h>
8
8
9
+ #include " encfs.h"
9
10
#include " pthread.h"
10
11
11
12
#include < errno.h>
15
16
#include < fcntl.h>
16
17
#include < fuse.h>
17
18
#include < winioctl.h>
19
+ #include < boost/scoped_array.hpp>
18
20
19
21
void pthread_mutex_init (pthread_mutex_t *mtx, int )
20
22
{
@@ -106,7 +108,7 @@ ssize_t pwrite(int fd, const void *buf, size_t count, __int64 offset)
106
108
107
109
int truncate (const char *path, __int64 length)
108
110
{
109
- int fd = open ( path, O_RDWR);
111
+ int fd = _wopen ( utf8_to_wfn ( path). c_str () , O_RDWR);
110
112
111
113
if (fd < 0 ) return -1 ;
112
114
@@ -283,25 +285,7 @@ utimes(const char *filename, const struct timeval times[2])
283
285
struct _utimbuf tm ;
284
286
tm .actime = times[0 ].tv_sec ;
285
287
tm .modtime = times[1 ].tv_sec ;
286
- return _utime (filename, &tm );
287
- }
288
-
289
- int
290
- my_stat (const char * fn, struct FUSE_STAT * st)
291
- {
292
- char buf[512 ];
293
- _snprintf (buf, 512 , " %s" , fn);
294
- buf[511 ] = 0 ;
295
-
296
- for (char *p = buf; *p; ++p)
297
- if (*p == ' /' )
298
- *p = ' \\ ' ;
299
-
300
- size_t l = strlen (buf);
301
- if (buf[l-1 ] == ' \\ ' )
302
- buf[l-1 ] = 0 ;
303
-
304
- return ::_stati64 (buf, st);
288
+ return _wutime (utf8_to_wfn (filename).c_str (), &tm );
305
289
}
306
290
307
291
int
@@ -338,12 +322,13 @@ set_sparse(HANDLE fd)
338
322
}
339
323
340
324
int
341
- my_open (const char *fn , int flags)
325
+ my_open (const char *fn_utf8 , int flags)
342
326
{
343
- HANDLE f = CreateFile (fn, flags == O_RDONLY ? GENERIC_WRITE : GENERIC_WRITE|GENERIC_READ, FILE_SHARE_DELETE, NULL , OPEN_EXISTING, 0 , NULL );
327
+ std::wstring fn = utf8_to_wfn (fn_utf8);
328
+ HANDLE f = CreateFileW (fn.c_str (), flags == O_RDONLY ? GENERIC_WRITE : GENERIC_WRITE|GENERIC_READ, FILE_SHARE_DELETE, NULL , OPEN_EXISTING, 0 , NULL );
344
329
if (f == INVALID_HANDLE_VALUE) {
345
330
int save_errno = win32_error_to_errno (GetLastError ());
346
- f = CreateFile (fn, flags == O_RDONLY ? GENERIC_WRITE : GENERIC_WRITE|GENERIC_READ, 0 , NULL , OPEN_EXISTING, 0 , NULL );
331
+ f = CreateFileW (fn. c_str () , flags == O_RDONLY ? GENERIC_WRITE : GENERIC_WRITE|GENERIC_READ, 0 , NULL , OPEN_EXISTING, 0 , NULL );
347
332
if (f == INVALID_HANDLE_VALUE) {
348
333
errno = save_errno;
349
334
return -1 ;
@@ -360,12 +345,132 @@ my_open(const char *fn, int flags)
360
345
return fd;
361
346
}
362
347
363
- namespace pthread {
348
+ int
349
+ open (const char *fn, int flags, ...)
350
+ {
351
+ int mode = 0 ;
352
+ va_list ap;
353
+ va_start (ap, flags);
354
+ if (flags & O_CREAT)
355
+ mode = va_arg (ap, int );
356
+ va_end (ap);
357
+ return _wopen (utf8_to_wfn (fn).c_str (), flags, mode);
358
+ }
359
+
360
+ int
361
+ utime (const char *filename, struct utimbuf *times)
362
+ {
363
+ return _wutime (utf8_to_wfn (filename).c_str (), (struct _utimbuf *) times);
364
+ }
365
+
366
+ int
367
+ mkdir (const char *fn, int mode)
368
+ {
369
+ return _wmkdir (utf8_to_wfn (fn).c_str ());
370
+ }
371
+
372
+ int
373
+ rename (const char *oldpath, const char *newpath)
374
+ {
375
+ return _wrename (utf8_to_wfn (oldpath).c_str (), utf8_to_wfn (newpath).c_str ());
376
+ }
377
+
378
+ int
379
+ unlink (const char *path)
380
+ {
381
+ return _wunlink (utf8_to_wfn (path).c_str ());
382
+ }
364
383
365
- int mkdir (const char *fn, int mode)
384
+ int
385
+ rmdir (const char *path)
366
386
{
367
- return :: mkdir (fn);
387
+ return _wrmdir ( utf8_to_wfn (path). c_str ());
368
388
}
369
389
390
+ int
391
+ _stati64 (const char *path, struct _stati64 *buffer)
392
+ {
393
+ std::wstring fn = utf8_to_wfn (path).c_str ();
394
+ if (fn.length () && fn[fn.length ()-1 ] == L' \\ ' )
395
+ fn.resize (fn.length ()-1 );
396
+ return _wstati64 (fn.c_str (), buffer);
397
+ }
398
+
399
+ int
400
+ chmod (const char * path, int mode)
401
+ {
402
+ return _wchmod (utf8_to_wfn (path).c_str (), mode);
403
+ }
404
+
405
+ struct MY_DIR
406
+ {
407
+ HANDLE hff;
408
+ struct dirent ent;
409
+ WIN32_FIND_DATAW wfd;
410
+ int pos;
411
+ };
412
+
413
+ MY_DIR*
414
+ my_opendir (const char *name)
415
+ {
416
+ MY_DIR *dir = (MY_DIR*) malloc (sizeof (MY_DIR));
417
+ if (!dir) {
418
+ errno = ENOMEM;
419
+ return NULL ;
420
+ }
421
+ memset (dir, 0 , sizeof (*dir));
422
+ std::wstring path = utf8_to_wfn (name) + L" \\ *" ;
423
+ dir->hff = FindFirstFileW (path.c_str (), &dir->wfd );
424
+ if (dir->hff == INVALID_HANDLE_VALUE) {
425
+ errno = win32_error_to_errno (GetLastError ());
426
+ free (dir);
427
+ return NULL ;
428
+ }
429
+ return dir;
430
+ }
431
+
432
+ int
433
+ my_closedir (MY_DIR* dir)
434
+ {
435
+ errno = 0 ;
436
+ if (dir && dir->hff != INVALID_HANDLE_VALUE)
437
+ FindClose (dir->hff );
438
+ free (dir);
439
+ return 0 ;
440
+ }
441
+
442
+ void utf8_to_wchar_buf (const char *src, wchar_t *res, int maxlen);
443
+ std::string wchar_to_utf8_cstr (const wchar_t *str);
444
+
445
+ struct dirent *
446
+ my_readdir (MY_DIR* dir)
447
+ {
448
+ errno = EBADF;
449
+ if (!dir) return NULL ;
450
+ errno = 0 ;
451
+ if (dir->pos < 0 ) return NULL ;
452
+ if (dir->pos == 0 ) {
453
+ ++dir->pos ;
454
+ } else if (!FindNextFileW (dir->hff , &dir->wfd )) {
455
+ errno = GetLastError () == ERROR_NO_MORE_FILES ? 0 : win32_error_to_errno (GetLastError ());
456
+ return NULL ;
457
+ }
458
+ std::string path = wchar_to_utf8_cstr (dir->wfd .cFileName );
459
+ strncpy (dir->ent .d_name , path.c_str (), sizeof (dir->ent .d_name ));
460
+ dir->ent .d_name [sizeof (dir->ent .d_name )-1 ] = 0 ;
461
+ dir->ent .d_namlen = strlen (dir->ent .d_name );
462
+ return &dir->ent ;
463
+ }
464
+
465
+ std::wstring
466
+ utf8_to_wfn (const std::string& src)
467
+ {
468
+ int len = src.length ()+1 ;
469
+ boost::scoped_array<wchar_t > buf (new wchar_t [len]);
470
+ utf8_to_wchar_buf (src.c_str (), buf.get (), len);
471
+ for (wchar_t *p = buf.get (); *p; ++p)
472
+ if (*p == L' /' )
473
+ *p = L' \\ ' ;
474
+ return buf.get ();
370
475
}
371
476
0 commit comments