--- libeio/eio.c 2011/07/19 04:56:43 1.95 +++ libeio/eio.c 2011/08/03 15:25:38 1.101 @@ -107,7 +107,10 @@ #undef PAGESIZE #define PAGESIZE 4096 /* GetSystemInfo? */ + /* TODO: look at how perl does stat (non-sloppy), unlink (ro-files), utime, link */ + #ifdef EIO_STRUCT_STATI64 + /* look at perl's non-sloppy stat */ #define stat(path,buf) _stati64 (path,buf) #define fstat(fd,buf) _fstati64 (fd,buf) #endif @@ -131,6 +134,37 @@ #define statvfs(path,buf) EIO_ENOSYS () #define fstatvfs(fd,buf) EIO_ENOSYS () + /* rename() uses MoveFile, which fails to overwrite */ + #define rename(old,neu) eio__rename (old, neu) + + static int + eio__rename (const char *old, const char *neu) + { + if (MoveFileEx (old, neu, MOVEFILE_REPLACE_EXISTING)) + return 0; + + /* should steal _dosmaperr */ + switch (GetLastError ()) + { + case ERROR_FILE_NOT_FOUND: + case ERROR_PATH_NOT_FOUND: + case ERROR_INVALID_DRIVE: + case ERROR_NO_MORE_FILES: + case ERROR_BAD_NETPATH: + case ERROR_BAD_NET_NAME: + case ERROR_BAD_PATHNAME: + case ERROR_FILENAME_EXCED_RANGE: + errno = ENOENT; + break; + + default: + errno = EACCES; + break; + } + + return -1; + } + /* we could even stat and see if it exists */ static int symlink (const char *old, const char *neu) @@ -166,7 +200,6 @@ #include #include #include - #include #include #include @@ -200,6 +233,18 @@ #endif +#if HAVE_UTIMES +# include +#endif + +#if HAVE_SYS_SYSCALL_H +# include +#endif + +#if HAVE_SYS_PRCTL_H +# include +#endif + #if HAVE_SENDFILE # if __linux # include @@ -281,11 +326,11 @@ static unsigned int max_poll_time; /* reslock */ static unsigned int max_poll_reqs; /* reslock */ -static volatile unsigned int nreqs; /* reqlock */ -static volatile unsigned int nready; /* reqlock */ -static volatile unsigned int npending; /* reqlock */ -static volatile unsigned int max_idle = 4; /* maximum number of threads that can idle indefinitely */ -static volatile unsigned int idle_timeout = 10; /* number of seconds after which an idle threads exit */ +static unsigned int nreqs; /* reqlock */ +static unsigned int nready; /* reqlock */ +static unsigned int npending; /* reqlock */ +static unsigned int max_idle = 4; /* maximum number of threads that can idle indefinitely */ +static unsigned int idle_timeout = 10; /* number of seconds after which an idle threads exit */ static xmutex_t wrklock; static xmutex_t reslock; @@ -936,6 +981,24 @@ # define fdatasync(fd) fsync (fd) #endif +static int +eio__syncfs (int fd) +{ + int res; + +#if HAVE_SYS_SYNCFS + res = (int)syscall (__NR_syncfs, (int)(fd)); +#else + res = -1; + errno = ENOSYS; +#endif + + if (res < 0 && errno == ENOSYS && fd >= 0) + sync (); + + return res; +} + /* sync_file_range always needs emulation */ static int eio__sync_file_range (int fd, off_t offset, size_t nbytes, unsigned int flags) @@ -1647,6 +1710,7 @@ { dirp = 0; + /* should steal _dosmaperr */ switch (GetLastError ()) { case ERROR_FILE_NOT_FOUND: @@ -1890,6 +1954,10 @@ struct timespec ts; etp_worker *self = (etp_worker *)thr_arg; +#if HAVE_PRCTL_SET_NAME + prctl (PR_SET_NAME, (unsigned long)"eio_thread", 0, 0, 0); +#endif + /* try to distribute timeouts somewhat evenly */ ts.tv_nsec = ((unsigned long)self & 1023UL) * (1000000000UL / 1024UL); @@ -2066,11 +2134,12 @@ case EIO_SYNC: req->result = 0; sync (); break; case EIO_FSYNC: req->result = fsync (req->int1); break; case EIO_FDATASYNC: req->result = fdatasync (req->int1); break; + case EIO_SYNCFS: req->result = eio__syncfs (req->int1); break; + case EIO_SYNC_FILE_RANGE: req->result = eio__sync_file_range (req->int1, req->offs, req->size, req->int2); break; case EIO_MSYNC: req->result = eio__msync (req->ptr2, req->size, req->int1); break; case EIO_MTOUCH: req->result = eio__mtouch (req); break; case EIO_MLOCK: req->result = eio__mlock (req->ptr2, req->size); break; case EIO_MLOCKALL: req->result = eio__mlockall (req->int1); break; - case EIO_SYNC_FILE_RANGE: req->result = eio__sync_file_range (req->int1, req->offs, req->size, req->int2); break; case EIO_FALLOCATE: req->result = eio__fallocate (req->int1, req->int2, req->offs, req->size); break; case EIO_READDIR: eio__scandir (req, self); break; @@ -2161,6 +2230,21 @@ REQ (EIO_MSYNC); req->ptr2 = addr; req->size = length; req->int1 = flags; SEND; } +eio_req *eio_fdatasync (int fd, int pri, eio_cb cb, void *data) +{ + REQ (EIO_FDATASYNC); req->int1 = fd; SEND; +} + +eio_req *eio_syncfs (int fd, int pri, eio_cb cb, void *data) +{ + REQ (EIO_SYNCFS); req->int1 = fd; SEND; +} + +eio_req *eio_sync_file_range (int fd, off_t offset, size_t nbytes, unsigned int flags, int pri, eio_cb cb, void *data) +{ + REQ (EIO_SYNC_FILE_RANGE); req->int1 = fd; req->offs = offset; req->size = nbytes; req->int2 = flags; SEND; +} + eio_req *eio_mtouch (void *addr, size_t length, int flags, int pri, eio_cb cb, void *data) { REQ (EIO_MTOUCH); req->ptr2 = addr; req->size = length; req->int1 = flags; SEND; @@ -2176,21 +2260,11 @@ REQ (EIO_MLOCKALL); req->int1 = flags; SEND; } -eio_req *eio_sync_file_range (int fd, off_t offset, size_t nbytes, unsigned int flags, int pri, eio_cb cb, void *data) -{ - REQ (EIO_SYNC_FILE_RANGE); req->int1 = fd; req->offs = offset; req->size = nbytes; req->int2 = flags; SEND; -} - eio_req *eio_fallocate (int fd, int mode, off_t offset, size_t len, int pri, eio_cb cb, void *data) { REQ (EIO_FALLOCATE); req->int1 = fd; req->int2 = mode; req->offs = offset; req->size = len; SEND; } -eio_req *eio_fdatasync (int fd, int pri, eio_cb cb, void *data) -{ - REQ (EIO_FDATASYNC); req->int1 = fd; SEND; -} - eio_req *eio_close (int fd, int pri, eio_cb cb, void *data) { REQ (EIO_CLOSE); req->int1 = fd; SEND;