Ejemplo n.º 1
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;
}
Ejemplo n.º 2
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);
}
Ejemplo n.º 3
0
ICACHE_FLASH_ATTR size_t
spiffs_fwrite(const void *ptr, size_t size, size_t count, int fd) {
  int res = SPIFFS_write(&fs, fd, (char *) ptr, size * count);
  set_errno(res);

  return res < 0 ? 0 : res;
}
Ejemplo n.º 4
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();
}
Ejemplo n.º 5
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)
	{

	}
}
Ejemplo n.º 6
0
size_t fileWrite(file_t file, const void* data, size_t size)
{
  int res = SPIFFS_write(&_filesystemStorageHandle, file, (void *)data, size);
  if (res < 0)
  {
    debugf("write errno %d\n", SPIFFS_errno(&_filesystemStorageHandle));
    return res;
  }
  return res;
}
Ejemplo n.º 7
0
_ssize_t _write_r(struct _reent *r, int fd, void *buf, size_t len) {
    if (fd < NUM_SYS_FD) {
        uart_write(fd, buf, len);
        return len;
    }

    int res = SPIFFS_write(&fs, fd - NUM_SYS_FD, (char *) buf, len);
    set_errno(res);
    return res;
}
Ejemplo n.º 8
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
Ejemplo n.º 9
0
} 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
Ejemplo n.º 10
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;
}
Ejemplo n.º 11
0
static int writer(lua_State* L, const void* p, size_t size, void* u)
{
  UNUSED(L);
  int file_fd = *( (int *)u );
  if ( FILE_NOT_OPENED == file_fd)
    return 1;
  //MCU_DBG("get fd:%d,size:%d\n", file_fd, size);

  if (size != 0 && (size != SPIFFS_write(&fs,file_fd, (char *)p, size)) )
    return 1;
  //MCU_DBG("write fd:%d,size:%d\n", file_fd, size);
  return 0;
}
Ejemplo n.º 12
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);
}
Ejemplo n.º 13
0
size_t myspiffs_write( int fd, const void* ptr, size_t len ){
#if 0
  if(fd==c_stdout || fd==c_stderr){
    uart0_tx_buffer((u8_t*)ptr, len);
    return len;
  }
#endif
  int res = SPIFFS_write(&fs, (spiffs_file)fd, (void *)ptr, len);
  if (res < 0) {
    NODE_DBG("write errno %i\n", SPIFFS_errno(&fs));
    return 0;
  }
  return res;
}
Ejemplo n.º 14
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
Ejemplo n.º 15
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);
}
Ejemplo n.º 16
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);
}
Ejemplo n.º 17
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
Ejemplo n.º 18
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;
}
Ejemplo n.º 19
0
_ssize_t _write_r(struct _reent *r, int fd, void *buf, size_t len) {
  (void) r;
  if (fd < NUM_SYS_FD) {
    if (fd == 1 && s_stdout_uart >= 0) {
      len = esp_sj_uart_write(s_stdout_uart, buf, len);
    } else if (fd == 2 && s_stderr_uart >= 0) {
      len = esp_sj_uart_write(s_stderr_uart, buf, len);
    } else if (fd == 0) {
      errno = EBADF;
      len = -1;
    }
    return len;
  }

  int res = SPIFFS_write(&fs, fd - NUM_SYS_FD, (char *) buf, len);
  set_errno(res);
  return res;
}
Ejemplo n.º 20
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
Ejemplo n.º 21
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);
}
Ejemplo n.º 22
0
Archivo: ftp.c Proyecto: maplefish/MICO
//----------------------------------
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");
    }
  }
}
Ejemplo n.º 23
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);
}
Ejemplo n.º 24
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;
}
Ejemplo n.º 25
0
//---------------------------------------------------------------------------------
static int32_t Ymodem_Receive ( char* FileName, uint32_t maxsize, uint8_t getname )
{
  uint8_t packet_data[PACKET_1K_SIZE + PACKET_OVERHEAD], file_size[FILE_SIZE_LENGTH], *file_ptr;
  int32_t i, packet_length, file_len, write_len, session_done, file_done, packets_received, errors, session_begin, size = 0;
  
  for (session_done = 0, errors = 0, session_begin = 0; ;)
  {
    for (packets_received = 0, file_done = 0; ;)
    {
      switch (Receive_Packet(packet_data, &packet_length, NAK_TIMEOUT))
      {
        case 0:
          switch (packet_length)
          {
            /* Abort by sender */
            case -1:
              Send_Byte(ACK);
              return 0;
            /* End of transmission */
            case 0:
              Send_Byte(ACK);
              file_done = 1;
              break;
            /* Normal packet */
            default:
              if ((packet_data[PACKET_SEQNO_INDEX] & 0xff) != (packets_received & 0xff))
              {
                errors ++;
                if (errors > MAX_ERRORS)
                {
                  send_CA();
                  return 0;
                }
                Send_Byte(NAK);
              }
              else
              {
                errors = 0;
                if (packets_received == 0)
                {
                  /* Filename packet */
                  if (packet_data[PACKET_HEADER] != 0)
                  {
                    // Filename packet has valid data
                    if (getname == 0) {
                      for (i = 0, file_ptr = packet_data + PACKET_HEADER; (*file_ptr != 0) && (i < SPIFFS_OBJ_NAME_LEN);)
                      {
                        FileName[i++] = *file_ptr++;
                      }
                      FileName[i++] = '\0';
                    }
                    for (i = 0, file_ptr = packet_data + PACKET_HEADER; (*file_ptr != 0) && (i < packet_length);)
                    {
                      file_ptr++;
                    }
                    for (i = 0, file_ptr ++; (*file_ptr != ' ') && (i < FILE_SIZE_LENGTH);)
                    {
                      file_size[i++] = *file_ptr++;
                    }
                    file_size[i++] = '\0';
                    Str2Int(file_size, &size);

                    // Test the size of the file
                    if (size < 1 || size > maxsize) {
                      file_fd = FILE_NOT_OPENED;
                      /* End session */
                      send_CA();
                      return -4;
                    }

                    /* *** Open the file *** */
                    if (FILE_NOT_OPENED != file_fd) {
                      SPIFFS_close(&fs,file_fd);
                      file_fd = FILE_NOT_OPENED;
                    }
                    file_fd = SPIFFS_open(&fs, (char*)FileName, mode2flag("w"), 0);
                    if (file_fd <= FILE_NOT_OPENED) {
                      file_fd = FILE_NOT_OPENED;
                      /* End session */
                      send_CA();
                      return -2;
                    }
                    file_len = 0;
                    Send_Byte(ACK);
                    Send_Byte(CRC16);
                  }
                  /* Filename packet is empty, end session */
                  else
                  {
                    Send_Byte(ACK);
                    file_done = 1;
                    session_done = 1;
                    break;
                  }
                }
                /* Data packet */
                else
                {
                  /* Write received data to file */
                  if (file_len < size) {
                    file_len = file_len + packet_length;
                    if (file_len > size) {
                      write_len = packet_length - (file_len - size);
                    }
                    else {
                      write_len = packet_length;
                    }
                    if (file_fd <= FILE_NOT_OPENED) {
                      file_fd = FILE_NOT_OPENED;
                      // File not opened, End session
                      send_CA();
                      return -2;
                    }
                    if (SPIFFS_write(&fs,file_fd, (char*)(packet_data + PACKET_HEADER), write_len) < 0)
                    { //failed
                      SPIFFS_close(&fs,file_fd);
                      file_fd = FILE_NOT_OPENED;
                      /* End session */
                      send_CA();
                      return -1;
                    }
                  }
                  //success
                  Send_Byte(ACK);
                }
                packets_received ++;
                session_begin = 1;
              }
          }
          break;
        case 1:
          send_CA();
          return -3;
        default:
          if (session_begin >= 0)
          {
            errors ++;
          }
          if (errors > MAX_ERRORS)
          {
            send_CA();
            return 0;
          }
          Send_Byte(CRC16);
          break;
      }
      if (file_done != 0)
      {
        break;
      }
    }
    if (session_done != 0)
    {
      break;
    }
  }
  return (int32_t)size;
}
Ejemplo n.º 26
0
int run_file_config(int cfg_count, tfile_conf* cfgs, int max_runs, int max_concurrent_files, int dbg) {
  int res;
  tfile *tfiles = malloc(sizeof(tfile) * max_concurrent_files);
  memset(tfiles, 0, sizeof(tfile) * max_concurrent_files);
  int run = 0;
  int cur_config_ix = 0;
  char name[32];
  while (run < max_runs)  {
    if (dbg) printf(" run %i/%i\n", run, max_runs);
    int i;
    for (i = 0; i < max_concurrent_files; i++) {
      sprintf(name, "file%i_%i", (1+run), i);
      tfile *tf = &tfiles[i];
      if (tf->state == 0 && cur_config_ix < cfg_count) {
// create a new file
        strcpy(tf->name, name);
        tf->state = 1;
        tf->cfg = cfgs[cur_config_ix];
        int size = tfile_get_size(tf->cfg.tsize);
        if (dbg) printf("   create new %s with cfg %i/%i, size %i\n", name, (1+cur_config_ix), cfg_count, size);

        if (tf->cfg.tsize == EMPTY) {
          res = SPIFFS_creat(FS, name, 0);
          CHECK_RES(res);
          int pfd = open(make_test_fname(name), O_TRUNC | O_CREAT | O_RDWR, S_IRUSR | S_IWUSR);
          close(pfd);
          int extra_flags = tf->cfg.ttype == APPENDED ? SPIFFS_APPEND : 0;
          spiffs_file fd = SPIFFS_open(FS, name, extra_flags | SPIFFS_RDWR, 0);
          CHECK(fd > 0);
          tf->fd = fd;
        } else {
          int extra_flags = tf->cfg.ttype == APPENDED ? SPIFFS_APPEND : 0;
          spiffs_file fd = SPIFFS_open(FS, name, extra_flags | SPIFFS_TRUNC | SPIFFS_CREAT | SPIFFS_RDWR, 0);
          CHECK(fd > 0);
          extra_flags = tf->cfg.ttype == APPENDED ? O_APPEND : 0;
          int pfd = open(make_test_fname(name), extra_flags | O_TRUNC | O_CREAT | O_RDWR, S_IRUSR | S_IWUSR);
          tf->fd = fd;
          uint8_t *buf = malloc(size);
          memrand(buf, size);
          res = SPIFFS_write(FS, fd, buf, size);
          CHECK_RES(res);
          write(pfd, buf, size);
          close(pfd);
          free(buf);
          res = read_and_verify(name);
          CHECK_RES(res);
        }

        cur_config_ix++;
      } else if (tf->state > 0) {
// hande file lifecycle
        switch (tf->cfg.ttype) {
        case UNTAMPERED: {
          break;
        }
        case APPENDED: {
          if (dbg) printf("   appending %s\n", tf->name);
          int size = SPIFFS_DATA_PAGE_SIZE(FS)*3;
          uint8_t *buf = malloc(size);
          memrand(buf, size);
          res = SPIFFS_write(FS, tf->fd, buf, size);
          CHECK_RES(res);
          int pfd = open(make_test_fname(tf->name), O_APPEND | O_RDWR);
          write(pfd, buf, size);
          close(pfd);
          free(buf);
          res = read_and_verify(tf->name);
          CHECK_RES(res);
          break;
        }
        case MODIFIED: {
          if (dbg) printf("   modify %s\n", tf->name);
          spiffs_stat stat;
          res = SPIFFS_fstat(FS, tf->fd, &stat);
          CHECK_RES(res);
          int size = stat.size / tf->cfg.tlife + SPIFFS_DATA_PAGE_SIZE(FS)/3;
          int offs = (stat.size / tf->cfg.tlife) * tf->state;
          res = SPIFFS_lseek(FS, tf->fd, offs, SPIFFS_SEEK_SET);
          CHECK_RES(res);
          uint8_t *buf = malloc(size);
          memrand(buf, size);
          res = SPIFFS_write(FS, tf->fd, buf, size);
          CHECK_RES(res);
          int pfd = open(make_test_fname(tf->name), O_RDWR);
          lseek(pfd, offs, SEEK_SET);
          write(pfd, buf, size);
          close(pfd);
          free(buf);
          res = read_and_verify(tf->name);
          CHECK_RES(res);
          break;
        }
        case REWRITTEN: {
          if (tf->fd > 0) {
            SPIFFS_close(FS, tf->fd);
          }
          if (dbg) printf("   rewriting %s\n", tf->name);
          spiffs_file fd = SPIFFS_open(FS, tf->name, SPIFFS_TRUNC | SPIFFS_CREAT | SPIFFS_RDWR, 0);
          CHECK(fd > 0);
          int pfd = open(make_test_fname(tf->name), O_TRUNC | O_CREAT | O_RDWR, S_IRUSR | S_IWUSR);
          tf->fd = fd;
          int size = tfile_get_size(tf->cfg.tsize);
          uint8_t *buf = malloc(size);
          memrand(buf, size);
          res = SPIFFS_write(FS, fd, buf, size);
          CHECK_RES(res);
          write(pfd, buf, size);
          close(pfd);
          free(buf);
          res = read_and_verify(tf->name);
          CHECK_RES(res);
          break;
        }
        }
        tf->state++;
        if (tf->state > tf->cfg.tlife) {
// file outlived its time, kill it
          if (tf->fd > 0) {
            SPIFFS_close(FS, tf->fd);
          }
          if (dbg) printf("   removing %s\n", tf->name);
          res = read_and_verify(tf->name);
          CHECK_RES(res);
          res = SPIFFS_remove(FS, tf->name);
          CHECK_RES(res);
          remove(make_test_fname(tf->name));
          memset(tf, 0, sizeof(tf));
        }

      }
    }

    run++;
  }
  free(tfiles);
  return 0;
}
Ejemplo n.º 27
0
} TEST_END
#endif

