s32_t SPIFFS_read_notify(spiffs *fs, spiffs_file fh, void *buf, s32_t chunk_no, s32_t chunk_len, void (*notify_handle)(void*, void*), void* notify_data) {
  SPIFFS_API_CHECK_CFG(fs);
  SPIFFS_API_CHECK_MOUNT(fs);
  SPIFFS_LOCK(fs);

  spiffs_fd *fd;
  s32_t res;
	s32_t len = chunk_no * chunk_len;

  fh = SPIFFS_FH_UNOFFS(fs, fh);
  res = spiffs_fd_get(fs, fh, &fd);
  SPIFFS_API_CHECK_RES_UNLOCK(fs, res);

  if ((fd->flags & SPIFFS_RDONLY) == 0) {
    res = SPIFFS_ERR_NOT_READABLE;
    SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
  }

  if (fd->size == SPIFFS_UNDEFINED_LEN && len > 0) {
    // special case for zero sized files
    res = SPIFFS_ERR_END_OF_OBJECT;
    SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
  }

#if SPIFFS_CACHE_WR
  spiffs_fflush_cache(fs, fh);
#endif

  if (fd->fdoffset + len >= fd->size) {
    // reading beyond file size
    s32_t avail = fd->size - fd->fdoffset;
    if (avail <= 0) {
      SPIFFS_API_CHECK_RES_UNLOCK(fs, SPIFFS_ERR_END_OF_OBJECT);
    }
    res = spiffs_object_read_notify(fd, fd->fdoffset, avail, buf, chunk_len, notify_handle, notify_data);
    if (res == SPIFFS_ERR_END_OF_OBJECT) {
      fd->fdoffset += avail;
      SPIFFS_UNLOCK(fs);
      return avail;
    } else {
      SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
      len = avail;
    }
  } else {
    // reading within file size
    res = spiffs_object_read_notify(fd, fd->fdoffset, len, buf, chunk_len, notify_handle, notify_data);
    SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
  }
  fd->fdoffset += len;

  SPIFFS_UNLOCK(fs);

  return len;
}
spiffs_file SPIFFS_open_by_dirent(spiffs *fs, struct spiffs_dirent *e, spiffs_flags flags, spiffs_mode mode) {
  SPIFFS_API_CHECK_MOUNT(fs);
  SPIFFS_LOCK(fs);

  spiffs_fd *fd;

  s32_t res = spiffs_fd_find_new(fs, &fd);
  SPIFFS_API_CHECK_RES_UNLOCK(fs, res);

  res = spiffs_object_open_by_page(fs, e->pix, fd, flags, mode);
  if (res < SPIFFS_OK) {
    spiffs_fd_return(fs, fd->file_nbr);
  }
  SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
  if (flags & SPIFFS_TRUNC) {
    res = spiffs_object_truncate(fd, 0, 0);
    if (res < SPIFFS_OK) {
      spiffs_fd_return(fs, fd->file_nbr);
    }
    SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
  }

  fd->fdoffset = 0;

  SPIFFS_UNLOCK(fs);

  return fd->file_nbr;
}
Beispiel #3
0
s32_t SPIFFS_format(spiffs *fs) {
#if SPIFFS_READ_ONLY
  (void)fs;
  return SPIFFS_ERR_RO_NOT_IMPL;
#else
  SPIFFS_API_CHECK_CFG(fs);
  if (SPIFFS_CHECK_MOUNT(fs)) {
    fs->err_code = SPIFFS_ERR_MOUNTED;
    return -1;
  }

  s32_t res;
  SPIFFS_LOCK(fs);

  spiffs_block_ix bix = 0;
  while (bix < fs->block_count) {
    fs->max_erase_count = 0;
    res = spiffs_erase_block(fs, bix);
    if (res != SPIFFS_OK) {
      res = SPIFFS_ERR_ERASE_FAIL;
    }
    SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
    bix++;
  }

  SPIFFS_UNLOCK(fs);

  return 0;
#endif // SPIFFS_READ_ONLY
}
s32_t SPIFFS_remove(spiffs *fs, const char *path) {
  SPIFFS_API_CHECK_MOUNT(fs);
  SPIFFS_LOCK(fs);

  spiffs_fd *fd;
  spiffs_page_ix pix;
  s32_t res;

  res = spiffs_fd_find_new(fs, &fd);
  SPIFFS_API_CHECK_RES_UNLOCK(fs, res);

  res = spiffs_object_find_object_index_header_by_name(fs, (u8_t *)path, &pix);
  if (res != SPIFFS_OK) {
    spiffs_fd_return(fs, fd->file_nbr);
  }
  SPIFFS_API_CHECK_RES_UNLOCK(fs, res);

  res = spiffs_object_open_by_page(fs, pix, fd, 0,0);
  if (res != SPIFFS_OK) {
    spiffs_fd_return(fs, fd->file_nbr);
  }
  SPIFFS_API_CHECK_RES_UNLOCK(fs, res);

  res = spiffs_object_truncate(fd, 0, 1);
  if (res != SPIFFS_OK) {
    spiffs_fd_return(fs, fd->file_nbr);
  }
  SPIFFS_API_CHECK_RES_UNLOCK(fs, res);

  SPIFFS_UNLOCK(fs);
  return 0;
}
s32_t SPIFFS_fremove(spiffs *fs, spiffs_file fh) {
  SPIFFS_API_CHECK_MOUNT(fs);
  SPIFFS_LOCK(fs);

  spiffs_fd *fd;
  s32_t res;
  res = spiffs_fd_get(fs, fh, &fd);
  SPIFFS_API_CHECK_RES_UNLOCK(fs, res);

  if ((fd->flags & SPIFFS_WRONLY) == 0) {
    res = SPIFFS_ERR_NOT_WRITABLE;
    SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
  }

#if SPIFFS_CACHE_WR
  spiffs_cache_fd_release(fs, fd->cache_page);
#endif

  res = spiffs_object_truncate(fd, 0, 1);

  SPIFFS_API_CHECK_RES_UNLOCK(fs, res);

  SPIFFS_UNLOCK(fs);

  return 0;
}
Beispiel #6
0
s32_t SPIFFS_format(spiffs *fs) {
    SPIFFS_API_CHECK_CFG(fs);
    if (SPIFFS_CHECK_MOUNT(fs)) {
        fs->err_code = SPIFFS_ERR_MOUNTED;
        return -1;
    }

    s32_t res;
    SPIFFS_LOCK(fs);

    spiffs_block_ix bix = 0;
    while (bix < fs->block_count) {
        fs->max_erase_count = 0;
        res = spiffs_erase_block(fs, bix);
        if (res != SPIFFS_OK) {
            res = SPIFFS_ERR_ERASE_FAIL;
        }
        SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
        bix++;
    }

    SPIFFS_UNLOCK(fs);

    return 0;
}
Beispiel #7
0
spiffs_file spiffs_open_by_dirent(spiffs *fs, spiffs_dirent *e, spiffs_flags flags, spiffs_mode mode) {
    SPIFFS_API_CHECK_CFG(fs);
    SPIFFS_API_CHECK_MOUNT(fs);
    SPIFFS_LOCK(fs);

    spiffs_fd *fd;

    s32_t res = spiffs_fd_find_new(fs, &fd);
    SPIFFS_API_CHECK_RES_UNLOCK(fs, res);

    res = spiffs_object_open_by_page(fs, e->pix, fd, flags, mode);
    if (res < SPIFFS_OK) {
        spiffs_fd_return(fs, fd->file_nbr);
    }
    SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
#if !SPIFFS_READ_ONLY
    if (flags & SPIFFS_O_TRUNC) {
        res = spiffs_object_truncate(fd, 0, 0);
        if (res < SPIFFS_OK) {
            spiffs_fd_return(fs, fd->file_nbr);
        }
        SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
    }
#endif // !SPIFFS_READ_ONLY

    fd->fdoffset = 0;

    SPIFFS_UNLOCK(fs);

    return SPIFFS_FH_OFFS(fs, fd->file_nbr);
}
Beispiel #8
0
s32_t spiffs_creat(spiffs *fs, const char *path, spiffs_mode mode) {
#if SPIFFS_READ_ONLY
    (void)fs;
    (void)path;
    (void)mode;
    return SPIFFS_ERR_RO_NOT_IMPL;
#else
    (void)mode;
    SPIFFS_API_CHECK_CFG(fs);
    SPIFFS_API_CHECK_MOUNT(fs);
    if (strlen(path) > SPIFFS_OBJ_NAME_LEN - 1) {
        SPIFFS_API_CHECK_RES(fs, SPIFFS_ERR_NAME_TOO_LONG);
    }
    SPIFFS_LOCK(fs);
    spiffs_obj_id obj_id;
    s32_t res;

    res = spiffs_obj_lu_find_free_obj_id(fs, &obj_id, (const u8_t*)path);
    SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
    res = spiffs_object_create(fs, obj_id, (const u8_t*)path, SPIFFS_TYPE_FILE, 0);
    SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
    SPIFFS_UNLOCK(fs);
    return 0;
#endif // SPIFFS_READ_ONLY
}
struct spiffs_dirent *SPIFFS_readdir(spiffs_DIR *d, struct spiffs_dirent *e) {
  if (!SPIFFS_CHECK_MOUNT(d->fs)) {
    d->fs->errno = SPIFFS_ERR_NOT_MOUNTED;
    return 0;
  }
  SPIFFS_LOCK(fs);

