st_blksize返回值是以位还是字节为单位?

时间:2017-06-08 13:57:56

标签: c posix stat

我正在尝试使用以下行找到我的机器中的块大小:

struct stat fi;
stat("/tmp, &fi);
BLOCK_SIZE = fi.st_blksize;

BLOCK_SIZE是否表示单个块中字节的数量或位数

文档说:

  

“blksize_t st_blksize; / * blocksize for file system I / O * /”

3 个答案:

答案 0 :(得分:6)

如果您阅读this POSIX <sys/stat.h> reference,您会找到

  

st_blocks 结构的 stat 成员的单位未在POSIX.1-2008中定义。在一些实现中,它是512字节。它可能因文件系统而异。 st_blocks st_blksize 以及 f_bsize 的值之间没有相关性(来自<sys/statvfs.h>结构成员。

因此虽然st_blksize没有标准化单位,但它不太可能是位数。要获得实际单位,您必须研究当前的操作系统,甚至可能使用的文件系统。

Linux stat manual page没有明确提及单位,但在the example中它表示单位是以字节为单位。

答案 1 :(得分:1)

它在Bytes。请参阅Linux手册页示例代码here: -

printf("Preferred I/O block size: %ld bytes\n", (long) sb.st_blksize);

  The following program calls stat() and displays selected fields in
   the returned stat structure.

   #include <sys/types.h>
   #include <sys/stat.h>
   #include <time.h>
   #include <stdio.h>
   #include <stdlib.h>
   #include <sys/sysmacros.h>

   int
   main(int argc, char *argv[])
   {
       struct stat sb;

       if (argc != 2) {
           fprintf(stderr, "Usage: %s <pathname>\n", argv[0]);
           exit(EXIT_FAILURE);
       }

       if (stat(argv[1], &sb) == -1) {
           perror("stat");
           exit(EXIT_FAILURE);
       }

       printf("ID of containing device:  [%lx,%lx]\n",
            (long) major(sb.st_dev), (long) minor(sb.st_dev));

       printf("File type: ");

       switch (sb.st_mode & S_IFMT) {
       case S_IFBLK:  printf("block device\n");            break;
       case S_IFCHR:  printf("character device\n");        break;
       case S_IFDIR:  printf("directory\n");               break;
       case S_IFIFO:  printf("FIFO/pipe\n");               break;
       case S_IFLNK:  printf("symlink\n");                 break;
       case S_IFREG:  printf("regular file\n");            break;
       case S_IFSOCK: printf("socket\n");                  break;
       default:       printf("unknown?\n");                break;
       }

       printf("I-node number:            %ld\n", (long) sb.st_ino);

       printf("Mode:                     %lo (octal)\n",
               (unsigned long) sb.st_mode);

       printf("Link count:               %ld\n", (long) sb.st_nlink);
       printf("Ownership:                UID=%ld   GID=%ld\n",
               (long) sb.st_uid, (long) sb.st_gid);

       printf("Preferred I/O block size: %ld bytes\n",
               (long) sb.st_blksize);
       printf("File size:                %lld bytes\n",
               (long long) sb.st_size);
       printf("Blocks allocated:         %lld\n",
               (long long) sb.st_blocks);

       printf("Last status change:       %s", ctime(&sb.st_ctime));
       printf("Last file access:         %s", ctime(&sb.st_atime));
       printf("Last file modification:   %s", ctime(&sb.st_mtime));

       exit(EXIT_SUCCESS);
   }

答案 2 :(得分:0)

块大小通常以字节为单位,而stat则为。把它给成碎片没有多大意义。