Advertisement
image28

raw.c

Aug 26th, 2016
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.25 KB | None | 0 0
  1. // Image28:
  2. // A raw filesystem for use with usb keys, functions with a put/get system.
  3. // Drive appears un-formated to all operating systems known to me
  4. // Files are stored sequentially, so if index is lost they can be recovered
  5. // Index is meant to be stored on a separate drive.
  6.  
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9.  
  10. #define OPT1 "print"
  11.  
  12. int main(int argc, char *argv[])
  13. {
  14.     int file=0;
  15.  
  16.     if ( argc < 2 )
  17.     {
  18.         printf("Incorrect arguments\n");
  19.         exit(-1);
  20.     }
  21.  
  22.     if ( ! strcmp(argv[1],"print") )
  23.     {
  24.         // index
  25.         print_index(argv[2]);
  26.     }else if ( ! strcmp(argv[1],"put") )
  27.     {
  28.         // path newfile index device
  29.         put_file(argv[2],argv[3],argv[4], argv[5]);
  30.     }else if ( ! strcmp(argv[1],"get") ){
  31.         file=atoi(argv[2]);
  32.  
  33.         // file number index device
  34.         get_file(file, argv[3], argv[4]);
  35.  
  36.     }else{
  37.         test(argv[1]);
  38.         printf("Unknown Option\n");
  39.     }
  40.    
  41.  
  42.     return(0); 
  43. }
  44.  
  45. int print_index(char *file)
  46. {
  47.     FILE *input;
  48.     int count=0;
  49.     int fileCount=0;
  50.     int filesize=0;
  51.     char curfile[32768];
  52.  
  53.     if ( ( input=fopen(file,"rb") ) == NULL )
  54.     {
  55.         printf("No Index File Found\n");
  56.         exit(-1);
  57.     }
  58.  
  59.     while ( ! feof(input) )
  60.     {
  61.         filesize=0;
  62.         fread(&filesize,1,4,input);
  63.         if ( ! feof(input) )
  64.         {
  65.             fread(&curfile[count],1,1,input);
  66.             while ( curfile[count] != '\n' )
  67.             {
  68.                 count++;
  69.                 fread(&curfile[count],1,1,input);
  70.             }
  71.             curfile[count]='\0';
  72.             printf("%d: %s\t%d\n",fileCount,curfile,filesize);
  73.             count=0;
  74.             curfile[0]='\0';
  75.             fileCount++;
  76.         }
  77.     }
  78.  
  79.     fclose(input);
  80.  
  81.     return(0);
  82. }
  83.  
  84. int get_file(int file, char *index, char *devfile)
  85. {
  86.     FILE *output;
  87.     FILE *input;
  88.     FILE *device;
  89.     int inbyte=0;
  90.     char outfile[32768];
  91.     int d=0;
  92.     int found=0;
  93.     int filesize=0;
  94.     int count=0;
  95.     char curfile[32768];
  96.     int fileCount=0;
  97.     int start=0;
  98.     int pos=0;
  99.     int tempsize=0;
  100.  
  101.     if ( ( input=fopen(index,"rb") ) == NULL )
  102.     {
  103.         printf("No Index File Found\n");
  104.         exit(-1);
  105.     }
  106.  
  107.     if ( ( device=fopen(devfile,"rb") ) == NULL )
  108.     {
  109.         printf("Could not open raw device\n");
  110.         exit(-1);
  111.     }
  112.  
  113.     while ( ( ! feof(input) ) || ( ! found ) )
  114.     {
  115.         filesize=0;
  116.         fread(&filesize,1,4,input);
  117.        
  118.         if ( ! feof(input) )
  119.         {
  120.             fread(&curfile[count],1,1,input);
  121.             while ( curfile[count] != '\n' )
  122.             {
  123.                 count++;
  124.                 fread(&curfile[count],1,1,input);
  125.             }
  126.             curfile[count]='\0';
  127.            
  128.             if ( fileCount == file )
  129.             {
  130.                 for(d=strlen(curfile); d > -1; d--)
  131.                 {
  132.                     if ( curfile[d] == '/' )
  133.                     {
  134.                         start=d+1;
  135.                         d=-1;
  136.                     }
  137.                 }
  138.  
  139.                 for(d=start;d<strlen(curfile);d++)
  140.                 {  
  141.                     outfile[d-start]=curfile[d];
  142.                    
  143.                 }
  144.                 outfile[strlen(curfile)-start]='\0';
  145.                 found=1;
  146.                 tempsize=filesize;
  147.             }else{
  148.                 fseek(device,filesize,SEEK_CUR);
  149.             }
  150.  
  151.             count=0;
  152.             curfile[0]='\0';
  153.             fileCount++;
  154.         }
  155.     }
  156.  
  157.     fclose(input);
  158.  
  159.     if ( found )   
  160.     {
  161.         if ( ( output=fopen(outfile,"wb") ) == NULL )
  162.         {
  163.             printf("Could not create output file\n");
  164.             exit(-1);
  165.         }
  166.  
  167.    
  168.         while ( pos < tempsize )   
  169.         {
  170.             fread(&inbyte,1,1,device);
  171.             printf("%c %d\n",inbyte,pos);
  172.             fwrite(&inbyte,1,1,output);
  173.             pos++;
  174.         }
  175.  
  176.         fclose(output);
  177.        
  178.     }
  179.  
  180.     return(0);
  181. }
  182.  
  183. int put_file(char *path, char *newfile, char *index, char *devfile)
  184. {
  185.     FILE *add;
  186.     FILE *input;
  187.     FILE *device;
  188.     int inbyte=0;
  189.     int pos=0;
  190.     int filesize=0;
  191.     char curfile[32768];
  192.     int start=0;
  193.     int addsize=0;
  194.     int count=0;
  195.     int fileCount=0;
  196.     int d=0;
  197.     int temp=0;
  198.  
  199.     if ( ( input=fopen(index,"a+") ) == NULL )
  200.     {
  201.         printf("No Index File Found\n");
  202.         exit(-1);
  203.     }
  204.  
  205.     if ( ( device=fopen(devfile,"wb") ) == NULL )
  206.     {
  207.         printf("Could not open raw device\n");
  208.         exit(-1);
  209.     }
  210.  
  211.     if ( ( add=fopen(newfile,"rb") ) == NULL )
  212.     {
  213.         printf("Could not open file to add to device\n");
  214.         exit(-1);
  215.     }
  216.  
  217.     // Get the filesize of the new file to be added
  218.     fseek(add,0L,SEEK_END);
  219.     addsize=ftell(add);
  220.     fseek(add,0L,SEEK_SET);
  221.  
  222.     // read the index and seek to the correct position in the device
  223.     fseek(input,0L,SEEK_SET);
  224.  
  225.     while ( ! feof(input) )
  226.     {
  227.         filesize=0;
  228.         fread(&filesize,1,4,input);
  229.         fseek(device,filesize,SEEK_CUR);
  230.         if ( ! feof(input) )
  231.         {
  232.             fread(&curfile[count],1,1,input);
  233.             while ( curfile[count] != '\n' )
  234.             {
  235.                 count++;
  236.                 fread(&curfile[count],1,1,input);
  237.             }
  238.             curfile[count]='\0';
  239.             printf("%d: %s\t%d\n",fileCount,curfile,filesize);
  240.             count=0;
  241.             curfile[0]='\0';
  242.             fileCount++;
  243.         }
  244.     }
  245.  
  246.     fseek(input,0L,SEEK_END);
  247.     // write the index entry
  248.     fwrite(&addsize,1,4,input);
  249.     for(d=0;d<strlen(path);d++)
  250.     {
  251.         temp=*(path+d);
  252.         fwrite(&temp,1,1,input);
  253.     }
  254.    
  255.     for(d=strlen(newfile); d > -1; d--)
  256.     {
  257.         if ( *(newfile+d) == '/' )
  258.         {
  259.             start=d+1;
  260.             d=-1;
  261.         }
  262.     }
  263.  
  264.     for(d=start;d<strlen(newfile);d++)
  265.     {
  266.         temp=0;
  267.         temp=*(newfile+d);
  268.         fwrite(&temp,1,1,input);
  269.     }
  270.     temp='\n';
  271.     fwrite(&temp,1,1,input);
  272.  
  273.  
  274.     // write the file to the device
  275.     while ( pos < addsize )
  276.     {
  277.         inbyte=0;
  278.         fread(&inbyte,1,1,add);
  279.         fwrite(&inbyte,1,1,device);
  280.         pos++;
  281.     }
  282.  
  283.     fclose(input);
  284.     fclose(add);
  285.     fclose(device);
  286.  
  287.     return(0);
  288. }
  289.  
  290. int test(char *devfile)
  291. {
  292.     int count=0;
  293.     FILE *device;
  294.     char inbyte='\0';
  295.  
  296.     device=fopen(devfile,"rb") ;
  297.        
  298.     while ( count < 64 )
  299.     {
  300.         fread(&inbyte,1,1,device);
  301.         printf("%c",inbyte);
  302.         count++;
  303.     }
  304.  
  305.     printf("\n");
  306.     fclose(device);
  307.     return(0);
  308. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement