ç统计结构没有st_ctime场,但只有st_ctim [英] C stat struct does not have st_ctime field but only st_ctim

查看:370
本文介绍了ç统计结构没有st_ctime场,但只有st_ctim的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在已经googleing这个约两个小时,但我无法找到任何方式帮助任何答案。

在手册页spcifiedSTAT的定义说,一个st_ctime字段存在。


  struct stat中{
           的dev_t st_dev; / *包含设备的ID文件* /
           ino_t过去st_ino中; / *索引节点号* /
           mode_t ST_MODE; / *保护* /
           nlink_t st_nlink; / *硬链接数* /
           将uid_t st_uid; / *用户所有者的ID * /
           gid_t st_gid; / *组所有者*的ID /
           的dev_t st_rdev; / *设备ID(如有特殊文件)* /
           off_t st_size; / *总字节大小* /
           blksize_t st_blksize; / *块大小文件系统I / O * /
           blkcnt_t的st_blocks; / *分配512B块数* /
           time_t的st_atime的; / *上次访问时间的* /
           time_t的st_mtime; / *最后修改时间* /
           time_t的st_ctime; / *最后状态改变的时间* /
       };


不过,这似乎并不为我的系统真正即使我用gcc(应按照标准来行事)。

在事实上,所有的时间字段(的atime,修改时间的ctime)的缺失,因此该结构包含一些ATIM,MTIM和CTIM值而返回的timespec,而不是期望的time_t的值。

现在我的问题:


  1. 为什么会这样呢?也许我包括错误的头,但我真的知道它得是SYS / stat.h。

  2. 我一直没找到有关的timespec太多的信息,它是什么,为什么在这里回来了?

  3. 就算我找到一个解决方法,它有助于米或将任何其他系统无法执行我的code?

我使用Ubuntu 11.10和gcc 4.6.1。

我的code(部分):

  struct stat中FILE_INFO;
    time_t的吨;    如果(LSTAT(路径和放大器; FILE_INFO)== 0){        结构TM * timeinfo;
        T = file_info.st_ctime;
        timeinfo =本地时间(& T公司);

我会很高兴,如果你能与这一个帮助,我真的得到了不知道为什么我不能编译使用我的结构的st_ctime领域和往常一样的gcc没有太大的帮助,当谈到交谈有关错误; - )

也许这是必须做的#include与问题的东西,但我不能确定什么。


解决方案

2008年POSIX的要求,统计()的返回结构的timespec,使时间戳的小数部分可用于时间戳的精precision - 1秒分辨率是不够的文件时间

增加:

手册页

由于内核即使生效,stat结构支持这三个文件的时间戳字段纳秒的分辨率。 glibc的公开使用,如果_BSD_SOURCE或_SVID_SOURCE功能测试宏定义的形式st_atim.tv_nsec名称每个字段的纳秒组件。这些字段在POSIX.1-2008指定,并且,从版本2.12,的glibc如果_POSIX_C_SOURCE与值200809L或更大,或_XOPEN_SOURCE与值700或更大的定义定义也暴露这些字段名。如果没有上述的宏被定义,则纳秒的值被暴露与表单st_atimensec的名称。在不支持秒级时间戳的文件系统,纳秒级字段值为0返回。

I've now been googleing this for about two hours, yet I was unable to find any answers that helped.

The definition of 'stat' as spcified in the manpage says that a st_ctime field exists.

       struct stat {
           dev_t     st_dev;     /* ID of device containing file */
           ino_t     st_ino;     /* inode number */
           mode_t    st_mode;    /* protection */
           nlink_t   st_nlink;   /* number of hard links */
           uid_t     st_uid;     /* user ID of owner */
           gid_t     st_gid;     /* group ID of owner */
           dev_t     st_rdev;    /* device ID (if special file) */
           off_t     st_size;    /* total size, in bytes */
           blksize_t st_blksize; /* blocksize for file system I/O */
           blkcnt_t  st_blocks;  /* number of 512B blocks allocated */
           time_t    st_atime;   /* time of last access */
           time_t    st_mtime;   /* time of last modification */
           time_t    st_ctime;   /* time of last status change */
       };

However, this does not seem to be true for my system even though I'm using gcc (which should be behaving according to the standard).

In fact, all of the time fields (atime, mtime, ctime) are missing and therefore the struct contains some atim, mtim and ctim values which return a timespec instead of the desired time_t value.

Now my questions:

  1. Why is this so? Maybe I included the wrong header but I'm really sure it's gotta be sys/stat.h.
  2. I have not been finding too much information about timespec, what is it and why is it returned here?
  3. Even if I find a workaround, does it help m or will any other system fail to execute my code?

I am using Ubuntu 11.10 and gcc 4.6.1.

My code (partly):

struct stat file_info;
    time_t t;

    if( lstat( path, &file_info ) == 0 ) {

        struct tm* timeinfo;
        t = file_info.st_ctime;
        timeinfo = localtime( &t );

I'd be really glad if you could help with this one, I really got no clue why I can not compile using the st_ctime field of my struct and as usual gcc is not too much of a help when it comes to talking about errors ;-)

Probably it's got to do something with #include problems, but I'm not able to determine what.

解决方案

POSIX 2008 requires that stat() return struct timespec, so that the fractional portion of a timestamp is available for refined precision of timestamps -- one second resolution is not sufficient for file times.

ADDED:

From man page

Since kernel 2.5.48, the stat structure supports nanosecond resolution for the three file timestamp fields. Glibc exposes the nanosecond component of each field using names of the form st_atim.tv_nsec if the _BSD_SOURCE or _SVID_SOURCE feature test macro is defined. These fields are specified in POSIX.1-2008, and, starting with version 2.12, glibc also exposes these field names if _POSIX_C_SOURCE is defined with the value 200809L or greater, or _XOPEN_SOURCE is defined with the value 700 or greater. If none of the aforementioned macros are defined, then the nanosecond values are exposed with names of the form st_atimensec. On file systems that do not support subsecond timestamps, the nanosecond fields are returned with the value 0.

这篇关于ç统计结构没有st_ctime场,但只有st_ctim的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