Changeset 11

Show
Ignore:
Timestamp:
10/28/07 17:24:58 (10 months ago)
Author:
nlawren2
Message:

--

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • tarfs/tarfs.c

    r10 r11  
    11#include <stdlib.h> 
    22#include <stdio.h> 
     3 
    34#include <string.h> 
     5#include <unistd.h> 
     6#include <ctype.h> 
    47 
    58#include <fuse/fuse.h> 
     
    6568} 
    6669 
    67 static int tarfs_readdir(const char *file_path, void *buffer, fuse_fill_dir_t filler, off_t offset, struct fuse_file_info *fi); 
    68  
    69 static int tarfs_getattr(const char*, struct stat*) 
    70 
    71    int res = 0; 
    72  
    73    stbuf->st_mode = S_IFDIR | 0555; 
    74    stbuf->st_nlink = 2; 
    75    return res; 
    76 
    77  
    78 static int tarfs_read(const char *file_path, void *buffer, size_t size, off_t offset, struct fuse_file_info *fi); 
     70static int tarfs_readdir(const char *file_path, void *buffer, fuse_fill_dir_t filler, off_t offset, struct fuse_file_info *fi){ 
     71        char buffer[BLOCK_SIZE]; 
    7972        char *result; 
    8073        char delims[]="/"; 
    8174        inode *i=file_tree, *node; 
    82         size_t len
    83  
     75        int size
     76         
    8477        /* Traverse the file tree */ 
    8578        result=strtok(file_path, delims); 
     
    9285                                } 
    9386                                else{ 
     87                                        return -ENOENT; 
     88                                } 
     89                        } 
     90                        i=i->next; 
     91                } 
     92                result=strtok(NULL, delims); 
     93        } 
     94 
     95        return -ENOENT; 
     96 
     97readdir: 
     98        while(i){ 
     99                filler(buffer,i->file_name,NULL,0); 
     100                i=i->next; 
     101        } 
     102         
     103        return 0; 
     104} 
     105 
     106static int tarfs_getattr(const char*, struct stat*) 
     107{ 
     108   int res = 0; 
     109   stbuf->st_mode = S_IFDIR | 0555; 
     110   stbuf->st_nlink = 2; 
     111   return res; 
     112} 
     113 
     114static int tarfs_read(const char *file_path, void *buffer, size_t size, off_t offset, struct fuse_file_info *fi); 
     115        char *result; 
     116        char delims[]="/"; 
     117        inode *i=file_tree, *node; 
     118        size_t len; 
     119 
     120        /* Traverse the file tree */ 
     121        result=strtok(file_path, delims); 
     122        while( result != NULL ){ 
     123                while(i){ 
     124                        if(!strcmp(result, i->file_name)){ 
     125                                if(i->subdir){ 
     126                                        i=i->subdir; 
     127                                        break; 
     128                                } 
     129                                else{ 
    94130                                        node=i; 
    95131                                        goto read; 
     
    106142        /* Get the file length */ 
    107143        len=node->file_length; 
    108  
    109144        if (offset < len) { 
    110145                /* Seek to the position of the file in the tar */ 
     
    128163        char delims[]="/"; 
    129164        posix_header header; 
    130         inode *file_tree, *i, *last_i, *new; 
     165        inode *i, *last_i, *new; 
    131166 
    132167        /* Create a root directory of the tree */ 
     
    225260} 
    226261 
     262static struct fuse_operations tarfs_oper = { 
     263        .getattr =  tarfs_getattr, 
     264        .read = tarfs_read, 
     265        .open = tarfs_open, 
     266        .readdir = tarfs_readdir, 
     267        .destroy = tarfs_destroy 
     268} 
     269 
    227270int main(int argc, char **argv){ 
    228         // Getopts to parse -f filepath 
    229          
    230         char filepath[NAME_SIZE]; 
    231         if(argc < 2){ 
    232                 eprintf("Use %s filename", argv[0]); 
     271        int c; 
     272         
     273        /* Parse filepath and initialize the file tree */ 
     274        while( (c=getopt(argc, argv, "f")) != -1 ){ 
     275                switch(c){ 
     276                case 'f': 
     277                { 
     278                        tar_file = fopen(optarg); 
     279                        if( !tar_file ){ 
     280                                eprintf("Invalid filename: %s\n", optarg); 
     281                                return 1; 
     282                        } 
     283                        file_tree=tarfs_init(tar_file); 
     284                        if( !file_tree ){ 
     285                                eprintf("Invalid file: %s\n", optarg); 
     286                                return 1; 
     287                        } 
     288                        break; 
     289                } 
     290                case '?': 
     291                default: 
     292                        break; 
     293                } 
     294        } 
     295         
     296        if( !file_tree ){ 
     297                eprintf("Use: %s -f filepath\n", argv[0]); 
    233298                return 1; 
    234299        } 
    235300 
    236         tar_file = fopen(argv[1],"r"); 
    237         if( tar_file == NULL ){ 
    238                 eprintf("Invalid filename: %s", argv[1]); 
    239                 return 1; 
    240         } 
    241  
    242         file_tree=create_file_tree(tar_file); 
    243         if( file_tree == NULL ){ 
    244                 eprintf("Invalid filetype: %s", argv[1]); 
    245                 return 1; 
    246         } 
    247          
    248         inode *i=read_file(tar_file, file_tree, filepath); 
    249          
    250         if( i == NULL ){ 
    251                 eprintf("Invalid filename: %s", filepath); 
    252                 return 1; 
    253         } 
    254  
    255         // fuse_main 
    256          
    257         destroy_file_tree(file_tree); 
    258  
    259         return 0; 
    260 
     301        return fuse_main(argc, argv, tarfs_opers); 
     302