/* test appending file */ static BOOL cmdTest5(const char *tail) { int fd = -1; URET ret; char buf[100]; const char *name; if (!tail) { return FALSE; } name = cli_getparam(tail, NULL); fd = uffs_open(name, UO_RDWR|UO_APPEND); if (fd < 0) { MSGLN("Can't open %s", name); goto fail; } sprintf(buf, "append test..."); ret = uffs_write(fd, buf, strlen(buf)); if (ret != strlen(buf)) { MSGLN("write file failed, %d/%d", ret, strlen(buf)); } else { MSGLN("write %d bytes to file, content: %s", ret, buf); } uffs_close(fd); fail: return TRUE; }
static BOOL cmdTestFormat(const char *tail) { URET ret; const char *mount = "/"; uffs_Device *dev; const char *next; UBOOL force = U_FALSE; const char *test_file = "/a.txt"; int fd; if (tail) { mount = cli_getparam(tail, &next); if (next && strcmp(next, "-f") == 0) force = U_TRUE; } fd = uffs_open(test_file, UO_RDWR | UO_CREATE); if (fd < 0) { MSGLN("can't create test file %s", test_file); return U_TRUE; } MSGLN("Formating %s ... ", mount); dev = uffs_GetDeviceFromMountPoint(mount); if (dev == NULL) { MSGLN("Can't get device from mount point."); } else { ret = uffs_FormatDevice(dev, force); if (ret != U_SUCC) { MSGLN("Format fail."); } else { MSGLN("Format succ."); } uffs_PutDevice(dev); } uffs_close(fd); // this should fail on signature check ! return TRUE; }
/* Test file append and 'random' write */ static BOOL cmdTest3(const char *tail) { const char *name; int i; int write_test_seq[] = { 20, 10, 500, 40, 1140, 900, 329, 4560, 352, 1100 }; if (!tail) { return FALSE; } name = cli_getparam(tail, NULL); MSGLN("Test append file %s ...", name); for (i = 1; i < 500; i += 29) { if (test_append_file(name, i) != U_SUCC) { MSGLN("Append file %s test failed at %d !", name, i); return TRUE; } } MSGLN("Check file %s ... ", name); if (test_verify_file(name) != U_SUCC) { MSGLN("Verify file %s failed.", name); return TRUE; } MSGLN("Test write file ..."); for (i = 0; i < sizeof(write_test_seq) / sizeof(int) - 1; i++) { if (test_write_file(name, write_test_seq[i], write_test_seq[i+1]) != U_SUCC) { MSGLN("Test write file failed !"); return TRUE; } } MSGLN("Check file %s ... ", name); if (test_verify_file(name) != U_SUCC) { MSGLN("Verify file %s failed.", name); return TRUE; } MSGLN("Test succ !"); return TRUE; }
/* test create file, write file and read back */ BOOL cmdTest1(const char *tail) { int fd; URET ret; char buf[100]; const char *name; if (!tail) { return FALSE; } name = cli_getparam(tail, NULL); fd = uffs_open(name, UO_RDWR|UO_CREATE|UO_TRUNC); if (fd < 0) { MSGLN("Can't open %s", name); goto fail; } sprintf(buf, "123456789ABCDEF"); ret = uffs_write(fd, buf, strlen(buf)); MSGLN("write %d bytes to file, content: %s", ret, buf); ret = uffs_seek(fd, 3, USEEK_SET); MSGLN("new file position: %d", ret); memset(buf, 0, sizeof(buf)); ret = uffs_read(fd, buf, 5); MSGLN("read %d bytes, content: %s", ret, buf); uffs_close(fd); fail: return TRUE; }
/** * 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 BOOL cmdTestPopulateFiles(const char *tail) { const char *start = "/"; int count = 80; int i, fd, num; char name[128]; char buf[128]; uffs_DIR *dirp; struct uffs_dirent *ent; struct uffs_stat stat_buf; 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 (tail) { start = cli_getparam(tail, &tail); if (tail) { count = strtol(tail, 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 U_TRUE; }
//uffs拷贝函数,参数之间加空格 //需要从elm拷贝到uffs时(跨文件系统),参数名称前加:: //例如uffs_copy("::/01.hdc /dir1/01.hdc") //上例从SD卡拷贝一个文件01.hdc到flash中, //也可用dfs的函数,那样就不用考虑是否跨文件系统了. int uffs_copy(const char *tail) { const char *src; const char *des; char buf[100]; int fd1=-1, fd2=-1; int len; int src_local = FALSE, des_local = FALSE; int fd3=-1, fd4=-1; if(!tail) return FALSE; src = cli_getparam(tail, &des); if(!des) return FALSE; if(memcmp(src, "::", 2) == 0) { src += 2; src_local = TRUE; } if(memcmp(des, "::", 2) == 0) { des += 2; des_local = TRUE; } if(src_local) { //if((fp1 = fopen(src, "rb")) == NULL) if((fd3 = open(src,O_RDONLY,0)) < 0) { uffs_Perror(UFFS_ERR_NORMAL, "Can't open %s for copy.", src); goto fail_ext; } } else { if((fd1 = uffs_open(src, UO_RDONLY)) < 0) { uffs_Perror(UFFS_ERR_NORMAL, "Can't open %s for copy.", src); goto fail_ext; } } if(des_local) { if((fd4 = open(des,O_WRONLY | O_CREAT,0)) < 0) { uffs_Perror(UFFS_ERR_NORMAL, "Can't open %s for copy.", des); goto fail_ext; } } else { if((fd2 = uffs_open(des, UO_RDWR|UO_CREATE|UO_TRUNC)) < 0) { uffs_Perror(UFFS_ERR_NORMAL, "Can't open %s for copy.", des); goto fail_ext; } } uffs_Perror(UFFS_ERR_NORMAL, "copy %s to %s... ",src,des); while((src_local ? (1) : (uffs_eof(fd1) == 0))) { if(src_local) { len = read(fd3, buf, sizeof(buf)); } else { len = uffs_read(fd1, buf, sizeof(buf)); } if(len == 0) break; if(len < 0) { uffs_Perror(UFFS_ERR_NORMAL, "read file %s fail!", src); break; } if(des_local) { if(write(fd4, buf, len) != len) { uffs_Perror(UFFS_ERR_NORMAL, "write file %s fail!", des); break; } } else { if(uffs_write(fd2, buf, len) != len) { uffs_Perror(UFFS_ERR_NORMAL, "write file %s fail ?", des); break; } } } uffs_Perror(UFFS_ERR_NORMAL, "succ."); fail_ext: if(fd1 > 0) uffs_close(fd1); if(fd2 > 0) uffs_close(fd2); if(fd3 > 0) close(fd3); if(fd4 > 0) close(fd4); return TRUE; }