TEST(eof_tell_72) {
  fs_reset();

  s32_t res;

  spiffs_file fd = SPIFFS_open(FS, "file", SPIFFS_CREAT | SPIFFS_RDWR | SPIFFS_APPEND, 0);
  TEST_CHECK_GT(fd, 0);
  TEST_CHECK_EQ(SPIFFS_eof(FS, fd), 1);
  TEST_CHECK_EQ(SPIFFS_tell(FS, fd), 0);

  res = SPIFFS_write(FS, fd, "test", 4);
  TEST_CHECK_EQ(res, 4);
  TEST_CHECK_EQ(SPIFFS_eof(FS, fd), 1);
  TEST_CHECK_EQ(SPIFFS_tell(FS, fd), 4);

  res = SPIFFS_fflush(FS, fd);
  TEST_CHECK_EQ(res, SPIFFS_OK);
  TEST_CHECK_EQ(SPIFFS_eof(FS, fd), 1);
  TEST_CHECK_EQ(SPIFFS_tell(FS, fd), 4);

  res = SPIFFS_lseek(FS, fd, 2, SPIFFS_SEEK_SET);
  TEST_CHECK_EQ(res, 2);
  TEST_CHECK_EQ(SPIFFS_eof(FS, fd), 0);
  TEST_CHECK_EQ(SPIFFS_tell(FS, fd), 2);

  res = SPIFFS_write(FS, fd, "test", 4);
  TEST_CHECK_EQ(res, 4);
  TEST_CHECK_EQ(SPIFFS_eof(FS, fd), 1);
  TEST_CHECK_EQ(SPIFFS_tell(FS, fd), 8);

  res = SPIFFS_fflush(FS, fd);
  TEST_CHECK_EQ(res, SPIFFS_OK);
  TEST_CHECK_EQ(SPIFFS_eof(FS, fd), 1);
  TEST_CHECK_EQ(SPIFFS_tell(FS, fd), 8);

  res = SPIFFS_close(FS, fd);
  TEST_CHECK_EQ(res, SPIFFS_OK);
  TEST_CHECK_LT(SPIFFS_eof(FS, fd), SPIFFS_OK);
  TEST_CHECK_LT(SPIFFS_tell(FS, fd), SPIFFS_OK);

  fd = SPIFFS_open(FS, "file", SPIFFS_RDWR, 0);
  TEST_CHECK_GT(fd, 0);
  TEST_CHECK_EQ(SPIFFS_eof(FS, fd), 0);
  TEST_CHECK_EQ(SPIFFS_tell(FS, fd), 0);

  res = SPIFFS_lseek(FS, fd, 2, SPIFFS_SEEK_SET);
  TEST_CHECK_EQ(res, 2);
  TEST_CHECK_EQ(SPIFFS_eof(FS, fd), 0);
  TEST_CHECK_EQ(SPIFFS_tell(FS, fd), 2);

  res = SPIFFS_write(FS, fd, "test", 4);
  TEST_CHECK_EQ(res, 4);
  TEST_CHECK_EQ(SPIFFS_eof(FS, fd), 0);
  TEST_CHECK_EQ(SPIFFS_tell(FS, fd), 6);

  res = SPIFFS_fflush(FS, fd);
  TEST_CHECK_EQ(res, SPIFFS_OK);
  TEST_CHECK_EQ(SPIFFS_eof(FS, fd), 0);
  TEST_CHECK_EQ(SPIFFS_tell(FS, fd), 6);

  res = SPIFFS_lseek(FS, fd, 0, SPIFFS_SEEK_END);
  TEST_CHECK_EQ(res, 8);
  TEST_CHECK_EQ(SPIFFS_eof(FS, fd), 1);
  TEST_CHECK_EQ(SPIFFS_tell(FS, fd), 8);

  return TEST_RES_OK;
} TEST_END
Ejemplo n.º 28
0
ssize_t fs_spiffs_write(int fd, const void *buf, size_t count) {
  struct mount_info *m = &s_fsm;
  if (!m->valid) return set_errno(EBADF);
  return set_spiffs_errno(m, SPIFFS_write(&m->fs, fd, (void *) buf, count));
}
Ejemplo n.º 29
0
} TEST_END

