2010-10-21 20 views

Respuesta

71

cheque man statvfs(2)

Creo que se puede calcular 'espacio libre' como f_bsize * f_bfree.

NAME 
     statvfs, fstatvfs - get file system statistics 

SYNOPSIS 
     #include <sys/statvfs.h> 

     int statvfs(const char *path, struct statvfs *buf); 
     int fstatvfs(int fd, struct statvfs *buf); 

DESCRIPTION 
     The function statvfs() returns information about a mounted file system. 
     path is the pathname of any file within the mounted file system. buf 
     is a pointer to a statvfs structure defined approximately as follows: 

      struct statvfs { 
       unsigned long f_bsize; /* file system block size */ 
       unsigned long f_frsize; /* fragment size */ 
       fsblkcnt_t  f_blocks; /* size of fs in f_frsize units */ 
       fsblkcnt_t  f_bfree; /* # free blocks */ 
       fsblkcnt_t  f_bavail; /* # free blocks for unprivileged users */ 
       fsfilcnt_t  f_files; /* # inodes */ 
       fsfilcnt_t  f_ffree; /* # free inodes */ 
       fsfilcnt_t  f_favail; /* # free inodes for unprivileged users */ 
       unsigned long f_fsid;  /* file system ID */ 
       unsigned long f_flag;  /* mount flags */ 
       unsigned long f_namemax; /* maximum filename length */ 
      }; 
+1

Que se parece exactamente lo que necesito. ¡Aclamaciones! – Matt

+4

statvfs no parece funcionar para unidades montadas vfat. Lo probé con una partición FAT 32 y da 0 para los bloques disponibles. ¿Hay alguna forma de resolverlo? –

+0

@ShadmanAnwer desafortunadamente la página man dice: 'No se especifica si todos los miembros de la estructura devuelta tienen valores significativos en todos los sistemas de archivos. Por lo tanto, quizás FAT32 no sea compatible en este caso. –

24

Puede utilizar impulso :: sistema de archivos:

struct space_info // returned by space function 
{ 
    uintmax_t capacity; 
    uintmax_t free; 
    uintmax_t available; // free space available to a non-privileged process 
}; 

space_info space(const path& p); 
space_info space(const path& p, system::error_code& ec); 

Ejemplo:

#include <boost/filesystem.hpp> 
using namespace boost::filesystem; 
space_info si = space("."); 
cout << si.available << endl; 

Devuelve: Un objeto de tipo space_info. El valor del objeto space_info se determina como si utilizara POSIX statvfs() para obtener una estructura POSIX statvfs, y luego multiplicara sus miembros f_blocks, f_bfree y f_bavail por su miembro f_frsize, y asignando los resultados a la capacidad, libre, y miembros disponibles respectivamente. Cualquier miembro para el que no se pueda determinar el valor se establecerá en -1.

+0

+1 Funcionó perfectamente. –

+0

Esto incluso funciona para Windows. –

-3

Uno puede conseguir la salida de un comando en un programa mediante el uso de un tubo de esta manera:

char cmd[]="df -h /path/to/directory" ; 
FILE* apipe = popen(cmd, "r"); 
// if the popen succeeds read the commands output into the program with 
while ( fgets(line, 132 , apipe)) 
{ // handle the readed lines 
} 
pclose(apipe); 
// ----------------------------------- 
Cuestiones relacionadas