0,0 → 1,95 |
/* This file contains the procedures for support directory operations. */ |
|
/* Methods: |
* do_sum: return total number of directory entries |
* do_readentry: fill in the user buffer with directory entry specified by number |
*/ |
|
|
#include <stdio.h> |
#include "fs.h" |
#include "dir.h" |
#include "block.h" |
#include "inode.h" |
#include "param.h" |
|
|
int do_sum() |
{ |
|
/* Perform dsum system call. */ |
|
inode_t *rip; |
int dir_count; |
offset_t dir_size; |
|
rip = fp->fp_workdir; |
dir_size = rip->i_size; |
|
/* We must calculate with both versions of directory entry sizes. */ |
if (rip->i_sp->s_extend) { |
dir_count = dir_size/DIR_ENTRY_SIZE; |
} |
else { |
dir_count = dir_size/DIR_ENTRY_SIZE_EX; |
} |
if (dir_size % DIR_ENTRY_SIZE_EX != 0) { |
print_console("dir_size % DIR_ENTRY_SIZE_EX != 0\n"); |
} |
|
return dir_count; |
} |
|
|
int do_readentry() |
{ |
|
/* Perform readentry(numb, buffer) system call. */ |
|
int actual_sum, extend; |
offset_t pos; |
inode_t *rip; |
block_num_t b; |
block_t *bp; |
register int r; |
direct_t *dir; |
directex_t *direx; |
|
/* Get actual number of directory entries within the block. */ |
actual_sum = do_sum(); |
if (entry_number < 0 || actual_sum <= entry_number) |
return FS_EINVAL; |
|
rip = fp->fp_workdir; |
extend = rip->i_sp->s_extend; |
|
if (extend) { |
pos = entry_number * DIR_ENTRY_SIZE_EX; |
} |
else { |
pos = entry_number * DIR_ENTRY_SIZE; |
} |
|
/* Get number of block within the entry is. */ |
b = read_map(rip, pos); |
if (b == NO_BLOCK) |
return 0; |
|
bp = get_block(b); |
|
/* Copy the entry data into address space of actual consument. */ |
if (extend) { |
direx = &bp->b.b__direx[entry_number]; |
memcpy(fp->buffer, (void*)direx, DIR_ENTRY_SIZE_EX); |
} |
else { |
dir = &bp->b.b__dir[entry_number]; |
memcpy(fp->buffer, (void*)dir, DIR_ENTRY_SIZE); |
} |
|
r = (extend ? DIR_ENTRY_SIZE_EX : DIR_ENTRY_SIZE); |
|
return r; |
} |
|
|