Why do `.` and`..` have different inodes inside` / `on Mac?

I played with my own team pwd. To find the whole path, I need to go through the tree inodeuntil I get to the root, and the way to say that I got to the root is by checking the equal inodenumbers stored for .and ...

But on my Mac, which doesn't seem to be the case, at least if you look at this table below.

    dirent |       stat | link to
-----------+------------+--------
  34078072 |   34078072 | self
  31103058 |   31103058 | parent
  31103058 |   31103058 | self
  31103020 |   31103020 | parent
  31103020 |   31103020 | self
    613497 |     613497 | parent
    613497 |     613497 | self
    603204 |     603204 | parent
    603204 |     603204 | self
    157433 |     157433 | parent
    157433 |     157433 | self
         2 |          2 | parent
         2 |          2 | self   // This is root aka /
         1 |          2 | parent // There is something above it?

This has been generated using the code below. The structure statseems to be doing well, but direnthas a different meaning when it comes to /. Why is this so? Should tags direntand stathave the same values ​​for inode number? Why is it different from a Mac?

#include <stdio.h>
#include <dirent.h>
#include <sys/stat.h>
#include <string.h>
#include <unistd.h>

void inodes();

int main()
{
    printf("    dirent |       stat | link to\n");
    printf("-----------+------------+--------\n");
    inodes();
    return 0;
}

void inodes()
{
    DIR* directory = opendir(".");
    struct dirent* entry = NULL;
    struct stat status;
    ino_t self = -1;
    ino_t parent = -1;

    while ((entry = readdir(directory))) {
        stat(entry->d_name, &status);
        if (strcmp(entry->d_name, ".") == 0) {
            self = status.st_ino;
            printf("%10.llu | %10.llu | self\n", entry->d_ino, self);
        }
        if (strcmp(entry->d_name, "..") == 0) {
            parent = status.st_ino;
            printf("%10.llu | %10.llu | parent\n", entry->d_ino, parent);
        }
    }
    if (self != parent) {
        if (chdir("..") != -1) {
            inodes();
        }
    }
}
+4
1

Macintosh HFS + " ". Apple " TN1150 - HFS Plus" .

, 2 ID 1. TN1150

enum {
    kHFSRootParentID            = 1,
    kHFSRootFolderID            = 2,
    ...
}

kHFSRootParentID
    Parent ID of the root folder.
kHFSRootFolderID
    Folder ID of the root folder.

HFS + . , readdir() 2 "." inode 1 ".." . ( . http://www.opensource.apple.com:)

, ".." . ,

stat(entry->d_name, &status);

".." , stat() , 2.

+4

Source: https://habr.com/ru/post/1614207/


All Articles