DIR *fs_spiffs_opendir(const char *dir_name) { DIR *dir = NULL; struct mount_info *m = &s_fsm; if (!m->valid) { set_errno(EBADF); return NULL; } if (dir_name == NULL) { set_errno(ENOTDIR); return NULL; } dir = (DIR *) calloc(1, sizeof(*dir)); if (dir == NULL) { set_errno(ENOMEM); return NULL; } if (SPIFFS_opendir(&m->fs, (char *) dir_name, &dir->dh) == NULL) { free(dir); dir = NULL; } return dir; }
static void copy_file_task( void * pvParameters ) { FIL File; spiffs_DIR sf_dir; struct spiffs_dirent e; struct spiffs_dirent *pe = &e; s32_t err; void *temp = NULL; uint32_t remain; char *p_path_file = pvParameters; mn_screen_event_t fileLoad; choosedLinePosition = 0; choosedLine = 0; f_open(&File,p_path_file,FA_READ); spiffs_file *fd = &uspiffs[0].f; spiffs *fs = &uspiffs[0].gSPIFFS; SPIFFS_opendir(fs, "/", &sf_dir); do{ pe = SPIFFS_readdir(&sf_dir, pe); }while(strcmp((const char *)pe->name,"config.met") == 0); *fd = SPIFFS_open_by_dirent(fs, pe, SPIFFS_RDWR, 0); if(*fd >= SPIFFS_OK) { err = SPIFFS_fremove(fs, *fd); } *fd = SPIFFS_open(fs, p_path_file, SPIFFS_CREAT | SPIFFS_RDWR | SPIFFS_DIRECT, 0); temp = pvPortMalloc( FS_PAGE_SIZE ); while(!f_eof(&File)) { f_read(&File,temp,FS_PAGE_SIZE,(UINT *)&remain); err = SPIFFS_write(fs, *fd, (u8_t *)temp, remain); } vPortFree(temp); vPortFree(p_path_file); f_close(&File); SPIFFS_close(fs, *fd); fileLoad.event = FILE_LOAD_EVENT; xQueueSend( menu.qEvent, &fileLoad, 0 ); vTaskDelete( xHdlTaskFilecopy ); while(1) { } }
} TEST_END TEST(spiffs_12) { fs_reset_specific(0x4024c000, 0x4024c000 + 0, 192*1024, 4096, 4096*2, 256); int res; spiffs_file fd; int j = 1; while (1) { char fname[32]; sprintf(fname, "file%i.txt", j); fd = SPIFFS_open(FS, fname, SPIFFS_RDWR | SPIFFS_CREAT | SPIFFS_TRUNC | SPIFFS_DIRECT, 0); if (fd <=0) break; int i; res = SPIFFS_OK; for (i = 1; i <= 100; i++) { char *buf = "0123456789ABCDE\n"; res = SPIFFS_write(FS, fd, buf, strlen(buf)); if (res < 0) break; } SPIFFS_close(FS, fd); j++; } int errno = SPIFFS_errno(FS); TEST_CHECK(errno == SPIFFS_ERR_FULL); u32_t total; u32_t used; SPIFFS_info(FS, &total, &used); printf("total:%i (%iK)\nused:%i (%iK)\nremain:%i (%iK)\nerrno:%i\n", total, total/1024, used, used/1024, total-used, (total-used)/1024, errno); spiffs_DIR d; struct spiffs_dirent e; struct spiffs_dirent *pe = &e; SPIFFS_opendir(FS, "/", &d); while ((pe = SPIFFS_readdir(&d, pe))) { printf("%s [%04x] size:%i\n", pe->name, pe->obj_id, pe->size); } SPIFFS_closedir(&d); //SPIFFS_vis(FS); //dump_page(FS, 0); //dump_page(FS, 1); return TEST_RES_OK; } TEST_END
} TEST_END TEST(nodemcu_309) { fs_reset_specific(0, 0, 4096*20, 4096, 4096, 256); int res; spiffs_file fd; int j; for (j = 1; j <= 3; j++) { char fname[32]; sprintf(fname, "20K%i.txt", j); fd = SPIFFS_open(FS, fname, SPIFFS_RDWR | SPIFFS_CREAT | SPIFFS_TRUNC | SPIFFS_DIRECT, 0); TEST_CHECK(fd > 0); int i; spiffs_stat s; res = SPIFFS_OK; u8_t err = 0; for (i = 1; i <= 1280; i++) { char *buf = "0123456789ABCDE\n"; res = SPIFFS_write(FS, fd, buf, strlen(buf)); if (!err && res < 0) { printf("err @ %i,%i\n", i, j); err = 1; } } } int errno = SPIFFS_errno(FS); TEST_CHECK(errno == SPIFFS_ERR_FULL); u32_t total; u32_t used; SPIFFS_info(FS, &total, &used); printf("total:%i\nused:%i\nremain:%i\nerrno:%i\n", total, used, total-used, errno); //TEST_CHECK(total-used < 11000); // disabled, depends on too many variables spiffs_DIR d; struct spiffs_dirent e; struct spiffs_dirent *pe = &e; SPIFFS_opendir(FS, "/", &d); int spoon_guard = 0; while ((pe = SPIFFS_readdir(&d, pe))) { printf("%s [%04x] size:%i\n", pe->name, pe->obj_id, pe->size); TEST_CHECK(spoon_guard++ < 3); } TEST_CHECK(spoon_guard == 3); SPIFFS_closedir(&d); return TEST_RES_OK; } TEST_END
DIR *opendir(const char *dir_name) { DIR *dir = NULL; extern spiffs fs; if (dir_name != NULL && (dir = (DIR *) malloc(sizeof(*dir))) != NULL && SPIFFS_opendir(&fs, (char *) dir_name, &dir->dh) == NULL) { free(dir); dir = NULL; } return dir; }
//table = file.list() static int file_slist( lua_State* L ) { spiffs_DIR d; struct spiffs_dirent e; struct spiffs_dirent *pe = &e; char buff[LUAL_BUFFERSIZE]; SPIFFS_opendir(&fs, "/", &d); while ((pe = SPIFFS_readdir(&d, pe))) { sprintf(buff," %s size:%i\r\n", pe->name, pe->size); l_message(NULL,buff); } SPIFFS_closedir(&d); return 1; }
static void list (void) { spiffs_DIR dir; if (!SPIFFS_opendir (&fs, "/", &dir)) die ("spiffs_opendir"); struct spiffs_dirent de; while (SPIFFS_readdir (&dir, &de)) { static const char types[] = "?fdhs"; // file, dir, hardlink, softlink char name[sizeof(de.name)+1] = { 0 }; memcpy (name, de.name, sizeof(de.name)); printf("%c %6u %s\n", types[de.type], de.size, name); } SPIFFS_closedir (&dir); }
Vector<String> fileList() { Vector<String> result; spiffs_DIR d; spiffs_dirent info; SPIFFS_opendir(&_filesystemStorageHandle, "/", &d); while (true) { if (!SPIFFS_readdir(&d, &info)) break; result.add(String((char*)info.name)); } SPIFFS_closedir(&d); return result; }
//table = file.list() //================================== static int file_list( lua_State* L ) { spiffs_DIR d; struct spiffs_dirent e; struct spiffs_dirent *pe = &e; lua_newtable( L ); SPIFFS_opendir(&fs, "/", &d); while ((pe = SPIFFS_readdir(&d, pe))) { lua_pushinteger(L, pe->size); lua_setfield( L, -2, (char*)pe->name ); //MCU_DBG(" %s size:%i\r\n", pe->name, pe->size); } SPIFFS_closedir(&d); return 1; }
} TEST_END TEST(spiffs_dup_file_74) { fs_reset_specific(0, 0, 64*1024, 4096, 4096*2, 256); { spiffs_file fd = SPIFFS_open(FS, "/config", SPIFFS_CREAT | SPIFFS_TRUNC | SPIFFS_WRONLY, 0); TEST_CHECK(fd >= 0); char buf[5]; strncpy(buf, "test", sizeof(buf)); SPIFFS_write(FS, fd, buf, 4); SPIFFS_close(FS, fd); } { spiffs_file fd = SPIFFS_open(FS, "/data", SPIFFS_CREAT | SPIFFS_TRUNC | SPIFFS_WRONLY, 0); TEST_CHECK(fd >= 0); SPIFFS_close(FS, fd); } { spiffs_file fd = SPIFFS_open(FS, "/config", SPIFFS_RDONLY, 0); TEST_CHECK(fd >= 0); char buf[5]; int cb = SPIFFS_read(FS, fd, buf, sizeof(buf)); TEST_CHECK(cb > 0 && cb < sizeof(buf)); TEST_CHECK(strncmp("test", buf, cb) == 0); SPIFFS_close(FS, fd); } { spiffs_file fd = SPIFFS_open(FS, "/data", SPIFFS_CREAT | SPIFFS_TRUNC | SPIFFS_WRONLY, 0); TEST_CHECK(fd >= 0); spiffs_stat stat; SPIFFS_fstat(FS, fd, &stat); if (strcmp((const char*) stat.name, "/data") != 0) { // oops! lets check the list of files... spiffs_DIR dir; SPIFFS_opendir(FS, "/", &dir); struct spiffs_dirent dirent; while (SPIFFS_readdir(&dir, &dirent)) { printf("%s\n", dirent.name); } // this will print "/config" two times TEST_CHECK(0); } SPIFFS_close(FS, fd); } return TEST_RES_OK; } TEST_END
//================================== static int file_recv( lua_State* L ) { int32_t fsize = 0; uint8_t c, gnm; char fnm[SPIFFS_OBJ_NAME_LEN]; char buff[LUAL_BUFFERSIZE]; spiffs_DIR d; struct spiffs_dirent e; struct spiffs_dirent *pe = &e; uint32_t total, used; SPIFFS_info(&fs, &total, &used); if(total>2000000 || used>2000000 || used > total) { return luaL_error(L, "file system error");; } gnm = 0; if (lua_gettop(L) == 1 && lua_type( L, 1 ) == LUA_TSTRING) { size_t len; const char *fname = luaL_checklstring( L, 1, &len ); if (len > 0 && len < SPIFFS_OBJ_NAME_LEN) { // use given file name for (c=0; c<len; c++) { fnm[c] = fname[c]; } fnm[len] = '\0'; gnm = 1; } } if (FILE_NOT_OPENED != file_fd) { SPIFFS_close(&fs,file_fd); file_fd = FILE_NOT_OPENED; } l_message(NULL,"Start Ymodem file transfer..."); while (MicoUartRecv( MICO_UART_1, &c, 1, 10 ) == kNoErr) {} fsize = Ymodem_Receive(fnm, total-used-10000, gnm); luaWdgReload(); mico_thread_msleep(500); while (MicoUartRecv( MICO_UART_1, &c, 1, 10 ) == kNoErr) {} if (FILE_NOT_OPENED != file_fd) { SPIFFS_fflush(&fs,file_fd); SPIFFS_close(&fs,file_fd); file_fd = FILE_NOT_OPENED; } mico_thread_msleep(500); if (fsize > 0) { sprintf(buff,"\r\nReceived successfully, %d\r\n",fsize); l_message(NULL,buff); } else if (fsize == -1) { l_message(NULL,"\r\nFile write error!\r\n"); } else if (fsize == -2) { l_message(NULL,"\r\nFile open error!\r\n"); } else if (fsize == -3) { l_message(NULL,"\r\nAborted.\r\n"); } else if (fsize == -4) { l_message(NULL,"\r\nFile size too big, aborted.\r\n"); } else { l_message(NULL,"\r\nReceive failed!"); } if (fsize > 0) { SPIFFS_opendir(&fs, "/", &d); while ((pe = SPIFFS_readdir(&d, pe))) { sprintf(buff," %-32s size: %i", pe->name, pe->size); l_message(NULL,buff); } SPIFFS_closedir(&d); } return 0; }
DIR* opendir(char* path) { DIR* dir = malloc(sizeof(DIR)); SPIFFS_opendir(&fs, path, &dir->d); return dir; }
int main(int argc, char **argv) { FILE *fp; const char *filename = NULL; int i; int list = 0; const char *extDir = "."; for (i = 1; i < argc && argv[i][0] == '-'; i++) { if (strcmp(argv[i], "-d") == 0 && i + 1 < argc) { extDir = argv[i + 1]; i++; } else if (strcmp(argv[i], "-l") == 0) { list = 1; } else if (strcmp(argv[i], "-h") == 0) { show_usage(argv); } } if (argc - i < 1) { show_usage(argv); } filename = argv[i]; fp = fopen(filename, "r"); if (fp == NULL) { fprintf(stderr, "unable to open %s, err: %d\n", filename, errno); return 1; } fseek(fp, 0, SEEK_END); image_size = ftell(fp); fseek(fp, 0, SEEK_SET); image = (char *) malloc(image_size); if (fread(image, image_size, 1, fp) < 1) { fprintf(stderr, "cannot read %s, err: %d\n", filename, errno); return 1; } mem_spiffs_mount(); { spiffs_DIR d; struct spiffs_dirent de; SPIFFS_opendir(&fs, ".", &d); while (SPIFFS_readdir(&d, &de) != NULL) { if (list) { printf("%s\n", de.name); } else { char target[1024]; char *buf = NULL; FILE *out; spiffs_file in; sprintf(target, "%s/%s", extDir, de.name); fprintf(stderr, "extracting %s\n", de.name); out = fopen(target, "w"); if (out == NULL) { fprintf(stderr, "cannot write %s, err: %d\n", target, errno); return 1; } in = SPIFFS_open_by_dirent(&fs, &de, SPIFFS_RDONLY, 0); if (in < 0) { fprintf(stderr, "cannot open spiffs file %s, err: %d\n", de.name, SPIFFS_errno(&fs)); return 1; } buf = malloc(de.size); if (SPIFFS_read(&fs, in, buf, de.size) != de.size) { fprintf(stderr, "cannot read %s, err: %d\n", de.name, SPIFFS_errno(&fs)); return 1; } SPIFFS_close(&fs, in); fwrite(buf, de.size, 1, out); free(buf); fclose(out); } } SPIFFS_closedir(&d); } free(image); fclose(fp); return 0; }
/************************************************************************************ * config_init() - called once on hard reset * * Performs one of 2 actions: * (1) if persistence is set up or out-of-rev load RAM and NVM with settings.h defaults * (2) if persistence is set up and at current config version use NVM data for config * * You can assume the cfg struct has been zeroed by a hard reset. * Do not clear it as the version and build numbers have already been set by tg_init() * * NOTE: Config assertions are handled from the controller */ void config_init() { nvObj_t *nv = nv_reset_nv_list(); char *P_str_axis[3] = {"x","y", "z"}; config_init_assertions(); #ifdef __ARM // ++++ The following code is offered until persistence is implemented. // ++++ Then you can use the AVR code (or something like it) cfg.comm_mode = JSON_MODE; // initial value until EEPROM is read _set_defa(nv); #endif #ifdef __AVR cm_set_units_mode(MILLIMETERS); // must do inits in millimeter mode nv->index = 0; // this will read the first record in NVM read_persistent_value(nv); if (nv->value != cs.fw_build) { // case (1) NVM is not setup or not in revision // if (fp_NE(nv->value, cs.fw_build)) { _set_defa(nv); } else { // case (2) NVM is setup and in revision rpt_print_loading_configs_message(); for (nv->index=0; nv_index_is_single(nv->index); nv->index++) { if (GET_TABLE_BYTE(flags) & F_INITIALIZE) { strncpy_P(nv->token, cfgArray[nv->index].token, TOKEN_LEN); // read the token from the array read_persistent_value(nv); nv_set(nv); } } sr_init_status_report(); } #endif #ifdef __RX // ++++ The following code is offered until persistence is implemented. // ++++ Then you can use the AVR code (or something like it) cm_set_units_mode(MILLIMETERS); // must do inits in millimeter mode nv->index = 0; // this will read the first record in NVM spiffs_DIR sf_dir; struct spiffs_dirent e; struct spiffs_dirent *pe = &e; spiffs_file *fd = &uspiffs[0].f; spiffs *fs = &uspiffs[0].gSPIFFS; R_FlashDataAreaAccess(0xFFFF,0xFFFF); checkifParFlashed = (char *)(0x00100000); if (SPIFFS_opendir(fs, "/", &sf_dir) == NULL) { } pe = SPIFFS_readdir(&sf_dir, pe); *fd = SPIFFS_open(fs, "config.met", SPIFFS_RDWR | SPIFFS_DIRECT, 0); SPIFFS_close(fs, *fd); if (*fd == SPIFFS_ERR_NOT_FOUND && strcmp(checkifParFlashed,checkParPhrase)) { // case (1) NVM is not setup or not in revision *fd = SPIFFS_open(fs, "config.met", SPIFFS_CREAT | SPIFFS_RDWR | SPIFFS_DIRECT, 0); SPIFFS_close(fs, *fd); R_FlashEraseRange(0x00100000,0x20); R_FlashWrite(0x00100000,(uint32_t)checkParPhrase, 0x20); _set_defa(nv); } else { // case (2) NVM is setup and in revision rpt_print_loading_configs_message(); for (nv->index=0; nv_index_is_single(nv->index); nv->index++) { if (GET_TABLE_BYTE(flags) & F_INITIALIZE) { strncpy_P(nv->token, cfgArray[nv->index].token, TOKEN_LEN); // read the token from the array read_persistent_value(nv); nv_set(nv); } } sr_init_status_report(); } z_step_pulse = (M1_TRAVEL_PER_REV*M1_STEP_ANGLE)/(360*M1_MICROSTEPS); #endif }