What is lstat() alternative in windows?
Asked Answered
P

3

8

In linux, when stat() is used with broken link files, it fails with -1. So I used lstat() which succeeded.

For the same case in windows, _stat() fails with broken shortcuts, but there is no _lstat() in windows. Please help to find the alternative for lstat() in windows.

Pentyl answered 23/8, 2012 at 6:15 Comment(0)
C
7

GetFileAttributes or GetFileAttributesEx probably (if I understood stat and lstat right). Quoting from the docs:

Symbolic link behavior—If the path points to a symbolic link, the function returns attributes for the symbolic link.

Crispen answered 23/8, 2012 at 6:40 Comment(0)
S
10

The accepted answer does not provide a full stat equivalent. The stat struct is defined as

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

but GetFileAttributes.. does not provide any owner information (it returns data in a WIN32_FIND_DATA object). If you need that owner information, it looks like you can use GetSecurityInfo [1].

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

Suspensoid answered 30/1, 2015 at 18:14 Comment(0)
C
7

GetFileAttributes or GetFileAttributesEx probably (if I understood stat and lstat right). Quoting from the docs:

Symbolic link behavior—If the path points to a symbolic link, the function returns attributes for the symbolic link.

Crispen answered 23/8, 2012 at 6:40 Comment(0)
P
4

hey _stat() or stat() works fine on broken shortcuts as well. Thats the reason,there is no alternative like lstat(UNIX) in windows.

Where in Unix, stat() fails with broken links, so lstat is provided to fix the problem.

Thank you all for your help.

Pentyl answered 24/8, 2012 at 10:46 Comment(1)
I assume the question, and this answer, were made in the context of MSVC's runtime library? Regardless, thanks for posting this answer as it saved me from wondering why the heck anyone would go to the trouble of making a _stat() that follows shortcuts, but not make an _lstat() that does not do so to go with it. I mean, the former is a lot more work, to the point where anyone smart enough to do it should have ample time during the process to realize that they are making the latter necessary as well.Interleaf

© 2022 - 2024 — McMap. All rights reserved.