#define EPOC_TIME (116444736000000000ULL)
-struct ustat {
- int64_t st_atime;
- uint32_t st_atime_nsec;
- int64_t st_ctime;
- uint32_t st_ctime_nsec;
- int64_t st_mtime;
- uint32_t st_mtime_nsec;
- gid_t st_gid;
- /* 64bits ino */
- int64_t st_ino;
- mode_t st_mode;
- uint32_t st_nlink;
- uint64_t st_size;
- uid_t st_uid;
- dev_t st_dev;
- dev_t st_rdev;
-};
-
static void cpio_dosmaperr(unsigned long);
-/* Transform 64-bits ino into 32-bits by hashing.
- * You do not forget that really unique number size is 64-bits.
- */
-#define INOSIZE (8*sizeof(ino_t)) /* 32 */
-static __inline ino_t
-getino(struct ustat *ub)
-{
- ULARGE_INTEGER ino64;
-
- ino64.QuadPart = ub->st_ino;
- /* I don't know this hashing is correct way */
- return (ino_t)(ino64.LowPart ^ (ino64.LowPart >> INOSIZE));
-}
-
/*
* Prepend "\\?\" to the path name and convert it to unicode to permit
* an extended-length path for a maximum total path length of 32767
return (ret);
}
-int
-cpio_chdir(const char *path)
-{
- wchar_t *ws;
- int r;
-
- r = SetCurrentDirectoryA(path);
- if (r == 0) {
- if (GetLastError() != ERROR_FILE_NOT_FOUND) {
- cpio_dosmaperr(GetLastError());
- return (-1);
- }
- } else
- return (0);
- ws = permissive_name(path);
- if (ws == NULL) {
- errno = EINVAL;
- return (-1);
- }
- r = SetCurrentDirectoryW(ws);
- free(ws);
- if (r == 0) {
- cpio_dosmaperr(GetLastError());
- return (-1);
- }
- return (0);
-}
-
-int
-cpio_open(const char *path, int flags, ...)
-{
- va_list ap;
- wchar_t *ws;
- int r, pmode;
- DWORD attr;
-
- va_start(ap, flags);
- pmode = va_arg(ap, int);
- va_end(ap);
- ws = NULL;
- if ((flags & ~_O_BINARY) == _O_RDONLY) {
- /*
- * When we open a directory, _open function returns
- * "Permission denied" error.
- */
- attr = GetFileAttributesA(path);
- if (attr == -1 && GetLastError() == ERROR_PATH_NOT_FOUND) {
- ws = permissive_name(path);
- if (ws == NULL) {
- errno = EINVAL;
- return (-1);
- }
- attr = GetFileAttributesW(ws);
- }
- if (attr == -1) {
- cpio_dosmaperr(GetLastError());
- free(ws);
- return (-1);
- }
- if (attr & FILE_ATTRIBUTE_DIRECTORY) {
- HANDLE handle;
-
- if (ws != NULL)
- handle = CreateFileW(ws, 0, 0, NULL,
- OPEN_EXISTING,
- FILE_FLAG_BACKUP_SEMANTICS |
- FILE_ATTRIBUTE_READONLY,
- NULL);
- else
- handle = CreateFileA(path, 0, 0, NULL,
- OPEN_EXISTING,
- FILE_FLAG_BACKUP_SEMANTICS |
- FILE_ATTRIBUTE_READONLY,
- NULL);
- free(ws);
- if (handle == INVALID_HANDLE_VALUE) {
- cpio_dosmaperr(GetLastError());
- return (-1);
- }
- r = _open_osfhandle((intptr_t)handle, _O_RDONLY);
- return (r);
- }
- }
- if (ws == NULL) {
- r = _open(path, flags, pmode);
- if (r < 0 && errno == EACCES && (flags & O_CREAT) != 0) {
- /* simular other POSIX system action to pass a test */
- attr = GetFileAttributesA(path);
- if (attr == -1)
- cpio_dosmaperr(GetLastError());
- else if (attr & FILE_ATTRIBUTE_DIRECTORY)
- errno = EISDIR;
- else
- errno = EACCES;
- return (-1);
- }
- if (r >= 0 || errno != ENOENT)
- return (r);
- ws = permissive_name(path);
- if (ws == NULL) {
- errno = EINVAL;
- return (-1);
- }
- }
- r = _wopen(ws, flags, pmode);
- if (r < 0 && errno == EACCES && (flags & O_CREAT) != 0) {
- /* simular other POSIX system action to pass a test */
- attr = GetFileAttributesW(ws);
- if (attr == -1)
- cpio_dosmaperr(GetLastError());
- else if (attr & FILE_ATTRIBUTE_DIRECTORY)
- errno = EISDIR;
- else
- errno = EACCES;
- }
- free(ws);
- return (r);
-}
-
-ssize_t
-cpio_read(int fd, void *buf, size_t nbytes)
-{
- HANDLE handle;
- DWORD bytes_read, lasterr;
- int r;
-
-#ifdef _WIN64
- if (nbytes > UINT32_MAX)
- nbytes = UINT32_MAX;
-#endif
- if (fd < 0) {
- errno = EBADF;
- return (-1);
- }
- handle = (HANDLE)_get_osfhandle(fd);
- if (GetFileType(handle) == FILE_TYPE_PIPE) {
- DWORD sta;
- if (GetNamedPipeHandleState(
- handle, &sta, NULL, NULL, NULL, NULL, 0) != 0 &&
- (sta & PIPE_NOWAIT) == 0) {
- DWORD avail = -1;
- int cnt = 3;
-
- while (PeekNamedPipe(
- handle, NULL, 0, NULL, &avail, NULL) != 0 &&
- avail == 0 && --cnt)
- Sleep(100);
- if (avail == 0)
- return (0);
- }
- }
- r = ReadFile(handle, buf, (uint32_t)nbytes,
- &bytes_read, NULL);
- if (r == 0) {
- lasterr = GetLastError();
- if (lasterr == ERROR_NO_DATA) {
- errno = EAGAIN;
- return (-1);
- }
- if (lasterr == ERROR_BROKEN_PIPE)
- return (0);
- if (lasterr == ERROR_ACCESS_DENIED)
- errno = EBADF;
- else
- cpio_dosmaperr(lasterr);
- return (-1);
- }
- return ((ssize_t)bytes_read);
-}
-
-/* Convert Windows FILETIME to UTC */
-__inline static void
-fileTimeToUTC(const FILETIME *filetime, time_t *time, long *ns)
-{
- ULARGE_INTEGER utc;
-
- utc.HighPart = filetime->dwHighDateTime;
- utc.LowPart = filetime->dwLowDateTime;
- if (utc.QuadPart >= EPOC_TIME) {
- utc.QuadPart -= EPOC_TIME;
- *time = (time_t)(utc.QuadPart / 10000000); /* milli seconds base */
- *ns = (long)(utc.QuadPart % 10000000) * 100;/* nano seconds base */
- } else {
- *time = 0;
- *ns = 0;
- }
-}
-
-ssize_t
-cpio_write(int fd, const void *buf, size_t nbytes)
-{
- DWORD bytes_written;
-
-#ifdef _WIN64
- if (nbytes > UINT32_MAX)
- nbytes = UINT32_MAX;
-#endif
- if (fd < 0) {
- errno = EBADF;
- return (-1);
- }
- if (!WriteFile((HANDLE)_get_osfhandle(fd), buf, (uint32_t)nbytes,
- &bytes_written, NULL)) {
- DWORD lasterr;
-
- lasterr = GetLastError();
- if (lasterr == ERROR_ACCESS_DENIED)
- errno = EBADF;
- else
- cpio_dosmaperr(lasterr);
- return (-1);
- }
- return (bytes_written);
-}
-
-//#endif
/*
* The following function was modified from PostgreSQL sources and is
* subject to the copyright below.