Documenting stat(2)
In article <[email protected]> you write:
Quoted 4 lines
Basically, the _only_ think you should depend on is that
st_sizecontains:
- for regular files, the size of the file in bytes
- for symlinks, the length of the symlink.
I don't think this is right - for a symlink, stat should return the size of the file; lstat should return the size of the symlink.
Signature · 6 lines
David M. Meyer [email protected] - To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to [email protected] Please read the FAQ at http://www.tux.org/lkml/
Quoted 9 lines · [email protected] wrote:
In article <[email protected]> you write:
Quoted 4 lines
Basically, the _only_ think you should depend on is that
st_sizecontains:
- for regular files, the size of the file in bytes
- for symlinks, the length of the symlink.I don't think this is right - for a symlink, stat should return the size of the file; lstat should return the size of the symlink.
Nope stat should return the details of the symlink whereas lstat should return the details of the symlink target.
But there is another ambiguity when stating symlinks. In the current implementation the length of the symlink (name) is the same as the symlink file size. Will this always be the case? If not then the above statement is wrong. i.e.
Quoted 1 line
Quoted 1 line
- for symlinks, the length of the symlink.
should be
Quoted 2 lines
Quoted 2 lines
- for symlinks, the symlink file size in bytes (currently the length of the symlink).
Padraig.
-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to [email protected]
Please read the FAQ at http://www.tux.org/lkml/
Quoted 3 lines
Nope stat should return the details of the symlink whereas lstat should return the details of the symlink target.
It's the other way around according to the manpage, and my code also says it's the other way around.
It's logical the way it is..
I use lstat to check if a config file is a symlink, and if it is, it refuses to open it.
Igmar
-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to [email protected]
Please read the FAQ at http://www.tux.org/lkml/
Quoted 2 lines · Igmar Palsenberg
I use lstat to check if a config file is a symlink, and if it is, it refuses to open it.
Nice race condition.
mrc
Signature · 8 lines
Mike Castle Life is like a clock: You can work constantly [email protected] and be right all the time, or not work at all www.netcom.com/~dalgoda/ and be right at least twice a day. -- mrc We are all of us living in the shadow of Manhattan. -- Watchmen - To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to [email protected] Please read the FAQ at http://www.tux.org/lkml/
Quoted 5 lines · Mike Castle
Quoted 2 lines · Igmar Palsenberg
I use lstat to check if a config file is a symlink, and if it is, it refuses to open it.
Nice race condition.
Agree, but still better then opening things that are actually a symlink. Now would someone probably say : use the O_NOWFOLLOW option, but since I do other checks that wouldn't be an option.
Quoted 1 line
mrc
Igmar
-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to [email protected]
Please read the FAQ at http://www.tux.org/lkml/
In article <[email protected]> you write:
Quoted 2 lines
Nope stat should return the details of the symlink whereas lstat should return the details of the symlink target.
Not according to my manpages. From stat(2):
stat stats the file pointed to by file_name and fills in buf.
lstat is identical to stat, only the link itself is stated, not the file that is obtained by tracing the links.
Actually, the Solaris manpage is clearer:
The lstat() function obtains file attributes similar to stat(), except when the named file is a symbolic link; in that case lstat() returns information about the link, while stat() returns information about the file the link refer- ences.
and a short test program:
#include <stdio.h>
#include <sys/stat.h>
#include <unistd.h>
main()
{
struct stat buf;
stat("stattest.c",&buf);
printf("stat original file: %d\n",buf.st_size);
symlink("stattest.c","a_symlink");
stat("a_symlink",&buf);
printf("stat symlink: %d\n",buf.st_size);
lstat("a_symlink",&buf);
printf("lstat symlink: %d\n",buf.st_size);
}jhereg|dmeyer|~/dl> ./stattest
stat original file: 358
stat symlink: 358
lstat symlink: 10
stat clearly is giving the size of the target, and lstat the size of the link.
Signature · 6 lines
David M. Meyer [email protected] - To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to [email protected] Please read the FAQ at http://www.tux.org/lkml/
Padraig Brady <[email protected]> writes:
|> [email protected] wrote: |> |> > In article <[email protected]> you write: |> > |> >> Basically, the _only_ think you should depend on is that st_size |> >> contains: |> >> - for regular files, the size of the file in bytes |> >> - for symlinks, the length of the symlink. |> > I don't think this is right - for a symlink, stat should return the |> > size of the file; lstat should return the size of the symlink. |> |> Nope stat should return the details of the symlink |> whereas lstat should return the details of the symlink target.
Nope, check the facts.
Andreas.
Signature · 8 lines
Andreas Schwab "And now for something SuSE Labs completely different." [email protected] SuSE GmbH, Schanzäckerstr. 10, D-90443 Nürnberg - To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to [email protected] Please read the FAQ at http://www.tux.org/lkml/