-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsymlink.c
More file actions
108 lines (91 loc) · 2.29 KB
/
symlink.c
File metadata and controls
108 lines (91 loc) · 2.29 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
//Symlink and readlinks
int symlinks(char* old_file, char* new_file)
{
if(strcmp(old_file,"")==0)//check to make sure names were given
{
printf("No file given to link for old\n");
return;
}
if(strcmp(new_file,"")==0)//Check to make sure names were given
{
printf("No file given to link for new\n");
return;
}
int old = getino(old_file);//get the old file that needs to be linked
if(old == 0)//Doesn't exist
{
printf("This file does not exist yet\n");
return;
}
int new = getino(new_file);//make sure not created yet
if(new)//exists
{
printf("Already created\n");
return;
}
//now the old file exists and the new files does not at this point
tokenz(new_file);
MINODE* parent;
new_file[strlen(new_file)-strlen(nam[v-1])-1] = 0;
int parent_ino = getino(new_file);//get the parent inode
if( v == 1)
{
parent = running->cwd;
}
else
{
if(!parent_ino)//need to make sure exists
{
printf("Not found\n");
return;
}
parent = iget(dev,parent_ino);//get the inode
}
int ino = ialloc(dev);//new Inode
int blk = balloc(dev);//block
enter_name(parent, ino, nam[v-1]);//enter the name into the parent directory
char buf[BLKSIZE];
DIR* dp = (DIR *) buf;
char* cp = buf;
bzero(buf,BLKSIZE);//zero out
tokenz(old_file);//now the old file
dp->inode = parent_ino;//child
dp->rec_len = 1024;//remains
dp->name_len = strlen(nam[v-1]);//length of string
strcpy(dp->name,nam[v-1]);
MINODE* mip = iget(dev,ino);//get the new inode
INODE* ip = &mip->INODE;//pointer to the inode in the block
ip->i_mode = 0644 + 0xA000;
ip->i_uid = running->uid;
ip->i_block[0] = blk;
ip->i_gid = running->gid;
ip->i_size = strlen(nam[v-1]);
ip->i_links_count = 1;
time_t seconds = time(0L);
ip->i_atime = ip->i_ctime = ip->i_mtime = seconds;
mip->dirty = 1;
iput(mip);//write back to disk
put_block(dev,blk,buf);//write the block to the data
parent->dirty = 1;
if(parent != running->cwd)
{
iput(parent);
}
}
int readlinks(MINODE* mip, char* buf)
{
char temp[256];
char b[BLKSIZE];
if (!((mip->INODE.i_mode & 0xF000) == 0xA000))
{
printf("Not a link\n");
return;
}
get_block(dev,mip->INODE.i_block[0],b);
DIR* dp = (DIR *) b;
char* cp = b;
strncpy(temp, dp->name, dp->name_len);
temp[dp->name_len] = 0;
strcpy(buf,temp);
return dp->name_len;
}