irsis
irsis

Reputation: 1054

Windows API to fetch file attributes if path contains Symbolic link?

I used _stat64(const char *path, struct __stat64 *buffer); API to get the file/directory stats but this API fails if input path contains a symbolic link.

GetFileAttributes() and GetFileAttributesEx() APIs too fail if file path contains a symbolic link. But as mentioned at MSDN, If the path points to a symbolic link, these two functions returns attributes for the symbolic link. Even _stat64() works if path points to symbolic link.

Is there any way to get the states/attribues of file/directory if path contains [not points to] a symbolic link ?

For instance - how to get attributes of "test" directory if input path is "D:\temp\symbolic_link\test"

[Edit1] Since eryksun's comment made sense. I tried GetFileAttributesEx() and _stat64() again. It worked but this time I had granted the full permissions to target directory and symbolic link both. It appeared to be permission issue. If I pass "D:\temp\symbolic_link" then I get attributes/stats of symbolic link and if I pass "D:\temp\symbolic_link\test" then I get attributes/stats of test directory which is expected.

Upvotes: 5

Views: 3378

Answers (1)

None of the MSDN pages say anything related to your issue, but you may want to obtain the real path of your file by doing something like this:

void realpath(const char *filename, wchar_t *pathbuf, int size)
{
    OFSTRUCT of;
    HANDLE file = (HANDLE)OpenFile(filename,&of,OF_READ);
    GetFinalPathNameByHandle(file,pathbuf,size,FILE_NAME_OPENED);
    CloseHandle(file);
}

This will fill pathbuf with the reparsed path name of your file (up to the length of size).

I hope this helps.

Upvotes: 5

Related Questions