Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Image28:
- // A raw filesystem for use with usb keys, functions with a put/get system.
- // Drive appears un-formated to all operating systems known to me
- // Files are stored sequentially, so if index is lost they can be recovered
- // Index is meant to be stored on a separate drive.
- #include <stdio.h>
- #include <stdlib.h>
- #define OPT1 "print"
- int main(int argc, char *argv[])
- {
- int file=0;
- if ( argc < 2 )
- {
- printf("Incorrect arguments\n");
- exit(-1);
- }
- if ( ! strcmp(argv[1],"print") )
- {
- // index
- print_index(argv[2]);
- }else if ( ! strcmp(argv[1],"put") )
- {
- // path newfile index device
- put_file(argv[2],argv[3],argv[4], argv[5]);
- }else if ( ! strcmp(argv[1],"get") ){
- file=atoi(argv[2]);
- // file number index device
- get_file(file, argv[3], argv[4]);
- }else{
- test(argv[1]);
- printf("Unknown Option\n");
- }
- return(0);
- }
- int print_index(char *file)
- {
- FILE *input;
- int count=0;
- int fileCount=0;
- int filesize=0;
- char curfile[32768];
- if ( ( input=fopen(file,"rb") ) == NULL )
- {
- printf("No Index File Found\n");
- exit(-1);
- }
- while ( ! feof(input) )
- {
- filesize=0;
- fread(&filesize,1,4,input);
- if ( ! feof(input) )
- {
- fread(&curfile[count],1,1,input);
- while ( curfile[count] != '\n' )
- {
- count++;
- fread(&curfile[count],1,1,input);
- }
- curfile[count]='\0';
- printf("%d: %s\t%d\n",fileCount,curfile,filesize);
- count=0;
- curfile[0]='\0';
- fileCount++;
- }
- }
- fclose(input);
- return(0);
- }
- int get_file(int file, char *index, char *devfile)
- {
- FILE *output;
- FILE *input;
- FILE *device;
- int inbyte=0;
- char outfile[32768];
- int d=0;
- int found=0;
- int filesize=0;
- int count=0;
- char curfile[32768];
- int fileCount=0;
- int start=0;
- int pos=0;
- int tempsize=0;
- if ( ( input=fopen(index,"rb") ) == NULL )
- {
- printf("No Index File Found\n");
- exit(-1);
- }
- if ( ( device=fopen(devfile,"rb") ) == NULL )
- {
- printf("Could not open raw device\n");
- exit(-1);
- }
- while ( ( ! feof(input) ) || ( ! found ) )
- {
- filesize=0;
- fread(&filesize,1,4,input);
- if ( ! feof(input) )
- {
- fread(&curfile[count],1,1,input);
- while ( curfile[count] != '\n' )
- {
- count++;
- fread(&curfile[count],1,1,input);
- }
- curfile[count]='\0';
- if ( fileCount == file )
- {
- for(d=strlen(curfile); d > -1; d--)
- {
- if ( curfile[d] == '/' )
- {
- start=d+1;
- d=-1;
- }
- }
- for(d=start;d<strlen(curfile);d++)
- {
- outfile[d-start]=curfile[d];
- }
- outfile[strlen(curfile)-start]='\0';
- found=1;
- tempsize=filesize;
- }else{
- fseek(device,filesize,SEEK_CUR);
- }
- count=0;
- curfile[0]='\0';
- fileCount++;
- }
- }
- fclose(input);
- if ( found )
- {
- if ( ( output=fopen(outfile,"wb") ) == NULL )
- {
- printf("Could not create output file\n");
- exit(-1);
- }
- while ( pos < tempsize )
- {
- fread(&inbyte,1,1,device);
- printf("%c %d\n",inbyte,pos);
- fwrite(&inbyte,1,1,output);
- pos++;
- }
- fclose(output);
- }
- return(0);
- }
- int put_file(char *path, char *newfile, char *index, char *devfile)
- {
- FILE *add;
- FILE *input;
- FILE *device;
- int inbyte=0;
- int pos=0;
- int filesize=0;
- char curfile[32768];
- int start=0;
- int addsize=0;
- int count=0;
- int fileCount=0;
- int d=0;
- int temp=0;
- if ( ( input=fopen(index,"a+") ) == NULL )
- {
- printf("No Index File Found\n");
- exit(-1);
- }
- if ( ( device=fopen(devfile,"wb") ) == NULL )
- {
- printf("Could not open raw device\n");
- exit(-1);
- }
- if ( ( add=fopen(newfile,"rb") ) == NULL )
- {
- printf("Could not open file to add to device\n");
- exit(-1);
- }
- // Get the filesize of the new file to be added
- fseek(add,0L,SEEK_END);
- addsize=ftell(add);
- fseek(add,0L,SEEK_SET);
- // read the index and seek to the correct position in the device
- fseek(input,0L,SEEK_SET);
- while ( ! feof(input) )
- {
- filesize=0;
- fread(&filesize,1,4,input);
- fseek(device,filesize,SEEK_CUR);
- if ( ! feof(input) )
- {
- fread(&curfile[count],1,1,input);
- while ( curfile[count] != '\n' )
- {
- count++;
- fread(&curfile[count],1,1,input);
- }
- curfile[count]='\0';
- printf("%d: %s\t%d\n",fileCount,curfile,filesize);
- count=0;
- curfile[0]='\0';
- fileCount++;
- }
- }
- fseek(input,0L,SEEK_END);
- // write the index entry
- fwrite(&addsize,1,4,input);
- for(d=0;d<strlen(path);d++)
- {
- temp=*(path+d);
- fwrite(&temp,1,1,input);
- }
- for(d=strlen(newfile); d > -1; d--)
- {
- if ( *(newfile+d) == '/' )
- {
- start=d+1;
- d=-1;
- }
- }
- for(d=start;d<strlen(newfile);d++)
- {
- temp=0;
- temp=*(newfile+d);
- fwrite(&temp,1,1,input);
- }
- temp='\n';
- fwrite(&temp,1,1,input);
- // write the file to the device
- while ( pos < addsize )
- {
- inbyte=0;
- fread(&inbyte,1,1,add);
- fwrite(&inbyte,1,1,device);
- pos++;
- }
- fclose(input);
- fclose(add);
- fclose(device);
- return(0);
- }
- int test(char *devfile)
- {
- int count=0;
- FILE *device;
- char inbyte='\0';
- device=fopen(devfile,"rb") ;
- while ( count < 64 )
- {
- fread(&inbyte,1,1,device);
- printf("%c",inbyte);
- count++;
- }
- printf("\n");
- fclose(device);
- return(0);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement