Пример #1
0
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);
}
Пример #2
0
// file.writeline("string")
//=======================================
static int file_writeline( lua_State* L )
{
  if(FILE_NOT_OPENED==file_fd)
    return luaL_error(L, "open a file first");
  
  size_t len;
  const char *s = luaL_checklstring(L, 1, &len);
  
  if(SPIFFS_write(&fs,file_fd, (char*)s, len)<0)
  {//failed
    lua_pushnil(L);
    SPIFFS_close(&fs,file_fd);
    file_fd = FILE_NOT_OPENED;
  }
  else
  {//success
     if(SPIFFS_write(&fs,file_fd, "\r\n", 2)<0)
     {
        SPIFFS_close(&fs,file_fd);
        file_fd = FILE_NOT_OPENED;
        lua_pushnil(L);
     }
     else
        lua_pushboolean(L, true);
  }
  return 1;
}
Пример #3
0
int fs_spiffs_close(int fd) {
  struct mount_info *m = &s_fsm;
  spiffs_fd *sfd;
  if (!m->valid) return set_errno(EBADF);
  if (spiffs_fd_get(&m->fs, fd, &sfd) == SPIFFS_OK &&
      (sfd->flags & SPIFFS_WRONLY)) {
    /* We are closing a file open for writing, close the backing store
     * to avoid the dreaded SL_FS_FILE_HAS_NOT_BEEN_CLOSE_CORRECTLY. */
    SPIFFS_close(&m->fs, fd);
    fs_close_container(m);
  } else {
    SPIFFS_close(&m->fs, fd);
  }
  return 0;
}
Пример #4
0
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);
}
Пример #5
0
int _close_r(struct _reent *r, int fd) {
    if (fd < NUM_SYS_FD) {
        return -1;
    }
    SPIFFS_close(&fs, fd - NUM_SYS_FD);
    return 0;
}
Пример #6
0
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();
}
Пример #7
0
void *mmap(void *addr, size_t len, int prot, int flags, int fd, off_t offset) {
  int pages = (len + SPIFFS_PAGE_DATA_SIZE - 1) / SPIFFS_PAGE_DATA_SIZE;
  struct mmap_desc *desc = alloc_mmap_desc();
  (void) addr;
  (void) prot;
  (void) flags;
  (void) offset;

  if (len == 0) {
    return NULL;
  }

  if (desc == NULL) {
    LOG(LL_ERROR, ("cannot allocate mmap desc"));
    return MAP_FAILED;
  }

  cur_mmap_desc = desc;
  desc->pages = 0;
  desc->blocks = (uint32_t *) calloc(sizeof(uint32_t), pages);
  desc->base = MMAP_ADDR_FROM_DESC(desc);
  SPIFFS_read(&fs, fd - NUM_SYS_FD, DUMMY_MMAP_BUFFER_START, len);
  /*
   * this breaks the posix-like mmap abstraction but file descriptors are a
   * scarse resource here.
   */
  SPIFFS_close(&fs, fd - NUM_SYS_FD);

  return desc->base;
}
Пример #8
0
// file.close()
//===================================
static int file_close( lua_State* L )
{
  if(FILE_NOT_OPENED!=file_fd){
    SPIFFS_close(&fs,file_fd);
    file_fd = FILE_NOT_OPENED;
  }
  return 0;  
}
Пример #9
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
Пример #10
0
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)
	{

	}
}
Пример #11
0
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);
}
Пример #12
0
//----------------------------------
static void _saveData(uint8_t close)
{
  if (!(status & FTP_RECEIVING)) {
    ftp_log("[FTP dta] Not receiving!\r\n");
    file_status = -4;
    return;
  }
  if ((ftpfile == NULL) || (file_fd == FILE_NOT_OPENED)) {
    ftp_log("[FTP dta] Receive file not opened\r\n");
    file_status = -2;
    return;
  }

  if (close == 0) {  
    if (SPIFFS_write(&fs, (spiffs_file)file_fd, (char*)recvDataBuf, recvDataLen) < 0)
    { //failed
      SPIFFS_fflush(&fs,file_fd);
      SPIFFS_close(&fs,file_fd);
      file_fd = FILE_NOT_OPENED;
      ftp_log("\r\n[FTP dta] Write to local file failed\r\n");
      file_status = -3;
    }
    else {
      ftp_log("\r[FTP dta] received: %d", recvDataLen);
      file_status += recvDataLen;
    }
  }
  else { // close file;
    SPIFFS_fflush(&fs,file_fd);
    SPIFFS_close(&fs,file_fd);
    file_fd = FILE_NOT_OPENED;
    free(ftpfile);
    ftpfile = NULL;
    if (close == 2) {
      ftp_log("\r\n[FTP dta] Data ERROR, file closed\r\n");
      file_status = -1;
    }
    else {
      ftp_log("\r\n[FTP dta] Data file closed\r\n");
    }
  }
}
Пример #13
0
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);
}
Пример #14
0
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;
}
Пример #15
0
} 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
Пример #16
0
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;
}
Пример #17
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;
}
Пример #18
0
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);
}
Пример #19
0
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;
}
Пример #20
0
} 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
Пример #21
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;
}
Пример #22
0
} 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
Пример #23
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);
}
Пример #24
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;
}
Пример #25
0
} 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
Пример #26
0
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);
}
Пример #27
0
//------------------------------
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;
}
Пример #28
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; 
}
Пример #29
0
void RamSpiffs::doInterrumptedWrite(){

    m_verbose = true;
    m_interruptTimer = 2;

    spiffs_file fd;

    const char *msg = "CALIFORNIA";
    int msglen = strlen(msg)+1;
    fd = SPIFFS_open(fs(), "interrupted.txt", SPIFFS_CREAT | SPIFFS_TRUNC | SPIFFS_RDWR, 0);

    // write a 2Kb file
    int i;
    for(i = 0; i < 4; i++){
        if (SPIFFS_write(fs(), fd, (u8_t *)msg, msglen) < 0){
            printf("errno %i\n", (int) SPIFFS_errno(fs()));
        }
    }

    SPIFFS_close(fs(), fd);

    m_verbose = false;
}
Пример #30
0
// file.rename("oldname", "newname")
//====================================
static int file_rename( lua_State* L )
{
  size_t len;

  if(FILE_NOT_OPENED!=file_fd){
    SPIFFS_close(&fs,file_fd);
    file_fd = FILE_NOT_OPENED;
  }

  const char *oldname = luaL_checklstring( L, 1, &len );
  if( len > SPIFFS_OBJ_NAME_LEN )
    return luaL_error(L, "filename too long");

  const char *newname = luaL_checklstring( L, 2, &len );
  if( len > SPIFFS_OBJ_NAME_LEN )
    return luaL_error(L, "filename too long");

  if(SPIFFS_OK==SPIFFS_rename(&fs, (char*)oldname, (char*)newname )){
    lua_pushboolean(L, 1);
  } else {
    lua_pushboolean(L, 0);
  }
  return 1;
}