2019-02-19 07:18:40 -05:00
|
|
|
// Copyright 2015-2019 Espressif Systems (Shanghai) PTE LTD
|
2016-10-25 09:02:39 -04:00
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <sys/errno.h>
|
2017-10-23 06:03:23 -04:00
|
|
|
#include <sys/fcntl.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <sys/unistd.h>
|
2018-05-07 03:01:56 -04:00
|
|
|
#include <sys/lock.h>
|
2018-05-03 04:41:10 -04:00
|
|
|
#include <sys/param.h>
|
2017-10-23 06:03:23 -04:00
|
|
|
#include <dirent.h>
|
2018-05-03 04:41:10 -04:00
|
|
|
#include "freertos/FreeRTOS.h"
|
|
|
|
#include "freertos/semphr.h"
|
2016-10-25 09:02:39 -04:00
|
|
|
#include "esp_vfs.h"
|
2018-05-29 05:01:25 -04:00
|
|
|
#include "sdkconfig.h"
|
|
|
|
|
|
|
|
#ifdef CONFIG_SUPPRESS_SELECT_DEBUG_OUTPUT
|
|
|
|
#define LOG_LOCAL_LEVEL ESP_LOG_NONE
|
|
|
|
#endif //CONFIG_SUPPRESS_SELECT_DEBUG_OUTPUT
|
2016-10-25 09:02:39 -04:00
|
|
|
#include "esp_log.h"
|
|
|
|
|
2018-05-29 05:01:25 -04:00
|
|
|
static const char *TAG = "vfs";
|
|
|
|
|
2018-05-07 03:01:56 -04:00
|
|
|
#define VFS_MAX_COUNT 8 /* max number of VFS entries (registered filesystems) */
|
|
|
|
#define LEN_PATH_PREFIX_IGNORED SIZE_MAX /* special length value for VFS which is never recognised by open() */
|
|
|
|
#define FD_TABLE_ENTRY_UNUSED (fd_table_t) { .permanent = false, .vfs_index = -1, .local_fd = -1 }
|
2018-04-19 23:51:41 -04:00
|
|
|
|
2018-05-07 03:01:56 -04:00
|
|
|
typedef uint8_t local_fd_t;
|
|
|
|
_Static_assert((1 << (sizeof(local_fd_t)*8)) >= MAX_FDS, "file descriptor type too small");
|
2018-04-19 23:51:41 -04:00
|
|
|
|
2018-05-07 03:01:56 -04:00
|
|
|
typedef int8_t vfs_index_t;
|
|
|
|
_Static_assert((1 << (sizeof(vfs_index_t)*8)) >= VFS_MAX_COUNT, "VFS index type too small");
|
|
|
|
_Static_assert(((vfs_index_t) -1) < 0, "vfs_index_t must be a signed type");
|
2016-10-25 09:02:39 -04:00
|
|
|
|
2018-05-07 03:01:56 -04:00
|
|
|
typedef struct {
|
|
|
|
bool permanent;
|
|
|
|
vfs_index_t vfs_index;
|
|
|
|
local_fd_t local_fd;
|
|
|
|
} fd_table_t;
|
2017-10-03 01:56:55 -04:00
|
|
|
|
2016-10-25 09:02:39 -04:00
|
|
|
typedef struct vfs_entry_ {
|
|
|
|
esp_vfs_t vfs; // contains pointers to VFS functions
|
|
|
|
char path_prefix[ESP_VFS_PATH_MAX]; // path prefix mapped to this VFS
|
|
|
|
size_t path_prefix_len; // micro-optimization to avoid doing extra strlen
|
|
|
|
void* ctx; // optional pointer which can be passed to VFS
|
|
|
|
int offset; // index of this structure in s_vfs array
|
|
|
|
} vfs_entry_t;
|
|
|
|
|
2018-05-03 04:41:10 -04:00
|
|
|
typedef struct {
|
|
|
|
bool isset; // none or at least one bit is set in the following 3 fd sets
|
|
|
|
fd_set readfds;
|
|
|
|
fd_set writefds;
|
|
|
|
fd_set errorfds;
|
|
|
|
} fds_triple_t;
|
|
|
|
|
2016-10-25 09:02:39 -04:00
|
|
|
static vfs_entry_t* s_vfs[VFS_MAX_COUNT] = { 0 };
|
|
|
|
static size_t s_vfs_count = 0;
|
|
|
|
|
2018-05-07 03:01:56 -04:00
|
|
|
static fd_table_t s_fd_table[MAX_FDS] = { [0 ... MAX_FDS-1] = FD_TABLE_ENTRY_UNUSED };
|
|
|
|
static _lock_t s_fd_table_lock;
|
|
|
|
|
|
|
|
static esp_err_t esp_vfs_register_common(const char* base_path, size_t len, const esp_vfs_t* vfs, void* ctx, int *vfs_index)
|
2016-10-25 09:02:39 -04:00
|
|
|
{
|
2017-10-03 01:56:55 -04:00
|
|
|
if (len != LEN_PATH_PREFIX_IGNORED) {
|
|
|
|
if ((len != 0 && len < 2) || (len > ESP_VFS_PATH_MAX)) {
|
|
|
|
return ESP_ERR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
if ((len > 0 && base_path[0] != '/') || base_path[len - 1] == '/') {
|
|
|
|
return ESP_ERR_INVALID_ARG;
|
|
|
|
}
|
2016-10-25 09:02:39 -04:00
|
|
|
}
|
|
|
|
vfs_entry_t *entry = (vfs_entry_t*) malloc(sizeof(vfs_entry_t));
|
|
|
|
if (entry == NULL) {
|
|
|
|
return ESP_ERR_NO_MEM;
|
|
|
|
}
|
2016-12-29 09:21:29 -05:00
|
|
|
size_t index;
|
|
|
|
for (index = 0; index < s_vfs_count; ++index) {
|
|
|
|
if (s_vfs[index] == NULL) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (index == s_vfs_count) {
|
|
|
|
if (s_vfs_count >= VFS_MAX_COUNT) {
|
|
|
|
free(entry);
|
|
|
|
return ESP_ERR_NO_MEM;
|
|
|
|
}
|
|
|
|
++s_vfs_count;
|
|
|
|
}
|
|
|
|
s_vfs[index] = entry;
|
2017-10-03 01:56:55 -04:00
|
|
|
if (len != LEN_PATH_PREFIX_IGNORED) {
|
|
|
|
strcpy(entry->path_prefix, base_path); // we have already verified argument length
|
|
|
|
} else {
|
|
|
|
bzero(entry->path_prefix, sizeof(entry->path_prefix));
|
|
|
|
}
|
2016-10-25 09:02:39 -04:00
|
|
|
memcpy(&entry->vfs, vfs, sizeof(esp_vfs_t));
|
|
|
|
entry->path_prefix_len = len;
|
|
|
|
entry->ctx = ctx;
|
2016-12-29 09:21:29 -05:00
|
|
|
entry->offset = index;
|
2017-10-03 01:56:55 -04:00
|
|
|
|
2018-05-07 03:01:56 -04:00
|
|
|
if (vfs_index) {
|
|
|
|
*vfs_index = index;
|
2017-10-03 01:56:55 -04:00
|
|
|
}
|
|
|
|
|
2016-10-25 09:02:39 -04:00
|
|
|
return ESP_OK;
|
|
|
|
}
|
|
|
|
|
2017-10-03 01:56:55 -04:00
|
|
|
esp_err_t esp_vfs_register(const char* base_path, const esp_vfs_t* vfs, void* ctx)
|
|
|
|
{
|
2018-05-07 03:01:56 -04:00
|
|
|
return esp_vfs_register_common(base_path, strlen(base_path), vfs, ctx, NULL);
|
2017-10-03 01:56:55 -04:00
|
|
|
}
|
|
|
|
|
2018-05-07 03:01:56 -04:00
|
|
|
esp_err_t esp_vfs_register_fd_range(const esp_vfs_t *vfs, void *ctx, int min_fd, int max_fd)
|
2017-10-03 01:56:55 -04:00
|
|
|
{
|
2018-05-07 03:01:56 -04:00
|
|
|
if (min_fd < 0 || max_fd < 0 || min_fd > MAX_FDS || max_fd > MAX_FDS || min_fd > max_fd) {
|
2018-05-29 05:01:25 -04:00
|
|
|
ESP_LOGD(TAG, "Invalid arguments: esp_vfs_register_fd_range(0x%x, 0x%x, %d, %d)", (int) vfs, (int) ctx, min_fd, max_fd);
|
2018-05-07 03:01:56 -04:00
|
|
|
return ESP_ERR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
|
|
|
|
int index = -1;
|
|
|
|
esp_err_t ret = esp_vfs_register_common("", LEN_PATH_PREFIX_IGNORED, vfs, ctx, &index);
|
|
|
|
|
|
|
|
if (ret == ESP_OK) {
|
|
|
|
_lock_acquire(&s_fd_table_lock);
|
|
|
|
for (int i = min_fd; i < max_fd; ++i) {
|
|
|
|
if (s_fd_table[i].vfs_index != -1) {
|
|
|
|
free(s_vfs[i]);
|
|
|
|
s_vfs[i] = NULL;
|
|
|
|
for (int j = min_fd; j < i; ++j) {
|
|
|
|
if (s_fd_table[j].vfs_index == index) {
|
|
|
|
s_fd_table[j] = FD_TABLE_ENTRY_UNUSED;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_lock_release(&s_fd_table_lock);
|
2018-05-29 05:01:25 -04:00
|
|
|
ESP_LOGD(TAG, "esp_vfs_register_fd_range cannot set fd %d (used by other VFS)", i);
|
2018-05-07 03:01:56 -04:00
|
|
|
return ESP_ERR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
s_fd_table[i].permanent = true;
|
|
|
|
s_fd_table[i].vfs_index = index;
|
|
|
|
s_fd_table[i].local_fd = i;
|
|
|
|
}
|
|
|
|
_lock_release(&s_fd_table_lock);
|
|
|
|
}
|
|
|
|
|
2018-05-29 05:01:25 -04:00
|
|
|
ESP_LOGD(TAG, "esp_vfs_register_fd_range is successful for range <%d; %d) and VFS ID %d", min_fd, max_fd, index);
|
|
|
|
|
2018-05-07 03:01:56 -04:00
|
|
|
return ret;
|
2017-10-03 01:56:55 -04:00
|
|
|
}
|
|
|
|
|
2018-05-11 09:56:53 -04:00
|
|
|
esp_err_t esp_vfs_register_with_id(const esp_vfs_t *vfs, void *ctx, esp_vfs_id_t *vfs_id)
|
|
|
|
{
|
|
|
|
if (vfs_id == NULL) {
|
|
|
|
return ESP_ERR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
|
|
|
|
*vfs_id = -1;
|
|
|
|
return esp_vfs_register_common("", LEN_PATH_PREFIX_IGNORED, vfs, ctx, vfs_id);
|
|
|
|
}
|
|
|
|
|
2016-12-29 09:21:29 -05:00
|
|
|
esp_err_t esp_vfs_unregister(const char* base_path)
|
|
|
|
{
|
2018-12-06 06:30:05 -05:00
|
|
|
const size_t base_path_len = strlen(base_path);
|
2016-12-29 09:21:29 -05:00
|
|
|
for (size_t i = 0; i < s_vfs_count; ++i) {
|
|
|
|
vfs_entry_t* vfs = s_vfs[i];
|
2017-06-21 01:54:04 -04:00
|
|
|
if (vfs == NULL) {
|
|
|
|
continue;
|
|
|
|
}
|
2018-12-06 06:30:05 -05:00
|
|
|
if (base_path_len == vfs->path_prefix_len &&
|
|
|
|
memcmp(base_path, vfs->path_prefix, vfs->path_prefix_len) == 0) {
|
2016-12-29 09:21:29 -05:00
|
|
|
free(vfs);
|
|
|
|
s_vfs[i] = NULL;
|
2018-05-07 03:01:56 -04:00
|
|
|
|
|
|
|
_lock_acquire(&s_fd_table_lock);
|
|
|
|
// Delete all references from the FD lookup-table
|
|
|
|
for (int j = 0; j < MAX_FDS; ++j) {
|
|
|
|
if (s_fd_table[j].vfs_index == i) {
|
|
|
|
s_fd_table[j] = FD_TABLE_ENTRY_UNUSED;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_lock_release(&s_fd_table_lock);
|
|
|
|
|
2016-12-29 09:21:29 -05:00
|
|
|
return ESP_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ESP_ERR_INVALID_STATE;
|
|
|
|
}
|
|
|
|
|
2018-05-11 09:56:53 -04:00
|
|
|
esp_err_t esp_vfs_register_fd(esp_vfs_id_t vfs_id, int *fd)
|
|
|
|
{
|
|
|
|
if (vfs_id < 0 || vfs_id >= s_vfs_count || fd == NULL) {
|
2018-05-29 05:01:25 -04:00
|
|
|
ESP_LOGD(TAG, "Invalid arguments for esp_vfs_register_fd(%d, 0x%x)", vfs_id, (int) fd);
|
2018-05-11 09:56:53 -04:00
|
|
|
return ESP_ERR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
|
|
|
|
esp_err_t ret = ESP_ERR_NO_MEM;
|
|
|
|
_lock_acquire(&s_fd_table_lock);
|
|
|
|
for (int i = 0; i < MAX_FDS; ++i) {
|
|
|
|
if (s_fd_table[i].vfs_index == -1) {
|
|
|
|
s_fd_table[i].permanent = true;
|
|
|
|
s_fd_table[i].vfs_index = vfs_id;
|
|
|
|
s_fd_table[i].local_fd = i;
|
|
|
|
*fd = i;
|
|
|
|
ret = ESP_OK;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_lock_release(&s_fd_table_lock);
|
|
|
|
|
2018-05-29 05:01:25 -04:00
|
|
|
ESP_LOGD(TAG, "esp_vfs_register_fd(%d, 0x%x) finished with %s", vfs_id, (int) fd, esp_err_to_name(ret));
|
|
|
|
|
2018-05-11 09:56:53 -04:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
esp_err_t esp_vfs_unregister_fd(esp_vfs_id_t vfs_id, int fd)
|
|
|
|
{
|
|
|
|
esp_err_t ret = ESP_ERR_INVALID_ARG;
|
|
|
|
|
|
|
|
if (vfs_id < 0 || vfs_id >= s_vfs_count || fd < 0 || fd >= MAX_FDS) {
|
2018-05-29 05:01:25 -04:00
|
|
|
ESP_LOGD(TAG, "Invalid arguments for esp_vfs_unregister_fd(%d, %d)", vfs_id, fd);
|
2018-05-11 09:56:53 -04:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
_lock_acquire(&s_fd_table_lock);
|
|
|
|
fd_table_t *item = s_fd_table + fd;
|
|
|
|
if (item->permanent == true && item->vfs_index == vfs_id && item->local_fd == fd) {
|
|
|
|
*item = FD_TABLE_ENTRY_UNUSED;
|
|
|
|
ret = ESP_OK;
|
|
|
|
}
|
|
|
|
_lock_release(&s_fd_table_lock);
|
|
|
|
|
2018-05-29 05:01:25 -04:00
|
|
|
ESP_LOGD(TAG, "esp_vfs_unregister_fd(%d, %d) finished with %s", vfs_id, fd, esp_err_to_name(ret));
|
|
|
|
|
2018-05-11 09:56:53 -04:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2018-05-07 03:01:56 -04:00
|
|
|
static inline const vfs_entry_t *get_vfs_for_index(int index)
|
2016-10-25 09:02:39 -04:00
|
|
|
{
|
2018-05-07 03:01:56 -04:00
|
|
|
if (index < 0 || index >= s_vfs_count) {
|
2016-10-25 09:02:39 -04:00
|
|
|
return NULL;
|
2018-05-07 03:01:56 -04:00
|
|
|
} else {
|
|
|
|
return s_vfs[index];
|
2016-10-25 09:02:39 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-07 03:01:56 -04:00
|
|
|
static inline bool fd_valid(int fd)
|
2016-10-25 09:02:39 -04:00
|
|
|
{
|
2018-05-07 03:01:56 -04:00
|
|
|
return (fd < MAX_FDS) && (fd >= 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static const vfs_entry_t *get_vfs_for_fd(int fd)
|
|
|
|
{
|
|
|
|
const vfs_entry_t *vfs = NULL;
|
|
|
|
if (fd_valid(fd)) {
|
|
|
|
const int index = s_fd_table[fd].vfs_index; // single read -> no locking is required
|
|
|
|
vfs = get_vfs_for_index(index);
|
|
|
|
}
|
|
|
|
return vfs;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline int get_local_fd(const vfs_entry_t *vfs, int fd)
|
|
|
|
{
|
|
|
|
int local_fd = -1;
|
|
|
|
|
|
|
|
if (vfs && fd_valid(fd)) {
|
|
|
|
local_fd = s_fd_table[fd].local_fd; // single read -> no locking is required
|
2017-10-03 01:56:55 -04:00
|
|
|
}
|
2018-05-07 03:01:56 -04:00
|
|
|
|
|
|
|
return local_fd;
|
2016-10-25 09:02:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static const char* translate_path(const vfs_entry_t* vfs, const char* src_path)
|
|
|
|
{
|
|
|
|
assert(strncmp(src_path, vfs->path_prefix, vfs->path_prefix_len) == 0);
|
2017-06-20 13:21:14 -04:00
|
|
|
if (strlen(src_path) == vfs->path_prefix_len) {
|
|
|
|
// special case when src_path matches the path prefix exactly
|
|
|
|
return "/";
|
|
|
|
}
|
2016-10-25 09:02:39 -04:00
|
|
|
return src_path + vfs->path_prefix_len;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const vfs_entry_t* get_vfs_for_path(const char* path)
|
|
|
|
{
|
2017-06-21 02:17:14 -04:00
|
|
|
const vfs_entry_t* best_match = NULL;
|
|
|
|
ssize_t best_match_prefix_len = -1;
|
2016-10-25 09:02:39 -04:00
|
|
|
size_t len = strlen(path);
|
|
|
|
for (size_t i = 0; i < s_vfs_count; ++i) {
|
|
|
|
const vfs_entry_t* vfs = s_vfs[i];
|
2017-10-03 01:56:55 -04:00
|
|
|
if (!vfs || vfs->path_prefix_len == LEN_PATH_PREFIX_IGNORED) {
|
2017-05-04 07:57:11 -04:00
|
|
|
continue;
|
|
|
|
}
|
2017-06-20 13:21:14 -04:00
|
|
|
// match path prefix
|
|
|
|
if (len < vfs->path_prefix_len ||
|
|
|
|
memcmp(path, vfs->path_prefix, vfs->path_prefix_len) != 0) {
|
2016-10-25 09:02:39 -04:00
|
|
|
continue;
|
|
|
|
}
|
2017-06-23 08:07:43 -04:00
|
|
|
// this is the default VFS and we don't have a better match yet.
|
|
|
|
if (vfs->path_prefix_len == 0 && !best_match) {
|
|
|
|
best_match = vfs;
|
|
|
|
continue;
|
|
|
|
}
|
2017-06-20 13:21:14 -04:00
|
|
|
// if path is not equal to the prefix, expect to see a path separator
|
|
|
|
// i.e. don't match "/data" prefix for "/data1/foo.txt" path
|
|
|
|
if (len > vfs->path_prefix_len &&
|
|
|
|
path[vfs->path_prefix_len] != '/') {
|
2016-10-25 09:02:39 -04:00
|
|
|
continue;
|
|
|
|
}
|
2017-06-21 02:17:14 -04:00
|
|
|
// Out of all matching path prefixes, select the longest one;
|
|
|
|
// i.e. if "/dev" and "/dev/uart" both match, for "/dev/uart/1" path,
|
|
|
|
// choose "/dev/uart",
|
|
|
|
// This causes all s_vfs_count VFS entries to be scanned when opening
|
|
|
|
// a file by name. This can be optimized by introducing a table for
|
|
|
|
// FS search order, sorted so that longer prefixes are checked first.
|
|
|
|
if (best_match_prefix_len < (ssize_t) vfs->path_prefix_len) {
|
|
|
|
best_match_prefix_len = (ssize_t) vfs->path_prefix_len;
|
|
|
|
best_match = vfs;
|
2016-10-25 09:02:39 -04:00
|
|
|
}
|
|
|
|
}
|
2017-06-21 02:17:14 -04:00
|
|
|
return best_match;
|
2016-10-25 09:02:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Using huge multi-line macros is never nice, but in this case
|
|
|
|
* the only alternative is to repeat this chunk of code (with different function names)
|
|
|
|
* for each syscall being implemented. Given that this define is contained within a single
|
|
|
|
* file, this looks like a good tradeoff.
|
|
|
|
*
|
|
|
|
* First we check if syscall is implemented by VFS (corresponding member is not NULL),
|
|
|
|
* then call the right flavor of the method (e.g. open or open_p) depending on
|
|
|
|
* ESP_VFS_FLAG_CONTEXT_PTR flag. If ESP_VFS_FLAG_CONTEXT_PTR is set, context is passed
|
|
|
|
* in as first argument and _p variant is used for the call.
|
|
|
|
* It is enough to check just one of them for NULL, as both variants are part of a union.
|
|
|
|
*/
|
|
|
|
#define CHECK_AND_CALL(ret, r, pvfs, func, ...) \
|
|
|
|
if (pvfs->vfs.func == NULL) { \
|
|
|
|
__errno_r(r) = ENOSYS; \
|
|
|
|
return -1; \
|
|
|
|
} \
|
|
|
|
if (pvfs->vfs.flags & ESP_VFS_FLAG_CONTEXT_PTR) { \
|
|
|
|
ret = (*pvfs->vfs.func ## _p)(pvfs->ctx, __VA_ARGS__); \
|
|
|
|
} else { \
|
|
|
|
ret = (*pvfs->vfs.func)(__VA_ARGS__);\
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-01-02 14:26:25 -05:00
|
|
|
#define CHECK_AND_CALLV(r, pvfs, func, ...) \
|
|
|
|
if (pvfs->vfs.func == NULL) { \
|
|
|
|
__errno_r(r) = ENOSYS; \
|
|
|
|
return; \
|
|
|
|
} \
|
|
|
|
if (pvfs->vfs.flags & ESP_VFS_FLAG_CONTEXT_PTR) { \
|
|
|
|
(*pvfs->vfs.func ## _p)(pvfs->ctx, __VA_ARGS__); \
|
|
|
|
} else { \
|
|
|
|
(*pvfs->vfs.func)(__VA_ARGS__);\
|
|
|
|
}
|
|
|
|
|
|
|
|
#define CHECK_AND_CALLP(ret, r, pvfs, func, ...) \
|
|
|
|
if (pvfs->vfs.func == NULL) { \
|
|
|
|
__errno_r(r) = ENOSYS; \
|
|
|
|
return NULL; \
|
|
|
|
} \
|
|
|
|
if (pvfs->vfs.flags & ESP_VFS_FLAG_CONTEXT_PTR) { \
|
|
|
|
ret = (*pvfs->vfs.func ## _p)(pvfs->ctx, __VA_ARGS__); \
|
|
|
|
} else { \
|
|
|
|
ret = (*pvfs->vfs.func)(__VA_ARGS__);\
|
|
|
|
}
|
|
|
|
|
2016-10-25 09:02:39 -04:00
|
|
|
int esp_vfs_open(struct _reent *r, const char * path, int flags, int mode)
|
|
|
|
{
|
2018-05-07 03:01:56 -04:00
|
|
|
const vfs_entry_t *vfs = get_vfs_for_path(path);
|
2016-10-25 09:02:39 -04:00
|
|
|
if (vfs == NULL) {
|
|
|
|
__errno_r(r) = ENOENT;
|
|
|
|
return -1;
|
|
|
|
}
|
2018-05-07 03:01:56 -04:00
|
|
|
const char *path_within_vfs = translate_path(vfs, path);
|
|
|
|
int fd_within_vfs;
|
|
|
|
CHECK_AND_CALL(fd_within_vfs, r, vfs, open, path_within_vfs, flags, mode);
|
|
|
|
if (fd_within_vfs >= 0) {
|
|
|
|
_lock_acquire(&s_fd_table_lock);
|
|
|
|
for (int i = 0; i < MAX_FDS; ++i) {
|
|
|
|
if (s_fd_table[i].vfs_index == -1) {
|
|
|
|
s_fd_table[i].permanent = false;
|
|
|
|
s_fd_table[i].vfs_index = vfs->offset;
|
|
|
|
s_fd_table[i].local_fd = fd_within_vfs;
|
|
|
|
_lock_release(&s_fd_table_lock);
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_lock_release(&s_fd_table_lock);
|
|
|
|
int ret;
|
|
|
|
CHECK_AND_CALL(ret, r, vfs, close, fd_within_vfs);
|
2018-05-29 05:01:25 -04:00
|
|
|
(void) ret; // remove "set but not used" warning
|
2018-05-07 03:01:56 -04:00
|
|
|
__errno_r(r) = ENOMEM;
|
|
|
|
return -1;
|
2016-11-07 01:26:21 -05:00
|
|
|
}
|
2018-05-07 03:01:56 -04:00
|
|
|
__errno_r(r) = ENOENT;
|
|
|
|
return -1;
|
2016-10-25 09:02:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
ssize_t esp_vfs_write(struct _reent *r, int fd, const void * data, size_t size)
|
|
|
|
{
|
|
|
|
const vfs_entry_t* vfs = get_vfs_for_fd(fd);
|
2018-05-07 03:01:56 -04:00
|
|
|
const int local_fd = get_local_fd(vfs, fd);
|
|
|
|
if (vfs == NULL || local_fd < 0) {
|
2016-10-25 09:02:39 -04:00
|
|
|
__errno_r(r) = EBADF;
|
|
|
|
return -1;
|
|
|
|
}
|
2017-06-08 12:48:17 -04:00
|
|
|
ssize_t ret;
|
2018-04-19 23:51:41 -04:00
|
|
|
CHECK_AND_CALL(ret, r, vfs, write, local_fd, data, size);
|
2016-10-25 09:02:39 -04:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2016-10-26 02:05:56 -04:00
|
|
|
off_t esp_vfs_lseek(struct _reent *r, int fd, off_t size, int mode)
|
2016-10-25 09:02:39 -04:00
|
|
|
{
|
|
|
|
const vfs_entry_t* vfs = get_vfs_for_fd(fd);
|
2018-05-07 03:01:56 -04:00
|
|
|
const int local_fd = get_local_fd(vfs, fd);
|
|
|
|
if (vfs == NULL || local_fd < 0) {
|
2016-10-25 09:02:39 -04:00
|
|
|
__errno_r(r) = EBADF;
|
|
|
|
return -1;
|
|
|
|
}
|
2017-06-08 12:48:17 -04:00
|
|
|
off_t ret;
|
2018-04-19 23:51:41 -04:00
|
|
|
CHECK_AND_CALL(ret, r, vfs, lseek, local_fd, size, mode);
|
2016-10-25 09:02:39 -04:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
ssize_t esp_vfs_read(struct _reent *r, int fd, void * dst, size_t size)
|
|
|
|
{
|
|
|
|
const vfs_entry_t* vfs = get_vfs_for_fd(fd);
|
2018-05-07 03:01:56 -04:00
|
|
|
const int local_fd = get_local_fd(vfs, fd);
|
|
|
|
if (vfs == NULL || local_fd < 0) {
|
2016-10-25 09:02:39 -04:00
|
|
|
__errno_r(r) = EBADF;
|
|
|
|
return -1;
|
|
|
|
}
|
2017-06-08 12:48:17 -04:00
|
|
|
ssize_t ret;
|
2018-04-19 23:51:41 -04:00
|
|
|
CHECK_AND_CALL(ret, r, vfs, read, local_fd, dst, size);
|
2016-10-25 09:02:39 -04:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int esp_vfs_close(struct _reent *r, int fd)
|
|
|
|
{
|
|
|
|
const vfs_entry_t* vfs = get_vfs_for_fd(fd);
|
2018-05-07 03:01:56 -04:00
|
|
|
const int local_fd = get_local_fd(vfs, fd);
|
|
|
|
if (vfs == NULL || local_fd < 0) {
|
2016-10-25 09:02:39 -04:00
|
|
|
__errno_r(r) = EBADF;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
int ret;
|
2018-04-19 23:51:41 -04:00
|
|
|
CHECK_AND_CALL(ret, r, vfs, close, local_fd);
|
2018-05-07 03:01:56 -04:00
|
|
|
|
|
|
|
_lock_acquire(&s_fd_table_lock);
|
|
|
|
if (!s_fd_table[fd].permanent) {
|
|
|
|
s_fd_table[fd] = FD_TABLE_ENTRY_UNUSED;
|
|
|
|
}
|
|
|
|
_lock_release(&s_fd_table_lock);
|
2016-10-25 09:02:39 -04:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int esp_vfs_fstat(struct _reent *r, int fd, struct stat * st)
|
|
|
|
{
|
|
|
|
const vfs_entry_t* vfs = get_vfs_for_fd(fd);
|
2018-05-07 03:01:56 -04:00
|
|
|
const int local_fd = get_local_fd(vfs, fd);
|
|
|
|
if (vfs == NULL || local_fd < 0) {
|
2016-10-25 09:02:39 -04:00
|
|
|
__errno_r(r) = EBADF;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
int ret;
|
2018-04-19 23:51:41 -04:00
|
|
|
CHECK_AND_CALL(ret, r, vfs, fstat, local_fd, st);
|
2016-10-25 09:02:39 -04:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int esp_vfs_stat(struct _reent *r, const char * path, struct stat * st)
|
|
|
|
{
|
|
|
|
const vfs_entry_t* vfs = get_vfs_for_path(path);
|
|
|
|
if (vfs == NULL) {
|
|
|
|
__errno_r(r) = ENOENT;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
const char* path_within_vfs = translate_path(vfs, path);
|
|
|
|
int ret;
|
|
|
|
CHECK_AND_CALL(ret, r, vfs, stat, path_within_vfs, st);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int esp_vfs_link(struct _reent *r, const char* n1, const char* n2)
|
|
|
|
{
|
|
|
|
const vfs_entry_t* vfs = get_vfs_for_path(n1);
|
|
|
|
if (vfs == NULL) {
|
|
|
|
__errno_r(r) = ENOENT;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
const vfs_entry_t* vfs2 = get_vfs_for_path(n2);
|
|
|
|
if (vfs != vfs2) {
|
|
|
|
__errno_r(r) = EXDEV;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
const char* path1_within_vfs = translate_path(vfs, n1);
|
|
|
|
const char* path2_within_vfs = translate_path(vfs, n2);
|
|
|
|
int ret;
|
|
|
|
CHECK_AND_CALL(ret, r, vfs, link, path1_within_vfs, path2_within_vfs);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int esp_vfs_unlink(struct _reent *r, const char *path)
|
|
|
|
{
|
|
|
|
const vfs_entry_t* vfs = get_vfs_for_path(path);
|
|
|
|
if (vfs == NULL) {
|
|
|
|
__errno_r(r) = ENOENT;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
const char* path_within_vfs = translate_path(vfs, path);
|
|
|
|
int ret;
|
|
|
|
CHECK_AND_CALL(ret, r, vfs, unlink, path_within_vfs);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int esp_vfs_rename(struct _reent *r, const char *src, const char *dst)
|
|
|
|
{
|
|
|
|
const vfs_entry_t* vfs = get_vfs_for_path(src);
|
|
|
|
if (vfs == NULL) {
|
|
|
|
__errno_r(r) = ENOENT;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
const vfs_entry_t* vfs_dst = get_vfs_for_path(dst);
|
|
|
|
if (vfs != vfs_dst) {
|
|
|
|
__errno_r(r) = EXDEV;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
const char* src_within_vfs = translate_path(vfs, src);
|
|
|
|
const char* dst_within_vfs = translate_path(vfs, dst);
|
|
|
|
int ret;
|
|
|
|
CHECK_AND_CALL(ret, r, vfs, rename, src_within_vfs, dst_within_vfs);
|
|
|
|
return ret;
|
|
|
|
}
|
2017-01-02 14:26:25 -05:00
|
|
|
|
|
|
|
DIR* opendir(const char* name)
|
|
|
|
{
|
|
|
|
const vfs_entry_t* vfs = get_vfs_for_path(name);
|
|
|
|
struct _reent* r = __getreent();
|
|
|
|
if (vfs == NULL) {
|
|
|
|
__errno_r(r) = ENOENT;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
const char* path_within_vfs = translate_path(vfs, name);
|
|
|
|
DIR* ret;
|
|
|
|
CHECK_AND_CALLP(ret, r, vfs, opendir, path_within_vfs);
|
|
|
|
if (ret != NULL) {
|
2018-05-07 03:01:56 -04:00
|
|
|
ret->dd_vfs_idx = vfs->offset;
|
2017-01-02 14:26:25 -05:00
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct dirent* readdir(DIR* pdir)
|
|
|
|
{
|
2018-05-07 03:01:56 -04:00
|
|
|
const vfs_entry_t* vfs = get_vfs_for_index(pdir->dd_vfs_idx);
|
2017-01-02 14:26:25 -05:00
|
|
|
struct _reent* r = __getreent();
|
|
|
|
if (vfs == NULL) {
|
|
|
|
__errno_r(r) = EBADF;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
struct dirent* ret;
|
|
|
|
CHECK_AND_CALLP(ret, r, vfs, readdir, pdir);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int readdir_r(DIR* pdir, struct dirent* entry, struct dirent** out_dirent)
|
|
|
|
{
|
2018-05-07 03:01:56 -04:00
|
|
|
const vfs_entry_t* vfs = get_vfs_for_index(pdir->dd_vfs_idx);
|
2017-01-02 14:26:25 -05:00
|
|
|
struct _reent* r = __getreent();
|
|
|
|
if (vfs == NULL) {
|
|
|
|
errno = EBADF;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
int ret;
|
|
|
|
CHECK_AND_CALL(ret, r, vfs, readdir_r, pdir, entry, out_dirent);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
long telldir(DIR* pdir)
|
|
|
|
{
|
2018-05-07 03:01:56 -04:00
|
|
|
const vfs_entry_t* vfs = get_vfs_for_index(pdir->dd_vfs_idx);
|
2017-01-02 14:26:25 -05:00
|
|
|
struct _reent* r = __getreent();
|
|
|
|
if (vfs == NULL) {
|
|
|
|
errno = EBADF;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
long ret;
|
|
|
|
CHECK_AND_CALL(ret, r, vfs, telldir, pdir);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
void seekdir(DIR* pdir, long loc)
|
|
|
|
{
|
2018-05-07 03:01:56 -04:00
|
|
|
const vfs_entry_t* vfs = get_vfs_for_index(pdir->dd_vfs_idx);
|
2017-01-02 14:26:25 -05:00
|
|
|
struct _reent* r = __getreent();
|
|
|
|
if (vfs == NULL) {
|
|
|
|
errno = EBADF;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
CHECK_AND_CALLV(r, vfs, seekdir, pdir, loc);
|
|
|
|
}
|
|
|
|
|
|
|
|
void rewinddir(DIR* pdir)
|
|
|
|
{
|
|
|
|
seekdir(pdir, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
int closedir(DIR* pdir)
|
|
|
|
{
|
2018-05-07 03:01:56 -04:00
|
|
|
const vfs_entry_t* vfs = get_vfs_for_index(pdir->dd_vfs_idx);
|
2017-01-02 14:26:25 -05:00
|
|
|
struct _reent* r = __getreent();
|
|
|
|
if (vfs == NULL) {
|
|
|
|
errno = EBADF;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
int ret;
|
|
|
|
CHECK_AND_CALL(ret, r, vfs, closedir, pdir);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int mkdir(const char* name, mode_t mode)
|
|
|
|
{
|
|
|
|
const vfs_entry_t* vfs = get_vfs_for_path(name);
|
|
|
|
struct _reent* r = __getreent();
|
|
|
|
if (vfs == NULL) {
|
|
|
|
__errno_r(r) = ENOENT;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
const char* path_within_vfs = translate_path(vfs, name);
|
|
|
|
int ret;
|
|
|
|
CHECK_AND_CALL(ret, r, vfs, mkdir, path_within_vfs, mode);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int rmdir(const char* name)
|
|
|
|
{
|
|
|
|
const vfs_entry_t* vfs = get_vfs_for_path(name);
|
|
|
|
struct _reent* r = __getreent();
|
|
|
|
if (vfs == NULL) {
|
|
|
|
__errno_r(r) = ENOENT;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
const char* path_within_vfs = translate_path(vfs, name);
|
|
|
|
int ret;
|
|
|
|
CHECK_AND_CALL(ret, r, vfs, rmdir, path_within_vfs);
|
|
|
|
return ret;
|
|
|
|
}
|
2017-08-16 02:58:06 -04:00
|
|
|
|
|
|
|
int fcntl(int fd, int cmd, ...)
|
|
|
|
{
|
|
|
|
const vfs_entry_t* vfs = get_vfs_for_fd(fd);
|
2018-05-07 03:01:56 -04:00
|
|
|
const int local_fd = get_local_fd(vfs, fd);
|
2017-08-16 02:58:06 -04:00
|
|
|
struct _reent* r = __getreent();
|
2018-05-07 03:01:56 -04:00
|
|
|
if (vfs == NULL || local_fd < 0) {
|
2017-08-16 02:58:06 -04:00
|
|
|
__errno_r(r) = EBADF;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
int ret;
|
|
|
|
va_list args;
|
|
|
|
va_start(args, cmd);
|
2018-04-19 23:51:41 -04:00
|
|
|
CHECK_AND_CALL(ret, r, vfs, fcntl, local_fd, cmd, args);
|
2017-08-16 02:58:06 -04:00
|
|
|
va_end(args);
|
|
|
|
return ret;
|
|
|
|
}
|
2017-10-03 18:24:40 -04:00
|
|
|
|
|
|
|
int ioctl(int fd, int cmd, ...)
|
|
|
|
{
|
|
|
|
const vfs_entry_t* vfs = get_vfs_for_fd(fd);
|
2018-05-07 03:01:56 -04:00
|
|
|
const int local_fd = get_local_fd(vfs, fd);
|
2017-10-03 18:24:40 -04:00
|
|
|
struct _reent* r = __getreent();
|
2018-05-07 03:01:56 -04:00
|
|
|
if (vfs == NULL || local_fd < 0) {
|
2017-10-03 18:24:40 -04:00
|
|
|
__errno_r(r) = EBADF;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
int ret;
|
|
|
|
va_list args;
|
|
|
|
va_start(args, cmd);
|
2018-04-19 23:51:41 -04:00
|
|
|
CHECK_AND_CALL(ret, r, vfs, ioctl, local_fd, cmd, args);
|
2017-10-03 18:24:40 -04:00
|
|
|
va_end(args);
|
|
|
|
return ret;
|
|
|
|
}
|
2017-10-17 02:00:01 -04:00
|
|
|
|
|
|
|
int fsync(int fd)
|
|
|
|
{
|
|
|
|
const vfs_entry_t* vfs = get_vfs_for_fd(fd);
|
2018-05-07 03:01:56 -04:00
|
|
|
const int local_fd = get_local_fd(vfs, fd);
|
2017-10-17 02:00:01 -04:00
|
|
|
struct _reent* r = __getreent();
|
2018-05-07 03:01:56 -04:00
|
|
|
if (vfs == NULL || local_fd < 0) {
|
2017-10-17 02:00:01 -04:00
|
|
|
__errno_r(r) = EBADF;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
int ret;
|
2018-04-19 23:51:41 -04:00
|
|
|
CHECK_AND_CALL(ret, r, vfs, fsync, local_fd);
|
2017-10-17 02:00:01 -04:00
|
|
|
return ret;
|
|
|
|
}
|
2018-05-04 05:44:38 -04:00
|
|
|
|
|
|
|
int access(const char *path, int amode)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
const vfs_entry_t* vfs = get_vfs_for_path(path);
|
|
|
|
struct _reent* r = __getreent();
|
|
|
|
if (vfs == NULL) {
|
|
|
|
__errno_r(r) = ENOENT;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
const char* path_within_vfs = translate_path(vfs, path);
|
|
|
|
CHECK_AND_CALL(ret, r, vfs, access, path_within_vfs, amode);
|
|
|
|
return ret;
|
|
|
|
}
|
2018-05-03 04:41:10 -04:00
|
|
|
|
2018-06-12 06:29:05 -04:00
|
|
|
int truncate(const char *path, off_t length)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
const vfs_entry_t* vfs = get_vfs_for_path(path);
|
|
|
|
struct _reent* r = __getreent();
|
|
|
|
if (vfs == NULL) {
|
|
|
|
__errno_r(r) = ENOENT;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
const char* path_within_vfs = translate_path(vfs, path);
|
|
|
|
CHECK_AND_CALL(ret, r, vfs, truncate, path_within_vfs, length);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2018-05-03 04:41:10 -04:00
|
|
|
static void call_end_selects(int end_index, const fds_triple_t *vfs_fds_triple)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < end_index; ++i) {
|
|
|
|
const vfs_entry_t *vfs = get_vfs_for_index(i);
|
|
|
|
const fds_triple_t *item = &vfs_fds_triple[i];
|
|
|
|
if (vfs && vfs->vfs.end_select && item->isset) {
|
|
|
|
vfs->vfs.end_select();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-29 05:01:25 -04:00
|
|
|
static inline bool esp_vfs_safe_fd_isset(int fd, const fd_set *fds)
|
|
|
|
{
|
|
|
|
return fds && FD_ISSET(fd, fds);
|
|
|
|
}
|
|
|
|
|
2018-05-03 04:41:10 -04:00
|
|
|
static int set_global_fd_sets(const fds_triple_t *vfs_fds_triple, int size, fd_set *readfds, fd_set *writefds, fd_set *errorfds)
|
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
for (int i = 0; i < size; ++i) {
|
|
|
|
const fds_triple_t *item = &vfs_fds_triple[i];
|
|
|
|
if (item->isset) {
|
|
|
|
for (int fd = 0; fd < MAX_FDS; ++fd) {
|
|
|
|
const int local_fd = s_fd_table[fd].local_fd; // single read -> no locking is required
|
2018-05-29 05:01:25 -04:00
|
|
|
if (readfds && esp_vfs_safe_fd_isset(local_fd, &item->readfds)) {
|
2018-05-29 05:01:25 -04:00
|
|
|
ESP_LOGD(TAG, "FD %d in readfds was set from VFS ID %d", fd, i);
|
2018-05-03 04:41:10 -04:00
|
|
|
FD_SET(fd, readfds);
|
|
|
|
++ret;
|
|
|
|
}
|
2018-05-29 05:01:25 -04:00
|
|
|
if (writefds && esp_vfs_safe_fd_isset(local_fd, &item->writefds)) {
|
2018-05-29 05:01:25 -04:00
|
|
|
ESP_LOGD(TAG, "FD %d in writefds was set from VFS ID %d", fd, i);
|
2018-05-03 04:41:10 -04:00
|
|
|
FD_SET(fd, writefds);
|
|
|
|
++ret;
|
|
|
|
}
|
2018-05-29 05:01:25 -04:00
|
|
|
if (errorfds && esp_vfs_safe_fd_isset(local_fd, &item->errorfds)) {
|
2018-05-29 05:01:25 -04:00
|
|
|
ESP_LOGD(TAG, "FD %d in errorfds was set from VFS ID %d", fd, i);
|
2018-05-03 04:41:10 -04:00
|
|
|
FD_SET(fd, errorfds);
|
|
|
|
++ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2018-05-29 05:01:25 -04:00
|
|
|
static void esp_vfs_log_fd_set(const char *fds_name, const fd_set *fds)
|
|
|
|
{
|
|
|
|
if (fds_name && fds) {
|
|
|
|
ESP_LOGD(TAG, "FDs in %s =", fds_name);
|
|
|
|
for (int i = 0; i < MAX_FDS; ++i) {
|
2018-05-29 05:01:25 -04:00
|
|
|
if (esp_vfs_safe_fd_isset(i, fds)) {
|
2018-05-29 05:01:25 -04:00
|
|
|
ESP_LOGD(TAG, "%d", i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-03 04:41:10 -04:00
|
|
|
int esp_vfs_select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *errorfds, struct timeval *timeout)
|
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
struct _reent* r = __getreent();
|
|
|
|
|
2018-05-29 05:01:25 -04:00
|
|
|
ESP_LOGD(TAG, "esp_vfs_select starts with nfds = %d", nfds);
|
|
|
|
if (timeout) {
|
|
|
|
ESP_LOGD(TAG, "timeout is %lds + %ldus", timeout->tv_sec, timeout->tv_usec);
|
|
|
|
}
|
|
|
|
esp_vfs_log_fd_set("readfds", readfds);
|
|
|
|
esp_vfs_log_fd_set("writefds", writefds);
|
|
|
|
esp_vfs_log_fd_set("errorfds", errorfds);
|
|
|
|
|
2018-05-03 04:41:10 -04:00
|
|
|
if (nfds > MAX_FDS || nfds < 0) {
|
2018-05-29 05:01:25 -04:00
|
|
|
ESP_LOGD(TAG, "incorrect nfds");
|
2018-05-03 04:41:10 -04:00
|
|
|
__errno_r(r) = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2019-10-14 03:22:55 -04:00
|
|
|
// Capture s_vfs_count to a local variable in case a new driver is registered or removed during this actual select()
|
|
|
|
// call. s_vfs_count cannot be protected with a mutex during a select() call (which can be one without a timeout)
|
|
|
|
// because that could block the registration of new driver.
|
|
|
|
const size_t vfs_count = s_vfs_count;
|
2018-05-03 04:41:10 -04:00
|
|
|
fds_triple_t *vfs_fds_triple;
|
2019-10-14 03:22:55 -04:00
|
|
|
if ((vfs_fds_triple = calloc(vfs_count, sizeof(fds_triple_t))) == NULL) {
|
2018-05-03 04:41:10 -04:00
|
|
|
__errno_r(r) = ENOMEM;
|
2018-05-29 05:01:25 -04:00
|
|
|
ESP_LOGD(TAG, "calloc is unsuccessful");
|
2018-05-03 04:41:10 -04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int (*socket_select)(int, fd_set *, fd_set *, fd_set *, struct timeval *) = NULL;
|
|
|
|
for (int fd = 0; fd < nfds; ++fd) {
|
|
|
|
_lock_acquire(&s_fd_table_lock);
|
|
|
|
const bool is_socket_fd = s_fd_table[fd].permanent;
|
|
|
|
const int vfs_index = s_fd_table[fd].vfs_index;
|
|
|
|
const int local_fd = s_fd_table[fd].local_fd;
|
|
|
|
_lock_release(&s_fd_table_lock);
|
|
|
|
|
|
|
|
if (vfs_index < 0) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-05-24 04:02:24 -04:00
|
|
|
if (is_socket_fd) {
|
|
|
|
if (!socket_select) {
|
|
|
|
// no socket_select found yet so take a look
|
2018-05-29 05:01:25 -04:00
|
|
|
if (esp_vfs_safe_fd_isset(fd, readfds) ||
|
|
|
|
esp_vfs_safe_fd_isset(fd, writefds) ||
|
|
|
|
esp_vfs_safe_fd_isset(fd, errorfds)) {
|
2018-05-24 04:02:24 -04:00
|
|
|
const vfs_entry_t *vfs = s_vfs[vfs_index];
|
|
|
|
socket_select = vfs->vfs.socket_select;
|
2019-04-01 06:50:54 -04:00
|
|
|
|
|
|
|
// get_socket_select_semaphore needs to be set for a socket driver where semaphore can be
|
|
|
|
// initialized outside interrupt handlers (ignoring this could result in unexpected failures)
|
|
|
|
if (vfs->vfs.get_socket_select_semaphore != NULL) {
|
|
|
|
vfs->vfs.get_socket_select_semaphore(); // Semaphore is returned and it was allocated if it
|
|
|
|
// wasn't before. We don't use the return value just need to be sure that it doesn't get
|
|
|
|
// allocated later from ISR.
|
|
|
|
// Note: ESP-IDF v4.0 will start to use this callback differently with some breaking changes
|
|
|
|
// in the VFS API.
|
|
|
|
}
|
2018-05-24 04:02:24 -04:00
|
|
|
}
|
2018-05-03 04:41:10 -04:00
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
fds_triple_t *item = &vfs_fds_triple[vfs_index]; // FD sets for VFS which belongs to fd
|
2018-05-29 05:01:25 -04:00
|
|
|
if (esp_vfs_safe_fd_isset(fd, readfds)) {
|
2018-05-03 04:41:10 -04:00
|
|
|
item->isset = true;
|
|
|
|
FD_SET(local_fd, &item->readfds);
|
|
|
|
FD_CLR(fd, readfds);
|
2018-05-29 05:01:25 -04:00
|
|
|
ESP_LOGD(TAG, "removing %d from readfds and adding as local FD %d to fd_set of VFS ID %d", fd, local_fd, vfs_index);
|
2018-05-03 04:41:10 -04:00
|
|
|
}
|
2018-05-29 05:01:25 -04:00
|
|
|
if (esp_vfs_safe_fd_isset(fd, writefds)) {
|
2018-05-03 04:41:10 -04:00
|
|
|
item->isset = true;
|
|
|
|
FD_SET(local_fd, &item->writefds);
|
|
|
|
FD_CLR(fd, writefds);
|
2018-05-29 05:01:25 -04:00
|
|
|
ESP_LOGD(TAG, "removing %d from writefds and adding as local FD %d to fd_set of VFS ID %d", fd, local_fd, vfs_index);
|
2018-05-03 04:41:10 -04:00
|
|
|
}
|
2018-05-29 05:01:25 -04:00
|
|
|
if (esp_vfs_safe_fd_isset(fd, errorfds)) {
|
2018-05-03 04:41:10 -04:00
|
|
|
item->isset = true;
|
|
|
|
FD_SET(local_fd, &item->errorfds);
|
|
|
|
FD_CLR(fd, errorfds);
|
2018-05-29 05:01:25 -04:00
|
|
|
ESP_LOGD(TAG, "removing %d from errorfds and adding as local FD %d to fd_set of VFS ID %d", fd, local_fd, vfs_index);
|
2018-05-03 04:41:10 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// all non-socket VFSs have their FD sets in vfs_fds_triple
|
|
|
|
// the global readfds, writefds and errorfds contain only socket FDs (if
|
|
|
|
// there any)
|
|
|
|
|
2018-05-29 05:01:25 -04:00
|
|
|
/* Semaphore used for waiting select events from other VFS drivers when socket
|
|
|
|
* select is not used (not registered or socket FDs are not observed by the
|
|
|
|
* given call of select)
|
|
|
|
*/
|
|
|
|
SemaphoreHandle_t select_sem = NULL;
|
|
|
|
|
2018-05-03 04:41:10 -04:00
|
|
|
if (!socket_select) {
|
|
|
|
// There is no socket VFS registered or select() wasn't called for
|
|
|
|
// any socket. Therefore, we will use our own signalization.
|
2018-05-29 05:01:25 -04:00
|
|
|
if ((select_sem = xSemaphoreCreateBinary()) == NULL) {
|
2018-05-03 04:41:10 -04:00
|
|
|
free(vfs_fds_triple);
|
|
|
|
__errno_r(r) = ENOMEM;
|
2018-05-29 05:01:25 -04:00
|
|
|
ESP_LOGD(TAG, "cannot create select_sem");
|
2018-05-03 04:41:10 -04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-14 03:22:55 -04:00
|
|
|
for (int i = 0; i < vfs_count; ++i) {
|
2018-05-03 04:41:10 -04:00
|
|
|
const vfs_entry_t *vfs = get_vfs_for_index(i);
|
|
|
|
fds_triple_t *item = &vfs_fds_triple[i];
|
|
|
|
|
|
|
|
if (vfs && vfs->vfs.start_select && item->isset) {
|
|
|
|
// call start_select for all non-socket VFSs with has at least one FD set in readfds, writefds, or errorfds
|
|
|
|
// note: it can point to socket VFS but item->isset will be false for that
|
2018-05-29 05:01:25 -04:00
|
|
|
ESP_LOGD(TAG, "calling start_select for VFS ID %d with the following local FDs", i);
|
|
|
|
esp_vfs_log_fd_set("readfds", &item->readfds);
|
|
|
|
esp_vfs_log_fd_set("writefds", &item->writefds);
|
|
|
|
esp_vfs_log_fd_set("errorfds", &item->errorfds);
|
2018-05-29 05:01:25 -04:00
|
|
|
esp_err_t err = vfs->vfs.start_select(nfds, &item->readfds, &item->writefds, &item->errorfds, &select_sem);
|
2018-05-03 04:41:10 -04:00
|
|
|
|
|
|
|
if (err != ESP_OK) {
|
|
|
|
call_end_selects(i, vfs_fds_triple);
|
2019-10-14 03:22:55 -04:00
|
|
|
(void) set_global_fd_sets(vfs_fds_triple, vfs_count, readfds, writefds, errorfds);
|
2018-05-29 05:01:25 -04:00
|
|
|
if (select_sem) {
|
|
|
|
vSemaphoreDelete(select_sem);
|
|
|
|
select_sem = NULL;
|
2018-05-03 04:41:10 -04:00
|
|
|
}
|
|
|
|
free(vfs_fds_triple);
|
2018-05-29 05:01:25 -04:00
|
|
|
__errno_r(r) = EINTR;
|
2018-05-29 05:01:25 -04:00
|
|
|
ESP_LOGD(TAG, "start_select failed");
|
2018-05-03 04:41:10 -04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (socket_select) {
|
2018-05-29 05:01:25 -04:00
|
|
|
ESP_LOGD(TAG, "calling socket_select with the following FDs");
|
|
|
|
esp_vfs_log_fd_set("readfds", readfds);
|
|
|
|
esp_vfs_log_fd_set("writefds", writefds);
|
|
|
|
esp_vfs_log_fd_set("errorfds", errorfds);
|
2018-05-03 04:41:10 -04:00
|
|
|
ret = socket_select(nfds, readfds, writefds, errorfds, timeout);
|
2018-05-29 05:01:25 -04:00
|
|
|
ESP_LOGD(TAG, "socket_select returned %d and the FDs are the following", ret);
|
|
|
|
esp_vfs_log_fd_set("readfds", readfds);
|
|
|
|
esp_vfs_log_fd_set("writefds", writefds);
|
|
|
|
esp_vfs_log_fd_set("errorfds", errorfds);
|
2018-05-03 04:41:10 -04:00
|
|
|
} else {
|
|
|
|
if (readfds) {
|
|
|
|
FD_ZERO(readfds);
|
|
|
|
}
|
|
|
|
if (writefds) {
|
|
|
|
FD_ZERO(writefds);
|
|
|
|
}
|
|
|
|
if (errorfds) {
|
|
|
|
FD_ZERO(errorfds);
|
|
|
|
}
|
|
|
|
|
|
|
|
TickType_t ticks_to_wait = portMAX_DELAY;
|
|
|
|
if (timeout) {
|
|
|
|
uint32_t timeout_ms = timeout->tv_sec * 1000 + timeout->tv_usec / 1000;
|
|
|
|
ticks_to_wait = timeout_ms / portTICK_PERIOD_MS;
|
2018-05-29 05:01:25 -04:00
|
|
|
ESP_LOGD(TAG, "timeout is %dms", timeout_ms);
|
2018-05-03 04:41:10 -04:00
|
|
|
}
|
2018-05-29 05:01:25 -04:00
|
|
|
ESP_LOGD(TAG, "waiting without calling socket_select");
|
2018-05-29 05:01:25 -04:00
|
|
|
xSemaphoreTake(select_sem, ticks_to_wait);
|
2018-05-03 04:41:10 -04:00
|
|
|
}
|
|
|
|
|
2019-10-14 03:22:55 -04:00
|
|
|
call_end_selects(vfs_count, vfs_fds_triple); // for VFSs for start_select was called before
|
2018-05-03 04:41:10 -04:00
|
|
|
if (ret >= 0) {
|
2019-10-14 03:22:55 -04:00
|
|
|
ret += set_global_fd_sets(vfs_fds_triple, vfs_count, readfds, writefds, errorfds);
|
2018-05-03 04:41:10 -04:00
|
|
|
}
|
2018-05-29 05:01:25 -04:00
|
|
|
if (select_sem) {
|
|
|
|
vSemaphoreDelete(select_sem);
|
|
|
|
select_sem = NULL;
|
2018-05-03 04:41:10 -04:00
|
|
|
}
|
|
|
|
free(vfs_fds_triple);
|
2018-05-29 05:01:25 -04:00
|
|
|
|
|
|
|
ESP_LOGD(TAG, "esp_vfs_select returns %d", ret);
|
|
|
|
esp_vfs_log_fd_set("readfds", readfds);
|
|
|
|
esp_vfs_log_fd_set("writefds", writefds);
|
|
|
|
esp_vfs_log_fd_set("errorfds", errorfds);
|
2018-05-03 04:41:10 -04:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2018-05-29 05:01:25 -04:00
|
|
|
void esp_vfs_select_triggered(SemaphoreHandle_t *signal_sem)
|
2018-05-03 04:41:10 -04:00
|
|
|
{
|
2018-05-29 05:01:25 -04:00
|
|
|
if (signal_sem && (*signal_sem)) {
|
|
|
|
xSemaphoreGive(*signal_sem);
|
2018-05-03 04:41:10 -04:00
|
|
|
} else {
|
|
|
|
// Another way would be to go through s_fd_table and find the VFS
|
|
|
|
// which has a permanent FD. But in order to avoid to lock
|
|
|
|
// s_fd_table_lock we go through the VFS table.
|
|
|
|
for (int i = 0; i < s_vfs_count; ++i) {
|
2019-10-14 03:22:55 -04:00
|
|
|
// Note: s_vfs_count could have changed since the start of vfs_select() call. However, that change doesn't
|
|
|
|
// matter here stop_socket_select() will be called for only valid VFS drivers.
|
2018-05-03 04:41:10 -04:00
|
|
|
const vfs_entry_t *vfs = s_vfs[i];
|
|
|
|
if (vfs != NULL && vfs->vfs.stop_socket_select != NULL) {
|
|
|
|
vfs->vfs.stop_socket_select();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-29 05:01:25 -04:00
|
|
|
void esp_vfs_select_triggered_isr(SemaphoreHandle_t *signal_sem, BaseType_t *woken)
|
2018-05-03 04:41:10 -04:00
|
|
|
{
|
2018-05-29 05:01:25 -04:00
|
|
|
if (signal_sem && (*signal_sem)) {
|
|
|
|
xSemaphoreGiveFromISR(*signal_sem, woken);
|
2018-05-03 04:41:10 -04:00
|
|
|
} else {
|
|
|
|
// Another way would be to go through s_fd_table and find the VFS
|
|
|
|
// which has a permanent FD. But in order to avoid to lock
|
|
|
|
// s_fd_table_lock we go through the VFS table.
|
|
|
|
for (int i = 0; i < s_vfs_count; ++i) {
|
2019-10-14 03:22:55 -04:00
|
|
|
// Note: s_vfs_count could have changed since the start of vfs_select() call. However, that change doesn't
|
|
|
|
// matter here stop_socket_select() will be called for only valid VFS drivers.
|
2018-05-03 04:41:10 -04:00
|
|
|
const vfs_entry_t *vfs = s_vfs[i];
|
|
|
|
if (vfs != NULL && vfs->vfs.stop_socket_select_isr != NULL) {
|
|
|
|
vfs->vfs.stop_socket_select_isr(woken);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-08-14 07:39:30 -04:00
|
|
|
|
|
|
|
#ifdef CONFIG_SUPPORT_TERMIOS
|
|
|
|
int tcgetattr(int fd, struct termios *p)
|
|
|
|
{
|
|
|
|
const vfs_entry_t* vfs = get_vfs_for_fd(fd);
|
|
|
|
const int local_fd = get_local_fd(vfs, fd);
|
|
|
|
struct _reent* r = __getreent();
|
|
|
|
if (vfs == NULL || local_fd < 0) {
|
|
|
|
__errno_r(r) = EBADF;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
int ret;
|
|
|
|
CHECK_AND_CALL(ret, r, vfs, tcgetattr, local_fd, p);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int tcsetattr(int fd, int optional_actions, const struct termios *p)
|
|
|
|
{
|
|
|
|
const vfs_entry_t* vfs = get_vfs_for_fd(fd);
|
|
|
|
const int local_fd = get_local_fd(vfs, fd);
|
|
|
|
struct _reent* r = __getreent();
|
|
|
|
if (vfs == NULL || local_fd < 0) {
|
|
|
|
__errno_r(r) = EBADF;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
int ret;
|
|
|
|
CHECK_AND_CALL(ret, r, vfs, tcsetattr, local_fd, optional_actions, p);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int tcdrain(int fd)
|
|
|
|
{
|
|
|
|
const vfs_entry_t* vfs = get_vfs_for_fd(fd);
|
|
|
|
const int local_fd = get_local_fd(vfs, fd);
|
|
|
|
struct _reent* r = __getreent();
|
|
|
|
if (vfs == NULL || local_fd < 0) {
|
|
|
|
__errno_r(r) = EBADF;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
int ret;
|
|
|
|
CHECK_AND_CALL(ret, r, vfs, tcdrain, local_fd);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int tcflush(int fd, int select)
|
|
|
|
{
|
|
|
|
const vfs_entry_t* vfs = get_vfs_for_fd(fd);
|
|
|
|
const int local_fd = get_local_fd(vfs, fd);
|
|
|
|
struct _reent* r = __getreent();
|
|
|
|
if (vfs == NULL || local_fd < 0) {
|
|
|
|
__errno_r(r) = EBADF;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
int ret;
|
|
|
|
CHECK_AND_CALL(ret, r, vfs, tcflush, local_fd, select);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int tcflow(int fd, int action)
|
|
|
|
{
|
|
|
|
const vfs_entry_t* vfs = get_vfs_for_fd(fd);
|
|
|
|
const int local_fd = get_local_fd(vfs, fd);
|
|
|
|
struct _reent* r = __getreent();
|
|
|
|
if (vfs == NULL || local_fd < 0) {
|
|
|
|
__errno_r(r) = EBADF;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
int ret;
|
|
|
|
CHECK_AND_CALL(ret, r, vfs, tcflow, local_fd, action);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
pid_t tcgetsid(int fd)
|
|
|
|
{
|
|
|
|
const vfs_entry_t* vfs = get_vfs_for_fd(fd);
|
|
|
|
const int local_fd = get_local_fd(vfs, fd);
|
|
|
|
struct _reent* r = __getreent();
|
|
|
|
if (vfs == NULL || local_fd < 0) {
|
|
|
|
__errno_r(r) = EBADF;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
int ret;
|
|
|
|
CHECK_AND_CALL(ret, r, vfs, tcgetsid, local_fd);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int tcsendbreak(int fd, int duration)
|
|
|
|
{
|
|
|
|
const vfs_entry_t* vfs = get_vfs_for_fd(fd);
|
|
|
|
const int local_fd = get_local_fd(vfs, fd);
|
|
|
|
struct _reent* r = __getreent();
|
|
|
|
if (vfs == NULL || local_fd < 0) {
|
|
|
|
__errno_r(r) = EBADF;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
int ret;
|
|
|
|
CHECK_AND_CALL(ret, r, vfs, tcsendbreak, local_fd, duration);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
#endif // CONFIG_SUPPORT_TERMIOS
|
2018-10-25 05:53:52 -04:00
|
|
|
|
|
|
|
int esp_vfs_utime(const char *path, const struct utimbuf *times)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
const vfs_entry_t* vfs = get_vfs_for_path(path);
|
|
|
|
struct _reent* r = __getreent();
|
|
|
|
if (vfs == NULL) {
|
|
|
|
__errno_r(r) = ENOENT;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
const char* path_within_vfs = translate_path(vfs, path);
|
|
|
|
CHECK_AND_CALL(ret, r, vfs, utime, path_within_vfs, times);
|
|
|
|
return ret;
|
|
|
|
}
|
2019-02-19 07:18:40 -05:00
|
|
|
|
|
|
|
int esp_vfs_poll(struct pollfd *fds, nfds_t nfds, int timeout)
|
|
|
|
{
|
|
|
|
struct timeval tv = {
|
|
|
|
// timeout is in milliseconds
|
|
|
|
.tv_sec = timeout / 1000,
|
|
|
|
.tv_usec = (timeout % 1000) * 1000,
|
|
|
|
};
|
|
|
|
int max_fd = -1;
|
|
|
|
fd_set readfds;
|
|
|
|
fd_set writefds;
|
|
|
|
fd_set errorfds;
|
|
|
|
struct _reent* r = __getreent();
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
if (fds == NULL) {
|
|
|
|
__errno_r(r) = ENOENT;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
FD_ZERO(&readfds);
|
|
|
|
FD_ZERO(&writefds);
|
|
|
|
FD_ZERO(&errorfds);
|
|
|
|
|
|
|
|
for (int i = 0; i < nfds; ++i) {
|
|
|
|
fds[i].revents = 0;
|
|
|
|
|
|
|
|
if (fds[i].fd < 0) {
|
|
|
|
// revents should remain 0 and events ignored (according to the documentation of poll()).
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fds[i].fd >= MAX_FDS) {
|
|
|
|
fds[i].revents |= POLLNVAL;
|
|
|
|
++ret;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fds[i].events & (POLLIN | POLLRDNORM | POLLRDBAND | POLLPRI)) {
|
|
|
|
FD_SET(fds[i].fd, &readfds);
|
|
|
|
FD_SET(fds[i].fd, &errorfds);
|
|
|
|
max_fd = MAX(max_fd, fds[i].fd);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fds[i].events & (POLLOUT | POLLWRNORM | POLLWRBAND)) {
|
|
|
|
FD_SET(fds[i].fd, &writefds);
|
|
|
|
FD_SET(fds[i].fd, &errorfds);
|
|
|
|
max_fd = MAX(max_fd, fds[i].fd);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const int select_ret = esp_vfs_select(max_fd + 1, &readfds, &writefds, &errorfds, timeout < 0 ? NULL: &tv);
|
|
|
|
|
|
|
|
if (select_ret > 0) {
|
|
|
|
ret += select_ret;
|
|
|
|
|
|
|
|
for (int i = 0; i < nfds; ++i) {
|
|
|
|
if (FD_ISSET(fds[i].fd, &readfds)) {
|
|
|
|
fds[i].revents |= POLLIN;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (FD_ISSET(fds[i].fd, &writefds)) {
|
|
|
|
fds[i].revents |= POLLOUT;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (FD_ISSET(fds[i].fd, &errorfds)) {
|
|
|
|
fds[i].revents |= POLLERR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
ret = select_ret;
|
|
|
|
// keeping the errno from select()
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|