@@ -0,0 +1,200 @@
#include "include.h"

//change shutup to dir

typedef unsigned int u32;

typedef struct ext2_group_desc GD;
typedef struct ext2_super_block SUPER;
typedef struct ext2_inode INODE;
typedef struct ext2_dir_entry_2 DIR;

GD *gd;
SUPER *sp;
INODE *ip;
DIR *dp;

char *disk = "mydisk";

char *entries[256] = { 0 };

int search(INODE * inodePtr, char * name)
{
//get the data block of inodePtr
printf("===================================\n");
ip = inodePtr;
int k, i;

char *folder_name = basename(name);

//printf("Found %d token(s)\n", count);

char *cp; char temp[256];
DIR *dp;

// ASSUME INODE *ip -> INODE
for (i = 0; i < 12; i++)
{
printf("i_block[%d] = %d\n", i, ip->i_block[i]); // print blk number
get_block(fd, ip->i_block[i], buf); // read INODE's i_block[0]
cp = buf;
dp = (DIR*)buf;
while(cp < buf + BLKSIZE){
strncpy(temp, dp->name, dp->name_len);
temp[dp->name_len] = 0;
if (dp->rec_len == 0) { return printf("Could not find %s\n", folder_name); }
printf("%.4d %.4d %.4d [%s]\n", dp->inode, dp->rec_len, dp->name_len, temp);
if (strcmp(dp->name, folder_name) == 0)
{
printf("Found %s\n", folder_name);

return dp->inode;
}
//getchar();
// move to the next DIR entry:
cp += (dp->rec_len); // advance cp by rec_len BYTEs
dp = (DIR*)cp; // pull dp along to the next record
}
}

return 0;
}

int get_block(int fd, int blk, char buf[ ])
{
lseek(fd, (long)blk*BLKSIZE, 0);
read(fd, buf, BLKSIZE);
}

int print_superblock()
{
printf("-----------------------------------------\n");
printf("\t\tSUPERBLOCK INFORMATION\n");
get_block(fd,1, buf);
sp = (SUPER*)buf;

if (sp->s_magic != 0xEF53){
printf("NOT an EXT2 FS\n");
exit(1);
}

printf("EXT2 FS OK\n");

printf("s_inodes_count = %d\n", sp->s_inodes_count);
printf("s_blocks_count = %d\n", sp->s_blocks_count);
printf("s_free_blocks_count = %d\n", sp->s_free_blocks_count);
printf("s_free_inodes_count = %d\n", sp->s_free_inodes_count);
printf("s_first_data_block = %d\n", sp->s_first_data_block);
printf("s_log_block_size = %d\n", sp->s_log_block_size);
printf("s_blocks_per_group = %d\n", sp->s_blocks_per_group);

printf("s_mnt_count = %d\n", sp->s_mnt_count);
printf("s_max_mnt_count = %d\n", sp->s_max_mnt_count);

printf("s_magic = %.4x\n", sp->s_magic);

printf("s_state = %d\n", sp->s_state);
printf("s_errors = %d\n", sp->s_errors);
printf("s_minor_rev_level = %d\n", sp->s_minor_rev_level);
printf("s_lastcheck = %d\n", sp->s_lastcheck);
printf("s_checkinterval = %d\n", sp->s_checkinterval);
printf("s_creator_os = %d\n", sp->s_creator_os);
printf("s_rev_level = %d\n", sp->s_rev_level);
printf("s_def_resuid = %d\n", sp->s_def_resuid);
printf("s_def_resgid = %d\n", sp->s_def_resgid);
printf("s_first_ino = %d\n", sp->s_first_ino);
printf("s_inode_size = %d\n", sp->s_inode_size);

printf("-----------------------------------------\n");
}

