예제 #1
0
// Checks if there are any cached writes for the object id associated with
// given filehandle. If so, these writes are flushed.
static s32_t spiffs_fflush_cache(spiffs *fs, spiffs_file fh) {
  s32_t res = SPIFFS_OK;
#if SPIFFS_CACHE_WR

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

  if ((fd->flags & SPIFFS_DIRECT) == 0) {
    if (fd->cache_page == 0) {
      // see if object id is associated with cache already
      fd->cache_page = spiffs_cache_page_get_by_fd(fs, fd);
    }
    if (fd->cache_page) {
      SPIFFS_CACHE_DBG("CACHE_WR_DUMP: dumping cache page %d for fd %d:%04x, flush, offs:%d size:%d\n",
          fd->cache_page->ix, fd->file_nbr,  fd->obj_id, fd->cache_page->offset, fd->cache_page->size);
      res = spiffs_hydro_write(fs, fd,
          spiffs_get_cache_page(fs, spiffs_get_cache(fs), fd->cache_page->ix),
          fd->cache_page->offset, fd->cache_page->size);
      if (res < SPIFFS_OK) {
        fs->errno = res;
      }
      spiffs_cache_fd_release(fs, fd->cache_page);
    }
  }
#endif

  return res;
}
예제 #2
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;
}
예제 #3
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
}
예제 #4
0
s32_t SPIFFS_write(spiffs *fs, spiffs_file fh, void *buf, u32_t len) {
  SPIFFS_API_CHECK_MOUNT(fs);
  SPIFFS_LOCK(fs);

  spiffs_fd *fd;
  s32_t res;
  u32_t offset;

  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);
  }

  offset = fd->fdoffset;

#if SPIFFS_CACHE_WR
  if (fd->cache_page == 0) {
    // see if object id is associated with cache already
    fd->cache_page = spiffs_cache_page_get_by_fd(fs, fd);
  }
#endif
  if (fd->flags & SPIFFS_APPEND) {
    if (fd->size == SPIFFS_UNDEFINED_LEN) {
      offset = 0;
    } else {
      offset = fd->size;
    }
#if SPIFFS_CACHE_WR
    if (fd->cache_page) {
      offset = MAX(offset, fd->cache_page->offset + fd->cache_page->size);
    }
#endif
  }

  SPIFFS_DBG("SPIFFS_write %d %04x offs:%d len %d\n", fh, fd->obj_id, offset, len);

#if SPIFFS_CACHE_WR
  if ((fd->flags & SPIFFS_DIRECT) == 0) {
    if (len < (s32_t)SPIFFS_CFG_LOG_PAGE_SZ(fs)) {
      // small write, try to cache it
      u8_t alloc_cpage = 1;
      if (fd->cache_page) {
        // have a cached page for this fd already, check cache page boundaries
        if (offset < fd->cache_page->offset || // writing before cache
            offset > fd->cache_page->offset + fd->cache_page->size || // writing after cache
            offset + len > fd->cache_page->offset + SPIFFS_CFG_LOG_PAGE_SZ(fs)) // writing beyond cache page
        {
          // boundary violation, write back cache first and allocate new
          SPIFFS_CACHE_DBG("CACHE_WR_DUMP: dumping cache page %d for fd %d:&04x, boundary viol, offs:%d size:%d\n",
              fd->cache_page->ix, fd->file_nbr, fd->obj_id, fd->cache_page->offset, fd->cache_page->size);
          res = spiffs_hydro_write(fs, fd,
              spiffs_get_cache_page(fs, spiffs_get_cache(fs), fd->cache_page->ix),
              fd->cache_page->offset, fd->cache_page->size);
          spiffs_cache_fd_release(fs, fd->cache_page);
        } else {
          // writing within cache
          alloc_cpage = 0;
        }
      }

      if (alloc_cpage) {
        fd->cache_page = spiffs_cache_page_allocate_by_fd(fs, fd);
        if (fd->cache_page) {
          fd->cache_page->offset = offset;
          fd->cache_page->size = 0;
          SPIFFS_CACHE_DBG("CACHE_WR_ALLO: allocating cache page %d for fd %d:%04x\n",
              fd->cache_page->ix, fd->file_nbr, fd->obj_id);
        }
      }

      if (fd->cache_page) {
        u32_t offset_in_cpage = offset - fd->cache_page->offset;
        SPIFFS_CACHE_DBG("CACHE_WR_WRITE: storing to cache page %d for fd %d:%04x, offs %d:%d len %d\n",
            fd->cache_page->ix, fd->file_nbr, fd->obj_id,
            offset, offset_in_cpage, len);
        spiffs_cache *cache = spiffs_get_cache(fs);
        u8_t *cpage_data = spiffs_get_cache_page(fs, cache, fd->cache_page->ix);
        c_memcpy(&cpage_data[offset_in_cpage], buf, len);
        fd->cache_page->size = MAX(fd->cache_page->size, offset_in_cpage + len);
        fd->fdoffset += len;
        SPIFFS_UNLOCK(fs);
        return len;
      } else {
        res = spiffs_hydro_write(fs, fd, buf, offset, len);
        SPIFFS_API_CHECK_RES(fs, res);
        fd->fdoffset += len;
        SPIFFS_UNLOCK(fs);
        return res;
      }
    } else {
      // big write, no need to cache it - but first check if there is a cached write already
      if (fd->cache_page) {
        // write back cache first
        SPIFFS_CACHE_DBG("CACHE_WR_DUMP: dumping cache page %d for fd %d:%04x, big write, offs:%d size:%d\n",
            fd->cache_page->ix, fd->file_nbr, fd->obj_id, fd->cache_page->offset, fd->cache_page->size);
        res = spiffs_hydro_write(fs, fd,
            spiffs_get_cache_page(fs, spiffs_get_cache(fs), fd->cache_page->ix),
            fd->cache_page->offset, fd->cache_page->size);
        spiffs_cache_fd_release(fs, fd->cache_page);
        res = spiffs_hydro_write(fs, fd, buf, offset, len);
        SPIFFS_API_CHECK_RES(fs, res);
      }
    }
  }
#endif

  res = spiffs_hydro_write(fs, fd, buf, offset, len);
  SPIFFS_API_CHECK_RES(fs, res);
  fd->fdoffset += len;

  SPIFFS_UNLOCK(fs);

  return res;
}