Repository navigation
Expand file tree
/
Copy pathpath.c
More file actions
122 lines (106 loc) · 3.04 KB
/
Copy pathpath.c
File metadata and controls
122 lines (106 loc) · 3.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#include "inc.h"
static int search_dir(
struct inode *ldir_ptr, /* dir record parent */
char string[NAME_MAX], /* component to search for */
ino_t *numb /* pointer to new dir record */
) {
/* The search_dir function performs the operation of searching for the
* component ``string" in ldir_ptr. It returns the response and the
* number of the inode in numb.
*/
struct inode *dir_tmp;
size_t pos = 0;
int r;
/*
* This function search a particular element (in string) in a inode and
* return its number.
*/
if ((ldir_ptr->i_stat.st_mode & S_IFMT) != S_IFDIR)
return ENOTDIR;
if (strcmp(string, ".") == 0) {
*numb = ldir_ptr->i_stat.st_ino;
return OK;
}
/*
* Parent directories need special attention to make sure directory
* inodes stay consistent.
*/
if (strcmp(string, "..") == 0) {
if (ldir_ptr->i_stat.st_ino ==
v_pri.inode_root->i_stat.st_ino) {
*numb = v_pri.inode_root->i_stat.st_ino;
return OK;
}
else {
dir_tmp = alloc_inode();
r = read_inode(dir_tmp, ldir_ptr->extent, pos, &pos);
if ((r != OK) || (pos >= ldir_ptr->i_stat.st_size)) {
put_inode(dir_tmp);
return ENOENT;
}
/* Temporary fix for extent spilling */
put_inode(dir_tmp);
dir_tmp = alloc_inode();
/* End of fix */
r = read_inode(dir_tmp, ldir_ptr->extent, pos, &pos);
if ((r != OK) || (pos >= ldir_ptr->i_stat.st_size)) {
put_inode(dir_tmp);
return ENOENT;
}
*numb = dir_tmp->i_stat.st_ino;
put_inode(dir_tmp);
return OK;
}
}
/* Read the dir's content */
while (TRUE) {
dir_tmp = alloc_inode();
r = read_inode(dir_tmp, ldir_ptr->extent, pos, &pos);
if ((r != OK) || (pos >= ldir_ptr->i_stat.st_size)) {
put_inode(dir_tmp);
return ENOENT;
}
if ((strcmp(dir_tmp->i_name, string) == 0) ||
(strcmp(dir_tmp->i_name, "..") &&
strcmp(string, "..") == 0)) {
if (dir_tmp->i_stat.st_ino ==
v_pri.inode_root->i_stat.st_ino) {
*numb = v_pri.inode_root->i_stat.st_ino;
put_inode(dir_tmp);
return OK;
}
*numb = dir_tmp->i_stat.st_ino;
put_inode(dir_tmp);
return OK;
}
put_inode(dir_tmp);
}
}
int fs_lookup(ino_t dir_nr, char *name, struct fsdriver_node *node,
int *is_mountpt)
{
/* Given a directory and a component of a path, look up the component
* in the directory, find the inode, open it, and return its details.
*/
struct inode *dirp, *rip;
ino_t ino_nr;
int r;
/* Find the starting inode. */
if ((dirp = find_inode(dir_nr)) == NULL)
return EINVAL;
/* Look up the directory entry. */
if ((r = search_dir(dirp, name, &ino_nr)) != OK)
return r;
/* The component has been found in the directory. Get the inode. */
if ((rip = get_inode(ino_nr)) == NULL)
return EIO; /* FIXME: this could have multiple causes */
/* Return its details to the caller. */
node->fn_ino_nr = rip->i_stat.st_ino;
node->fn_mode = rip->i_stat.st_mode;
node->fn_size = rip->i_stat.st_size;
node->fn_uid = rip->i_stat.st_uid;
node->fn_gid = rip->i_stat.st_gid;
node->fn_dev = rip->i_stat.st_rdev;
*is_mountpt = rip->i_mountpoint;
return OK;
}