www.pudn.com > isync2[1].0.rar > show_free.cpp
#include#include #include #include #include #include #include #ifndef CHAR_BIT # define CHAR_BIT 8 #endif #define ME_DUMMY(fs_name, fs_type) (!strcmp(fs_type, "auto") || !strcmp(fs_type, "ignore")) #define ME_REMOTE(fs_name, fs_type) (strchr (fs_name, ':') != 0) #define PROPAGATE_ALL_ONES(x) ((x) == -1 ? (uintmax_t)-1 : (uintmax_t)(x)) #define EXTRACT_TOP_BIT(x) ((x) & ((uintmax_t) 1 << (sizeof (x) * CHAR_BIT - 1))) #define PROPAGATE_TOP_BIT(x) ((x) | ~ (EXTRACT_TOP_BIT (x) - 1)) struct mount_entry { char *me_devname; // Device node pathname, including "/dev/". char *me_mountdir; // Mount point directory pathname. char *me_type; // "nfs", "4.2", etc. dev_t me_dev; // Device number of me_mountdir. unsigned int me_dummy : 1; // Nonzero for dummy filesystems. unsigned int me_remote : 1; // Nonzero for remote fileystems. }; struct fs_usage { int fsu_blocksize; // Size of a block. uintmax_t fsu_blocks; // Total blocks. uintmax_t fsu_bfree; // Free blocks available to superuser. uintmax_t fsu_bavail; // Free blocks available to non-superuser. int fsu_bavail_top_bit_set; // 1 if fsu_bavail represents a value < 0. uintmax_t fsu_files; // Total file nodes. uintmax_t fsu_ffree; // Free file nodes. }; size_t show_free(char *,char *); void read_filesystem_list(char *,char *); size_t get_free(const char *disk, const char *mount_point, const char *fstype, int me_dummy, int me_remote); int get_fs_usage(const char *path, const char *disk, fs_usage *fsp); static mount_entry *mount_list; void read_filesystem_list(char * device,char *mount_point)//device=null,mountdir起作用,mountdir=null,device起作用 { mount_list = new mount_entry; mount_list->me_devname = device; mount_list->me_mountdir = mount_point; mount_list->me_type = "nfs"; mount_list->me_dummy = ME_DUMMY(mount_list->me_devname, mount_list->me_type); mount_list->me_remote = ME_REMOTE(mount_list->me_devname, mount_list->me_type); mount_list->me_dev = (dev_t)-1; return; } int get_fs_usage(const char *path, const char *disk, fs_usage *fsp) { struct statvfs fsd; if (statvfs (path, &fsd) < 0) return -1; fsp->fsu_blocksize =PROPAGATE_ALL_ONES (fsd.f_frsize ? fsd.f_frsize : fsd.f_bsize); fsp->fsu_blocks = PROPAGATE_ALL_ONES (fsd.f_blocks); fsp->fsu_bfree = PROPAGATE_ALL_ONES (fsd.f_bfree); fsp->fsu_bavail = PROPAGATE_TOP_BIT (fsd.f_bavail); fsp->fsu_bavail_top_bit_set = EXTRACT_TOP_BIT (fsd.f_bavail) != 0; fsp->fsu_files = PROPAGATE_ALL_ONES (fsd.f_files); fsp->fsu_ffree = PROPAGATE_ALL_ONES (fsd.f_ffree); return 0; } size_t get_free(const char *disk, const char *mount_point,const char *fstype, int me_dummy, int me_remote) { struct fs_usage fsu; const char *stat_file; stat_file = mount_point ? mount_point : disk; if(get_fs_usage(stat_file, disk, &fsu)) return 0; size_t free = fsu.fsu_bavail * fsu.fsu_blocksize; return free; } size_t show_free(char * device,char *mount_point) { read_filesystem_list(device,mount_point); if(mount_list == NULL) return 0; mount_entry *me = mount_list; size_t df = (size_t)get_free(me->me_devname, me->me_mountdir, me->me_type, me->me_dummy, me->me_remote); delete mount_list; return df; }