A directory is a table of names and inode numbers. To open /home/ann/notes, the kernel starts at the root directory, looks up home in it, then ann in that directory, then notes. A hard link is simply a second entry that names the same inode.
What to build
Resolve absolute paths in a small file system, then print the link count of every inode.
Requirements
- Inodes are numbered
0ton − 1; inode 0 is the root directory. The type string gives the type of each inode:dfor a directory,ffor a file. Each entryparent name childsays that directoryparenthas an entrynamethat names inodechild. A file may have several entries (hard links); a directory has exactly one (except the root, which has none). - A path starts with
/and has components separated by/. Start at inode 0. For each component: if the current inode is not a directory, the answer isnot a directory..stays where it is,..goes to the parent directory (the root is its own parent). Any other name is looked up in the current directory; if it is not there the answer isnot found. If every component works, print the final inode number. The path/is inode 0. - After all paths print
inode i: Lfor each inode. For a fileLis the number of entries that name it. For a directoryLis 2 plus the number of entries in the file system that name a directory whose parent it is (its subdirectories).
Input
The first line has n and m (1 ≤ n ≤ 12, 0 ≤ m ≤ 30). The second line has the type string of n letters. Then m lines parent name child (names are 1 to 8 letters or digits). Then q (1 ≤ q ≤ 15) and q paths, one per line, each at most 60 characters, with no trailing / except the path / itself.
Output
One line per path (a number, not found or not a directory), then n lines inode i: L.
Constraints
Entries only name inodes of the right type; no directory is named twice.
Example
Input:
6 5
ddfddf
0 home 1
1 ann 3
3 notes 2
0 etc 4
4 hosts 5
4
/home/ann/notes
/home/ann/../ann/.
/etc/hosts/x
/home/bob
Output:
2
3
not a directory
not found
inode 0: 4
inode 1: 3
inode 2: 1
inode 3: 2
inode 4: 2
inode 5: 1
Why it works
Path lookup is a loop of table searches, one per component, and each step only works if the thing it goes through is a directory. Because the name lives in the directory and not in the inode, a file can have several names; the link count is what lets the file system know when the last name is gone and the blocks can be freed.
Examples
6 5 ddfddf 0 home 1 1 ann 3 3 notes 2 0 etc 4 4 hosts 5 4 /home/ann/notes /home/ann/../ann/. /etc/hosts/x /home/bob
2 3 not a directory not found inode 0: 4 inode 1: 3 inode 2: 1 inode 3: 2 inode 4: 2 inode 5: 1
2 1 df 0 a 1 3 / /a /a/b
0 1 not a directory inode 0: 2 inode 1: 1
Hints
Hint 1
Store each directory entry as a parent inode, a name and a child inode. To look a name up in directory d, scan the entries.
Hint 2
Go through the path one component at a time. Before looking up a component, check that the current inode is a directory.
Hint 3
For .. use parent[cur], which you fill from the entries whose child is a directory. The root's parent is the root.
Solution
Show one correct solution (try it yourself first)
#include <stdio.h>
#include <string.h>
int main(void) {
int n, m, q, par[30], child[30], parent_of[12];
char types[16], name[30][12], path[80];
scanf("%d %d", &n, &m);
scanf("%15s", types);
for (int i = 0; i < n; i++) parent_of[i] = 0;
for (int i = 0; i < m; i++) {
scanf("%d %11s %d", &par[i], name[i], &child[i]);
if (types[child[i]] == 'd') parent_of[child[i]] = par[i];
}
scanf("%d", &q);
for (int i = 0; i < q; i++) {
scanf("%79s", path);
int cur = 0, status = 0; /* 0 ok, 1 not found, 2 not a directory */
int pos = 1, len = (int)strlen(path);
while (pos < len && status == 0) {
char part[12];
int k = 0;
while (pos < len && path[pos] != '/') part[k++] = path[pos++];
part[k] = '\0';
pos++;
if (types[cur] != 'd') {
status = 2;
} else if (strcmp(part, ".") == 0) {
/* stay */
} else if (strcmp(part, "..") == 0) {
cur = parent_of[cur];
} else {
int found = -1;
for (int e = 0; e < m; e++)
if (par[e] == cur && strcmp(name[e], part) == 0) found = child[e];
if (found < 0) status = 1;
else cur = found;
}
}
if (status == 1) printf("not found\n");
else if (status == 2) printf("not a directory\n");
else printf("%d\n", cur);
}
for (int i = 0; i < n; i++) {
int links = 0;
if (types[i] == 'd') {
links = 2;
for (int e = 0; e < m; e++)
if (types[child[e]] == 'd' && par[e] == i) links++;
} else {
for (int e = 0; e < m; e++)
if (child[e] == i) links++;
}
printf("inode %d: %d\n", i, links);
}
return 0;
}
▶ Open this solution in the compiler, then press Visualize to watch it run step by step