8000 minix/minix/fs/ext2/read.c at master · estinca/minix · GitHub
[go: up one dir, main page]

Skip to content

Latest commit

 

History

History
557 lines (473 loc) · 17.6 KB

read.c

File metadata and controls

557 lines (473 loc) · 17.6 KB
off_t pos, off, block_pos, new_pos, ent_pos;
struct buf *bp;
struct ext2_disk_dir_desc *d_desc;
ino_t child_nr;
/* Check whether the position is properly aligned */
pos = *posp;
if ((unsigned int) pos % DIR_ENTRY_ALIGN)
return(ENOENT);
if ((rip = get_inode(fs_dev, ino_nr)) == NULL)
return(EINVAL);
block_size = rip->i_sp->s_block_size;
off = (pos % block_size); /* Offset in block */
block_pos = pos - off;
done = FALSE; /* Stop processing directory blocks when done is set */
fsdriver_dentry_init(&fsdentry, data, bytes, getdents_buf,
sizeof(getdents_buf));
/* The default position for the next request is EOF. If the user's buffer
* fills up before EOF, new_pos will be modified. */
new_pos = rip->i_size;
r = 0;
for (; block_pos < rip->i_size; block_pos += block_size) {
off_t temp_pos = block_pos;
/* Since directories don't have holes, 'bp' cannot be NULL. */
bp = get_block_map(rip, block_pos); /* get a dir block */
assert(bp != NULL);
assert(bp != NULL);
/* Search a directory block. */
d_desc = (struct ext2_disk_dir_desc*) &b_data(bp);
/* we need to seek to entry at off bytes.
* when NEXT_DISC_DIR_POS == block_size it's last dentry.
*/
for (; temp_pos + conv2(le_CPU, d_desc->d_rec_len) <= pos
&& NEXT_DISC_DIR_POS(d_desc, &b_data(bp)) < block_size;
d_desc = NEXT_DISC_DIR_DESC(d_desc)) {
temp_pos += conv2(le_CPU, d_desc->d_rec_len);
}
for (; CUR_DISC_DIR_POS(d_desc, &b_data(bp)) < block_size;
d_desc = NEXT_DISC_DIR_DESC(d_desc)) {
if (d_desc->d_ino == 0)
continue; /* Entry is not in use */
len = d_desc->d_name_len;
assert(len <= NAME_MAX);
assert(len <= EXT2_NAME_MAX);
/* Need the position of this entry in the directory */
ent_pos = block_pos + ((char *)d_desc - b_data(bp));
child_nr = (ino_t) conv4(le_CPU, d_desc->d_ino);
r = fsdriver_dentry_add(&fsdentry, child_nr, d_desc->d_name,
len, get_dtype(d_desc));
/* If the user buffer is full, or an error occurred, stop. */
if (r <= 0) {
done = TRUE;
/* Record the position of this entry, it is the
* starting point of the next request (unless the
* position is modified with lseek).
*/
new_pos = ent_pos;
break;
}
}
put_block(bp);
if (done)
break;
}
if (r >= 0 && (r = fsdriver_dentry_finish(&fsdentry)) >= 0) {
*posp = new_pos;
rip->i_update |= ATIME;
rip->i_dirt = IN_DIRTY;
}
put_inode(rip); /* release the inode */
return(r);
}
0