...
| Wiki Markup |
|---|
When creating new files, it may be possible to use functions which only create a new file where a file does not already exist. This prevents the application from overwriting an existing file during file creation (see \[[FIO03-A. Do not make assumptions about fopen() and file creation]\]). |
In rare cases, it is necessary to check for the existence of symbolic or hard links to ensure that a program is reading from an intended file and not a different file in another directory. In these cases, avoid creating a race condition when checking for the existence of symbolic links.
...
| Code Block |
|---|
|
if ((fd = open("/home/rcs/.conf", O_RDWR, 0600)) == -1) {
/* handle error */
}
write(fd, userbuf, userlen);
|
...
| Code Block |
|---|
|
struct stat lstat_info;
int fd;
if (lstat("some_file", &lstat_info) == -1) {
/* handle error */
}
if (!S_ISLNK(lstat_info.st_mode)) {
if ((fd = open("some_file", O_RDWR, 0600)) == -1) {
/* handle error */
}
}
write(fd, userbuf, userlen);
|
...
| Code Block |
|---|
|
int fd;
if ((fd = open("some_file", O_RDWR | O_NOFOLLOW, 0600)) == -1) {
/* handle error */
}
write(fd, userbuf, userlen);
|
...
| Code Block |
|---|
|
struct stat lstat_info, fstat_info;
int fd;
if (lstat("some_file", &lstat_info) == -1) {
/* handle error */
}
if ((fd = open("some_file", O_RDWR, 0600)) == -1) {
/* handle error */
}
if (fstat(fd, &fstat_info) == -1) {
/* handle error */
}
if (lstat_info.st_mode == fstat_info.st_mode &&
lstat_info.st_ino == fstat_info.st_ino &&
lstat_info.st_dev == fstat_info.st_dev) {
write(fd, userbuf, userlen);
}
|
...