int showblock(char *path)
{
get_block(fd, 2, buf);
gd = (GD*)buf;

bg_inode_table = gd->bg_inode_table;

get_block(fd, bg_inode_table, buf);

INODE *InodesBeginBlock = (INODE *)buf;

if (InodesBeginBlock == 0)
printf("Failed to find InodesBeginBlock\n");
else
printf("InodesBeginBlock is not null\n");

InodesBeginBlock += 1;//sample_dir->rec_len;

return;

int n = 0;
char *entries[256] = { 0 };
int i;

char * s = strtok(path, "/");

if (s == 0) s = "/";

printf("%s\n", s);

entries[i] = s;
i++;

while ( s = strtok(0, "/") )
{
printf("%s\n", s);
entries[i] = s;
i++;
}

entries[i] = 0;
n = i;

printf("n = %d\n\n", n);

getchar();

int previous = -1;

for (i = 0; i < n; i++)
{
char *current_name = entries[i];
int res = search(InodesBeginBlock,current_name);
if (res == 0){ return printf("Could not find %s\n", path); }
else { //get the next one
int next_inode = (res - 1) / 8 + bg_inode_table;
int offset = (res - 1) % 8;
get_block(fd, next_inode, buf);
InodesBeginBlock = (INODE *)buf + offset;
}
}
}

int showblock_main(int argc, char *argv[])
{
if (argc > 1)
{
disk = argv[1];
}

printf("Opening %s\n", disk);

fd = open(disk, O_RDONLY);

if (fd < 0)
{
printf("Open failed\n");
exit(1);
}

char path[1024];
getcwd(path, 1024);

if (argc > 1) strcpy(path, argv[2]);

print_superblock();
showblock(path);

return 0;
}
111 type.h
@@ -0,0 +1,111 @@
/* type.h for CS360 Project */

#ifndef TYPE_H
#define TYPE_H

#include <stdio.h>
#include <stdlib.h>

#include <fcntl.h>
#include <ext2fs/ext2_fs.h>
#include <libgen.h>
#include <string.h>
#include <sys/stat.h>

// define shorter TYPES, save typing efforts
typedef struct ext2_group_desc GD;
typedef struct ext2_super_block SUPER;
typedef struct ext2_inode INODE;
typedef struct ext2_dir_entry_2 DIR; // need this for new version of e2fs

GD *gp;
SUPER *sp;
INODE *ip;
DIR *dp;

#define BLOCK_SIZE 1024
#define BLKSIZE 1024
#define BITS_PER_BLOCK (8*BLOCK_SIZE)
#define INODES_PER_BLOCK (BLOCK_SIZE/sizeof(INODE))

// Block number of EXT2 FS on FD
#define SUPERBLOCK 1
#define GDBLOCK 2
#define BBITMAP 3
#define IBITMAP 4
#define INODEBLOCK 5
#define ROOT_INODE 2

// Default dir and regulsr file modes
#define DIR_MODE 0040777
#define FILE_MODE 0100644
#define SUPER_MAGIC 0xEF53
#define SUPER_USER 0

// Proc status
#define FREE 0
#define BUSY 1
#define KILLED 2

// Table sizes
#define NMINODES 50
#define NMOUNT 10
#define NPROC 10
#define NFD 10
#define NOFT 50

// Open File Table
typedef struct Oft{
int mode;
int refCount;
struct Minode *inodeptr;
long offset;
} OFT;

// PROC structure
typedef struct Proc{
int uid;
int pid;
int gid;
int ppid;
int status;

struct Minode *cwd;
OFT *fd[NFD];

struct Proc *next;
struct Proc *parent;
struct Proc *child;
struct Proc *sibling;
} PROC;

// In-memory inodes structure
typedef struct Minode{
INODE INODE; // disk inode
int dev, ino;

int refCount;
int dirty;
int mounted;
struct Mount *mountptr;
char name[128]; // name string of file
} MINODE;

// Mount Table structure
typedef struct Mount{
int ninodes;
int nblocks;
int bmap;
int imap;
int iblock;
int dev, busy;
struct Minode *mounted_inode;
char name[256];
char mount_name[64];
} MOUNT;

// function proto types
MINODE *iget();
OFT *falloc();

#endif