int rand_file_remove( char* fn ) { char tfn[64]; rand_file_file_name( tfn, fn ); uffs_remove( tfn ); uffs_remove( fn ); return 0; }
static int dfs_uffs_unlink(struct dfs_filesystem* fs, const char* path) { int result; struct uffs_stat s; /* judge file type, dir is to be delete by uffs_rmdir, others by uffs_remove */ if (uffs_lstat(path, &s) < 0) { return uffs_result_to_dfs(uffs_get_error()); } switch(s.st_mode & US_IFMT) { case US_IFREG: result = uffs_remove(path); break; case US_IFDIR: result = uffs_rmdir(path); break; default: /* unknown file type */ return -1; } if (result < 0) return uffs_result_to_dfs(uffs_get_error()); return 0; }
/** rm <obj> */ static int cmd_rm(int argc, char *argv[]) { const char *name = NULL; int ret = 0; struct uffs_stat st; CHK_ARGC(2, 2); name = argv[1]; ret = uffs_stat(name, &st); if (ret < 0) { MSGLN("Can't stat '%s'", name); return ret; } if (st.st_mode & US_IFDIR) { ret = uffs_rmdir(name); } else { ret = uffs_remove(name); } if (ret == 0) MSGLN("Delete '%s' succ.", name); else MSGLN("Delete '%s' fail!", name); return ret; }
int property_set( char *name, char *val ) { char buf[96]; int line_no = 0; int kc; char *ki[15]; int tf, f; char *t_property_file_name = "/tmp_perperty.tr"; int new_key = 1; f = uffs_open( g_property_file_name, UO_RDONLY ); if( f == -1 ){ f = uffs_open( g_property_file_name, UO_RDWR|UO_CREATE ); if( f == -1 ) return -1; } tf = uffs_open( t_property_file_name, UO_WRONLY|UO_CREATE ); if( tf == -1 ){ uffs_close( f ); return -1; } while(1){ if( uffs_read_line( f, buf, sizeof( buf ) ) < 0 ) break; kc = analysis_string_to_strings_by_decollator( buf, "=", ki, 15 ); if( kc !=2 ) continue; if( s_strcmp( ki[0], name ) == 0 ){ //int len; //strncpy( buf, &ki[1], sizeof(buf) ); if( new_key == 0 ) continue; sprintf( buf, "%s=%s\n", name, val ); new_key = 0; } else strcat( buf, "\n" ); uffs_write( tf, buf, strlen( buf ) ); } if( new_key == 1 ){ uffs_close( tf ); uffs_close( f ); f = uffs_open( g_property_file_name, UO_RDWR|UO_CREATE ); if( f == -1 ) return -1; uffs_seek( f, 0, _SEEK_END ); sprintf( buf, "%s=%s\n", name, val ); uffs_write( f, buf, strlen( buf ) ); uffs_close( f ); } else{ uffs_close( tf ); uffs_close( f ); uffs_remove( g_property_file_name ); uffs_rename( t_property_file_name, g_property_file_name ); } return 0; }
int rand_file_create( char* fn, int record_count, int record_size ) { char tfn[64]; int i,f; unsigned char flag; char tbuf[ _d_max_rand_file_record_size ]; // check file if exist. if( record_size > 128 ) return -1; rand_file_file_name( tfn, fn ); f = uffs_open( tfn, UO_RDONLY ); if( f >= 0 ) return -1; uffs_close( f ); // create and write the base info f = uffs_open( tfn, UO_RDWR | UO_CREATE ); if( f < 0 ) return -1; uffs_write( f, &record_size, 4 ); uffs_write( f, &record_count, 4 ); memset( tbuf, 0xff, sizeof( tbuf ) ); flag = 0xff; for( i=0; i<record_count; i++ ){ uffs_write( f, &flag, 1 ); } uffs_close(f); uffs_remove( fn ); f = uffs_open( fn, UO_RDWR | UO_CREATE ); if( f < 0 ){ uffs_remove( tfn ); return -1; } for( i=0; i<record_count; i++ ){ uffs_write( f, tbuf, record_size ); } uffs_close( f ); return 0; }
static byte DeleteCmd(const unsigned char *cmd, const CLS1_ConstStdIOType *io) { /* precondition: cmd starts with "delete" */ char f_name[UFFS_PATHSIZE]; byte res = ERR_OK; if (UTIL1_ReadEscapedName(cmd + sizeof("delete"), (unsigned char*)f_name, sizeof(f_name), NULL, NULL, NULL) == ERR_OK) { res = uffs_remove( f_name ); if(uffs_get_error() == UEISDIR) { res = uffs_rmdir( f_name ); } } else { CmdUsageError(cmd, (unsigned char*) "delete fileName", io); res = ERR_FAILED; } return res; }
/** * usage: t_pfs <start> <n> * * for example: t_pfs /x/ 100 * * This test case performs: * 1) create <n> files under <start>, write full file name as file content * 2) list files under <start>, check files are all listed once * 3) check file content aganist file name * 4) delete files on success */ static int cmd_TestPopulateFiles(int argc, char *argv[]) { const char *start = "/"; int count = 80; int i, fd, num; char name[128]; char buf[128]; uffs_DIR *dirp; struct uffs_dirent *ent; unsigned long bitmap[50] = {0}; // one bit per file, maximu 32*50 = 1600 files UBOOL succ = U_TRUE; #define SBIT(n) bitmap[(n)/(sizeof(bitmap[0]) * 8)] |= (1 << ((n) % (sizeof(bitmap[0]) * 8))) #define GBIT(n) (bitmap[(n)/(sizeof(bitmap[0]) * 8)] & (1 << ((n) % (sizeof(bitmap[0]) * 8)))) if (argc > 1) { start = argv[1]; if (argc > 2) { count = strtol(argv[2], NULL, 10); } } if (count > sizeof(bitmap) * 8) count = sizeof(bitmap) * 8; for (i = 0, fd = -1; i < count; i++) { sprintf(name, "%sFile%03d", start, i); fd = uffs_open(name, UO_RDWR|UO_CREATE|UO_TRUNC); if (fd < 0) { MSGLN("Create file %s failed", name); break; } if (uffs_write(fd, name, strlen(name)) != strlen(name)) { // write full path name to file MSGLN("Write to file %s failed", name); uffs_close(fd); break; } uffs_close(fd); } if (i < count) { // not success, need to clean up for (; i >= 0; i--) { sprintf(name, "%sFile%03d", start, i); if (uffs_remove(name) < 0) MSGLN("Delete file %s failed", name); } succ = U_FALSE; goto ext; } MSGLN("%d files created.", count); // list files dirp = uffs_opendir(start); if (dirp == NULL) { MSGLN("Can't open dir %s !", start); succ = U_FALSE; goto ext; } ent = uffs_readdir(dirp); while (ent && succ) { if (!(ent->d_type & FILE_ATTR_DIR) && // not a dir ent->d_namelen == strlen("File000") && // check file name length memcmp(ent->d_name, "File", strlen("File")) == 0) { // file name start with "File" MSGLN("List entry %s", ent->d_name); num = strtol(ent->d_name + 4, NULL, 10); if (GBIT(num)) { // file already listed ? MSGLN("File %d listed twice !", ent->d_name); succ = U_FALSE; break; } SBIT(num); // check file content sprintf(name, "%s%s", start, ent->d_name); fd = uffs_open(name, UO_RDONLY); if (fd < 0) { MSGLN("Open file %d for read failed !", name); } else { memset(buf, 0, sizeof(buf)); num = uffs_read(fd, buf, sizeof(buf)); if (num != strlen(name)) { MSGLN("%s Read data length expect %d but got %d !", name, strlen(name), num); succ = U_FALSE; } else { if (memcmp(name, buf, num) != 0) { MSGLN("File %s have wrong content '%s' !", name, buf); succ = U_FALSE; } } uffs_close(fd); } } ent = uffs_readdir(dirp); } uffs_closedir(dirp); // check absent files for (i = 0; i < count; i++) { if (GBIT(i) == 0) { sprintf(name, "%sFile%03d", start, i); MSGLN("File %s not listed !", name); succ = U_FALSE; } } // delete files if pass the test for (i = 0; succ && i < count; i++) { sprintf(name, "%sFile%03d", start, i); if (uffs_remove(name) < 0) { MSGLN("Delete file %s failed", name); succ = U_FALSE; } } ext: MSGLN("Populate files test %s !", succ ? "SUCC" : "FAILED"); return succ ? 0 : -1; }
static byte UFFS_Benchmark(const unsigned char *cmd, const CLS1_ConstStdIOType *io) { uint16_t i, j; uint8_t read_buf[10]; TIMEREC time, startTime; int32_t start_mseconds, mseconds; int fd, fd2; /* write benchmark */ MSGLN("Benchmark: open file, write 10k times 10 bytes (100'000 bytes), close file:"); MSGLN("Deleting existing benchmark files..."); uffs_remove("/bench.txt"); uffs_remove("/copy.txt"); MSGLN("Creating benchmark file..."); (void)TmDt1_GetTime(&startTime); fd = uffs_open("/bench.txt", UO_CREATE|UO_WRONLY); if (fd < 0) { MSGLN("*** Failed opening benchmark file!"); return ERR_FAILED; } for(i=0;i<10000;i++) { if (uffs_write(fd, "benchmark ", sizeof("benchmark ")-1) <= 0) { MSGLN("*** Failed writing file!"); uffs_close(fd); return ERR_FAILED; } } uffs_close(fd); (void)TmDt1_GetTime(&time); start_mseconds = startTime.Hour*60*60*1000 + startTime.Min*60*1000 + startTime.Sec*1000 + startTime.Sec100*10; mseconds = time.Hour*60*60*1000 + time.Min*60*1000 + time.Sec*1000 + time.Sec100*10 - start_mseconds; MSGLN("%ld ms needed for creating.",mseconds); /* read benchmark */ MSGLN("Reading benchmark file..."); (void)TmDt1_GetTime(&startTime); fd = uffs_open("/bench.txt", UO_RDONLY); if (fd < 0) { MSGLN("*** Failed opening benchmark file!"); return ERR_FAILED; } for(i=0;i<10000;i++) { if (uffs_read(fd, read_buf, sizeof(read_buf)) <= 0) { MSGLN("*** Failed reading file!"); uffs_close(fd); return ERR_FAILED; } } uffs_close(fd); (void)TmDt1_GetTime(&time); start_mseconds = startTime.Hour*60*60*1000 + startTime.Min*60*1000 + startTime.Sec*1000 + startTime.Sec100*10; mseconds = time.Hour*60*60*1000 + time.Min*60*1000 + time.Sec*1000 + time.Sec100*10 - start_mseconds; MSGLN("%ld ms needed for reading.",mseconds); /* copy benchmark */ MSGLN("Copy file (100'000 bytes)..."); (void)TmDt1_GetTime(&startTime); fd2 = uffs_open("/copy.txt", UO_CREATE|UO_WRONLY); if (fd2 < 0) { MSGLN("*** Failed opening copy file!"); return ERR_FAILED; } fd = uffs_open("/bench.txt", UO_RDONLY); if (fd < 0) { MSGLN("*** Failed opening benchmark file!"); return ERR_FAILED; } i = 0; do { j = uffs_read(fd, read_buf, sizeof(read_buf)); i += j; if (uffs_write(fd2, read_buf, j) < j) { MSGLN("*** Failed writing file!"); uffs_close(fd); uffs_close(fd2); return ERR_FAILED; } } while( j > 0); uffs_close(fd); uffs_close(fd2); (void)TmDt1_GetTime(&time); start_mseconds = startTime.Hour*60*60*1000 + startTime.Min*60*1000 + startTime.Sec*1000 + startTime.Sec100*10; mseconds = time.Hour*60*60*1000 + time.Min*60*1000 + time.Sec*1000 + time.Sec100*10 - start_mseconds; MSGLN("%ld ms needed for copy.",mseconds); return ERR_OK; }