//----------------------------------------------------------------------------- // fl_fgetc: Get a character in the stream //----------------------------------------------------------------------------- int fl_fgetc(void *f) { int res; unsigned char data = 0; res = fl_fread(&data, 1, 1, f); if (res == 1) return (int)data; else return res; }
void fs_test() { file_t* file; char buffer[512]; //char text[] = { 't', 'e', 's', 't', '\n', '\r' }; char text[] = "new file\n\r\0"; devices_init(); mmchs_init(); fs_init(); fl_listdirectory("/"); file = (file_t*) fl_fopen("/test/thenewest.txt", "a"); fl_fwrite(text, sizeof(text), sizeof(text), file); fl_fclose(file); fl_listdirectory("/test/"); file = (file_t*) fl_fopen("/test/thenewest.txt", "r"); fl_fread(buffer, 512, sizeof(text), file); kernel_print(buffer); fl_fclose(file); }
int cmd_dd_exec(CLI *cli, char *argv[]) { int verbose = 0, seek = 0, skip = 0, count = -1, block = 32; int size, rc, err = 0, argc = cli_num_args(argv); char infile[PATH_MAX], outfile[PATH_MAX]; char *inname = NULL, *outname = NULL; FL_FILE *in = NULL, *out = NULL; int c, opt_ind; //char *buffer = NULL; for (c = 0, optind = 0, opt_ind = 0; c != -1;) { c = getopt_long(argc, argv, "vi:o:b:c:s:k:", long_options, &opt_ind); switch (c) { case 'v': verbose = 1; break; case 'i': inname = optarg; break; case 'o': outname = optarg; break; case 'b': block = atoi(optarg); break; case 's': skip = atoi(optarg); break; case 'c': count = atoi(optarg); break; case 'k': seek = atoi(optarg); break; } } if (verbose) { printf("inname = %s\n", inname); printf("outname = %s\n", outname); printf("block = %i\n", block); printf("skip = %i\n", skip); printf("seek = %i\n", seek); printf("count = %i\n", count); } //buff = (char *)mall c(block); char buffer[block]; if (argc - optind != 1) { printf("dd - incorrect number of arguments. Try 'man dd'\n"); err++; goto cleanup; } if (buffer == NULL) { printf("dd: out of memory.\n"); err++; goto cleanup; } if (inname) { cli_clean_path(cli, inname, infile); if (fl_is_dir(infile)) { printf("dd: '%s' is a directory\n", infile); err++; goto cleanup; } in = (FL_FILE *)fl_fopen(infile, "r"); if (!in) { printf("dd: could not open '%s' for reading\n", infile); err++; goto cleanup; } fl_fseek(in, skip * block, SEEK_SET); } else { printf("dd: no input file specified - dumping zeros\n"); memset(buffer, 0, block); } if (outname) { cli_clean_path(cli, outname, outfile); if (fl_is_dir(outfile)) { printf("dd: '%s' is a directory\n", outfile); err++; goto cleanup; } out = (FL_FILE *)fl_fopen(outfile, "r+"); if (out) { rc = fl_fseek(out, seek * block, SEEK_SET); printf("WARNING: seeking in outfile beyond EOF is unsupported!\n"); } else { out = (FL_FILE *)fl_fopen(outfile, "w"); } if (!out) { printf("dd: could not open '%s' for writeing\n", outfile); err++; goto cleanup; } } else { printf("dd: no output file specified - using stdout\n"); } if (count >= 0) { size = count; } else { // TODO size of (infile - skip) / block } while (size > 0) { int blk = count - size; if (verbose) printf("dd: reading block %i\n", blk); if (in) { rc = fl_fread(buffer, block, 1, in); if (rc < 0) { printf("dd: Error reading block %i\n", blk); err++; goto cleanup; } else { // short read -> set block size to number of bytes read block = rc; } } if (verbose) printf("dd: writeing block %i\n", blk); if (out) { rc = fl_fwrite(buffer, block, 1, out); if (rc < 0) { printf("dd: Error writeing block %i\n", blk); err++; goto cleanup; } } else { printf("%s\n", buffer); } size--; } // TODO print summary cleanup: fl_fclose(in); fl_fclose(out); //free(buffer); return (err ? -1 : 0); }
//----------------------------------------------------------------- // Main: Test bench file to create 5 files with psuedo random // sequences in of varying lengths - read them back and complete // then remove them. //----------------------------------------------------------------- void main() { int i,j,x; FL_FILE * files[5]; FL_FILE *readFile; char filenames[5][260]; BYTE fileData[5][10000]; BYTE readBuffer[10000]; int fileLengths[5]; BYTE *massiveData; time_t timeStart, timeEnd; #define TESTFILES 6 char *testfile[] = { "X:\\1", "X:\\1.bin", "X:\\12345678.321", "X:\\mylongfilename", "X:\\mylongfilename.bin", "X:\\the Quick Brown Fox jumped over the lazy dog.elf.binfile.jar" }; srand(time(NULL)); // Initialise FAT32_InitDrive(); fl_init(); if (fl_attach_media(FAT_ReadSector, FAT_WriteSector) != FAT_INIT_OK) return; // List directory fl_listdirectory("C:\\"); // return ; test_start: // Generate 5 random files memset(filenames, 0x00, 260*5); for (j=0;j<5;j++) { // Length fileLengths[j] = GetRandom(9999); // Data for (x=0;x<fileLengths[j];x++) fileData[j][x] = (BYTE)GetRandom(255); // Names sprintf(filenames[j], "X:\\Auto Generated Filename Number %d", j+1); } // Create some files for (j=0;j<5;j++) { printf("Creating File: %s [%d bytes]\n", filenames[j], fileLengths[j]); // Create File files[j] = fl_fopen(filenames[j], "w"); if (files[j]!=NULL) { if (fl_fwrite(fileData[j], 1, fileLengths[j], files[j])!=fileLengths[j]) { printf("ERROR: File Write Block Failed File %s Length %d\n", filenames[j], fileLengths[j]); fl_assert(0); } } else { printf("ERROR: Error Creating File %s\n", filenames[j]); fl_assert(0); } fl_fclose(files[j]); // Clear buffer for (i=0;i<sizeof(readBuffer);i++) readBuffer[i] = 0; // Verify File readFile = fl_fopen(filenames[j], "r"); if (readFile!=NULL) { int failed = FALSE; printf("File %s Read Check (fread whole file) [%d bytes]\n", filenames[j], fileLengths[j]); if (fl_fread(readBuffer, 1, fileLengths[j], readFile)!=fileLengths[j]) { printf("ERROR: File %s Read Length Error %d\n", filenames[j], fileLengths[j]); fl_assert(0); } for (i=0;i<fileLengths[j];i++) if ( fileData[j][i] != (BYTE)readBuffer[i] ) failed = TRUE; if (failed) { printf("ERROR: File %s Data Verify Failed\n", filenames[j]); fl_assert(0); } } fl_fclose(readFile); // Clear buffer for (i=0;i<sizeof(readBuffer);i++) readBuffer[i] = 0; // Verify File using fgetc readFile = fl_fopen(filenames[j], "r"); if (readFile!=NULL) { int failed = FALSE; printf("File %s Read Check (fgetc) [%d bytes]\n", filenames[j], fileLengths[j]); i = 0; while (i < fileLengths[j]) { int res = fl_fgetc(readFile); if (res == -1) break; readBuffer[i++] = (BYTE)res; } if (i != fileLengths[j]) { printf("ERROR: File %s Read Length Error %d\n", filenames[j], fileLengths[j]); fl_assert(0); } for (i=0;i<fileLengths[j];i++) if ( fileData[j][i] != (BYTE)readBuffer[i] ) failed = TRUE; if (failed) { printf("ERROR: File %s Data Verify Failed\n", filenames[j]); fl_assert(0); } } fl_fclose(readFile); // Clear buffer for (i=0;i<sizeof(readBuffer);i++) readBuffer[i] = 0; // Verify File chunks readFile = fl_fopen(filenames[j], "r"); if (readFile!=NULL) { int failed = FALSE; printf("File %s Read Check (fread chunks) [%d bytes]\n", filenames[j], fileLengths[j]); i = 0; while (i < fileLengths[j]) { int read_length = GetRandom(1025); if (read_length > (fileLengths[j] - i)) read_length = fileLengths[j] - i; if (fl_fread(readBuffer + i, 1, read_length, readFile) != read_length) { printf("ERROR: File %s fread error\n", filenames[j]); fl_assert(0); break; } i += read_length; } if (i != fileLengths[j]) { printf("ERROR: File %s Read Length Error %d\n", filenames[j], fileLengths[j]); fl_assert(0); } for (i=0;i<fileLengths[j];i++) if ( fileData[j][i] != (BYTE)readBuffer[i] ) { failed = TRUE; break; } if (failed) { printf("ERROR: File %s Data Verify Failed at %d\n", filenames[j], i); fl_assert(0); } } fl_fclose(readFile); // Delete File if (fl_remove(filenames[j])<0) printf("ERROR: Delete File %s Failed\n", filenames[j]); // Verify file is no longer present! readFile = fl_fopen(filenames[j], "r"); if (readFile != NULL) { printf("ERROR: File %s still present after delete!\n", filenames[j]); fl_assert(0); fl_fclose(readFile); } } // Create folder fl_createdirectory("C:\\folder1"); #if 0 // Create massive file #define MASSIVE_FILE_LEN (1024 * 1024) printf("Creating Massive File [%d bytes]\n", MASSIVE_FILE_LEN); massiveData = malloc(MASSIVE_FILE_LEN); if (!massiveData) { printf("ERROR: Could not allocate memory for massive array!\n"); fl_assert(0); fl_shutdown(); return ; } // Create random data for file for (x=0;x<MASSIVE_FILE_LEN;x++) massiveData[x] = (BYTE)GetRandom(255); // Remove if it already exists! fl_remove("X:\\folder1\\massive file.bin"); timeStart = time(NULL); // Create Large File readFile = fl_fopen("X:\\folder1\\massive file.bin", "w"); if (readFile != NULL) { if (fl_fwrite(massiveData, 1, MASSIVE_FILE_LEN, readFile) != MASSIVE_FILE_LEN) { printf("ERROR: File Write Block Failed for massive file (Length %d)\n", MASSIVE_FILE_LEN); fl_assert(0); } } else { printf("ERROR: Error Creating massive file\n"); fl_assert(0); } fl_fclose(readFile); // Verify Massive File readFile = fl_fopen("X:\\folder1\\massive file.bin", "r"); if (readFile!=NULL) { int failed = FALSE; printf("File Massive File Read Check (fread whole file) [%d bytes]\n", MASSIVE_FILE_LEN); i = 0; while (i < MASSIVE_FILE_LEN) { int read_length = GetRandom(2048) + 128; if (read_length > (MASSIVE_FILE_LEN - i)) read_length = MASSIVE_FILE_LEN - i; if (fl_fread(readBuffer, 1, read_length, readFile) != read_length) { printf("ERROR: File massive file fread error\n"); fl_assert(0); break; } for (j=0;j<read_length;j++) if ( massiveData[i+j] != (BYTE)readBuffer[j] ) { printf("ERROR: File Massive File Data Verify Failed at %d\n", i+j); fl_assert(0); break; } i += read_length; } if (i != MASSIVE_FILE_LEN) { printf("ERROR: File massive file Read Length Error %d\n", MASSIVE_FILE_LEN); fl_assert(0); } } fl_fclose(readFile); timeEnd = time(NULL); printf("Time taken %d seconds\n", (int)(timeEnd-timeStart)); free(massiveData); #endif // Filename test for (i=0;i<TESTFILES;i++) { // Create File readFile = fl_fopen(testfile[i], "w"); if (readFile != NULL) ; else { printf("ERROR: Error Creating File %s\n", testfile[i]); fl_assert(0); } fl_fputc(0, readFile); fl_fclose(readFile); readFile = fl_fopen(testfile[i], "r"); assert(readFile); fl_fclose(readFile); } // List directory fl_listdirectory("C:\\"); for (i=0;i<TESTFILES;i++) { // Delete File if (fl_remove(testfile[i])<0) { printf("ERROR: Delete File %s Failed\n", testfile[i]); fl_assert(0); } } fl_shutdown(); printf("\r\nCompleted\r\n"); // List directory fl_listdirectory("C:\\"); }
int32_t file_read(void* buffer, int32_t size, int32_t count, file_handle_t handle) { return fl_fread(buffer, size, count, handle); }
int cmd_hd_exec(CLI *cli, char *argv[]) { int argc = cli_num_args(argv); char buffer[16], path[PATH_MAX]; int eof = 0, rc, line = 0, i; FL_FILE *file; if (argc != 2) { printf("hd - incorrect number of arguments. Try 'man hd'\n"); return (-1); } cli_clean_path(cli, argv[1], path); if (fl_is_dir(path)) { printf("hd: '%s' is a directory\n", path); return (-1); } file = (FL_FILE *)fl_fopen(path, "r"); if (!file) { printf("hd: Could not open '%s'\n", path); return (-1); } while (rc != EOF) { rc = fl_fread(buffer, 16, 1, file); printf("%04hhx:%04hhx ", line * 16, line * 16 + rc -1); for (i=0; i<rc; i++) { printf("%02hhx ", buffer[i]); if (i == 7) printf(" "); } for (i=rc; i<16; i++) { printf(" "); if (i == 7) printf(" "); } printf(" |"); for (i=0; i<rc; i++) { if ((buffer[i] < 32) || (buffer[i] > 127)) { printf("."); } else { printf("%c", buffer[i]); } } printf("|\n"); if (rc < 16) break; line++; } fl_fclose(file); return (0); }