Example #1
0
int fs_spiffs_unlink(const char *filename) {
  struct mount_info *m = &s_fsm;
  if (!m->valid) return set_errno(EBADF);
  int res = SPIFFS_remove(&m->fs, (char *) filename);
  if (res == SPIFFS_OK) fs_close_container(m);
  return set_spiffs_errno(m, res);
}
Example #2
0
int fs_spiffs_rename(const char *from, const char *to) {
  struct mount_info *m = &s_fsm;
  if (!m->valid) return set_errno(EBADF);
  int res = SPIFFS_rename(&m->fs, (char *) from, (char *) to);
  if (res == SPIFFS_OK) fs_close_container(m);
  return set_spiffs_errno(m, res);
}
_i32 fs_umount(struct mount_info *m) {
  LOG(LL_INFO, ("Unmounting %s.%d", m->cpfx, m->cidx));
  SPIFFS_unmount(&m->fs);
  free(m->cpfx);
  free(m->work);
  free(m->fds);
  fs_close_container(m);
  memset(m, 0, sizeof(*m));
  return 1;
}
Example #4
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;
}
static _i32 fs_switch_container(struct mount_info *m, _u32 mask_begin,
                                _u32 mask_len) {
  int r;
  int new_cidx = m->cidx ^ 1;
  _i32 old_fh = m->fh, new_fh;
  _u8 *buf;
  _u32 offset, len, buf_size;
  LOG(LL_DEBUG, ("%s %d -> %d", m->cpfx, m->cidx, new_cidx));
  if (old_fh > 0 && m->rw) {
    /*
     * During the switch the destination container will be unusable.
     * If switching from a writeable container (likely in response to an erase),
     * close the old container first to make it safe and reopen for reading.
     */
    fs_close_container(m);
    old_fh = -1;
  }
  if (old_fh < 0) {
    _u8 fname[MAX_FS_CONTAINER_FNAME_LEN];
    fs_container_fname(m->cpfx, m->cidx, fname);
    r = sl_FsOpen(fname, FS_MODE_OPEN_READ, NULL, &old_fh);
    DBG(("fopen %s %d", m->cpfx, r));
    if (r < 0) {
      r = SPIFFS_ERR_NOT_READABLE;
      goto out_close_old;
    }
  }
  miot_wdt_feed();
  new_fh = fs_create_container(m->cpfx, new_cidx, m->fs.cfg.phys_size);
  if (new_fh < 0) {
    r = new_fh;
    goto out_close_old;
  }

  buf_size = 1024;
  buf = get_buf(&buf_size);
  if (buf == NULL) {
    r = SPIFFS_ERR_INTERNAL;
    goto out_close_new;
  }

  for (offset = 0; offset < m->fs.cfg.phys_size;) {
    len = buf_size;
    if (offset == mask_begin) {
      offset = mask_begin + mask_len;
    } else if (offset + len > mask_begin && offset < mask_begin + mask_len) {
      len = mask_begin - offset;
    }
    if (offset + len > m->fs.cfg.phys_size) {
      len = m->fs.cfg.phys_size - offset;
    }
    DBG(("copy %d @ %d", (int) len, (int) offset));
    if (len > 0) {
      r = sl_FsRead(old_fh, offset, buf, len);
      if (r != len) {
        r = SPIFFS_ERR_NOT_READABLE;
        goto out_free;
      }
      r = sl_FsWrite(new_fh, offset, buf, len);
      if (r != len) {
        r = SPIFFS_ERR_NOT_WRITABLE;
        goto out_free;
      }
      offset += len;
    }
  }

  m->seq--;
  m->cidx = new_cidx;
  m->fh = new_fh;
  new_fh = -1;
  m->rw = 1;

  r = fs_write_mount_meta(m);

  m->last_write = mg_time();

out_free:
  free(buf);
out_close_new:
  if (new_fh > 0) sl_FsClose(new_fh, NULL, NULL, 0);
out_close_old:
  sl_FsClose(old_fh, NULL, NULL, 0);
  LOG((r == 0 ? LL_DEBUG : LL_ERROR), ("%d", r));
  return r;
}