  spiffs_block_ix bix;
  int entry;
  s32_t res;
  struct spiffs_dirent *ret = 0;

  res = spiffs_obj_lu_find_entry_visitor(d->fs,
      d->block,
      d->entry,
      SPIFFS_VIS_NO_WRAP,
      0,
      spiffs_read_dir_v,
      0,
      e,
      &bix,
      &entry);
  if (res == SPIFFS_OK) {
    d->block = bix;
    d->entry = entry + 1;
    ret = e;
  } else {
    d->fs->errno = res;
  }
  SPIFFS_UNLOCK(fs);
  return ret;
}
s32_t ICACHE_FLASH_ATTR SPIFFS_mount(spiffs *fs, spiffs_config *config, u8_t *work,
    u8_t *fd_space, u32_t fd_space_size,
    void *cache, u32_t cache_size,
    spiffs_check_callback check_cb_f) {
  SPIFFS_LOCK(fs);
  memset(fs, 0, sizeof(spiffs));
  memcpy(&fs->cfg, config, sizeof(spiffs_config));
  fs->block_count = SPIFFS_CFG_PHYS_SZ(fs) / SPIFFS_CFG_LOG_BLOCK_SZ(fs);
  fs->work = &work[0];
  fs->lu_work = &work[SPIFFS_CFG_LOG_PAGE_SZ(fs)];
  memset(fd_space, 0, fd_space_size);
  // align fd_space pointer to pointer size byte boundary, below is safe
  u8_t ptr_size = sizeof(void*);
// #pragma GCC diagnostic push
// #pragma GCC diagnostic ignored "-Wpointer-to-int-cast"
  u8_t addr_lsb = (u8_t)(((u32_t)fd_space) & (ptr_size-1));
// #pragma GCC diagnostic pop
  if (addr_lsb) {
    fd_space += (ptr_size-addr_lsb);
    fd_space_size -= (ptr_size-addr_lsb);
  }
  fs->fd_space = fd_space;
  fs->fd_count = (fd_space_size/sizeof(spiffs_fd));

  // align cache pointer to 4 byte boundary, below is safe
// #pragma GCC diagnostic push
// #pragma GCC diagnostic ignored "-Wpointer-to-int-cast"
  addr_lsb = (u8_t)(((u32_t)cache) & (ptr_size-1));
// #pragma GCC diagnostic pop
  if (addr_lsb) {
    cache = (u8_t *)cache + (ptr_size-addr_lsb);
    cache_size -= (ptr_size-addr_lsb);
  }
  if (cache_size & (ptr_size-1)) {
    cache_size -= (cache_size & (ptr_size-1));
  }
#if SPIFFS_CACHE
  fs->cache = cache;
  fs->cache_size = cache_size;
  spiffs_cache_init(fs);
#endif

  s32_t res = spiffs_obj_lu_scan(fs);
  SPIFFS_API_CHECK_RES_UNLOCK(fs, res);

  SPIFFS_DBG("page index byte len:         %i\n", SPIFFS_CFG_LOG_PAGE_SZ(fs));
  SPIFFS_DBG("object lookup pages:         %i\n", SPIFFS_OBJ_LOOKUP_PAGES(fs));
  SPIFFS_DBG("page pages per block:        %i\n", SPIFFS_PAGES_PER_BLOCK(fs));
  SPIFFS_DBG("page header length:          %i\n", sizeof(spiffs_page_header));
  SPIFFS_DBG("object header index entries: %i\n", SPIFFS_OBJ_HDR_IX_LEN(fs));
  SPIFFS_DBG("object index entries:        %i\n", SPIFFS_OBJ_IX_LEN(fs));
  SPIFFS_DBG("available file descriptors:  %i\n", fs->fd_count);
  SPIFFS_DBG("free blocks:                 %i\n", fs->free_blocks);

  fs->check_cb_f = check_cb_f;

  SPIFFS_UNLOCK(fs);

  return 0;
}
Beispiel #11
0
s32_t SPIFFS_fremove(spiffs *fs, spiffs_file fh) {
#if SPIFFS_READ_ONLY
  (void)fs; (void)fh;
  return SPIFFS_ERR_RO_NOT_IMPL;
#else
  SPIFFS_API_CHECK_CFG(fs);
  SPIFFS_API_CHECK_MOUNT(fs);
  SPIFFS_LOCK(fs);

  spiffs_fd *fd;
  s32_t res;
  fh = SPIFFS_FH_UNOFFS(fs, fh);
  res = spiffs_fd_get(fs, fh, &fd);
  SPIFFS_API_CHECK_RES_UNLOCK(fs, res);

  if ((fd->flags & SPIFFS_O_WRONLY) == 0) {
    res = SPIFFS_ERR_NOT_WRITABLE;
    SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
  }

#if SPIFFS_CACHE_WR
  spiffs_cache_fd_release(fs, fd->cache_page);
#endif

  res = spiffs_object_truncate(fd, 0, 1);

  SPIFFS_API_CHECK_RES_UNLOCK(fs, res);

  SPIFFS_UNLOCK(fs);

  return 0;
#endif // SPIFFS_READ_ONLY
}
Beispiel #12
0
spiffs_file SPIFFS_open(spiffs *fs, char *path, spiffs_flags flags, spiffs_mode mode) {
  (void)mode;
  SPIFFS_API_CHECK_CFG(fs);
  SPIFFS_API_CHECK_MOUNT(fs);
  SPIFFS_LOCK(fs);

  spiffs_fd *fd;
  spiffs_page_ix pix;

  s32_t res = spiffs_fd_find_new(fs, &fd);
  SPIFFS_API_CHECK_RES_UNLOCK(fs, res);

  res = spiffs_object_find_object_index_header_by_name(fs, (u8_t*)path, &pix);
  if ((flags & SPIFFS_CREAT) == 0) {
    if (res < SPIFFS_OK) {
      spiffs_fd_return(fs, fd->file_nbr);
    }
    SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
  }

  if ((flags & SPIFFS_CREAT) && res == SPIFFS_ERR_NOT_FOUND) {
    spiffs_obj_id obj_id;
    // no need to enter conflicting name here, already looked for it above
    res = spiffs_obj_lu_find_free_obj_id(fs, &obj_id, 0);
    if (res < SPIFFS_OK) {
      spiffs_fd_return(fs, fd->file_nbr);
    }
    SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
    res = spiffs_object_create(fs, obj_id, (u8_t*)path, SPIFFS_TYPE_FILE, &pix);
    if (res < SPIFFS_OK) {
      spiffs_fd_return(fs, fd->file_nbr);
    }
    SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
    flags &= ~SPIFFS_TRUNC;
  } else {
    if (res < SPIFFS_OK) {
      spiffs_fd_return(fs, fd->file_nbr);
    }
    SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
  }
  res = spiffs_object_open_by_page(fs, pix, fd, flags, mode);
  if (res < SPIFFS_OK) {
    spiffs_fd_return(fs, fd->file_nbr);
  }
  SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
  if (flags & SPIFFS_TRUNC) {
    res = spiffs_object_truncate(fd, 0, 0);
    if (res < SPIFFS_OK) {
      spiffs_fd_return(fs, fd->file_nbr);
    }
    SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
  }

  fd->fdoffset = 0;

  SPIFFS_UNLOCK(fs);

  return fd->file_nbr;
}
Beispiel #13
0
s32_t SPIFFS_read(spiffs *fs, spiffs_file fh, void *buf, s32_t len) {
  SPIFFS_API_CHECK_CFG(fs);
  SPIFFS_API_CHECK_MOUNT(fs);
  SPIFFS_LOCK(fs);

  spiffs_fd *fd;
  s32_t res;

  res = spiffs_fd_get(fs, fh, &fd);
  SPIFFS_API_CHECK_RES_UNLOCK(fs, res);

  if ((fd->flags & SPIFFS_RDONLY) == 0) {
    res = SPIFFS_ERR_NOT_READABLE;
    SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
  }

#if SPIFFS_CACHE_WR
  spiffs_fflush_cache(fs, fh);
#endif

  if (fd->fdoffset + len >= fd->size) {
    // reading beyond file size
    s32_t avail = fd->size - fd->fdoffset;
    if (avail <= 0) {
      SPIFFS_API_CHECK_RES_UNLOCK(fs, SPIFFS_ERR_END_OF_OBJECT);
    }
    res = spiffs_object_read(fd, fd->fdoffset, avail, (u8_t*)buf);
    if (res == SPIFFS_ERR_END_OF_OBJECT) {
      fd->fdoffset += avail;
      SPIFFS_UNLOCK(fs);
      return avail;
    } else {
      SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
      len = avail;
    }
  } else {
    // reading within file size
    res = spiffs_object_read(fd, fd->fdoffset, len, (u8_t*)buf);
    SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
  }
  fd->fdoffset += len;

  SPIFFS_UNLOCK(fs);

  return len;
}
spiffs_file ICACHE_FLASH_ATTR SPIFFS_open(spiffs *fs, const char *path, spiffs_flags flags, spiffs_mode mode) {
  SPIFFS_API_CHECK_MOUNT(fs);
  SPIFFS_LOCK(fs);

  spiffs_fd *fd;
  spiffs_page_ix pix;

  s32_t res = spiffs_fd_find_new(fs, &fd);
  SPIFFS_API_CHECK_RES_UNLOCK(fs, res);

  res = spiffs_object_find_object_index_header_by_name(fs, (u8_t*)path, &pix);
  if ((flags & SPIFFS_CREAT) == 0) {
    if (res < SPIFFS_OK) {
      spiffs_fd_return(fs, fd->file_nbr);
    }
    SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
  }

  if ((flags & SPIFFS_CREAT) && res == SPIFFS_ERR_NOT_FOUND) {
    spiffs_obj_id obj_id;
    res = spiffs_obj_lu_find_free_obj_id(fs, &obj_id);
    if (res < SPIFFS_OK) {
      spiffs_fd_return(fs, fd->file_nbr);
    }
    SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
    res = spiffs_object_create(fs, obj_id, (u8_t*)path, SPIFFS_TYPE_FILE, &pix);
    if (res < SPIFFS_OK) {
      spiffs_fd_return(fs, fd->file_nbr);
    }
    SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
    flags &= ~SPIFFS_TRUNC;
  } else {
    if (res < SPIFFS_OK) {
      spiffs_fd_return(fs, fd->file_nbr);
    }
    SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
  }
  res = spiffs_object_open_by_page(fs, pix, fd, flags, flags);
  if (res < SPIFFS_OK) {
    spiffs_fd_return(fs, fd->file_nbr);
  }
  SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
  if (flags & SPIFFS_TRUNC) {
    res = spiffs_object_truncate(fd, 0, 0);
    if (res < SPIFFS_OK) {
      spiffs_fd_return(fs, fd->file_nbr);
    }
    SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
  }

  fd->fdoffset = 0;

  SPIFFS_UNLOCK(fs);

  return fd->file_nbr;
}
s32_t SPIFFS_fflush(spiffs *fs, spiffs_file fh) {
  SPIFFS_API_CHECK_MOUNT(fs);
  s32_t res = SPIFFS_OK;
#if SPIFFS_CACHE_WR
  SPIFFS_LOCK(fs);
  res = spiffs_fflush_cache(fs, fh);
  SPIFFS_API_CHECK_RES_UNLOCK(fs,res);
  SPIFFS_UNLOCK(fs);
#endif

  return res;
}
s32_t ICACHE_FLASH_ATTR SPIFFS_creat(spiffs *fs, const char *path, spiffs_mode mode) {
  SPIFFS_API_CHECK_MOUNT(fs);
  SPIFFS_LOCK(fs);
  spiffs_obj_id obj_id;
  s32_t res;

  res = spiffs_obj_lu_find_free_obj_id(fs, &obj_id);
  SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
  res = spiffs_object_create(fs, obj_id, (u8_t*)path, SPIFFS_TYPE_FILE, 0);
  SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
  SPIFFS_UNLOCK(fs);
  return 0;
}
void SPIFFS_close(spiffs *fs, spiffs_file fh) {
  if (!SPIFFS_CHECK_MOUNT(fs)) {
    fs->errno = SPIFFS_ERR_NOT_MOUNTED;
    return;
  }
  SPIFFS_LOCK(fs);

#if SPIFFS_CACHE
  spiffs_fflush_cache(fs, fh);
#endif
  spiffs_fd_return(fs, fh);

  SPIFFS_UNLOCK(fs);
}
Beispiel #18
0
s32_t spiffs_rename(spiffs *fs, const char *old_path, const char *new_path) {
#if SPIFFS_READ_ONLY
    (void)fs;
    (void)old_path;
    (void)new_path;
    return SPIFFS_ERR_RO_NOT_IMPL;
#else
    SPIFFS_API_CHECK_CFG(fs);
    SPIFFS_API_CHECK_MOUNT(fs);
    if (strlen(new_path) > SPIFFS_OBJ_NAME_LEN - 1 ||
            strlen(old_path) > SPIFFS_OBJ_NAME_LEN - 1) {
        SPIFFS_API_CHECK_RES(fs, SPIFFS_ERR_NAME_TOO_LONG);
    }
    SPIFFS_LOCK(fs);

    spiffs_page_ix pix_old, pix_dummy;
    spiffs_fd *fd;

    s32_t res = spiffs_object_find_object_index_header_by_name(fs, (const u8_t*)old_path, &pix_old);
    SPIFFS_API_CHECK_RES_UNLOCK(fs, res);

    res = spiffs_object_find_object_index_header_by_name(fs, (const u8_t*)new_path, &pix_dummy);
    if (res == SPIFFS_ERR_NOT_FOUND) {
        res = SPIFFS_OK;
    } else if (res == SPIFFS_OK) {
        res = SPIFFS_ERR_CONFLICTING_NAME;
    }
    SPIFFS_API_CHECK_RES_UNLOCK(fs, res);

    res = spiffs_fd_find_new(fs, &fd);
    SPIFFS_API_CHECK_RES_UNLOCK(fs, res);

    res = spiffs_object_open_by_page(fs, pix_old, fd, 0, 0);
    if (res != SPIFFS_OK) {
        spiffs_fd_return(fs, fd->file_nbr);
    }
    SPIFFS_API_CHECK_RES_UNLOCK(fs, res);

    res = spiffs_object_update_index_hdr(fs, fd, fd->obj_id, fd->objix_hdr_pix, 0, (const u8_t*)new_path,
                                         0, &pix_dummy);

    spiffs_fd_return(fs, fd->file_nbr);

    SPIFFS_API_CHECK_RES_UNLOCK(fs, res);

    SPIFFS_UNLOCK(fs);

    return res;
#endif // SPIFFS_READ_ONLY
}
Beispiel #19
0
s32_t SPIFFS_creat(spiffs *fs, char *path, spiffs_mode mode) {
    (void)mode;
    SPIFFS_API_CHECK_CFG(fs);
    SPIFFS_API_CHECK_MOUNT(fs);
    SPIFFS_LOCK(fs);
    spiffs_obj_id obj_id;
    s32_t res;

    res = spiffs_obj_lu_find_free_obj_id(fs, &obj_id, (u8_t *)path);
    SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
    res = spiffs_object_create(fs, obj_id, (u8_t *)path, SPIFFS_TYPE_FILE, 0);
    SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
    SPIFFS_UNLOCK(fs);
    return 0;
}
Beispiel #20
0
s32_t SPIFFS_fflush(spiffs *fs, spiffs_file fh) {
  (void)fh;
  SPIFFS_API_CHECK_CFG(fs);
  SPIFFS_API_CHECK_MOUNT(fs);
  s32_t res = SPIFFS_OK;
#if !SPIFFS_READ_ONLY && SPIFFS_CACHE_WR
  SPIFFS_LOCK(fs);
  fh = SPIFFS_FH_UNOFFS(fs, fh);
  res = spiffs_fflush_cache(fs, fh);
  SPIFFS_API_CHECK_RES_UNLOCK(fs,res);
  SPIFFS_UNLOCK(fs);
#endif

  return res;
}
s32_t SPIFFS_check(spiffs *fs) {
  s32_t res;
  SPIFFS_API_CHECK_MOUNT(fs);
  SPIFFS_LOCK(fs);

  res = spiffs_lookup_consistency_check(fs, 0);

  res = spiffs_object_index_consistency_check(fs);

  res = spiffs_page_consistency_check(fs);

  res = spiffs_obj_lu_scan(fs);

  SPIFFS_UNLOCK(fs);
  return res;
}
s32_t SPIFFS_stat(spiffs *fs, const char *path, spiffs_stat *s) {
  SPIFFS_API_CHECK_MOUNT(fs);
  SPIFFS_LOCK(fs);

  s32_t res;
  spiffs_page_ix pix;

  res = spiffs_object_find_object_index_header_by_name(fs, (u8_t*)path, &pix);
  SPIFFS_API_CHECK_RES_UNLOCK(fs, res);

  res = spiffs_stat_pix(fs, pix, 0, s);

  SPIFFS_UNLOCK(fs);

  return res;
}
s32_t ICACHE_FLASH_ATTR SPIFFS_check(spiffs *fs) {
  s32_t res;
  SPIFFS_API_CHECK_MOUNT(fs);
  SPIFFS_LOCK(fs);

  res = spiffs_lookup_consistency_check(fs, 0);

  res = spiffs_object_index_consistency_check(fs);
// NODE_ERR("before spiffs_object_index_consistency_check\n");
  res = spiffs_page_consistency_check(fs);
// NODE_ERR("spiffs_page_consistency_check\n");
  res = spiffs_obj_lu_scan(fs);
// NODE_ERR("spiffs_obj_lu_scan\n");
  SPIFFS_UNLOCK(fs);
  return res;
}
void SPIFFS_unmount(spiffs *fs) {
  if (!SPIFFS_CHECK_MOUNT(fs)) return;
  SPIFFS_LOCK(fs);
  u32_t i;
  spiffs_fd *fds = (spiffs_fd *)fs->fd_space;
  for (i = 0; i < fs->fd_count; i++) {
    spiffs_fd *cur_fd = &fds[i];
    if (cur_fd->file_nbr != 0) {
#if SPIFFS_CACHE
      (void)spiffs_fflush_cache(fs, cur_fd->file_nbr);
#endif
      spiffs_fd_return(fs, cur_fd->file_nbr);
    }
  }
  fs->block_count = 0;
  SPIFFS_UNLOCK(fs);
}
Beispiel #25
0
s32_t SPIFFS_gc_quick(spiffs *fs, u16_t max_free_pages) {
#if SPIFFS_READ_ONLY
  (void)fs; (void)max_free_pages;
  return SPIFFS_ERR_RO_NOT_IMPL;
#else
  s32_t res;
  SPIFFS_API_CHECK_CFG(fs);
  SPIFFS_API_CHECK_MOUNT(fs);
  SPIFFS_LOCK(fs);

  res = spiffs_gc_quick(fs, max_free_pages);

  SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
  SPIFFS_UNLOCK(fs);
  return 0;
#endif // SPIFFS_READ_ONLY
}
Beispiel #26
0
s32_t SPIFFS_gc(spiffs *fs, u32_t size) {
#if SPIFFS_READ_ONLY
  (void)fs; (void)size;
  return SPIFFS_ERR_RO_NOT_IMPL;
#else
  s32_t res;
  SPIFFS_API_CHECK_CFG(fs);
  SPIFFS_API_CHECK_MOUNT(fs);
  SPIFFS_LOCK(fs);

  res = spiffs_gc_check(fs, size);

  SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
  SPIFFS_UNLOCK(fs);
  return 0;
#endif // SPIFFS_READ_ONLY
}
Beispiel #27
0
spiffs_file SPIFFS_open_by_page(spiffs *fs, spiffs_page_ix page_ix, spiffs_flags flags, spiffs_mode mode) {
  SPIFFS_API_CHECK_CFG(fs);
  SPIFFS_API_CHECK_MOUNT(fs);
  SPIFFS_LOCK(fs);

  spiffs_fd *fd;

  s32_t res = spiffs_fd_find_new(fs, &fd);
  SPIFFS_API_CHECK_RES_UNLOCK(fs, res);

  if (SPIFFS_IS_LOOKUP_PAGE(fs, page_ix)) {
    res = SPIFFS_ERR_NOT_A_FILE;
    spiffs_fd_return(fs, fd->file_nbr);
    SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
  }

  res = spiffs_object_open_by_page(fs, page_ix, fd, flags, mode);
  if (res == SPIFFS_ERR_IS_FREE ||
      res == SPIFFS_ERR_DELETED ||
      res == SPIFFS_ERR_NOT_FINALIZED ||
      res == SPIFFS_ERR_NOT_INDEX ||
      res == SPIFFS_ERR_INDEX_SPAN_MISMATCH) {
    res = SPIFFS_ERR_NOT_A_FILE;
  }
  if (res < SPIFFS_OK) {
    spiffs_fd_return(fs, fd->file_nbr);
  }
  SPIFFS_API_CHECK_RES_UNLOCK(fs, res);

#if !SPIFFS_READ_ONLY
  if (flags & SPIFFS_O_TRUNC) {
    res = spiffs_object_truncate(fd, 0, 0);
    if (res < SPIFFS_OK) {
      spiffs_fd_return(fs, fd->file_nbr);
    }
    SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
  }
#endif // !SPIFFS_READ_ONLY

  fd->fdoffset = 0;

  SPIFFS_UNLOCK(fs);

  return SPIFFS_FH_OFFS(fs, fd->file_nbr);
}
Beispiel #28
0
s32_t SPIFFS_lseek(spiffs *fs, spiffs_file fh, s32_t offs, int whence) {
  SPIFFS_API_CHECK_CFG(fs);
  SPIFFS_API_CHECK_MOUNT(fs);
  SPIFFS_LOCK(fs);

  spiffs_fd *fd;
  s32_t res;
  fh = SPIFFS_FH_UNOFFS(fs, fh);
  res = spiffs_fd_get(fs, fh, &fd);
  SPIFFS_API_CHECK_RES(fs, res);

#if SPIFFS_CACHE_WR
  spiffs_fflush_cache(fs, fh);
#endif

  switch (whence) {
  case SPIFFS_SEEK_CUR:
    offs = fd->fdoffset+offs;
    break;
  case SPIFFS_SEEK_END:
    offs = (fd->size == SPIFFS_UNDEFINED_LEN ? 0 : fd->size) + offs;
    break;
  }

  if ((offs > (s32_t)fd->size) && (SPIFFS_UNDEFINED_LEN != fd->size)) {
    res = SPIFFS_ERR_END_OF_OBJECT;
  }
  SPIFFS_API_CHECK_RES_UNLOCK(fs, res);

  spiffs_span_ix data_spix = offs / SPIFFS_DATA_PAGE_SIZE(fs);
  spiffs_span_ix objix_spix = SPIFFS_OBJ_IX_ENTRY_SPAN_IX(fs, data_spix);
  if (fd->cursor_objix_spix != objix_spix) {
    spiffs_page_ix pix;
    res = spiffs_obj_lu_find_id_and_span(
        fs, fd->obj_id | SPIFFS_OBJ_ID_IX_FLAG, objix_spix, 0, &pix);
    SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
    fd->cursor_objix_spix = objix_spix;
    fd->cursor_objix_pix = pix;
  }
  fd->fdoffset = offs;

  SPIFFS_UNLOCK(fs);

  return offs;
}
s32_t SPIFFS_tell(spiffs *fs, spiffs_file fh) {
  SPIFFS_API_CHECK_MOUNT(fs);
  SPIFFS_LOCK(fs);

  spiffs_fd *fd;
  s32_t res;
  res = spiffs_fd_get(fs, fh, &fd);
  SPIFFS_API_CHECK_RES(fs, res);

#if SPIFFS_CACHE_WR
  spiffs_fflush_cache(fs, fh);
#endif

  res = fd->fdoffset;

  SPIFFS_UNLOCK(fs);
  return res;
}
s32_t ICACHE_FLASH_ATTR SPIFFS_eof(spiffs *fs, spiffs_file fh) {
  SPIFFS_API_CHECK_MOUNT(fs);
  SPIFFS_LOCK(fs);

  spiffs_fd *fd;
  s32_t res;
  res = spiffs_fd_get(fs, fh, &fd);
  SPIFFS_API_CHECK_RES(fs, res);

#if SPIFFS_CACHE_WR
  spiffs_fflush_cache(fs, fh);
#endif

  res = (fd->fdoffset == fd->size);

  SPIFFS_UNLOCK(fs);
  return res;
}