Advertisement
Guest User

Untitled

a guest
Dec 8th, 2019
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 24.03 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <float.h>
  4. #include <string.h>
  5. #include <stdbool.h>
  6.  
  7. #define EXIT 0
  8. #define INFO 1
  9. #define SIZE 2
  10. #define LS 3
  11. #define CD 4
  12. #define CREAT 5
  13. #define MKDIR 6
  14. #define OPEN 7
  15. #define CLOSE 8
  16. #define READ 9
  17. #define WRITE 10
  18. #define RM 11
  19. #define RMDIR 12
  20.  
  21. #define FILE_READ 1
  22. #define FILE_WRITE 2
  23. #define FILE_READWRITE 3
  24.  
  25. struct FAT32BootRegion{
  26. unsigned char jmpBoot[3];
  27. unsigned char oem[8];
  28. unsigned short bytes_per_sector;
  29. unsigned char sectors_per_cluster;
  30. unsigned short reserved_sectors;
  31. unsigned char number_of_fats;
  32. unsigned short root_dir_entries;
  33. unsigned short num_of_sectors;
  34. unsigned char media;
  35. unsigned short fat_size_count_short;
  36. unsigned short sectors_per_track;
  37. unsigned short num_of_heads;
  38. unsigned int hidden_sectors;
  39. unsigned int total_sectors;
  40.  
  41.  
  42. //Extended BPB Structure
  43. unsigned int BPB_FATSz32;
  44. unsigned short BPB_ExtFlags;
  45. unsigned short BPB_FSVer;
  46. unsigned int BPB_RootCluster;
  47. unsigned short BPB_FSInfo;
  48. unsigned short backup_BS_sector;
  49. unsigned char reserved_0[12];
  50. unsigned char drive_number;
  51. unsigned char reserved_1;
  52. unsigned char boot_signature;
  53. unsigned int volume_id;
  54. char volume_label[11];
  55. char fs_type[8];
  56. char misc_boot_code[420];
  57. unsigned short signature_word;
  58. }__attribute__((packed));
  59.  
  60. struct FAT32Dir{
  61. unsigned char Name[11];
  62. unsigned char Attr;
  63. unsigned char NTRes;
  64. unsigned char CrtTimeTenth;
  65. unsigned short CrtTime;
  66. unsigned short CrtDate;
  67. unsigned short LsrAccDate;
  68. unsigned short FstClusHI;
  69. unsigned short WrtTime;
  70. unsigned short WrtDate;
  71. unsigned short FstClusLO;
  72. unsigned int FileSize;
  73. }__attribute__((packed));
  74.  
  75. struct FAT32File{
  76. int mode;
  77. char * name;
  78. int first_cluster_num;
  79. };
  80.  
  81. //***********************GLOBAL VARIABLES***************************
  82. struct FAT32BootRegion boot_region;
  83. FILE * fp;
  84. static char * commands[13] = {"exit", "info", "size", "ls", "cd", "creat", "mkdir", "open", "close", "read", "write", "rm", "rmdir"};
  85. struct FAT32File openFiles[500] = {{0}};
  86.  
  87. //**********************HELPER FUNCTIONS*****************************
  88.  
  89. void parseBootSector(){
  90.  
  91. fread(&boot_region, sizeof(struct FAT32BootRegion), 1, fp);
  92. }
  93.  
  94. void prompt(){
  95. printf("FAT32-SHELL~> ");
  96. }
  97.  
  98. //Separates tokens by space
  99. int getTokens(char ** tokens){
  100.  
  101. char raw_input[1000];
  102. char * command;
  103. char * token;
  104. char * pre_processed_cmd;
  105. int token_count = 0;
  106.  
  107. fgets(raw_input, 999, stdin);
  108.  
  109. command = strtok(raw_input, "\n");
  110.  
  111. if (command == NULL)
  112. return 0;
  113.  
  114. pre_processed_cmd = strdup(command);
  115.  
  116. token = strtok(command, " ");
  117.  
  118. if(token != NULL && !strcmp(token, "write")){
  119.  
  120. char * write_string;
  121. while(token != NULL && token[0] != '"'){
  122. printf("Token: %s\n", token);
  123. tokens[token_count++] = strdup(token);
  124. token = strtok(NULL, " ");
  125. }
  126. write_string = strchr(pre_processed_cmd, '"');
  127. if(write_string != NULL){
  128. token = strtok(write_string+1, "\"");
  129. tokens[token_count++] = strdup(token);
  130. return token_count;
  131. }
  132. }
  133.  
  134. while(token != NULL){
  135. tokens[token_count++] = strdup(token);
  136. token = strtok(NULL, " ");
  137. }
  138. return token_count;
  139. }
  140.  
  141. //First Sector of Cluster given
  142. unsigned int getFirstSectorofCluster(int cluster){
  143.  
  144. unsigned int FirstDataSector = boot_region.reserved_sectors + (boot_region.number_of_fats*boot_region.BPB_FATSz32);
  145. unsigned int FirstSectorofCluster = ((cluster-2) * boot_region.sectors_per_cluster) + FirstDataSector;
  146.  
  147. return FirstSectorofCluster*boot_region.bytes_per_sector;
  148. }
  149.  
  150. //get FAT[clus] or next cluster in chain
  151. unsigned int getFatEntry(int cluster){
  152.  
  153. unsigned int fatEntry;
  154. unsigned int fatOffset = cluster * 4;
  155. unsigned int fatSector = boot_region.reserved_sectors*boot_region.bytes_per_sector + fatOffset;
  156. fseek(fp,fatSector, SEEK_SET);
  157. fread(&fatEntry, sizeof(int), 1, fp);
  158.  
  159. //printf("Fat Entry at cluster[%d]: %d\n",cluster, fatEntry);
  160. return fatEntry & 0x0FFFFFFF;
  161. }
  162.  
  163. void setFatEntry(unsigned int cluster, unsigned int entry){
  164. int fatOffset = boot_region.reserved_sectors + boot_region.bytes_per_sector * cluster*4;
  165. fseek(fp, fatOffset, SEEK_SET);
  166. fwrite(&entry, sizeof(unsigned int), 1, fp);
  167. }
  168.  
  169. //Converts stored file name into 8.3 format
  170. char * getFileName(char * storedFileName){
  171.  
  172. int i = 0;
  173. char * fileNameFormatted = strdup(storedFileName);
  174.  
  175. for(i = 0; i < strlen(storedFileName); i++){
  176.  
  177. if(!isspace(storedFileName[i]))
  178. fileNameFormatted[i] = tolower(storedFileName[i]);
  179.  
  180. else if(isspace(storedFileName[i])){
  181.  
  182. if(!isspace(storedFileName[i+1]) && (i < strlen(storedFileName)-1) )
  183. fileNameFormatted[i] = '.';
  184. else
  185. fileNameFormatted[i] = '\0';
  186. }
  187. else
  188. fileNameFormatted[i] = tolower(storedFileName[i]);
  189. }
  190.  
  191.  
  192. return fileNameFormatted;
  193.  
  194. }
  195.  
  196. //Converts stored file name into 8.3 format
  197. char * getDirName(char * storedDirName){
  198.  
  199. int i;
  200. char * dirNameFormatted = strdup(storedDirName);
  201. for(i = 0; i < strlen(storedDirName); i++){
  202.  
  203. if(isspace(storedDirName[i]))
  204. dirNameFormatted[i] = '\0';
  205.  
  206. else if(isalpha(storedDirName[i])){
  207.  
  208. dirNameFormatted[i] = tolower(storedDirName[i]);
  209. }
  210. else
  211. dirNameFormatted[i] = storedDirName[i];
  212. }
  213. return dirNameFormatted;
  214. }
  215.  
  216. //Read 32 byte FAT directory entries until we find a match by name
  217. struct FAT32Dir getDirectory(unsigned int cluster, char * directoryName){
  218. int i = 0;
  219. struct FAT32Dir directory;
  220. struct FAT32Dir emptyDir = {{0}};
  221. unsigned int FirstSectorofCluster = getFirstSectorofCluster(cluster);
  222. unsigned int FATEntry = getFatEntry(cluster);
  223.  
  224. fseek(fp, FirstSectorofCluster, SEEK_SET);
  225.  
  226. while(i*sizeof(struct FAT32Dir) < boot_region.bytes_per_sector){
  227.  
  228. fread(&directory, sizeof(struct FAT32Dir), 1, fp);
  229.  
  230. if(!strcmp(getDirName(directory.Name), directoryName))
  231. return directory;
  232.  
  233. i++;
  234. }
  235.  
  236. if( FATEntry != 0x0FFFFFF8 && FATEntry != 0x00000000 && FATEntry != 0xFFFFFFFF)
  237. return getDirectory(FATEntry, directoryName);
  238.  
  239. return emptyDir;
  240.  
  241. }
  242.  
  243. bool hasCorrectFormat(int command, int token_count){
  244.  
  245. switch(command){
  246.  
  247. case INFO:
  248. return (token_count == 1);
  249. case SIZE:
  250. return (token_count == 2);
  251. case LS:
  252. return (token_count == 2 || token_count == 1);
  253. case CD:
  254. return (token_count == 2);
  255. case CREAT:
  256. return (token_count == 2);
  257. case MKDIR:
  258. return (token_count == 2);
  259. case OPEN:
  260. return (token_count == 3);
  261. case CLOSE:
  262. return (token_count == 2);
  263. case READ:
  264. return (token_count == 4);
  265. case WRITE:
  266. return (token_count == 5);
  267. case RM:
  268. return (token_count == 2);
  269. case RMDIR:
  270. return (token_count == 2);
  271. }
  272.  
  273. return 0;
  274. }
  275.  
  276. void run_ls(unsigned int cluster_num){
  277. int i = 0;
  278. struct FAT32Dir directory;
  279. unsigned int fatEntry = getFatEntry(cluster_num);
  280. unsigned int FirstSectorofCluster = getFirstSectorofCluster(cluster_num);
  281.  
  282. fseek(fp, FirstSectorofCluster,SEEK_SET);
  283.  
  284. while(i*sizeof( struct FAT32Dir) < boot_region.bytes_per_sector){
  285.  
  286. fread(&directory, sizeof(struct FAT32Dir), 1, fp);
  287.  
  288. if(directory.Name[0] == 0x00){
  289. ++ i;
  290. continue;
  291. }
  292.  
  293. if(directory.Attr == 0x10){
  294.  
  295. printf("\e[1;34m%s\e[0m\n", getDirName((char *)directory.Name));
  296. }
  297. else
  298. printf("%s\n", getFileName((char *)directory.Name));
  299.  
  300. i++;
  301. }
  302.  
  303. if(fatEntry != 0x0FFFFFF8 &&fatEntry != 0x0FFFFFFF &&fatEntry != 0x00000000)
  304. run_ls(fatEntry);
  305. }
  306.  
  307.  
  308. //*************************** FILE SYSTEM COMMANDS********************************
  309. void info(){
  310. printf("Bytes per sector: %d\n",boot_region.bytes_per_sector);
  311. printf("Sectors per cluster: %hu\n",boot_region.sectors_per_cluster);
  312. printf("Reserved Sector Count: %d\n",boot_region.reserved_sectors);
  313. printf("Number of fats: %d\n",boot_region.number_of_fats);
  314. printf("Total Sectors: %d\n",boot_region.total_sectors);
  315. printf("FAT Size: %d\n", (boot_region.fat_size_count_short == 0) ? boot_region.BPB_FATSz32 : boot_region.fat_size_count_short == 0);
  316. printf("Root Cluster: %d\n", boot_region.BPB_RootCluster);
  317. }
  318.  
  319. unsigned int cd(unsigned int cluster, char * directoryName){
  320.  
  321. printf("Changing directory to: %s\n", directoryName);
  322.  
  323. struct FAT32Dir dir = getDirectory(cluster, directoryName);
  324.  
  325. printf("Found Directory: %s\n", dir.Name);
  326. printf("Directory Attribute: %c\n", dir.Attr);
  327.  
  328. if(dir.Attr == 0x10 && strcmp(getDirName((char *)dir.Name), directoryName) == 0){
  329.  
  330. if(dir.FstClusHI+dir.FstClusLO == 0)
  331. return boot_region.BPB_RootCluster;
  332.  
  333. return dir.FstClusHI*0x100 + dir.FstClusLO;
  334. }
  335. else{
  336.  
  337. if(strcmp(directoryName, "."))
  338. printf("Error! Directory not found\n");
  339.  
  340. return cluster;
  341. }
  342.  
  343. }
  344.  
  345. void size(unsigned int current_cluster, char * directoryName){
  346.  
  347. struct FAT32Dir directory = getDirectory(current_cluster, directoryName);
  348.  
  349. if(!strcmp(getFileName((char *)directory.Name), directoryName) && directory.Attr != 0x10)
  350. printf("Size of [%s]: %d bytes\n", directoryName, directory.FileSize);
  351. else
  352. printf("[%s] is not a file.\n");
  353. }
  354.  
  355. void ls(unsigned int root_cluster, unsigned int current_cluster, int token_count, char ** tokens){
  356.  
  357. if(token_count == 1)
  358. run_ls(current_cluster);
  359. else{
  360. if(!strcmp(tokens[1], ".")){
  361. run_ls(current_cluster);
  362. return;
  363. }
  364.  
  365. unsigned int next_cluster = cd(current_cluster, tokens[1]);
  366. if(next_cluster != current_cluster){
  367. root_cluster = current_cluster;
  368. current_cluster = next_cluster;
  369. run_ls(current_cluster);
  370. current_cluster = root_cluster;
  371. }
  372. else{
  373. printf("%s: Is not a directory.\n",tokens[1]);
  374.  
  375. }
  376. }
  377. }
  378.  
  379. int findEmptyCluster(){
  380.  
  381. int i;
  382. unsigned int fatStart = boot_region.reserved_sectors * boot_region.bytes_per_sector;
  383. unsigned fatEnd = fatStart + boot_region.total_sectors;
  384.  
  385. for(i = 0; fatStart + i * (sizeof(int)) < fatEnd; i++){
  386. if(!getFatEntry(i))
  387. return i;
  388.  
  389. }
  390. return -1;
  391. }
  392.  
  393. void linkNewCluster(unsigned int parent_cluster, unsigned int new_cluster){
  394.  
  395. unsigned int lastEntry = 0x0FFFFFFF;
  396. setFatEntry(parent_cluster, new_cluster);
  397. setFatEntry(new_cluster, lastEntry);
  398.  
  399. }
  400. int findFirstEmptyDirEntry(unsigned int current_cluster){
  401.  
  402. int i = 0;
  403. int next_free_cluster;
  404. struct FAT32Dir directory;
  405. unsigned int fatEntry = getFatEntry(current_cluster);
  406. unsigned int FirstSectorofCluster = getFirstSectorofCluster(current_cluster);
  407.  
  408. printf("First sector of cluster: %u\n", FirstSectorofCluster);
  409.  
  410. fseek(fp, FirstSectorofCluster, SEEK_SET);
  411.  
  412. while(i * sizeof(struct FAT32Dir) < boot_region.bytes_per_sector){
  413.  
  414. fread(&directory, sizeof(struct FAT32Dir), 1, fp);
  415. printf("Directory name: %s\n", directory.Name);
  416. if(directory.Name[0] == 0x00){
  417. return (i * sizeof(struct FAT32Dir)) + current_cluster;
  418. }
  419.  
  420. i++;
  421. }
  422.  
  423. if(fatEntry != 0x0FFFFFF8 &&fatEntry != 0x0FFFFFFF)
  424. findFirstEmptyDirEntry(fatEntry);
  425.  
  426. //CLuster is full, get next free cluster
  427. if(fatEntry == 0x0FFFFFF8 || fatEntry == 0x0FFFFFFF){
  428. next_free_cluster = findEmptyCluster();
  429. linkNewCluster(current_cluster, next_free_cluster);
  430. return getFirstSectorofCluster(next_free_cluster);
  431. }
  432.  
  433. return -1;
  434. }
  435.  
  436. void makeNewDirectory(struct FAT32Dir* directory, unsigned char attr, char * name, unsigned short FstClusHI, unsigned FstClusLO){
  437.  
  438. strcpy((char *) directory-> Name, name);
  439. directory->Attr = attr;
  440. directory->FstClusHI = FstClusHI;
  441. directory->FstClusLO = FstClusLO;
  442. }
  443.  
  444. void mkdir(unsigned int current_cluster, char * directoryName){
  445.  
  446. int firstEmptyDirectory;
  447. int nextFreeCluster;
  448. struct FAT32Dir newDir, dotDir, doubleDotDir;
  449. struct FAT32Dir test_directory = getDirectory(current_cluster, directoryName);
  450.  
  451. if(!strcmp(getDirName(test_directory.Name), directoryName)){
  452.  
  453. printf("%s\n", "Directory Already Exists!");
  454. return;
  455.  
  456. }
  457.  
  458. //Write new directory into free space
  459. firstEmptyDirectory = findFirstEmptyDirEntry(current_cluster);
  460. //nextFreeCluster = findEmptyCluster();
  461. //setFatEntry(nextFreeCluster, 0x0FFFFFF8);
  462. makeNewDirectory(&newDir, 0x10, directoryName, nextFreeCluster/0x100, nextFreeCluster%0x100);
  463. fseek(fp, firstEmptyDirectory, SEEK_SET);
  464. fwrite(&newDir, sizeof(struct FAT32Dir), 1, fp);
  465.  
  466.  
  467. //Add '.' and '..' entries for new directory
  468. makeNewDirectory(&dotDir, 0x10, ".", nextFreeCluster/0x100, nextFreeCluster&0x100);
  469. firstEmptyDirectory = findFirstEmptyDirEntry(nextFreeCluster);
  470. fseek(fp, firstEmptyDirectory, SEEK_SET);
  471. fwrite(&dotDir, sizeof(struct FAT32Dir), 1, fp);
  472.  
  473.  
  474. makeNewDirectory(&doubleDotDir, 0x10, "..", nextFreeCluster/0x100, nextFreeCluster&0x100);
  475. firstEmptyDirectory = findFirstEmptyDirEntry(nextFreeCluster);
  476. fseek(fp, firstEmptyDirectory, SEEK_SET);
  477. fwrite(&dotDir, sizeof(struct FAT32Dir), 1, fp);
  478.  
  479.  
  480. }
  481.  
  482. void open( char * fileName, char * mode, unsigned int current_cluster){
  483.  
  484. struct FAT32File file;
  485. struct FAT32Dir directory;
  486. int m;
  487.  
  488. if(!strcmp(mode, "r"))
  489. m = FILE_READ;
  490. else if (!strcmp(mode, "w"))
  491. m = FILE_WRITE;
  492. else if(!strcmp(mode, "rw") || !strcmp(mode, "wr"))
  493. m = FILE_READWRITE;
  494. else{
  495. printf("Error! Mode is not valid.");
  496. return;
  497. }
  498.  
  499. directory = getDirectory(current_cluster, fileName);
  500.  
  501. if (!strcmp(getFileName(directory.Name), fileName)){
  502. printf("Found directory: %s\n", getFileName(directory.Name));
  503. file.name = strdup(fileName);
  504. file.first_cluster_num = directory.FstClusHI*0x100 + directory.FstClusLO;
  505. file.mode = m;
  506. } else {
  507. printf("Error! File [%s] does not exist\n", (char *)fileName);
  508. }
  509.  
  510. int i;
  511. for(i = 0; i < 500; i++){
  512.  
  513. if(openFiles[i].name != NULL && !strcmp(openFiles[i].name, getFileName(directory.Name))){
  514.  
  515. if(m == openFiles[i].mode){
  516. printf("File [%s] is already in mode [%s]\n", fileName, mode);
  517. return;
  518. }
  519. openFiles[i].mode = m;
  520. return;
  521. }
  522. }
  523. for(i = 0; i < 500; i++){
  524.  
  525. if(openFiles[i].name == NULL){
  526. openFiles[i] = file;
  527. break;
  528. }
  529. }
  530. }
  531.  
  532. void close(char * fileName){
  533.  
  534. int i;
  535.  
  536. for(i = 0; i < 500; i++){
  537. if(openFiles[i].name == NULL){
  538. continue;
  539. }
  540.  
  541. if(!strcmp(fileName, openFiles[i].name)){
  542. openFiles[i].name = NULL;
  543. openFiles[i].mode = 0;
  544. openFiles[i].first_cluster_num = 0;
  545. return;
  546. }
  547. }
  548. printf("File [%s] was not opened!\n", fileName);
  549. }
  550.  
  551.  
  552. void run_read(char * string, unsigned int current_cluster, int offset, int length){
  553.  
  554. unsigned int FirstSectorofCluster = getFirstSectorofCluster(current_cluster);
  555. unsigned int fatEntry = getFatEntry(current_cluster);
  556. unsigned int new_file_cluster;
  557.  
  558.  
  559. fseek(fp, FirstSectorofCluster+offset, SEEK_SET);
  560.  
  561. if(length + offset > boot_region.bytes_per_sector){
  562. fread(strlen(string)+string,(boot_region.bytes_per_sector-offset), 1, fp);
  563. printf("%.*s", length, string + offset);
  564. length = length - (boot_region.bytes_per_sector-offset);
  565. }
  566. else{
  567. fread(string, length , 1, fp);
  568. printf("%.*s\n", length, string + offset);
  569. return;
  570. }
  571.  
  572. if(fatEntry != 0x0FFFFFF8 || fatEntry != 0x0FFFFFFF || fatEntry != 0x00000000){
  573. run_read(string, fatEntry,0,length);
  574. }
  575.  
  576. }
  577.  
  578. void read(unsigned int current_cluster, char * fileName, int offset, int length) // wil
  579. {
  580. int i = 0;
  581. int flag = 0;
  582. bool found = false;
  583. char * formatString;
  584. struct FAT32Dir directory = getDirectory(current_cluster, fileName);
  585. unsigned int fatEntry = getFatEntry(current_cluster);
  586. unsigned int FirstSectorofCluster = getFirstSectorofCluster(current_cluster);
  587.  
  588.  
  589. if(offset > directory.FileSize)
  590. {
  591. printf("Error! OFFSET is larger than file size\n");
  592. return;
  593.  
  594. }
  595.  
  596.  
  597.  
  598. if(!strcmp(getFileName((char *)directory.Name), fileName)) // check if file exist
  599. {
  600. if(directory.Attr != 0x10){
  601.  
  602.  
  603. if(length > directory.FileSize || offset+length > directory.FileSize){
  604. length = directory.FileSize - offset;
  605. formatString = (char *)calloc(length+1, sizeof(char));
  606. }
  607.  
  608. //else
  609. {
  610.  
  611. for(i = 0; i < 500; i++){
  612.  
  613. if(openFiles[i].name != NULL && !strcmp(openFiles[i].name, getFileName(directory.Name))){
  614.  
  615. if(openFiles[i].mode == FILE_READ || openFiles[i].mode == FILE_READWRITE){
  616.  
  617. fatEntry = openFiles[i].first_cluster_num;
  618. flag = offset/boot_region.bytes_per_sector;
  619.  
  620. while(flag != 0){
  621. if(fatEntry == 0x00000000 || fatEntry == 0xFFFFFFF8){
  622. return;
  623. }
  624.  
  625. fatEntry = getFatEntry(fatEntry);
  626. flag--;
  627. }
  628. run_read(formatString, fatEntry, (offset%boot_region.bytes_per_sector), length);
  629. found = true;
  630. break;
  631. /*
  632. long int hex = FirstSectorofCluster + (openFiles[i].first_cluster_num - 2) * (boot_region.bytes_per_sector * boot_region.sectors_per_cluster);
  633. unsigned char buffer[5000000];
  634.  
  635. fseek(fp, hex, SEEK_SET);
  636. fread(buffer,sizeof(buffer), 1, fp);
  637. printf("%.*s\n", len, buffer + offs);
  638. */
  639. }
  640. }
  641. }
  642.  
  643. if(found == false)
  644. printf("File [%s] is not in reading mode\n", fileName);
  645. }
  646. }
  647. else
  648. printf("Error! [%s] is a directory.\n", fileName);
  649. }
  650.  
  651. else
  652. printf("Error! [%s] does not exist.\n", fileName);
  653. }
  654.  
  655.  
  656.  
  657. void run_write(unsigned int current_cluster, int offset, int len, char * string){
  658.  
  659. unsigned int FirstSectorofCluster = getFirstSectorofCluster(current_cluster);
  660. unsigned int fatEntry = getFatEntry(current_cluster);
  661. unsigned int new_file_cluster;
  662. int string_offset;
  663.  
  664.  
  665. fseek(fp, FirstSectorofCluster+offset, SEEK_SET);
  666.  
  667. if(len + offset > boot_region.bytes_per_sector){
  668. fwrite(string,boot_region.bytes_per_sector-offset, 1, fp);
  669. string_offset = (boot_region.bytes_per_sector-offset);
  670. len = len - (boot_region.bytes_per_sector-offset);
  671. }
  672. else{
  673. fwrite(string, len , 1, fp);
  674. return;
  675. }
  676.  
  677.  
  678. if(fatEntry != 0x0FFFFFF8 || fatEntry != 0x0FFFFFFF || fatEntry != 0x00000000){
  679. run_write(fatEntry, 0, len, string + string_offset);
  680. }
  681.  
  682. if(fatEntry == 0x0FFFFFF8 || fatEntry == 0x0FFFFFFF ){
  683. new_file_cluster = findEmptyCluster();
  684. linkNewCluster(current_cluster, new_file_cluster);
  685. run_write(new_file_cluster, 0, len, string+string_offset);
  686. }
  687. }
  688.  
  689. void updateDirectory(struct FAT32Dir directory, unsigned int current_cluster){
  690.  
  691. int i = 0;
  692. int next_free_cluster;
  693. struct FAT32Dir temp;
  694. unsigned int fatEntry = getFatEntry(current_cluster);
  695. unsigned int FirstSectorofCluster = getFirstSectorofCluster(current_cluster);
  696.  
  697. fseek(fp, FirstSectorofCluster, SEEK_SET);
  698.  
  699. while(i * sizeof(struct FAT32Dir) < boot_region.bytes_per_sector){
  700.  
  701. fread(&temp, sizeof(struct FAT32Dir), 1, fp);
  702.  
  703. if(!strcmp(getFileName(temp.Name), getFileName(directory.Name) )){
  704. fseek(fp, i*sizeof(struct FAT32Dir)+ FirstSectorofCluster, SEEK_SET);
  705. fwrite(&directory, sizeof(struct FAT32Dir), 1, fp);
  706. }
  707.  
  708. i++;
  709. }
  710.  
  711. if(fatEntry != 0x0FFFFFF8 &&fatEntry != 0x0FFFFFFF)
  712. updateDirectory(directory, fatEntry);
  713.  
  714. }
  715.  
  716. void write(unsigned int current_cluster, char * fileName, int offs, int len, char * string) // wil
  717. {
  718. //char * dup;
  719. int i = 0;
  720. int flag = 0;
  721. unsigned int fatEntry;
  722. char * formatString;
  723. struct FAT32Dir directory = getDirectory(current_cluster, fileName);
  724.  
  725. if(strlen(string) <= len){
  726. formatString = (char *)calloc(len, sizeof(char));
  727. strcpy(formatString, string);
  728. }
  729.  
  730. if(!strcmp(getFileName((char *)directory.Name), fileName))
  731. {
  732. if(directory.Attr != 0x10)
  733. {
  734.  
  735. if(offs > directory.FileSize)
  736. printf("Error! OFFSET is larger than file size\n");
  737.  
  738. else
  739. {
  740.  
  741. bool found = false;
  742.  
  743. for(i = 0; i < 500; i++)
  744. {
  745. if(openFiles[i].name != NULL && !strcmp(openFiles[i].name, getFileName(directory.Name)))
  746. {
  747. if(openFiles[i].mode == FILE_WRITE || openFiles[i].mode == FILE_READWRITE)
  748. {
  749. fatEntry = openFiles[i].first_cluster_num;
  750. flag = offs/boot_region.bytes_per_sector;
  751.  
  752. while(flag != 0){
  753. if(fatEntry == 0x00000000 || fatEntry == 0xFFFFFFF8){
  754. return;
  755. }
  756.  
  757. fatEntry = getFatEntry(fatEntry);
  758. flag --;
  759. }
  760. run_write(fatEntry, (offs%boot_region.bytes_per_sector), len, formatString);
  761.  
  762. if(offs + len > directory.FileSize)
  763. {
  764. directory.FileSize = directory.FileSize + (strlen(string) - directory.FileSize) + offs;
  765. directory.Attr = 0x20;
  766. updateDirectory(directory, current_cluster);
  767.  
  768. found = true;
  769. break;
  770. }
  771. }
  772. }
  773.  
  774. if(found == false)
  775. printf("File [%s] is not in writing mode\n", fileName);
  776. }
  777. }
  778. }
  779.  
  780. else
  781. printf("Error! [%s] is a directory.\n", fileName);
  782. }
  783.  
  784. else
  785. printf("Error! [%s] does not exist.\n", fileName);
  786. }
  787.  
  788.  
  789.  
  790.  
  791. void rm();
  792.  
  793.  
  794. int main(int argc, char *argv[])
  795. {
  796. unsigned int root_cluster;
  797. unsigned int current_cluster;
  798. char * tokens[5];
  799. int token_count;
  800. int command = -1;
  801.  
  802. if(argv[1] == NULL){
  803. printf("No image file found!\nUse format: [executable] [FAT32 Image]\n");
  804. exit(0);
  805. }
  806.  
  807. fp = fopen(argv[1], "r+");
  808.  
  809. if(fp == NULL){
  810. printf("%s\n\n", "Could not open file");
  811.  
  812. }
  813.  
  814. parseBootSector();
  815.  
  816. root_cluster = boot_region.BPB_RootCluster;
  817. current_cluster = root_cluster;
  818.  
  819. while(1){
  820.  
  821. prompt();
  822. token_count = getTokens(tokens);
  823. printf("This the number of tokens: %d\n", token_count);
  824.  
  825. if (token_count == 0)
  826. continue;
  827.  
  828. int i;
  829. for(i = 0; i < 13; i++){
  830.  
  831. if(!strcmp(commands[i], tokens[0])){
  832. command = i;
  833. break;
  834. }
  835. }
  836.  
  837. //EXIT
  838. if (command == 0)
  839. break;
  840.  
  841. if(!(hasCorrectFormat(command, token_count))){
  842. printf("%s\n", "Error! Invalid Command.");
  843. continue;
  844. }
  845. switch(command){
  846.  
  847. case INFO:
  848. info();
  849. break;
  850. case SIZE:
  851. size(current_cluster, tokens[1]);
  852. break;
  853. case LS:
  854. ls(root_cluster, current_cluster, token_count, tokens);
  855. break;
  856. case CD:
  857. current_cluster = cd(current_cluster, tokens[1]);
  858. break;
  859. case MKDIR:
  860. mkdir(current_cluster, tokens[1]);
  861. break;
  862. case OPEN:
  863. open(tokens[1], tokens[2], current_cluster);
  864. break;
  865. case CLOSE:
  866. close(tokens[1]);
  867. break;
  868. case READ:
  869. read(current_cluster, tokens[1], atoi(tokens[2]), atoi(tokens[3]));
  870. break;
  871. case WRITE:
  872. write(current_cluster, tokens[1], atoi(tokens[2]), atoi(tokens[3]), tokens[4]);
  873. break;
  874. }
  875.  
  876. }
  877.  
  878. return 0;
  879. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement