什么是windows中的lstat()替代品?

时间:2012-08-23 06:15:24

标签: c stat

在linux中,当stat()与断开的链接文件一起使用时,它会因-1而失败。所以我使用了成功的lstat()

对于Windows中的相同情况,_stat()因快捷方式失败而失败,但Windows中没有_lstat()。请帮助在Windows中找到lstat()的替代方案。

3 个答案:

答案 0 :(得分:7)

GetFileAttributesGetFileAttributesEx可能(如果我理解statlstat正确的话)。引用文档:

  

符号链接行为 - 如果路径指向符号链接,则该函数返回符号链接的属性。

答案 1 :(得分:6)

接受的答案并不能提供完整的stat等效答案。 stat结构定义为

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 filesystem 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 */
           };

但是GetFileAttributes..没有提供任何所有者信息(它返回WIN32_FIND_DATA对象中的数据)。如果您需要该所有者信息,您似乎可以使用GetSecurityInfo [1]。

[1] https://msdn.microsoft.com/en-us/library/windows/desktop/aa446629%28v=vs.85%29.aspx

答案 2 :(得分:3)

hey _stat()或stat()也适用于损坏的快捷方式。这就是原因,在Windows中没有像lstat(UNIX)这样的替代方案。

在Unix中,stat()因链接断开而失败,因此提供lstat来解决问题。

谢谢大家的帮助。