void test_spiffs() { char buf[12] = {0}; // Surely, I've mounted spiffs before entering here spiffs_file fd; spiffs_stat st = {0}; SPIFFS_stat(&_filesystemStorageHandle, "my_file.txt", &st); if (st.size <= 0) { fd = SPIFFS_open(&_filesystemStorageHandle, "my_file.txt", SPIFFS_CREAT | SPIFFS_TRUNC | SPIFFS_RDWR, 0); if (SPIFFS_write(&_filesystemStorageHandle, fd, (u8_t *)"Hello world", 11) < 0) debugf("errno %d\n", SPIFFS_errno(&_filesystemStorageHandle)); SPIFFS_close(&_filesystemStorageHandle, fd); debugf("file created"); } else debugf("file %s exist :)", st.name); fd = SPIFFS_open(&_filesystemStorageHandle, "my_file.txt", SPIFFS_RDWR, 0); if (SPIFFS_read(&_filesystemStorageHandle, fd, (u8_t *)buf, 11) < 0) debugf("errno %d\n", SPIFFS_errno(&_filesystemStorageHandle)); SPIFFS_close(&_filesystemStorageHandle, fd); debugf("--> %s <--\n", buf); }
} TEST_END TEST(zero_sized_file_44) { fs_reset(); spiffs_file fd = SPIFFS_open(FS, "zero", SPIFFS_RDWR | SPIFFS_CREAT, 0); TEST_CHECK_GE(fd, 0); int res = SPIFFS_close(FS, fd); TEST_CHECK_GE(res, 0); fd = SPIFFS_open(FS, "zero", SPIFFS_RDWR, 0); TEST_CHECK_GE(fd, 0); u8_t buf[8]; res = SPIFFS_read(FS, fd, buf, 8); TEST_CHECK_LT(res, 0); TEST_CHECK_EQ(SPIFFS_errno(FS), SPIFFS_ERR_END_OF_OBJECT); res = SPIFFS_read(FS, fd, buf, 0); TEST_CHECK_GE(res, 0); res = SPIFFS_read(FS, fd, buf, 0); TEST_CHECK_GE(res, 0); buf[0] = 1; buf[1] = 2; res = SPIFFS_write(FS, fd, buf, 2); TEST_CHECK_EQ(res, 2); res = SPIFFS_lseek(FS, fd, 0, SPIFFS_SEEK_SET); TEST_CHECK_GE(res, 0); u8_t b; res = SPIFFS_read(FS, fd, &b, 1); TEST_CHECK_EQ(res, 1); TEST_CHECK_EQ(b, 1); res = SPIFFS_read(FS, fd, &b, 1); TEST_CHECK_EQ(res, 1); TEST_CHECK_EQ(b, 2); res = SPIFFS_read(FS, fd, buf, 8); TEST_CHECK_LT(res, 0); TEST_CHECK_EQ(SPIFFS_errno(FS), SPIFFS_ERR_END_OF_OBJECT); return TEST_RES_OK; } TEST_END
void test_spiffs() { char buf[12]; // Surely, I've mounted spiffs before entering here spiffs_file fd = SPIFFS_open(&fs, "my_file", SPIFFS_CREAT | SPIFFS_TRUNC | SPIFFS_RDWR, 0); if (SPIFFS_write(&fs, fd, (u8_t *)"Hello world", 12) < 0) NODE_DBG("errno %i\n", SPIFFS_errno(&fs)); SPIFFS_close(&fs, fd); fd = SPIFFS_open(&fs, "my_file", SPIFFS_RDWR, 0); if (SPIFFS_read(&fs, fd, (u8_t *)buf, 12) < 0) NODE_DBG("errno %i\n", SPIFFS_errno(&fs)); SPIFFS_close(&fs, fd); NODE_DBG("--> %s <--\n", buf); }
} TEST_END #if !SPIFFS_READ_ONLY TEST(truncate_48) { fs_reset(); u32_t len = SPIFFS_DATA_PAGE_SIZE(FS)/2; s32_t res = test_create_and_write_file("small", len, len); TEST_CHECK_GE(res, 0); spiffs_file fd = SPIFFS_open(FS, "small", SPIFFS_RDWR, 0); TEST_CHECK_GE(fd, 0); spiffs_fd *desc; #if SPIFFS_FILEHDL_OFFSET res = spiffs_fd_get(FS, fd - TEST_SPIFFS_FILEHDL_OFFSET, &desc); #else res = spiffs_fd_get(FS, fd, &desc); #endif TEST_CHECK_GE(res, 0); TEST_CHECK_EQ(desc->size, len); u32_t new_len = len/2; res = spiffs_object_truncate(desc, new_len, 0); TEST_CHECK_GE(res, 0); TEST_CHECK_EQ(desc->size, new_len); res = SPIFFS_close(FS, fd); TEST_CHECK_GE(res, 0); spiffs_stat s; res = SPIFFS_stat(FS, "small", &s); TEST_CHECK_GE(res, 0); TEST_CHECK_EQ(s.size, new_len); res = SPIFFS_remove(FS, "small"); TEST_CHECK_GE(res, 0); fd = SPIFFS_open(FS, "small", SPIFFS_RDWR, 0); TEST_CHECK_LT(fd, 0); TEST_CHECK_EQ(SPIFFS_errno(FS), SPIFFS_ERR_NOT_FOUND); return TEST_RES_OK; } TEST_END
void command_process(void) { if (!handle_data) { return; } // sd_nvic_critical_region_enter(); uint8_t command_id = data_buf[0]; switch (command_id) { case COMMAND_OPEN_FILE_STREAM: data_upload_fd = SPIFFS_open(&fs, "watchset", SPIFFS_CREAT | SPIFFS_TRUNC | SPIFFS_RDWR, 0); break; case COMMAND_APPEND_DATA_TO_FILE_STREAM: SPIFFS_write(&fs, data_upload_fd, data_buf+1, data_ptr-1); break; case COMMAND_CLOSE_FILE_STREAM: SPIFFS_close(&fs, data_upload_fd); break; } handle_data = false; // sd_nvic_critical_region_exit(); ble_peripheral_confirm_command_processed(); }
static void scr_status_init(void) { spiffs_file fd = SPIFFS_open(&fs, "u/status", SPIFFS_RDONLY, 0); if (fd >= 0) { SPIFFS_lseek(&fs, fd, 0, SPIFFS_SEEK_SET); scr_mngr_show_screen_with_param(SCR_WATCH_SET, 2<<24 | fd); } }
int _open_r(struct _reent *r, const char *filename, int flags, int mode) { spiffs_mode sm = 0; int res; int rw = (flags & 3); if (rw == O_RDONLY || rw == O_RDWR) sm |= SPIFFS_RDONLY; if (rw == O_WRONLY || rw == O_RDWR) sm |= SPIFFS_WRONLY | SPIFFS_CREAT; if (flags & O_CREAT) sm |= SPIFFS_CREAT; if (flags & O_TRUNC) sm |= SPIFFS_TRUNC; if (flags & O_APPEND) sm |= SPIFFS_APPEND | SPIFFS_CREAT; /* Supported in newer versions of SPIFFS. */ /* if (flags && O_EXCL) sm |= SPIFFS_EXCL; */ /* if (flags && O_DIRECT) sm |= SPIFFS_DIRECT; */ /* spiffs doesn't support directories, not even the trivial ./something */ while (*filename != '\0' && (*filename == '/' || *filename == '.')) { filename++; } res = SPIFFS_open(&fs, (char *) filename, sm, 0); if (res >= 0) { res += NUM_SYS_FD; } set_errno(res); return res; }
spiffs_file config_get_default_watch_face_fd(void) { char buf[32]; ext_ram_read_data(EXT_RAM_CONFIG + OFFSET_DEFAULT_WATCH_FACE, (uint8_t*)buf, SPIFFS_OBJ_NAME_LEN); if (buf[0] == 0) { return -1; } return SPIFFS_open(&fs, buf, SPIFFS_RDONLY, 0); }
} 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
int read_and_verify(char *name) { int32_t res; int fd = SPIFFS_open(&__fs, name, SPIFFS_RDONLY, 0); if (fd < 0) { printf(" read_and_verify: could not open file %s\n", name); return fd; } return read_and_verify_fd(fd, name); }
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) { } }
static void cat (char *fname) { spiffs_file fh = SPIFFS_open (&fs, fname, SPIFFS_RDONLY, 0); char buff[512]; s32_t n; while ((n = SPIFFS_read (&fs, fh, buff, sizeof (buff))) > 0) write (STDOUT_FILENO, buff, n); SPIFFS_close (&fs, fh); }
void ICACHE_FLASH_ATTR test_spiffs() { spiffs_config cfg; cfg.phys_size = PHYS_FLASH_SIZE; // use all spi flash cfg.phys_addr = SPIFFS_FLASH_SIZE; // start spiffs at start of spi flash cfg.phys_erase_block = SECTOR_SIZE; // according to datasheet cfg.log_block_size = LOG_BLOCK; // let us not complicate things cfg.log_page_size = LOG_PAGE; // as we said cfg.hal_read_f = my_spiffs_read; cfg.hal_write_f = my_spiffs_write; cfg.hal_erase_f = my_spiffs_erase; int res = SPIFFS_mount(&fs, &cfg, spiffs_work_buf, spiffs_fds, sizeof(spiffs_fds), spiffs_cache_buf, sizeof(spiffs_cache_buf), 0); os_printf("mount res: %d\n", res); char buf[12]; // Surely, I've mounted spiffs before entering here spiffs_file fd = SPIFFS_open(&fs, "my_file", SPIFFS_CREAT | SPIFFS_TRUNC | SPIFFS_RDWR, 0); os_printf("fd = %d\n", fd); if (SPIFFS_write(&fs, fd, (u8_t *)"Hello world", 12) < 0) { os_printf("errno %d\n", SPIFFS_errno(&fs)); return; } SPIFFS_close(&fs, fd); fd = SPIFFS_open(&fs, "my_file", SPIFFS_RDWR, 0); if (SPIFFS_read(&fs, fd, (u8_t *)buf, 12) < 0) { os_printf("errno %d\n", SPIFFS_errno(&fs)); return; } SPIFFS_close(&fs, fd); os_printf("--> %s <--\n", buf); }
} 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
static void server_serve(struct espconn *c, void *p, unsigned short len) { int res; char *buf = NULL; char *filename; const char *ok = "HTTP/1.1 200 OK\r\n\r\n"; const char *ok_gzip = "HTTP/1.1 200 OK\r\nContent-Encoding: gzip\n\r\n"; const char *resp = ok; const char not_found[] = "HTTP/1.1 404 Not Found\r\n\r\n"; const char server_error[] = "HTTP/1.1 500 Internal error\r\n\r\n"; spiffs_stat stat; spiffs_file fd = 0; (void) p; (void) len; if (len >= 16 && strncmp(p, "GET /favicon.ico", 16) == 0) { espconn_sent(c, (char *) not_found, sizeof(not_found)); return; } else { filename = "index.html"; } if ((res = SPIFFS_stat(&fs, filename, &stat)) < 0) { espconn_sent(c, (char *) server_error, sizeof(server_error)); return; } if ((fd = SPIFFS_open(&fs, filename, SPIFFS_RDONLY, 0)) < 0) { espconn_sent(c, (char *) server_error, sizeof(server_error)); return; } int resp_size = strlen(resp) + stat.size; buf = malloc(resp_size + 1); if (buf == NULL) { espconn_sent(c, (char *) server_error, sizeof(server_error)); goto cleanup; } memcpy(buf, resp, strlen(resp)); if ((res = SPIFFS_read(&fs, fd, buf + strlen(resp), stat.size)) < 0) { espconn_sent(c, (char *) server_error, sizeof(server_error)); goto cleanup; } buf[resp_size] = '\0'; espconn_sent(c, (char *) buf, resp_size); cleanup: if (buf) free(buf); SPIFFS_close(&fs, fd); return; }
} 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
int test_create_and_write_file(char *name, int size, int chunk_size) { int res; spiffs_file fd; printf(" create and write %s", name); res = test_create_file(name); if (res < 0) { printf(" failed creation, %i\n",res); } CHECK(res >= 0); fd = SPIFFS_open(FS, name, SPIFFS_APPEND | SPIFFS_RDWR, 0); if (res < 0) { printf(" failed open, %i\n",res); } CHECK(fd >= 0); int pfd = open(make_test_fname(name), O_APPEND | O_TRUNC | O_CREAT | O_RDWR, S_IRUSR | S_IWUSR); int offset = 0; int mark = 0; while (offset < size) { int len = MIN(size-offset, chunk_size); if (offset > mark) { mark += size/16; printf("."); fflush(stdout); } uint8_t *buf = malloc(len); memrand(buf, len); res = SPIFFS_write(FS, fd, buf, len); write(pfd, buf, len); free(buf); if (res < 0) { printf("\n error @ offset %i, res %i\n", offset, res); } offset += len; CHECK(res >= 0); } printf("\n"); close(pfd); spiffs_stat stat; res = SPIFFS_fstat(FS, fd, &stat); if (res < 0) { printf(" failed fstat, %i\n",res); } CHECK(res >= 0); if (stat.size != size) { printf(" failed size, %i != %i\n", stat.size, size); } CHECK(stat.size == size); SPIFFS_close(FS, fd); return 0; }
ROFFS_FILE ICACHE_FLASH_ATTR *roffs_create(const char *fileName, int size) { ROFFS_FILE *file; spiffs_file fd; if ((fd = SPIFFS_open(&fs, fileName, SPIFFS_O_CREAT | SPIFFS_O_WRONLY, 0)) < 0) return NULL; if (!(file = (ROFFS_FILE *)os_malloc(sizeof(ROFFS_FILE)))) { SPIFFS_close(&fs, fd); return NULL; } file->fd = fd; return file; }
void copy(char *path, char *fname) { int size; u8_t *buf; struct stat st; spiffs_file sfd; int ifd; ifd = open(path, O_RDONLY); if (ifd == -1) { fprintf(stderr, "cannot open %s\n", path); perror("cannot open"); return; } if (fstat(ifd, &st) == -1) { fprintf(stderr, "cannot stat %s\n", path); return; } size = st.st_size; if (size == -1) { fprintf(stderr, "skipping %s\n", path); return; } buf = malloc(size); if (read(ifd, buf, size) != size) { fprintf(stderr, "unable to read file %s\n", fname); return; } if ((sfd = SPIFFS_open(&fs, fname, SPIFFS_CREAT | SPIFFS_TRUNC | SPIFFS_RDWR, 0)) == -1) { fprintf(stderr, "SPIFFS_open %s failed: %d\n", fname, SPIFFS_errno(&fs)); goto cleanup; } if (SPIFFS_write(&fs, sfd, (uint8_t *) buf, size) == -1) { fprintf(stderr, "SPIFFS_write %s failed: %d\n", fname, SPIFFS_errno(&fs)); goto spifs_cleanup; } fprintf(stderr, "a %s\n", fname); spifs_cleanup: SPIFFS_close(&fs, sfd); cleanup: free(buf); close(ifd); }
int test_create_file(char *name) { spiffs_stat s; spiffs_file fd; int res = SPIFFS_creat(FS, name, 0); CHECK_RES(res); fd = SPIFFS_open(FS, name, SPIFFS_RDONLY, 0); CHECK(fd >= 0); res = SPIFFS_fstat(FS, fd, &s); CHECK_RES(res); CHECK(strcmp((char*)s.name, name) == 0); CHECK(s.size == 0); SPIFFS_close(FS, fd); return 0; }
static int file_compile( lua_State* L ) { Proto* f; int file_fd = FILE_NOT_OPENED; size_t len; const char *fname = luaL_checklstring( L, 1, &len ); if ( len > SPIFFS_OBJ_NAME_LEN ) return luaL_error(L, "filename too long"); char output[SPIFFS_OBJ_NAME_LEN]; strcpy(output, fname); // check here that filename end with ".lua". if (len < 4 || (strcmp( output + len - 4, ".lua") != 0) ) return luaL_error(L, "not a .lua file"); output[strlen(output) - 2] = 'c'; output[strlen(output) - 1] = '\0'; if (luaL_loadfile(L, fname) != 0) { return luaL_error(L, lua_tostring(L, -1)); } f = toproto(L, -1); int stripping = 1; /* strip debug information? */ file_fd = SPIFFS_open(&fs,(char*)output,mode2flag("w+"),0); if (file_fd < FILE_NOT_OPENED) { return luaL_error(L, "cannot open/write to file"); } lua_lock(L); int result = luaU_dump(L, f, writer, &file_fd, stripping); lua_unlock(L); SPIFFS_fflush(&fs,file_fd); SPIFFS_close(&fs,file_fd); file_fd =FILE_NOT_OPENED; if (result == LUA_ERR_CC_INTOVERFLOW) { return luaL_error(L, "value too big or small for target integer type"); } if (result == LUA_ERR_CC_NOTINTEGER) { return luaL_error(L, "target lua_Number is integral but fractional value found"); } return 0; }
static void spiffs_mount_internal(spiffs_config *cfg) { if (cfg->phys_addr == 0) { SYSTEM_ERROR("Can't start file system, wrong address"); return; } debugf("fs.start: size:%d Kb, offset:0x%X\n", cfg->phys_size / 1024, cfg->phys_addr - INTERNAL_FLASH_START_ADDRESS); cfg->hal_read_f = api_spiffs_read; cfg->hal_write_f = api_spiffs_write; cfg->hal_erase_f = api_spiffs_erase; uint32_t dat; bool writeFirst = false; flashmem_read(&dat, cfg->phys_addr, 4); //debugf("%X", dat); if (dat == UINT32_MAX) { debugf("First init file system"); spiffs_format_internal(cfg); writeFirst = true; } int res = SPIFFS_mount(&_filesystemStorageHandle, cfg, spiffs_work_buf, spiffs_fds, sizeof(spiffs_fds), spiffs_cache, sizeof(spiffs_cache), NULL); debugf("mount res: %d\n", res); if (writeFirst) { file_t fd = SPIFFS_open(&_filesystemStorageHandle, "initialize_fs_header.dat", SPIFFS_CREAT | SPIFFS_TRUNC | SPIFFS_RDWR, 0); SPIFFS_write(&_filesystemStorageHandle, fd, (u8_t *)"1", 1); SPIFFS_fremove(&_filesystemStorageHandle, fd); SPIFFS_close(&_filesystemStorageHandle, fd); } //dat=0; //flashmem_read(&dat, cfg.phys_addr, 4); //debugf("%X", dat); }
int fs_spiffs_open(const char *pathname, int flags, mode_t mode) { struct mount_info *m = &s_fsm; spiffs_mode sm = 0; int rw = (flags & 3); if (!s_fsm.valid) return -1; if (rw == O_RDONLY || rw == O_RDWR) sm |= SPIFFS_RDONLY; if (rw == O_WRONLY || rw == O_RDWR) sm |= SPIFFS_WRONLY; if (flags & O_CREAT) sm |= SPIFFS_CREAT; if (flags & O_TRUNC) sm |= SPIFFS_TRUNC; if (flags & O_APPEND) sm |= SPIFFS_APPEND; /* Supported in newer versions of SPIFFS. */ /* if (flags && O_EXCL) sm |= SPIFFS_EXCL; */ /* if (flags && O_DIRECT) sm |= SPIFFS_DIRECT; */ return set_spiffs_errno(m, SPIFFS_open(&m->fs, (char *) pathname, sm, 0)); }
bool spiffs_mount() { spiffs_config cfg = spiffs_get_storage_config(); if (cfg.phys_addr == 0) { SYSTEM_ERROR("Can't start file system, wrong address"); return false; } debugf("fs.start:%x, size:%d Kb\n", cfg.phys_addr, cfg.phys_size / 1024); cfg.hal_read_f = api_spiffs_read; cfg.hal_write_f = api_spiffs_write; cfg.hal_erase_f = api_spiffs_erase; uint32_t dat; bool writeFirst = false; flashmem_read(&dat, cfg.phys_addr, 4); if (dat == UINT32_MAX) { debugf("First init file system"); if(!spiffs_format_internal()) { SYSTEM_ERROR("Can't format file system"); return false; } writeFirst = true; } int res = SPIFFS_mount(&_filesystemStorageHandle, &cfg, spiffs_work_buf, spiffs_fds, sizeof(spiffs_fds), spiffs_cache, sizeof(spiffs_cache), NULL); debugf("mount res: %d\n", res); if(res != 0) return false; if (writeFirst) { file_t fd = SPIFFS_open(&_filesystemStorageHandle, "initialize_fs_header.dat", SPIFFS_CREAT | SPIFFS_TRUNC | SPIFFS_RDWR, 0); SPIFFS_write(&_filesystemStorageHandle, fd, (u8_t *)"1", 1); SPIFFS_fremove(&_filesystemStorageHandle, fd); SPIFFS_close(&_filesystemStorageHandle, fd); } return true; }
int fs_spiffs_open(const char *pathname, int flags, mode_t mode) { struct mount_info *m = &s_fsm; spiffs_mode sm = 0; int rw = (flags & 3); if (!s_fsm.valid) return -1; if (rw == O_RDONLY || rw == O_RDWR) sm |= SPIFFS_RDONLY; if (rw == O_WRONLY || rw == O_RDWR) sm |= SPIFFS_WRONLY; if (flags & O_CREAT) sm |= SPIFFS_CREAT; if (flags & O_TRUNC) sm |= SPIFFS_TRUNC; if (flags & O_APPEND) sm |= SPIFFS_APPEND; #ifdef O_EXCL if (flags & O_EXCL) sm |= SPIFFS_EXCL; #endif int res = SPIFFS_open(&m->fs, (char *) pathname, sm, 0); if (res < 0) set_spiffs_errno(m, res); return res; }
file_t fileOpen(const String name, FileOpenFlags flags) { int res; // Special fix to prevent known spifFS bug: manual delete file if ((flags & eFO_CreateNewAlways) == eFO_CreateNewAlways) { if (fileExist(name)) fileDelete(name); flags = (FileOpenFlags)((int)flags & ~eFO_Truncate); } res = SPIFFS_open(&_filesystemStorageHandle, name.c_str(), (spiffs_flags)flags, 0); if (res < 0) debugf("open errno %d\n", SPIFFS_errno(&_filesystemStorageHandle)); return res; }
} TEST_END TEST(robert) { // create a clean file system starting at address 0, 2 megabytes big, // sector size 65536, block size 65536, page size 256 fs_reset_specific(0, 0, 1024*1024*2, 65536, 65536, 256); int res; spiffs_file fd; char fname[32]; sprintf(fname, "test.txt"); fd = SPIFFS_open(FS, fname, SPIFFS_RDWR | SPIFFS_CREAT | SPIFFS_TRUNC, 0); TEST_CHECK(fd > 0); int i; res = SPIFFS_OK; char buf[500]; memset(buf, 0xaa, 500); res = SPIFFS_write(FS, fd, buf, 500); TEST_CHECK(res >= SPIFFS_OK); SPIFFS_close(FS, fd); int errno = SPIFFS_errno(FS); TEST_CHECK(errno == SPIFFS_OK); //SPIFFS_vis(FS); // unmount SPIFFS_unmount(FS); // remount res = fs_mount_specific(0, 1024*1024*2, 65536, 65536, 256); TEST_CHECK(res== SPIFFS_OK); //SPIFFS_vis(FS); spiffs_stat s; TEST_CHECK(SPIFFS_stat(FS, fname, &s) == SPIFFS_OK); printf("file %s stat size %i\n", s.name, s.size); TEST_CHECK(s.size == 500); return TEST_RES_OK; } TEST_END
static void import (char *src, char *dst) { int fd = open (src, O_RDONLY); if (fd < 0) die (src); spiffs_file fh = SPIFFS_open (&fs, dst, SPIFFS_CREAT | SPIFFS_TRUNC | SPIFFS_WRONLY, 0); if (fh < 0) die ("spiffs_open"); char buff[512]; s32_t n; while ((n = read (fd, buff, sizeof (buff))) > 0) if (SPIFFS_write (&fs, fh, buff, n) < 0) die ("spiffs_write"); SPIFFS_close (&fs, fh); close (fd); }
//------------------------------ static int _openFile(char *mode) { // open the file if (ftpfile == NULL) { ftp_log("[FTP fil] Ftpfile not assigned\r\n" ); return -1; } ftp_log("[FTP fil] Opening local file: %s\r\n", ftpfile ); if (FILE_NOT_OPENED != file_fd) { ftp_log("[FTP fil] Closing file first\r\n" ); SPIFFS_close(&fs,file_fd); file_fd = FILE_NOT_OPENED; } file_fd = SPIFFS_open(&fs, (char*)ftpfile, mode2flag(mode), 0); if (file_fd <= FILE_NOT_OPENED) { ftp_log("[FTP fil] Error opening local file: %d\r\n", file_fd ); file_fd = FILE_NOT_OPENED; return -2; } return 0; }
// file.open(filename, mode) static int file_open( lua_State* L ) { size_t len; const char *fname = luaL_checklstring( L, 1, &len ); if( len > SPIFFS_OBJ_NAME_LEN ) return luaL_error(L, "filename too long"); if(FILE_NOT_OPENED!=file_fd){ SPIFFS_close(&fs,file_fd); file_fd = FILE_NOT_OPENED; } const char *mode = luaL_optstring(L, 2, "r"); file_fd = SPIFFS_open(&fs,(char*)fname,mode2flag((char*)mode),0); if(file_fd < FILE_NOT_OPENED){ file_fd = FILE_NOT_OPENED; lua_pushnil(L); } else { lua_pushboolean(L, true); } return 1; }