Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
bgColor#ccccff
struct stat st;
dev_t dev; /* device */
ino_t ino; /* file serial number */
int fd = open(filename, O_RDWR);
if ( (fd != -1) && 
     (fstat(fd, &st) != -1) &&
     (st.st_ino == ino) &&
     (st.st_dev == dev) 
   ) {
   ...	  
}
close(fd);

The structure members st_mode, st_ino, st_dev, st_uid, st_gid, st_atime, st_ctime, and st_mtime should all have meaningful values for all file types on POSIX compliant systems. The st_ino field contains the file serial number. The st_dev field identifies the device containing the file. The st_ino and st_dev, taken together, uniquely identifies the file. The st_dev value is not necessarily consistent across reboots or system crashes, however.

...

Code Block
bgColor#ccccff
struct stat st;
dev_t dev = 773; /* device */
ino_t ino = 11321585;  /* file serial number */
FILE *fd = fopen(filename, "r");
if ( (fd) && 
     (fstat(fileno(fd), &st) != -1) &&
     (st.st_ino == ino) &&
     (st.st_dev == dev) 
     ) {
   ...
}
fclose(fd);

Risk Assessment

...

Wiki Markup
\[[Seacord 05|AA. C References#Seacord 05]\] Chapter 7, "File I/O"
\[[ISO/IEC 9899-1999|AA. C References#ISO/IEC 9899-1999]\] SectionsSection 7.19.3, Files;"Files," and Section 7.19.4, "Operations on Files"
\[[Open Group 04|AA. C References#Open Group 04]\] "The open function," "The fstat function"
\[[Drepper 06|AA. C References#Drepper 06]\] Section 2.2.1 "Identification When Opening"