Repository navigation
Expand file tree
/
Copy pathinode.c
More file actions
296 lines (240 loc) · 8.76 KB
/
Copy pathinode.c
File metadata and controls
296 lines (240 loc) · 8.76 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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
/* This file contains all the function that handle the dir records
* (inodes) for the ISO9660 filesystem.*/
#include "inc.h"
#include "buf.h"
#in
10BC8
clude <minix/vfsif.h>
/*===========================================================================*
* fs_putnode *
*===========================================================================*/
int fs_putnode()
{
/* Find the inode specified by the request message and decrease its counter. */
int count;
struct dir_record *dir = NULL;
dir = get_dir_record(fs_m_in.m_vfs_fs_putnode.inode);
release_dir_record(dir);
count = fs_m_in.m_vfs_fs_putnode.count;
if (count <= 0) return(EINVAL);
if (count > dir->d_count) {
printf("put_inode: count too high: %d > %d\n", count, dir->d_count);
return(EINVAL);
}
if (dir->d_count > 1)
dir->d_count = dir->d_count - count + 1;/*Keep at least one reference*/
release_dir_record(dir); /* Actual inode release, might be last reference */
return(OK);
}
/*===========================================================================*
* release_dir_record *
*===========================================================================*/
int release_dir_record(dir)
struct dir_record *dir;
{
/* Release a dir record (decrement the counter) */
if (dir == NULL)
return(EINVAL);
if (--dir->d_count == 0) {
if (dir->ext_attr != NULL)
dir->ext_attr->count = 0;
dir->ext_attr = NULL;
dir->d_mountpoint = FALSE;
dir->d_prior = NULL;
if (dir->d_next != NULL)
release_dir_record(dir);
dir->d_next = NULL;
}
return(OK);
}
/*===========================================================================*
* get_free_dir_record *
*===========================================================================*/
struct dir_record *get_free_dir_record(void)
{
/* Get a free dir record */
struct dir_record *dir;
for(dir = dir_records; dir < &dir_records[NR_ATTR_RECS]; dir++) {
if (dir->d_count == 0) { /* The record is free */
dir->d_count = 1; /* Set count to 1 */
dir->ext_attr = NULL;
return(dir);
}
}
return(NULL);
}
/*===========================================================================*
* get_dir_record *
*===========================================================================*/
struct dir_record *get_dir_record(id_dir_record)
ino_t id_dir_record;
{
struct dir_record *dir = NULL;
u32_t address;
int i;
/* Search through the cache if the inode is still present */
for(i = 0; i < NR_DIR_RECORDS && dir == NULL; ++i) {
if (dir_records[i].d_ino_nr == id_dir_record
&& dir_records[i].d_count > 0) {
dir = dir_records + i;
dir->d_count++;
}
}
if (dir == NULL) {
address = (u32_t)id_dir_record;
dir = load_dir_record_from_disk(address);
}
if (dir == NULL) return(NULL);
return(dir);
}
/*===========================================================================*
* get_free_ext_attr *
*===========================================================================*/
struct ext_attr_rec *get_free_ext_attr(void) {
/* Get a free extended attribute structure */
struct ext_attr_rec *dir;
for(dir = ext_attr_recs; dir < &ext_attr_recs[NR_ATTR_RECS]; dir++) {
if (dir->count == 0) { /* The record is free */
dir->count = 1;
return(dir);
}
}
return(NULL);
}
/*===========================================================================*
* create_ext_attr *
*===========================================================================*/
int create_ext_attr(struct ext_attr_rec *ext,char *buffer)
{
/* Fill an extent structure from the data read on the device */
if (ext == NULL) return(EINVAL);
/* In input we have a stream of bytes that are physically read from the
* device. This stream of data is copied to the data structure. */
memcpy(&ext->own_id,buffer,sizeof(u32_t));
memcpy(&ext->group_id,buffer + 4,sizeof(u32_t));
memcpy(&ext->permissions,buffer + 8,sizeof(u16_t));
memcpy(&ext->file_cre_date,buffer + 10,ISO9660_SIZE_VOL_CRE_DATE);
memcpy(&ext->file_mod_date,buffer + 27,ISO9660_SIZE_VOL_MOD_DATE);
memcpy(&ext->file_exp_date,buffer + 44,ISO9660_SIZE_VOL_EXP_DATE);
memcpy(&ext->file_eff_date,buffer + 61,ISO9660_SIZE_VOL_EFF_DATE);
memcpy(&ext->rec_format,buffer + 78,sizeof(u8_t));
memcpy(&ext->rec_attrs,buffer + 79,sizeof(u8_t));
memcpy(&ext->rec_length,buffer + 80,sizeof(u32_t));
memcpy(&ext->system_id,buffer + 84,ISO9660_SIZE_SYS_ID);
memcpy(&ext->system_use,buffer + 116,ISO9660_SIZE_SYSTEM_USE);
memcpy(&ext->ext_attr_rec_ver,buffer + 180,sizeof(u8_t));
memcpy(&ext->len_esc_seq,buffer + 181,sizeof(u8_t));
return(OK);
}
/*===========================================================================*
* create_ext_attr *
*===========================================================================*/
int create_dir_record(dir,buffer,address)
struct dir_record *dir;
char *buffer;
u32_t address;
{
/* Fills a dir record structure from the data read on the device */
/* If the flag assign id is active it will return the id associated;
* otherwise it will return OK. */
short size;
size = buffer[0];
if (dir == NULL) return(EINVAL);
/* The data structure dir record is filled with the stream of data
* that is read. */
dir->length = size;
dir->ext_attr_rec_length = *((u8_t*)buffer + 1);
memcpy(&dir->loc_extent_l,buffer + 2,sizeof(u32_t));
memcpy(&dir->loc_extent_m,buffer + 6,sizeof(u32_t));
memcpy(&dir->data_length_l,buffer + 10,sizeof(u32_t));
memcpy(&dir->data_length_m,buffer + 14,sizeof(u32_t));
memcpy(dir->rec_date,buffer + 18, sizeof(dir->rec_date));
dir->file_flags = *((u8_t*)buffer + 25);
dir->file_unit_size = *((u8_t*)buffer + 26);
dir->inter_gap_size = *((u8_t*)buffer + 27);
dir->vol_seq_number = *((u8_t*)buffer + 28);
dir->length_file_id = *((u8_t*)buffer + 32);
memcpy(dir->file_id,buffer + 33,dir->length_file_id);
dir->ext_attr = NULL;
/* set memory attrs */
if ((dir->file_flags & D_TYPE) == D_DIRECTORY)
dir->d_mode = I_DIRECTORY;
else
dir->d_mode = I_REGULAR;
/* Set permission to read only. Equal for all users. */
dir->d_mode |= R_BIT | X_BIT;
dir->d_mode |= R_BIT << 3 | X_BIT << 3;
dir->d_mode |= R_BIT << 6 | X_BIT << 6;
dir->d_mountpoint = FALSE;
dir->d_next = NULL;
dir->d_prior = NULL;
dir->d_file_size = dir->data_length_l;
/* Set physical address of the dir record */
dir->d_phy_addr = address;
dir->d_ino_nr = address;
return(OK);
}
/*===========================================================================*
* load_dir_record_from_disk *
*===========================================================================*/
struct dir_record *load_dir_record_from_disk(address)
u32_t address;
{
/* This function load a particular dir record from a specific address
* on the device */
int block_nr, offset, block_size, new_pos;
struct buf *bp;
struct dir_record *dir, *dir_next, *dir_parent, *dir_tmp;
char name[NAME_MAX + 1];
char old_name[NAME_MAX + 1];
u32_t new_address, size;
block_size = v_pri.logical_block_size_l; /* Block size */
block_nr = address / block_size; /* Block number from the address */
offset = address % block_size; /* Offset starting from the block */
bp = get_block(block_nr); /* Read the block from the device */
if (bp == NULL)
return(NULL);
dir = get_free_dir_record(); /* Get a free record */
if (dir == NULL)
return(NULL);
/* Fill the dir record with the data read from the device */
create_dir_record(dir,b_data(bp) + offset, address);
/* In case the file is composed of more file sections, load also the
* next section into the structure */
new_pos = offset + dir->length;
dir_parent = dir;
new_address = address + dir->length;
while (new_pos < block_size) {
dir_next = get_free_dir_record();
create_dir_record(dir_next, b_data(bp) + new_pos, new_address);
if (dir_next->length > 0) {
strncpy(name,dir_next->file_id,dir_next->length_file_id);
name[dir_next->length_file_id] = '\0';
strncpy(old_name, dir_parent->file_id,
dir_parent->length_file_id);
old_name[dir_parent->length_file_id] = '\0';
if (strcmp(name, old_name) == 0) {
dir_parent->d_next = dir_next;
dir_next->d_prior = dir_parent;
/* Link the dir records */
dir_tmp = dir_next;
size = dir_tmp->data_length_l;
/* Update the file size */
while (dir_tmp->d_prior != NULL) {
dir_tmp = dir_tmp->d_prior;
size += dir_tmp->data_length_l;
dir_tmp->d_file_size = size;
}
new_pos += dir_parent->length;
new_address += dir_next->length;
dir_parent = dir_next;
} else { /* This is another inode. */
release_dir_record(dir_next);
new_pos = block_size;
}
} else { /* record not valid */
release_dir_record(dir_next);
new_pos = block_size; /* Exit from the while */
}
}
put_block(bp); /* Release the block read. */
return(dir);
}