int renameat(int olddirfd, const char *oldpath, int newdirfd, const char *newpath) { int rc; if(olddirfd != newdirfd) { fprintf(stderr, "tup compat renameat error: olddirfd=%i but newdirfd=%i\n", olddirfd, newdirfd); return -1; } dir_mutex_lock(olddirfd); #ifdef _WIN32 wchar_t woldpath[PATH_MAX]; wchar_t wnewpath[PATH_MAX]; MultiByteToWideChar(CP_UTF8, 0, oldpath, -1, woldpath, PATH_MAX); MultiByteToWideChar(CP_UTF8, 0, newpath, -1, wnewpath, PATH_MAX); if(MoveFileEx(woldpath, wnewpath, MOVEFILE_REPLACE_EXISTING)) { rc = 0; } else { rc = -1; } #else rc = rename(oldpath, newpath); #endif dir_mutex_unlock(); return rc; }
FILE *__wrap_tmpfile(void) { static int num = 0; int fd; char filename[64]; FILE *f = NULL; HANDLE h; dir_mutex_lock(tup_top_fd()); snprintf(filename, sizeof(filename), ".tup/tmp/tmpfile-%i", num); filename[sizeof(filename)-1] = 0; num++; /* Need to use CreateFile to be able to set it delete-on-close */ h = CreateFile(filename, GENERIC_WRITE | GENERIC_READ, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_TEMPORARY | FILE_FLAG_DELETE_ON_CLOSE, NULL); if(h == INVALID_HANDLE_VALUE) goto err_out; /* Convert from HANDLE to FILE* */ fd = _open_osfhandle((intptr_t)h, 0); if(fd < 0) goto err_out; f = fdopen(fd, "w+"); if(!f) { if(!close(fd)) { perror("close(fd) in tmpfile()"); goto err_out; } } err_out: dir_mutex_unlock(); return f; }
int readlinkat(int dirfd, const char *pathname, char *buf, size_t bufsiz) { int rc; dir_mutex_lock(dirfd); rc = readlink(pathname, buf, bufsiz); dir_mutex_unlock(); return rc; }
int symlinkat(const char *oldpath, int newdirfd, const char *newpath) { int rc; dir_mutex_lock(newdirfd); rc = symlink(oldpath, newpath); dir_mutex_unlock(); return rc; }
int mkdirat(int dirfd, const char *pathname, mode_t mode) { int rc; if(mode) {/* for win32 */} dir_mutex_lock(dirfd); rc = mkdir(pathname, mode); dir_mutex_unlock(); return rc; }
int faccessat(int dirfd, const char *pathname, int mode, int flags) { int rc; if(flags) {/* No way to access() a symlink? */} dir_mutex_lock(dirfd); rc = access(pathname, mode); dir_mutex_unlock(); return rc; }
int unlinkat(int dirfd, const char *pathname, int flags) { int rc; dir_mutex_lock(dirfd); if(flags == AT_REMOVEDIR) rc = rmdir(pathname); else rc = unlink(pathname); dir_mutex_unlock(); return rc; }
int fstatat(int dirfd, const char *pathname, struct stat *buf, int flags) { int rc; dir_mutex_lock(dirfd); if(flags & AT_SYMLINK_NOFOLLOW) { rc = lstat(pathname, buf); } else { rc = stat(pathname, buf); } dir_mutex_unlock(); return rc; }
int fchmodat(int dirfd, const char *pathname, mode_t mode, int flags) { int rc; dir_mutex_lock(dirfd); if(flags & AT_SYMLINK_NOFOLLOW) { fprintf(stderr, "tup error: fchmodat(AT_SYMLINK_NOFOLLOW) not supported in compat library.\n"); rc = -1; errno = ENOSYS; } else { rc = chmod(pathname, mode); } dir_mutex_unlock(); return rc; }
int utimensat(int dirfd, const char *pathname, const struct timespec times[2], int flags) { int rc; struct timeval tv[2]; tv[0].tv_sec = times[0].tv_sec; tv[0].tv_usec = times[0].tv_nsec / 1000; tv[1].tv_sec = times[1].tv_sec; tv[1].tv_usec = times[1].tv_nsec / 1000; dir_mutex_lock(dirfd); if(flags & AT_SYMLINK_NOFOLLOW) { rc = lutimes(pathname, tv); } else { rc = utimes(pathname, tv); } dir_mutex_unlock(); return rc; }
int fstatat(int dirfd, const char *pathname, struct stat *buf, int flags) { int rc; dir_mutex_lock(dirfd); if(flags & AT_SYMLINK_NOFOLLOW) { #ifdef _WIN32 open_notify(ACCESS_READ, pathname); #endif rc = lstat(pathname, buf); } else { #ifdef _WIN32 open_notify(ACCESS_READ, pathname); #endif rc = stat(pathname, buf); } dir_mutex_unlock(); return rc; }
int renameat(int olddirfd, const char *oldpath, int newdirfd, const char *newpath) { int rc; if(olddirfd != newdirfd) { fprintf(stderr, "tup compat renameat error: olddirfd=%i but newdirfd=%i\n", olddirfd, newdirfd); return -1; } dir_mutex_lock(olddirfd); #ifdef _WIN32 if(MoveFileEx(oldpath, newpath, MOVEFILE_REPLACE_EXISTING)) { rc = 0; } else { rc = -1; } #else rc = rename(oldpath, newpath); #endif dir_mutex_unlock(); return rc; }