unsigned long fgetlinecount(FILE * fp){ char * buffer; fpos_t curPos; unsigned long lineCount = 0, bytesRead = 0, i; // Thank you to http://stackoverflow.com/questions/24408006/fastest-way-to-count-number-of-lines struct statfs fsInfo = {0}; int fd = fileno(fp); // Get file descriptor from FILE*. long optimalSize; if(fgetpos(fp, &curPos)==0){ if (fstatfs(fd, &fsInfo) == -1) { // Querying failed! Fall back to a sane value, for example 8kB or 4MB. optimalSize = 4 * 1024 * 1024; } else { optimalSize = fsInfo.f_bsize; } buffer = malloc(optimalSize); while(!feof(fp)){ // size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream); bytesRead = fread(buffer, sizeof(char), optimalSize,fp); for(i=0; i<bytesRead; lineCount += (buffer[i++]=='\n')); } fsetpos(fp, &curPos); } return lineCount; }
PUBLIC enum btrfs_util_error btrfs_util_is_subvolume_fd(int fd) { struct statfs sfs; struct stat st; int ret; ret = fstatfs(fd, &sfs); if (ret == -1) return BTRFS_UTIL_ERROR_STATFS_FAILED; if (sfs.f_type != BTRFS_SUPER_MAGIC) { errno = EINVAL; return BTRFS_UTIL_ERROR_NOT_BTRFS; } ret = fstat(fd, &st); if (ret == -1) return BTRFS_UTIL_ERROR_STAT_FAILED; if (st.st_ino != BTRFS_FIRST_FREE_OBJECTID || !S_ISDIR(st.st_mode)) { errno = EINVAL; return BTRFS_UTIL_ERROR_NOT_SUBVOLUME; } return BTRFS_UTIL_OK; }
int fstatfs_portable(int fd, struct statfs_portable* stat) { struct statfs mips_stat; int ret = fstatfs(fd, &mips_stat); statfs_ntop(&mips_stat, stat); return ret; }
static int verify_smackfs_mnt(const char *mnt) { struct statfs sfbuf; int rc; int fd; fd = open(mnt, O_RDONLY, 0); if (fd < 0) return -1; do { rc = fstatfs(fd, &sfbuf); } while (rc < 0 && errno == EINTR); if (rc == 0) { if ((uint32_t) sfbuf.f_type == (uint32_t) SMACK_MAGIC) { pthread_mutex_lock(&smackfs_mnt_lock); if (smackfs_mnt_dirfd == -1) { smackfs_mnt = strdup(mnt); smackfs_mnt_dirfd = fd; } else { /* Some other thread won the race. */ close(fd); } pthread_mutex_unlock(&smackfs_mnt_lock); return 0; } } close(fd); return -1; }
// TODO: pull this out to per-OS files once they exist static bool useSparseFiles(int fd) { #if defined(__linux__) || defined(__freebsd__) struct statfs fs_stats; int ret = fstatfs(fd, &fs_stats); uassert(16062, "fstatfs failed: " + errnoWithDescription(), ret == 0); #endif #if defined(__linux__) // these are from <linux/magic.h> but that isn't available on all systems # define NFS_SUPER_MAGIC 0x6969 return (fs_stats.f_type == NFS_SUPER_MAGIC); #elif defined(__freebsd__) return (str::equals(fs_stats.f_fstypename, "zfs") || str::equals(fs_stats.f_fstypename, "nfs") || str::equals(fs_stats.f_fstypename, "oldnfs")); #elif defined(__sunos__) // assume using ZFS which is copy-on-write so no benefit to zero-filling // TODO: check which fs we are using like we do elsewhere return true; #else return false; #endif }
/* * Eject the current CD, if there is one. */ int gen_eject(struct wm_drive *d) { /* On some systems, we can check to see if the CD is mounted. */ struct stat stbuf; struct statfs buf; int rval; if (fstat(d->fd, &stbuf) != 0) return (-2); /* Is this a mounted filesystem? */ if (fstatfs(stbuf.st_rdev, &buf) == 0) return (-3); rval = ioctl(d->fd, CDIOCALLOW); if (rval == 0) rval = ioctl(d->fd, CDIOCEJECT); if (rval == 0) rval = ioctl(d->fd, CDIOCPREVENT); if (rval == 0) rval = close(d->fd); if (rval == 0) d->fd = -1; return rval; } /* gen_eject() */
/********************************************************************************************************* ** 函数名称: fstatvfs ** 功能描述: 获得文件系统信息 ** 输 入 : iFd 文件描述符 ** pstatvfs 文件系统信息结构 ** 输 出 : ERROR CODE ** 全局变量: ** 调用模块: *********************************************************************************************************/ int fstatvfs (int iFd, struct statvfs *pstatvfs) { int iRet; struct statfs statfsBuf; if (!pstatvfs) { errno = EINVAL; return (PX_ERROR); } iRet = fstatfs(iFd, &statfsBuf); if (iRet < 0) { return (iRet); } pstatvfs->f_bsize = statfsBuf.f_bsize; pstatvfs->f_frsize = statfsBuf.f_bsize; pstatvfs->f_blocks = statfsBuf.f_blocks; pstatvfs->f_bfree = statfsBuf.f_bfree; pstatvfs->f_bavail = statfsBuf.f_bavail; pstatvfs->f_files = statfsBuf.f_files; pstatvfs->f_ffree = statfsBuf.f_ffree; pstatvfs->f_favail = 0; pstatvfs->f_fsid = statfsBuf.f_fsid.val[0]; pstatvfs->f_flag = statfsBuf.f_flag; pstatvfs->f_namemax = statfsBuf.f_namelen; return (ERROR_NONE); }
/* * rm_overwrite -- * Overwrite the file 3 times with varying bit patterns. * * XXX * This is a cheap way to *really* delete files. Note that only regular * files are deleted, directories (and therefore names) will remain. * Also, this assumes a fixed-block filesystem (like FFS, or a V7 or a * System V filesystem). In a logging filesystem, you'll have to have * kernel support. */ static int rm_overwrite(const char *file, struct stat *sbp) { struct stat sb; struct statfs fsb; off_t len; int bsize, fd, wlen; char *buf = NULL; fd = -1; if (sbp == NULL) { if (lstat(file, &sb)) goto err; sbp = &sb; } if (!S_ISREG(sbp->st_mode)) { warnx("%s: cannot overwrite a non-regular file", file); return (1); } if (sbp->st_nlink > 1) { warnx("%s (inode %ju): not overwritten due to multiple links", file, (uintmax_t)sbp->st_ino); return (0); } if ((fd = open(file, O_WRONLY, 0)) == -1) goto err; if (fstatfs(fd, &fsb) == -1) goto err; bsize = MAX(fsb.f_iosize, 1024); if ((buf = malloc(bsize)) == NULL) err(1, "%s malloc failed", file); #define PASS(byte) { \ memset(buf, byte, bsize); \ for (len = sbp->st_size; len > 0; len -= wlen) { \ wlen = len < bsize ? len : bsize; \ if (write(fd, buf, wlen) != wlen) \ goto err; \ } \ } PASS(0xff); if (fsync(fd) || lseek(fd, (off_t)0, SEEK_SET)) goto err; PASS(0x00); if (fsync(fd) || lseek(fd, (off_t)0, SEEK_SET)) goto err; PASS(0xff); if (!fsync(fd) && !close(fd)) { free(buf); return (1); } err: eval = 1; if (buf) free(buf); if (fd != -1) close(fd); warn("%s", file); return (0); }
int main(int argc, char **argv) { int fd; struct statfs fsinfo; if (argc != 2) { fprintf(stderr, "usage: %s filename\n", argv[0]); return -1; } fd = open(argv[1], O_RDWR); if (fd < 0) { perror("Unable to open file"); return -2; } if (fstatfs(fd, &fsinfo)) { perror("Unable to stat filesystem"); return -3; } if (fsinfo.f_type != 0x482b) { fprintf(stderr, "Not an HFS+ filesystem\n"); return -4; } if (ioctl(fd, HFSPLUS_IOC_BLESS, NULL)) { perror("Unable to bless filesystem"); return -5; } close(fd); return 0; }
TEST(sys_vfs, fstatfs) { struct statfs sb; int fd = open("/proc", O_RDONLY); ASSERT_EQ(0, fstatfs(fd, &sb)); close(fd); Check(sb); }
static uintmax_t stv_fsspace(int fd, unsigned *bs) { uintmax_t bsize, bavail; #if defined(HAVE_SYS_STATVFS_H) struct statvfs fsst; AZ(fstatvfs(fd, &fsst)); bsize = fsst.f_frsize; bavail = fsst.f_bavail; #elif defined(HAVE_SYS_MOUNT_H) || defined(HAVE_SYS_VFS_H) struct statfs fsst; AZ(fstatfs(sc->fd, &fsst)); bsize = fsst.f_bsize; bavail = fsst.f_bavail; #else #error no struct statfs / struct statvfs #endif /* We use units of the larger of filesystem blocksize and pagesize */ if (*bs < bsize) *bs = bsize; xxxassert(*bs % bsize == 0); return (bsize * bavail); }
void order_files (const char *dir, char **basenames, size_t n_basenames) { int dir_fd_open_flags; int dir_fd; struct statfs fs; size_t i; dir_fd_open_flags = O_SEARCH | O_DIRECTORY; #ifdef O_PATH dir_fd_open_flags |= O_PATH; #endif dir_fd = open (dir, dir_fd_open_flags); if (dir_fd < 0) return; if (fstatfs (dir_fd, &fs) < 0) { close (dir_fd); return; } /* Sort files by the physical locations of their first blocks, in an * attempt to minimise disk drive head movements. This assumes that * files are small enough that they are likely to be in one block or * a small number of contiguous blocks, which seems a reasonable * assumption for manual pages. */ physical_offsets = hashtable_create (plain_hashtable_free); for (i = 0; i < n_basenames; ++i) { struct { struct fiemap fiemap; struct fiemap_extent extent; } fm; int fd; fd = openat (dir_fd, basenames[i], O_RDONLY); if (fd < 0) continue; memset (&fm, 0, sizeof (fm)); fm.fiemap.fm_start = 0; fm.fiemap.fm_length = fs.f_bsize; fm.fiemap.fm_flags = 0; fm.fiemap.fm_extent_count = 1; if (ioctl (fd, FS_IOC_FIEMAP, (unsigned long) &fm) == 0) { uint64_t *offset = XMALLOC (uint64_t); *offset = fm.fiemap.fm_extents[0].fe_physical; hashtable_install (physical_offsets, basenames[i], strlen (basenames[i]), offset); } close (fd); } qsort (basenames, n_basenames, sizeof *basenames, compare_physical_offsets); hashtable_free (physical_offsets); physical_offsets = NULL; close (dir_fd); }
int fd_is_temporary_fs(int fd) { struct statfs s; if (fstatfs(fd, &s) < 0) return -errno; return is_temporary_fs(&s); }
int fd_is_network_fs(int fd) { struct statfs s; if (fstatfs(fd, &s) < 0) return -errno; return is_network_fs(&s); }
int main(int argc, const char *argv[]) { int fd; fd = open("/lib/libc.so.7", O_RDONLY); return (fstatfs(fd, &buf)); }
int fd_is_fs_type(int fd, statfs_f_type_t magic_value) { struct statfs s; if (fstatfs(fd, &s) < 0) return -errno; return is_fs_type(&s, magic_value); }
void GonkDiskSpaceWatcher::OnFileCanReadWithoutBlocking(int aFd) { struct fanotify_event_metadata* fem = nullptr; char buf[4096]; struct statfs sfs; int32_t len, rc; do { len = read(aFd, buf, sizeof(buf)); } while(len == -1 && errno == EINTR); // Bail out if the file is busy. if (len < 0 && errno == ETXTBSY) { return; } // We should get an exact multiple of fanotify_event_metadata if (len <= 0 || (len % FAN_EVENT_METADATA_LEN != 0)) { MOZ_CRASH("About to crash: fanotify_event_metadata read error."); } fem = reinterpret_cast<fanotify_event_metadata *>(buf); while (FAN_EVENT_OK(fem, len)) { rc = fstatfs(fem->fd, &sfs); if (rc < 0) { NS_WARNING("Unable to stat fan_notify fd"); } else { bool firstRun = mFreeSpace == UINT64_MAX; mFreeSpace = sfs.f_bavail * sfs.f_bsize; // We change from full <-> free depending on the free space and the // low and high thresholds. // Once we are in 'full' mode we send updates for all size changes with // a minimum of time between messages or when we cross a size change // threshold. if (firstRun) { mIsDiskFull = mFreeSpace <= mLowThreshold; // Always notify the current state at first run. NotifyUpdate(); } else if (!mIsDiskFull && (mFreeSpace <= mLowThreshold)) { mIsDiskFull = true; NotifyUpdate(); } else if (mIsDiskFull && (mFreeSpace > mHighThreshold)) { mIsDiskFull = false; NotifyUpdate(); } else if (mIsDiskFull) { if (mTimeout < TimeStamp::Now() - mLastTimestamp || mSizeDelta < llabs(mFreeSpace - mLastFreeSpace)) { NotifyUpdate(); } } } close(fem->fd); fem = FAN_EVENT_NEXT(fem, len); } }
int nix_bsd_fstatfs(int fd, struct nix_statfs *buf, nix_env_t *env) { #if defined(HAVE_FSTATFS) struct statfs nsfs; #elif defined(HAVE_FSTATVFS) struct statvfs nsfs; #endif int rc; int rfd; if (buf == NULL) { nix_env_set_errno(env, EFAULT); return (-1); } if ((rfd = nix_fd_get(fd)) < 0) { nix_env_set_errno (env, EBADF); return (-1); } #if defined(HAVE_FSTATFS) rc = fstatfs(rfd, &nsfs); #elif defined(HAVE_FSTATVFS) rc = fstatvfs(rfd, &nsfs); #else errno = ENOSYS; rc = -1; #endif if (rc != 0) { nix_env_set_errno (env, errno); return (-1); } __nix_try { #if defined(HAVE_FSTATFS) || defined(HAVE_FSTATVFS) cvt_statfs(&nsfs, buf); #endif #if defined(HAVE_GETEXTMNTENT) rc = setmntpaths_fd(rfd, buf, env); #else rc = 0; #endif } __nix_catch_any { nix_env_set_errno(env, EFAULT); rc = -1; } __nix_end_try return (rc); }
err_t sys_fstatfs(fd_t fd, sys_statfs_t* buf) { err_t err_out; int got; #if SYS_HAS_STATFS struct statfs tmp; got = fstatfs(fd, &tmp); #if defined(__APPLE__) buf->f_bsize = (int64_t)tmp.f_iosize; buf->f_type = safe_inode_cast(tmp.f_type); buf->f_blocks = safe_inode_cast(tmp.f_blocks); buf->f_bfree = safe_inode_cast(tmp.f_bfree); buf->f_bavail = safe_inode_cast(tmp.f_bavail); buf->f_files = safe_inode_cast(tmp.f_files); buf->f_ffree = safe_inode_cast(tmp.f_ffree); buf->f_namelen = safe_inode_cast(MNAMELEN); #else // linux or cygwin // We don't have to deal with possible conversion from signed to unsigned // numbers here, since in linux the field will be set to 0 if it is // undefined for the FS. Since we know the field is >= 0 we can get rid of // all the branching logic that we had for apple buf->f_bsize = (int64_t)tmp.f_bsize; buf->f_type = (uint64_t)tmp.f_type; buf->f_blocks = (uint64_t)tmp.f_blocks; buf->f_bfree = (uint64_t)tmp.f_bfree; buf->f_bavail = (uint64_t)tmp.f_bavail; buf->f_files = (uint64_t)tmp.f_files; buf->f_ffree = (uint64_t)tmp.f_ffree; buf->f_namelen = (uint64_t)tmp.f_namelen; #endif #else // unable to get fstatfs, so set all the fields of the struct to be 0 to say // that we were unable to get any information buf->f_bsize = 0; buf->f_type = 0; buf->f_blocks = 0; buf->f_bfree = 0; buf->f_bavail = 0; buf->f_files = 0; buf->f_ffree = 0; buf->f_namelen = 0; got = ENOSYS; #endif if (got != -1) { err_out = 0; } else{ err_out = errno; } return err_out; }
int fstatvfs(int fd, struct statvfs *p) { struct statfs sb; int ret; ret = fstatfs(fd, &sb); if (ret == 0) cvt_statfs_to_statvfs(&sb, p); return (ret); }
long do_fstatfs(uint32_t arg1, struct statfs* arg2) { long ret; DPRINTF("fstatfs(0x%x, %p)\n", arg1, arg2); ret = get_errno(fstatfs(arg1, arg2)); if(!is_error(ret)) byteswap_statfs(arg2); return ret; }
int fstatvfs(int fd, struct statvfs *vfs) { struct statfs sfs; if (fstatfs(fd, &sfs) == -1) return -1; fs2vfs(vfs, &sfs); return 0; }
/* * rm_overwrite -- * Overwrite the file with varying bit patterns. * * XXX * This is a cheap way to *really* delete files. Note that only regular * files are deleted, directories (and therefore names) will remain. * Also, this assumes a fixed-block file system (like FFS, or a V7 or a * System V file system). In a logging file system, you'll have to have * kernel support. * Returns 1 for success. */ int rm_overwrite(char *file, struct stat *sbp) { struct stat sb, sb2; struct statfs fsb; size_t bsize; int fd; char *buf = NULL; fd = -1; if (sbp == NULL) { if (lstat(file, &sb)) goto err; sbp = &sb; } if (!S_ISREG(sbp->st_mode)) return (1); if (sbp->st_nlink > 1) { warnx("%s (inode %llu): not overwritten due to multiple links", file, (unsigned long long)sbp->st_ino); return (0); } if ((fd = open(file, O_WRONLY|O_NONBLOCK|O_NOFOLLOW, 0)) == -1) goto err; if (fstat(fd, &sb2)) goto err; if (sb2.st_dev != sbp->st_dev || sb2.st_ino != sbp->st_ino || !S_ISREG(sb2.st_mode)) { errno = EPERM; goto err; } if (fstatfs(fd, &fsb) == -1) goto err; bsize = MAXIMUM(fsb.f_iosize, 1024U); if ((buf = malloc(bsize)) == NULL) err(1, "%s: malloc", file); if (!pass(fd, sbp->st_size, buf, bsize)) goto err; if (fsync(fd)) goto err; close(fd); free(buf); return (1); err: warn("%s", file); close(fd); eval = 1; free(buf); return (0); }
int main(int argc, char *argv[]) { struct stat s; struct statfs sf; off_t offset; int fd; blksize_t blksz; off_t sz; int mode; int error; if (argc != 2) { printf("Usage: %s file\n", argv[0]); printf("Punches every other block in the file.\n"); return 1; } fd = open(argv[1], O_WRONLY); if (fd < 0) goto err; error = fstat(fd, &s); if (error) goto err; error = fstatfs(fd, &sf); if (error) goto err; sz = s.st_size; blksz = sf.f_bsize; mode = FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE; for (offset = 0; offset < sz; offset += blksz * 2) { error = fallocate(fd, mode, offset, blksz); if (error) goto err; } error = fsync(fd); if (error) goto err; error = close(fd); if (error) goto err; return 0; err: perror(argv[1]); return 2; }
int32_t omrfile_fstat(struct OMRPortLibrary *portLibrary, intptr_t fd, struct J9FileStat *buf) { struct stat statbuf; #if defined(LINUX) || defined(AIXPPC) || defined(OSX) PlatformStatfs statfsbuf; #endif /* defined(LINUX) || defined(AIXPPC) || defined(OSX) */ int32_t rc = 0; int localfd = (int)fd; Trc_PRT_file_fstat_Entry(fd); portLibrary->error_set_last_error(portLibrary, 0, 0); memset(buf, 0, sizeof(J9FileStat)); if (0 != fstat(localfd - FD_BIAS, &statbuf)) { intptr_t myerror = errno; Trc_PRT_file_fstat_fstatFailed(myerror); setPortableError(portLibrary, fileFStatErrorMsgPrefix, OMRPORT_ERROR_FILE_FSTAT_ERROR, myerror); rc = -1; goto _end; } #if defined(LINUX) || defined(OSX) if (0 != fstatfs(localfd - FD_BIAS, &statfsbuf)) { intptr_t myerror = errno; Trc_PRT_file_fstat_fstatfsFailed(myerror); setPortableError(portLibrary, fileFStatFSErrorMsgPrefix, OMRPORT_ERROR_FILE_FSTATFS_ERROR, myerror); rc = -1; goto _end; } updateJ9FileStat(portLibrary, buf, &statbuf, &statfsbuf); #elif defined(AIXPPC) && !defined(J9OS_I5) /* defined(LINUX) || defined(OSX) */ if (0 != fstatvfs(localfd - FD_BIAS, &statfsbuf)) { intptr_t myerror = errno; Trc_PRT_file_fstat_fstatvfsFailed(myerror); setPortableError(portLibrary, fileFStatVFSErrorMsgPrefix, OMRPORT_ERROR_FILE_FSTATVFS_ERROR, myerror); rc = -1; goto _end; } updateJ9FileStat(portLibrary, buf, &statbuf, &statfsbuf); #else /* defined(AIXPPC) && !defined(J9OS_I5) */ updateJ9FileStat(portLibrary, buf, &statbuf); #endif /* defined(LINUX) || defined(OSX) */ _end: Trc_PRT_file_fstat_Exit(rc); return rc; }
void update_fs_stats() { unsigned int i; struct statfs s; for (i=0; i<16; i++) { if (fs_stats[i].fd <= 0) break; fstatfs(fs_stats[i].fd, &s); fs_stats[i].size = (long long) s.f_blocks * s.f_bsize; /* bfree (root) or bavail (non-roots) ? */ fs_stats[i].avail = (long long) s.f_bavail * s.f_bsize; } }
int main(int ac, char **av) { int lc; /* loop counter */ int i; char *msg; /* message returned from parse_opts */ /* parse standard options */ if ((msg = parse_opts(ac, av, (option_t *) NULL, NULL)) != (char *)NULL) { tst_brkm(TBROK, cleanup, "OPTION PARSING ERROR - %s", msg); } setup(); /* set up the expected errnos */ TEST_EXP_ENOS(exp_enos); /* check looping state if -i option given */ for (lc = 0; TEST_LOOPING(lc); lc++) { /* reset Tst_count in case we are looping. */ Tst_count = 0; /* loop through the test cases */ for (i = 0; i < TST_TOTAL; i++) { TEST(fstatfs(TC[i].fd, TC[i].sbuf)); if (TEST_RETURN != -1) { tst_resm(TFAIL, "call succeeded unexpectedly"); continue; } TEST_ERROR_LOG(TEST_ERRNO); if (TEST_ERRNO == TC[i].error) { tst_resm(TPASS, "expected failure - " "errno = %d : %s", TEST_ERRNO, strerror(TEST_ERRNO)); } else { tst_resm(TFAIL, "unexpected error - %d : %s - " "expected %d", TEST_ERRNO, strerror(TEST_ERRNO), TC[i].error); } } } cleanup(); /*NOTREACHED*/ return 0; }
static int overcommit_file(int fd, size_t size) { struct statfs statfs; int ret; ret = fstatfs(fd, &statfs); if (ret) { warn("fstatfs(%d, &statfs) = %d", fd, ret); return 1; } if (size / statfs.f_bsize >= statfs.f_bavail) return 1; return 0; }
int check_file_btrfs(int fd, int *btrfs) { int ret; struct statfs fs; *btrfs = 0; ret = fstatfs(fd, &fs); if (ret) return errno; if (fs.f_type == BTRFS_SUPER_MAGIC) *btrfs = 1; return ret; }
int main(int ac, char **av) { int lc; int i; char *msg; if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) { tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); } setup(); /* set up the expected errnos */ TEST_EXP_ENOS(exp_enos); for (lc = 0; TEST_LOOPING(lc); lc++) { Tst_count = 0; /* loop through the test cases */ for (i = 0; i < TST_TOTAL; i++) { TEST(fstatfs(TC[i].fd, TC[i].sbuf)); if (TEST_RETURN != -1) { tst_resm(TFAIL, "call succeeded unexpectedly"); continue; } TEST_ERROR_LOG(TEST_ERRNO); if (TEST_ERRNO == TC[i].error) { tst_resm(TPASS, "expected failure - " "errno = %d : %s", TEST_ERRNO, strerror(TEST_ERRNO)); } else { tst_resm(TFAIL, "unexpected error - %d : %s - " "expected %d", TEST_ERRNO, strerror(TEST_ERRNO), TC[i].error); } } } cleanup(); tst_exit(); }