/*
* Copyright (c) 1987,1997, Prentice Hall
* All rights reserved.
*
* Redistribution and use of the MINIX operating system in source and
* binary forms, with or without modification, are permitted provided
* that the following conditions are met:
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* - Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
* - Neither the name of Prentice Hall nor the names of the software
* authors or contributors may be used to endorse or promote
* products derived from this software without specific prior
* written permission.
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS, AUTHORS, AND
* CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL PRENTICE HALL OR ANY AUTHORS OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/** @addtogroup FileSystemImpl
* @{
*/
/**
* @file dir.c
* @brief This file contains the procedures for support directory operations.
*/
#include <stdio.h>
#include "fs.h"
#include "dir.h"
#include "block.h"
#include "inode.h"
#include "param.h"
/**
* Return total number of directory entries
*/
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;
}
/**
* Fill in the user buffer with directory entry specified by number
*/
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;
}
/**
* }
*/