TEST(nodemcu_full_fs_2) {
  fs_reset_specific(0, 0, 4096*22, 4096, 4096, 256);

  int res;
  spiffs_file fd;

  printf("  fill up system by writing one byte a lot\n");
  fd = SPIFFS_open(FS, "test1.txt", SPIFFS_RDWR | SPIFFS_CREAT | SPIFFS_TRUNC, 0);
  TEST_CHECK(fd > 0);
  int i;
  spiffs_stat s;
  res = SPIFFS_OK;
  for (i = 0; i < 100*1000; i++) {
    u8_t buf = 'x';
    res = SPIFFS_write(FS, fd, &buf, 1);
  }

  int errno = SPIFFS_errno(FS);
  int res2 = SPIFFS_fstat(FS, fd, &s);
  TEST_CHECK(res2 == SPIFFS_OK);
  printf("  >>> file %s size: %i\n", s.name, s.size);

  TEST_CHECK(errno == SPIFFS_ERR_FULL);
  SPIFFS_close(FS, fd);

  res2 = SPIFFS_stat(FS, "test1.txt", &s);
  TEST_CHECK(res2 == SPIFFS_OK);

  SPIFFS_clearerr(FS);
  printf("  create small file\n");
  fd = SPIFFS_open(FS, "test2.txt", SPIFFS_RDWR | SPIFFS_CREAT | SPIFFS_TRUNC, 0);
#if 0
  // before gc in v3.1
  TEST_CHECK(SPIFFS_errno(FS) == SPIFFS_OK);
  TEST_CHECK(fd > 0);

  for (i = 0; i < 1000; i++) {
    u8_t buf = 'x';
    res = SPIFFS_write(FS, fd, &buf, 1);
  }

  TEST_CHECK(SPIFFS_errno(FS) == SPIFFS_ERR_FULL);
  res2 = SPIFFS_fstat(FS, fd, &s);
  TEST_CHECK(res2 == SPIFFS_OK);
  printf("  >>> file %s size: %i\n", s.name, s.size);
  TEST_CHECK(s.size == 0);
  SPIFFS_clearerr(FS);
#else
  TEST_CHECK(SPIFFS_errno(FS) == SPIFFS_ERR_FULL);
  SPIFFS_clearerr(FS);
#endif
  printf("  remove files\n");
  res = SPIFFS_remove(FS, "test1.txt");
  TEST_CHECK(res == SPIFFS_OK);
#if 0
  res = SPIFFS_remove(FS, "test2.txt");
  TEST_CHECK(res == SPIFFS_OK);
#endif

  printf("  create medium file\n");
  fd = SPIFFS_open(FS, "test3.txt", SPIFFS_RDWR | SPIFFS_CREAT | SPIFFS_TRUNC, 0);
  TEST_CHECK(SPIFFS_errno(FS) == SPIFFS_OK);
  TEST_CHECK(fd > 0);

  for (i = 0; i < 20*1000; i++) {
    u8_t buf = 'x';
    res = SPIFFS_write(FS, fd, &buf, 1);
  }
  TEST_CHECK(SPIFFS_errno(FS) == SPIFFS_OK);

  res2 = SPIFFS_fstat(FS, fd, &s);
  TEST_CHECK(res2 == SPIFFS_OK);
  printf("  >>> file %s size: %i\n", s.name, s.size);
  TEST_CHECK(s.size == 20*1000);

  return TEST_RES_OK;

} TEST_END
Ejemplo n.º 30
0
int ICACHE_FLASH_ATTR roffs_write(ROFFS_FILE *file, char *buf, int len)
{
    return SPIFFS_write(&fs, file->fd, buf, len);
}