bool os_fstat(int fd, const char* path, pony_stat_t* p) { #if defined(PLATFORM_IS_WINDOWS) HANDLE h = (HANDLE)_get_osfhandle(fd); BY_HANDLE_FILE_INFORMATION fa; if(!GetFileInformationByHandle(h, &fa)) return false; struct __stat64 st; if(_fstat64(fd, &st) != 0) return false; windows_stat(path, p, &st, fa.dwFileAttributes, &fa.ftLastAccessTime, &fa.ftLastWriteTime, &fa.ftCreationTime); return true; #elif defined(PLATFORM_IS_POSIX_BASED) (void)path; struct stat st; if(fstat(fd, &st) != 0) return false; unix_stat(p, &st); return true; #endif }
int util_fstat(int fileno, stat_type * stat_info) { #ifdef WINDOWS_LFS return _fstat64(fileno , stat_info); #else return fstat(fileno , stat_info); #endif }
bool ON::GetFileStats( FILE* fp, size_t* filesize, time_t* create_time, time_t* lastmodify_time ) { bool rc = false; if (filesize) *filesize = 0; if (create_time) *create_time = 0; if (lastmodify_time) *lastmodify_time = 0; if ( fp ) { #if defined(ON_COMPILER_MSC) // Microsoft compilers int fd = _fileno(fp); #if (_MSC_VER >= 1400) // VC 8 (2005) // works for file sizes > 4GB // when size_t is a 64 bit integer struct _stat64 sb; memset(&sb,0,sizeof(sb)); int fstat_rc = _fstat64(fd, &sb); #else // VC6 compiler // works on most compilers struct _stat sb; memset(&sb,0,sizeof(sb)); int fstat_rc = _fstat(fd, &sb); #endif #else // works on most compilers int fd = fileno(fp); struct stat sb; memset(&sb,0,sizeof(sb)); int fstat_rc = fstat(fd, &sb); #endif if (0 == fstat_rc) { if (filesize) *filesize = (size_t)sb.st_size; if (create_time) *create_time = (time_t)sb.st_ctime; if (lastmodify_time) *lastmodify_time = (time_t)sb.st_mtime; rc = true; } } return rc; }
size_t file_storage::file_size(int file_handle) { if (file_handle == INVALID_HANDLE) return 0; // This is required because off_t is defined as long, which is 32 bits in // msvc and 64 bits in linux/osx, and stat contains off_t. #ifdef _WIN32 #ifdef _WIN64 struct _stat64 sbuf; if (_fstat64(file_handle, &sbuf) == FAIL) return 0; #else struct _stat32 sbuf; if (_fstat32(file_handle, &sbuf) == FAIL) return 0; #endif #else // Limited to 32 bit files on 32 bit systems, see linux.die.net/man/2/open struct stat sbuf; if (fstat(file_handle, &sbuf) == FAIL) return 0; #endif // Convert signed to unsigned size. BITCOIN_ASSERT_MSG(sbuf.st_size > 0, "File size cannot be 0 bytes."); return static_cast<size_t>(sbuf.st_size); }
PassRefPtr<SharedBuffer> SharedBuffer::createWithContentsOfFile(const String& filePath) { String nullifiedPath = filePath; FILE* fileDescriptor = 0; if (_wfopen_s(&fileDescriptor, nullifiedPath.charactersWithNullTermination(), TEXT("r+b")) || !fileDescriptor) { LOG_ERROR("Failed to open file %s to create shared buffer", filePath.ascii().data()); return 0; } RefPtr<SharedBuffer> result; // Stat the file to get its size struct _stat64 fileStat; if (_fstat64(_fileno(fileDescriptor), &fileStat)) goto exit; result = new SharedBuffer(); result->m_buffer.resize(fileStat.st_size); if (result->m_buffer.size() != fileStat.st_size) { result = 0; goto exit; } if (fread(result->m_buffer.data(), 1, fileStat.st_size, fileDescriptor) != fileStat.st_size) LOG_ERROR("Failed to fully read contents of file %s - errno(%i)", filePath.ascii().data(), errno); exit: fclose(fileDescriptor); return result.release(); }
file::offset_type ufs_file_base::_size() { #ifdef BOOST_MSVC struct _stat64 st; stxxl_check_ge_0(_fstat64(file_des, &st), io_error); #else struct stat st; stxxl_check_ge_0(::fstat(file_des, &st), io_error); #endif return st.st_size; }
int64_t GetFileSize(void* fp) { int fd = _fileno((FILE*)fp); #if defined(TRINITY_PLATFORM_WINDOWS) struct __stat64 buf; if (_fstat64(fd, &buf)) { return -1; } else return buf.st_size; #else struct stat64 buf; if (fstat64(fd, &buf)) { return -1; } else return buf.st_size; #endif }
static int64_t wfile_seek(URLContext *h, int64_t pos, int whence) { #if defined(_MSC_VER) int fd = (intptr_t) h->priv_data; if (whence == AVSEEK_SIZE) { struct _stat64 st; int ret = _fstat64(fd, &st); return ret < 0 ? AVERROR(errno) : st.st_size; } return _lseeki64(fd, pos, whence); #else int fd = (intptr_t) h->priv_data; if (whence == AVSEEK_SIZE) { struct stat st; int ret = fstat(fd, &st); return ret < 0 ? AVERROR(errno) : st.st_size; } return _lseeki64(fd, pos, whence); #endif }
int __cdecl fstat(int _Desc,struct stat *_Stat) { struct _stat64 st; int ret=_fstat64(_Desc,&st); if (ret == -1) { memset(_Stat,0,sizeof(struct stat)); return -1; } /* struct stat and struct _stat64i32 are the same for this case. */ _Stat->st_dev=st.st_dev; _Stat->st_ino=st.st_ino; _Stat->st_mode=st.st_mode; _Stat->st_nlink=st.st_nlink; _Stat->st_uid=st.st_uid; _Stat->st_gid=st.st_gid; _Stat->st_rdev=st.st_rdev; _Stat->st_size=(_off_t) st.st_size; _Stat->st_atime=st.st_atime; _Stat->st_mtime=st.st_mtime; _Stat->st_ctime=st.st_ctime; return ret; }
static int _g_win32_stat_utf16_no_trailing_slashes (const gunichar2 *filename, int fd, GWin32PrivateStat *buf, gboolean for_symlink) { HANDLE file_handle; gboolean succeeded_so_far; DWORD error_code; struct __stat64 statbuf; BY_HANDLE_FILE_INFORMATION handle_info; FILE_STANDARD_INFO std_info; WIN32_FIND_DATAW finddata; DWORD immediate_attributes; gboolean is_symlink = FALSE; gboolean is_directory; DWORD open_flags; wchar_t *filename_target = NULL; int result; if (fd < 0) { immediate_attributes = GetFileAttributesW (filename); if (immediate_attributes == INVALID_FILE_ATTRIBUTES) { error_code = GetLastError (); errno = w32_error_to_errno (error_code); return -1; } is_symlink = (immediate_attributes & FILE_ATTRIBUTE_REPARSE_POINT) == FILE_ATTRIBUTE_REPARSE_POINT; is_directory = (immediate_attributes & FILE_ATTRIBUTE_DIRECTORY) == FILE_ATTRIBUTE_DIRECTORY; open_flags = FILE_ATTRIBUTE_NORMAL; if (for_symlink && is_symlink) open_flags |= FILE_FLAG_OPEN_REPARSE_POINT; if (is_directory) open_flags |= FILE_FLAG_BACKUP_SEMANTICS; file_handle = CreateFileW (filename, FILE_READ_ATTRIBUTES, FILE_SHARE_READ, NULL, OPEN_EXISTING, open_flags, NULL); if (file_handle == INVALID_HANDLE_VALUE) { error_code = GetLastError (); errno = w32_error_to_errno (error_code); return -1; } } else { file_handle = (HANDLE) _get_osfhandle (fd); if (file_handle == INVALID_HANDLE_VALUE) return -1; } succeeded_so_far = GetFileInformationByHandle (file_handle, &handle_info); error_code = GetLastError (); if (succeeded_so_far) { succeeded_so_far = GetFileInformationByHandleEx (file_handle, FileStandardInfo, &std_info, sizeof (std_info)); error_code = GetLastError (); } if (!succeeded_so_far) { if (fd < 0) CloseHandle (file_handle); errno = w32_error_to_errno (error_code); return -1; } /* It's tempting to use GetFileInformationByHandleEx(FileAttributeTagInfo), * but it always reports that the ReparseTag is 0. */ if (fd < 0) { memset (&finddata, 0, sizeof (finddata)); if (handle_info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) { HANDLE tmp = FindFirstFileW (filename, &finddata); if (tmp == INVALID_HANDLE_VALUE) { error_code = GetLastError (); errno = w32_error_to_errno (error_code); CloseHandle (file_handle); return -1; } FindClose (tmp); } if (is_symlink && !for_symlink) { /* If filename is a symlink, but we need the target. * To get information about the target we need to resolve * the symlink first. */ DWORD filename_target_len; DWORD new_len; /* Just in case, give it a real memory location instead of NULL */ new_len = GetFinalPathNameByHandleW (file_handle, (wchar_t *) &filename_target_len, 0, FILE_NAME_NORMALIZED); #define SANE_LIMIT 1024 * 10 if (new_len >= SANE_LIMIT) #undef SANE_LIMIT { new_len = 0; error_code = ERROR_BUFFER_OVERFLOW; } else if (new_len == 0) { error_code = GetLastError (); } if (new_len > 0) { /* Pretend that new_len doesn't count the terminating NUL char, * and ask for a bit more space than is needed, and allocate even more. */ filename_target_len = new_len + 3; filename_target = g_malloc ((filename_target_len + 1) * sizeof (wchar_t)); new_len = GetFinalPathNameByHandleW (file_handle, filename_target, filename_target_len, FILE_NAME_NORMALIZED); /* filename_target_len is already larger than needed, * new_len should be smaller than that, even if the size * is off by 1 for some reason. */ if (new_len >= filename_target_len - 1) { new_len = 0; error_code = ERROR_BUFFER_OVERFLOW; g_clear_pointer (&filename_target, g_free); } else if (new_len == 0) { g_clear_pointer (&filename_target, g_free); } /* GetFinalPathNameByHandle() is documented to return extended paths, * strip the extended prefix, if it is followed by a drive letter * and a colon. Otherwise keep it (the path could be * \\\\?\\Volume{GUID}\\ - it's only usable in extended form). */ else if (new_len > 0) { gsize len = new_len; /* Account for NUL-terminator maybe not being counted. * This is why we overallocated earlier. */ if (filename_target[len] != L'\0') { len++; filename_target[len] = L'\0'; } _g_win32_strip_extended_ntobjm_prefix (filename_target, &len); new_len = len; } } if (new_len == 0) succeeded_so_far = FALSE; } CloseHandle (file_handle); } /* else if fd >= 0 the file_handle was obtained via _get_osfhandle() * and must not be closed, it is owned by fd. */ if (!succeeded_so_far) { errno = w32_error_to_errno (error_code); return -1; } /* * We can't use _wstat64() here, because with UCRT it now gives * information about the target, even if we want information about * the link itself (unlike MSVCRT, which gave information about * the link, and if we needed information about the target we were * able to resolve it by ourselves prior to calling _wstat64()). */ if (fd < 0) result = _g_win32_fill_statbuf_from_handle_info (filename, filename_target, &handle_info, &statbuf); else result = _fstat64 (fd, &statbuf); if (result != 0) { int errsv = errno; g_free (filename_target); errno = errsv; return -1; } g_free (filename_target); buf->st_dev = statbuf.st_dev; buf->st_mode = statbuf.st_mode; buf->volume_serial = handle_info.dwVolumeSerialNumber; buf->file_index = (((guint64) handle_info.nFileIndexHigh) << 32) | handle_info.nFileIndexLow; /* Note that immediate_attributes is for the symlink * (if it's a symlink), while handle_info contains info * about the symlink or the target, depending on the flags * we used earlier. */ buf->attributes = handle_info.dwFileAttributes; buf->st_nlink = handle_info.nNumberOfLinks; buf->st_size = (((guint64) handle_info.nFileSizeHigh) << 32) | handle_info.nFileSizeLow; buf->allocated_size = std_info.AllocationSize.QuadPart; if (fd < 0 && buf->attributes & FILE_ATTRIBUTE_REPARSE_POINT) buf->reparse_tag = finddata.dwReserved0; else buf->reparse_tag = 0; buf->st_ctime = statbuf.st_ctime; buf->st_atime = statbuf.st_atime; buf->st_mtime = statbuf.st_mtime; return 0; }
void DumpFileToLog( char* path ) { FILE* fp; char buf[BUFFER_SIZE]; char* p; fp = fopen_unsafe(path, "r"); if (fp == NULL) { LogError("ERROR: DumpFileToLog couldn't open file '%s' with error '%s'", path, strerror_unsafe(errno)); } else { int fd = _fileno(fp); struct _stat64 fileStats; if (fd != -1 && _fstat64(fd, &fileStats) != -1) { char creationTime[256]; char accessTime[256]; char currTime[256]; __time64_t now = _time64(NULL); _ctime64_s(currTime, &now); _ctime64_s(creationTime, &fileStats.st_ctime); _ctime64_s(accessTime, &fileStats.st_atime); auto stripNewline = [](char *buf) { if (char *ptr = strchr(buf, '\n')) *ptr = '\0'; }; stripNewline(creationTime); stripNewline(accessTime); stripNewline(currTime); LogOut("ERROR: name of output file: %s; size: %I64d; creation: %s, last access: %s, now: %s", path, fileStats.st_size, creationTime, accessTime, currTime); } if (!FNoProgramOutput) { bool printlines = !FOnlyAssertOutput; if (printlines) { LogOut("ERROR: bad output file follows ============"); } while (fgets(buf, BUFFER_SIZE, fp) != NULL) { // Strip the newline, since LogOut adds one p = strchr(buf, '\n'); if (p != NULL) { *p = '\0'; } if (!printlines && strlen(buf) > 8 && buf[0] == 'A' && buf[1] == 'S' && buf[2] == 'S' && buf[3] == 'E' && buf[4] == 'R' && buf[5] == 'T') { printlines = true; LogOut("ERROR: bad output file follows ============"); } if (printlines) { LogOut("%s", buf); } } if (printlines) { LogOut("ERROR: end of bad output file ============"); } } fclose(fp); } }
int padding_decoder(char *file_name, unsigned long long content_length) { int fp_in; unsigned long long f_size; #ifdef _MSC_VER struct __stat64 file_stats; #else struct stat64 file_stats; #endif char file_name_in[256] = ""; char file_name_out[256] = ""; char *ptr; int retval; strcpy(file_name_in, file_name); ptr = strstr(file_name_in, PAD_SUFFIX); memcpy(file_name_out, file_name_in, (ptr - file_name_in)); #ifdef _MSC_VER fp_in = open((const char*)file_name_in, _O_RDWR | _O_CREAT | _O_BINARY, _S_IREAD | _S_IWRITE); #else fp_in = open64(file_name_in, O_RDWR | O_CREAT, S_IRWXU); #endif if(fp_in == -1) { printf("open error: %s\n", file_name_in); return -1; } #ifdef _MSC_VER _fstat64(fp_in, &file_stats); #else fstat64(fp_in, &file_stats); #endif f_size = file_stats.st_size; if(f_size > content_length) { #ifdef _MSC_VER retval = _chsize(fp_in, (long)content_length); /* TODO: 64 bits, how ??? */ #else retval = ftruncate64(fp_in, content_length); #endif if(retval != 0) { printf("Problem in padding decoding.\n" ); close(fp_in); return -1; } } close(fp_in); if(rename(file_name_in, file_name_out) < 0) { if(errno == EEXIST) { retval = remove(file_name_out); if(retval == -1) { printf("errno: %i\n", errno); fflush(stdout); close(fp_in); return -1; } if(rename(file_name_in, file_name_out) < 0) { printf("rename() error1: %s\n", file_name_in); fflush(stdout); close(fp_in); return -1; } } else { printf("rename() error2: %s\n", file_name_in); fflush(stdout); close(fp_in); return -1; } } return 0; }
bool RAUX::File :: Exists () const { #if defined ( __APPLE__ ) struct stat FStatData; #elif defined ( _WIN32 ) struct __stat64 FStatData; #else struct stat64 FStatData; #endif int ReturnCode; if ( Opened ) { #ifdef _WIN32 int FD = _fileno ( Handle ); #else int FD = fileno ( Handle ); #endif if ( FD == - 1 ) return false; #if defined ( __APPLE__ ) ReturnCode = fstat ( FD, & FStatData ); #elif defined ( _WIN32 ) ReturnCode = _fstat64 ( FD, & FStatData ); #else ReturnCode = fstat64 ( FD, & FStatData ); #endif } else { #if defined ( __APPLE__ ) ReturnCode = stat ( Name.c_str (), & FStatData ); #elif defined ( _WIN32 ) ReturnCode = _stat64 ( Name.c_str (), & FStatData ); #else ReturnCode = stat64 ( Name.c_str (), & FStatData ); #endif } if ( ReturnCode == - 1 ) return false; return true; }
int64_t RAUX::File :: GetLength ( uint32_t * Status ) const { * Status = kStatus_Success; #if defined ( __APPLE__ ) struct stat FStatData; #elif defined ( _WIN32 ) struct __stat64 FStatData; #else struct stat64 FStatData; #endif int ReturnCode; if ( Opened ) { #ifdef _WIN32 int FD = _fileno ( Handle ); #else int FD = fileno ( Handle ); #endif if ( FD == - 1 ) { * Status = kStatus_Failure_FileNotOpen; return - 1; } #if defined ( __APPLE__ ) ReturnCode = fstat ( FD, & FStatData ); #elif defined ( _WIN32 ) ReturnCode = _fstat64 ( FD, & FStatData ); #else ReturnCode = fstat64 ( FD, & FStatData ); #endif } else { #if defined ( __APPLE__ ) ReturnCode = stat ( Name.c_str (), & FStatData ); #elif defined ( _WIN32 ) ReturnCode = _stat64 ( Name.c_str (), & FStatData ); #else ReturnCode = stat64 ( Name.c_str (), & FStatData ); #endif } if ( ReturnCode == - 1 ) * Status = kStatus_Failure_Unknown; return FStatData.st_size; }
/* Retrieves the size of the file * This function uses the POSIX fstat function or equivalent * Returns 1 if successful or -1 on error */ int libcfile_stream_get_size( libcfile_stream_t *stream, size64_t *size, libcerror_error_t **error ) { #if defined( _MSC_VER ) struct __stat64 file_statistics; #elif defined( __BORLANDC__ ) struct stati64 file_statistics; #else struct stat file_statistics; #endif libcfile_internal_stream_t *internal_stream = NULL; static char *function = "libcfile_stream_get_size"; size_t file_statistics_size = 0; int file_descriptor = 0; #if defined( S_ISBLK ) && defined( S_ISCHR ) off64_t current_offset = 0; off64_t offset = 0; #endif if( stream == NULL ) { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_ARGUMENTS, LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, "%s: invalid stream.", function ); return( -1 ); } internal_stream = (libcfile_internal_stream_t *) stream; if( internal_stream->stream == NULL ) { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_RUNTIME, LIBCERROR_RUNTIME_ERROR_VALUE_MISSING, "%s: invalid stream - missing stream.", function ); return( -1 ); } if( size == NULL ) { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_ARGUMENTS, LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, "%s: invalid size.", function ); return( -1 ); } #if defined( _MSC_VER ) file_statistics_size = sizeof( struct __stat64 ); #elif defined( __BORLANDC__ ) file_statistics_size = sizeof( struct stati64 ); #else file_statistics_size = sizeof( struct stat ); #endif if( memory_set( &file_statistics, 0, file_statistics_size ) == NULL ) { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_MEMORY, LIBCERROR_MEMORY_ERROR_SET_FAILED, "%s: unable to clear file statistics.", function ); return( -1 ); } #if defined( WINAPI ) file_descriptor = _fileno( internal_stream->stream ); #else file_descriptor = fileno( internal_stream->stream ); #endif if( file_descriptor == -1 ) { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_RUNTIME, LIBCERROR_RUNTIME_ERROR_GET_FAILED, "%s: unable to retrieve file descriptor of stream.", function ); return( -1 ); } #if defined( _MSC_VER ) if( _fstat64( file_descriptor, &file_statistics ) != 0 ) #elif defined( __BORLANDC__ ) if( _fstati64( file_descriptor, &file_statistics ) != 0 ) #else if( fstat( file_descriptor, &file_statistics ) != 0 ) #endif { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_RUNTIME, LIBCERROR_RUNTIME_ERROR_GET_FAILED, "%s: unable to retrieve file statistics.", function ); return( -1 ); } /* TODO implement device support ? */ /* TODO does not work on Mac OS X or WINAPI */ #if defined( S_ISBLK ) && defined( S_ISCHR ) if( S_ISBLK( file_statistics.st_mode ) || S_ISCHR( file_statistics.st_mode ) ) { if( libcfile_stream_get_offset( stream, ¤t_offset, error ) != 1 ) { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_RUNTIME, LIBCERROR_RUNTIME_ERROR_GET_FAILED, "%s: unable to retrieve current offset.", function ); return( -1 ); } /* If the file is a device try to seek the end of the file */ offset = libcfile_stream_seek_offset( stream, 0, SEEK_END, error ); if( offset == -1 ) { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_IO, LIBCERROR_IO_ERROR_SEEK_FAILED, "%s: unable to seek end of file.", function ); return( -1 ); } *size = (size64_t) offset; offset = libcfile_stream_seek_offset( stream, current_offset, SEEK_SET, error ); if( offset == -1 ) { libcerror_error_set( error, LIBCERROR_ERROR_DOMAIN_IO, LIBCERROR_IO_ERROR_SEEK_FAILED, "%s: unable to seek offset: %" PRIi64 ".", function, current_offset ); return( -1 ); } } else #endif { *size = (size64_t) file_statistics.st_size; } return( 1 ); }
int __cdecl __imp___fstat64(__in int _FileDes, __out struct _stat64 * _Stat) { return _fstat64(_FileDes, _Stat); }