0

From this answer, I see that something like:

bool
ref_same_file(int fd1, int fd2)
{
  struct stat stat1, stat2;
  if (!fstat(fd1, &stat1) && !fstat(fd1, &stat2))
    return (stat1.st_dev == stat2.st_dev)
      && (stat1.st_ino == stat2.st_ino);
  perror("fstat");
  exit(errno);
}

could see if two file descriptors reference the same file. However, I am not totally clear on the st_dev. Is this not the block device? Can a single block device not hold multiple file systems?

I see the POSIX spec states that it makes it uniquely identifiable, but I am wondering how this is possible.

user129393192
  • 797
  • 1
  • 8
  • For a disk device the `st_dev` member identifies not only the disk itself (the *major* device number), but also the partition on the disk (the *minor* device number). A single disk partition can only hold a single filesystem. – Some programmer dude May 29 '23 at 06:42
  • How's that? I had thought that you could have multiple on a single partition. – user129393192 May 29 '23 at 06:44
  • 1
    Why do you think that? Would it really make sense to have a partition formatted for both NTFS and ext4? How would that even work? – Some programmer dude May 29 '23 at 06:47
  • I'm not sure. I had seen some other threads on it: [1](https://askubuntu.com/questions/1463833/can-we-create-two-different-filesystems-on-a-single-partition), [2](https://serverfault.com/questions/257800/can-we-create-two-filesystems-on-a-single-partition). – user129393192 May 29 '23 at 07:03
  • What the answers in those questions are talking about is to create a *file* that emulates a file-system. With the right driver support those can be mounted and read from or written to as any other drive and filesystem. *But* they are then different devices, with a different major and minor device number. And the file is still a file stored on a primary (actual physical) drive with one single filesystem. – Some programmer dude May 29 '23 at 07:08
  • I believe the accepted answer on the first attempted to demonstrate it, with a real partition, unless I'm mistaken. – user129393192 May 29 '23 at 07:10
  • That one is very academic and not practically feasible. And as the answer mentions, only one filesystem can be mounted at a time. So even in that highly theoretical situation, you can't have multiple filesystems for a single major/minor device pair at runtime. So the comparison in the shown code will still work. – Some programmer dude May 29 '23 at 07:21
  • @user129393192 When you run `df` on a POSIX system, what's the first column of the output? It's the **device** the file system resides on... – Andrew Henle May 29 '23 at 11:41
  • Does this mean that the device is not based on partition, but rather file system? What exactly denotes a device in this case? From my understanding, you could have a partition like `/dev/sda1` and also split that into devices, which you can use for “device ids” and individual file systems? – user129393192 Jun 01 '23 at 22:31

0 Answers0