__private_extern__ SInt32 _CFGetPathProperties(CFAllocatorRef alloc, char *path, Boolean *exists, SInt32 *posixMode, int64_t *size, CFDateRef *modTime, SInt32 *ownerID, CFArrayRef *dirContents) { Boolean fileExists; Boolean isDirectory = false; if ((exists == NULL) && (posixMode == NULL) && (size == NULL) && (modTime == NULL) && (ownerID == NULL) && (dirContents == NULL)) { // Nothing to do. return 0; } struct statinfo statBuf; if (stat(path, &statBuf) != 0) { // stat failed, but why? if (thread_errno() == ENOENT) { fileExists = false; } else { return thread_errno(); } } else { fileExists = true; isDirectory = ((statBuf.st_mode & S_IFMT) == S_IFDIR); } if (exists != NULL) { *exists = fileExists; } if (posixMode != NULL) { if (fileExists) { *posixMode = statBuf.st_mode; } else { *posixMode = 0; } } if (size != NULL) { if (fileExists) { *size = statBuf.st_size; } else { *size = 0; } } if (modTime != NULL) { if (fileExists) { #if DEPLOYMENT_TARGET_WINDOWS || DEPLOYMENT_TARGET_LINUX struct timespec ts = {statBuf.st_mtime, 0}; #else struct timespec ts = statBuf.st_mtimespec; #endif *modTime = CFDateCreate(alloc, _CFAbsoluteTimeFromFileTimeSpec(ts)); } else { *modTime = NULL; } } if (ownerID != NULL) { if (fileExists) { *ownerID = statBuf.st_uid; } else { *ownerID = -1; } } if (dirContents != NULL) { if (fileExists && isDirectory) { CFMutableArrayRef contents = _CFContentsOfDirectory(alloc, (char *)path, NULL, NULL, NULL); if (contents) { *dirContents = contents; } else { *dirContents = NULL; } } else { *dirContents = NULL; } } return 0; }
__private_extern__ SInt32 _CFGetFileProperties(CFAllocatorRef alloc, CFURLRef pathURL, Boolean *exists, SInt32 *posixMode, int64_t *size, CFDateRef *modTime, SInt32 *ownerID, CFArrayRef *dirContents) { Boolean fileExists; Boolean isDirectory = false; #if DEPLOYMENT_TARGET_WINDOWS struct _stati64 statBuf; int fd = -1; #else struct stat64 statBuf; #endif char path[CFMaxPathSize]; if ((exists == NULL) && (posixMode == NULL) && (size == NULL) && (modTime == NULL) && (ownerID == NULL) && (dirContents == NULL)) { // Nothing to do. return 0; } if (!CFURLGetFileSystemRepresentation(pathURL, true, (uint8_t *)path, CFMaxPathLength)) { return -1; } #if DEPLOYMENT_TARGET_WINDOWS SECURITY_ATTRIBUTES sa; sa.nLength = sizeof(sa); sa.bInheritHandle = TRUE; sa.lpSecurityDescriptor = NULL; HANDLE hFile = CreateFileA(path, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, &sa, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL); if (INVALID_HANDLE_VALUE == hFile) { DWORD error = GetLastError (); return -1; } fd = _open_osfhandle((intptr_t)hFile, _O_RDONLY|CF_OPENFLGS); if (fd < 0) { if (thread_errno() == ENOENT) { fileExists = false; } else { CloseHandle (hFile); return thread_errno(); } } else { if (_fstati64(fd, &statBuf) != 0) { close(fd); // stat failed, but why? if (thread_errno() == ENOENT) { fileExists = false; } else { return thread_errno(); } } else { fileExists = true; // Hack to work around Windows strange stat behavior: DWORD attributes = GetFileAttributesA (path); close(fd); if (attributes & FILE_ATTRIBUTE_DIRECTORY) { statBuf.st_mode &= ~S_IFREG; // Don't claim it's a regular file. statBuf.st_mode |= S_IFDIR; // Properly mark it as a directory } isDirectory = ((statBuf.st_mode & S_IFMT) == S_IFDIR); } } #else if (stat64(path, &statBuf) != 0) { // stat failed, but why? if (thread_errno() == ENOENT) { fileExists = false; } else { return thread_errno(); } } else { fileExists = true; isDirectory = ((statBuf.st_mode & S_IFMT) == S_IFDIR); } #endif if (exists != NULL) { *exists = fileExists; } if (posixMode != NULL) { if (fileExists) { *posixMode = statBuf.st_mode; } else { *posixMode = 0; } } if (size != NULL) { if (fileExists) { *size = statBuf.st_size; } else { *size = 0; } } if (modTime != NULL) { if (fileExists) { #if DEPLOYMENT_TARGET_WINDOWS CFAbsoluteTime theTime = (CFAbsoluteTime)statBuf.st_mtime; #elif DEPLOYMENT_TARGET_LINUX #if defined _BSD_SOURCE || defined _SVID_SOURCE CFAbsoluteTime theTime = (CFAbsoluteTime)statBuf.st_mtim.tv_sec - kCFAbsoluteTimeIntervalSince1970; theTime += (CFAbsoluteTime)statBuf.st_mtim.tv_nsec / 1000000000.0; #else CFAbsoluteTime theTime = (CFAbsoluteTime)statBuf.st_mtime - kCFAbsoluteTimeIntervalSince1970; theTime += (CFAbsoluteTime)statBuf.st_mtimensec / 1000000000.0; #endif /* defined _BSD_SOURCE || defined _SVID_SOURCE */ #else CFAbsoluteTime theTime = (CFAbsoluteTime)statBuf.st_mtimespec.tv_sec - kCFAbsoluteTimeIntervalSince1970; theTime += (CFAbsoluteTime)statBuf.st_mtimespec.tv_nsec / 1000000000.0; #endif *modTime = CFDateCreate(alloc, theTime); } else { *modTime = NULL; } } if (ownerID != NULL) { if (fileExists) { *ownerID = statBuf.st_uid; } else { *ownerID = -1; } } if (dirContents != NULL) { if (fileExists && isDirectory) { CFMutableArrayRef contents = _CFContentsOfDirectory(alloc, path, NULL, pathURL, NULL); if (contents) { *dirContents = contents; } else { *dirContents = NULL; } } else { *dirContents = NULL; } } return 0; }
__private_extern__ SInt32 _CFGetFileProperties(CFAllocatorRef alloc, CFURLRef pathURL, Boolean *exists, SInt32 *posixMode, int64_t *size, CFDateRef *modTime, SInt32 *ownerID, CFArrayRef *dirContents) { Boolean fileExists; Boolean isDirectory = false; struct stat statBuf; char path[CFMaxPathLength]; if ((exists == NULL) && (posixMode == NULL) && (size == NULL) && (modTime == NULL) && (ownerID == NULL) && (dirContents == NULL)) { // Nothing to do. return 0; } if (!CFURLGetFileSystemRepresentation(pathURL, true, path, CFMaxPathLength)) { return -1; } if (stat(path, &statBuf) != 0) { // stat failed, but why? if (thread_errno() == ENOENT) { fileExists = false; } else { return thread_errno(); } } else { fileExists = true; isDirectory = ((statBuf.st_mode & S_IFMT) == S_IFDIR); } if (exists != NULL) { *exists = fileExists; } if (posixMode != NULL) { if (fileExists) { *posixMode = statBuf.st_mode; } else { *posixMode = 0; } } if (size != NULL) { if (fileExists) { *size = statBuf.st_size; } else { *size = 0; } } if (modTime != NULL) { if (fileExists) { CFTimeInterval theTime; theTime = kCFAbsoluteTimeIntervalSince1970 + statBuf.st_mtime; *modTime = CFDateCreate(alloc, theTime); } else { *modTime = NULL; } } if (ownerID != NULL) { if (fileExists) { *ownerID = statBuf.st_uid; } else { *ownerID = -1; } } if (dirContents != NULL) { if (fileExists && isDirectory) { CFMutableArrayRef contents = _CFContentsOfDirectory(alloc, path, NULL, pathURL, NULL); if (contents) { *dirContents = contents; } else { *dirContents = NULL; } } else { *dirContents = NULL; } } return 0; }
__private_extern__ SInt32 _CFGetFileProperties(CFAllocatorRef alloc, CFURLRef pathURL, Boolean *exists, SInt32 *posixMode, int64_t *size, CFDateRef *modTime, SInt32 *ownerID, CFArrayRef *dirContents) { Boolean fileExists; Boolean isDirectory = false; #if DEPLOYMENT_TARGET_WINDOWS struct _stati64 statBuf; int fd = -1; #else struct stat64 statBuf; #endif char path[CFMaxPathSize]; if ((exists == NULL) && (posixMode == NULL) && (size == NULL) && (modTime == NULL) && (ownerID == NULL) && (dirContents == NULL)) { // Nothing to do. return 0; } if (!CFURLGetFileSystemRepresentation(pathURL, true, (uint8_t *)path, CFMaxPathLength)) { return -1; } #if DEPLOYMENT_TARGET_WINDOWS HANDLE hFile = CreateFileA(path, GENERIC_READ, FILE_SHARE_READ, 0, OPEN_EXISTING, 0, 0); if (INVALID_HANDLE_VALUE == hFile) return -1; fd = _open_osfhandle((intptr_t)hFile, _O_RDONLY|CF_OPENFLGS); if (fd < 0) { if (thread_errno() == ENOENT) { fileExists = false; } else { CloseHandle (hFile); return thread_errno(); } } else { if (_fstati64(fd, &statBuf) != 0) { close(fd); CloseHandle (hFile); // stat failed, but why? if (thread_errno() == ENOENT) { fileExists = false; } else { return thread_errno(); } } else { close(fd); CloseHandle (hFile); fileExists = true; isDirectory = ((statBuf.st_mode & S_IFMT) == S_IFDIR); } } #else if (stat64(path, &statBuf) != 0) { // stat failed, but why? if (thread_errno() == ENOENT) { fileExists = false; } else { return thread_errno(); } } else { fileExists = true; isDirectory = ((statBuf.st_mode & S_IFMT) == S_IFDIR); } #endif if (exists != NULL) { *exists = fileExists; } if (posixMode != NULL) { if (fileExists) { *posixMode = statBuf.st_mode; } else { *posixMode = 0; } } if (size != NULL) { if (fileExists) { *size = statBuf.st_size; } else { *size = 0; } } if (modTime != NULL) { if (fileExists) { #if DEPLOYMENT_TARGET_WINDOWS CFAbsoluteTime theTime = (CFAbsoluteTime)statBuf.st_mtime; #elif DEPLOYMENT_TARGET_LINUX #if defined _BSD_SOURCE || defined _SVID_SOURCE CFAbsoluteTime theTime = (CFAbsoluteTime)statBuf.st_mtim.tv_sec - kCFAbsoluteTimeIntervalSince1970; theTime += (CFAbsoluteTime)statBuf.st_mtim.tv_nsec / 1000000000.0; #else CFAbsoluteTime theTime = (CFAbsoluteTime)statBuf.st_mtime - kCFAbsoluteTimeIntervalSince1970; theTime += (CFAbsoluteTime)statBuf.st_mtimensec / 1000000000.0; #endif /* defined _BSD_SOURCE || defined _SVID_SOURCE */ #else CFAbsoluteTime theTime = (CFAbsoluteTime)statBuf.st_mtimespec.tv_sec - kCFAbsoluteTimeIntervalSince1970; theTime += (CFAbsoluteTime)statBuf.st_mtimespec.tv_nsec / 1000000000.0; #endif *modTime = CFDateCreate(alloc, theTime); } else { *modTime = NULL; } } if (ownerID != NULL) { if (fileExists) { *ownerID = statBuf.st_uid; } else { *ownerID = -1; } } if (dirContents != NULL) { if (fileExists && isDirectory) { CFMutableArrayRef contents = _CFContentsOfDirectory(alloc, path, NULL, pathURL, NULL); if (contents) { *dirContents = contents; } else { *dirContents = NULL; } } else { *dirContents = NULL; } } return 0; }