Implement VFS support for access()

Closes https://github.com/espressif/esp-idf/issues/1085
This commit is contained in:
Roland Dobai 2018-05-04 11:44:38 +02:00
parent adc3315677
commit 4345e198ce
5 changed files with 202 additions and 2 deletions

View file

@ -84,6 +84,7 @@ static void vfs_fat_seekdir(void* ctx, DIR* pdir, long offset);
static int vfs_fat_closedir(void* ctx, DIR* pdir);
static int vfs_fat_mkdir(void* ctx, const char* name, mode_t mode);
static int vfs_fat_rmdir(void* ctx, const char* name);
static int vfs_fat_access(void* ctx, const char *path, int amode);
static vfs_fat_ctx_t* s_fat_ctxs[FF_VOLUMES] = { NULL, NULL };
//backwards-compatibility with esp_vfs_fat_unregister()
@ -141,7 +142,8 @@ esp_err_t esp_vfs_fat_register(const char* base_path, const char* fat_drive, siz
.seekdir_p = &vfs_fat_seekdir,
.telldir_p = &vfs_fat_telldir,
.mkdir_p = &vfs_fat_mkdir,
.rmdir_p = &vfs_fat_rmdir
.rmdir_p = &vfs_fat_rmdir,
.access_p = &vfs_fat_access,
};
size_t ctx_size = sizeof(vfs_fat_ctx_t) + max_files * sizeof(FIL);
vfs_fat_ctx_t* fat_ctx = (vfs_fat_ctx_t*) calloc(1, ctx_size);
@ -720,3 +722,31 @@ static int vfs_fat_rmdir(void* ctx, const char* name)
}
return 0;
}
static int vfs_fat_access(void* ctx, const char *path, int amode)
{
FILINFO info;
int ret = 0;
FRESULT res;
vfs_fat_ctx_t* fat_ctx = (vfs_fat_ctx_t*) ctx;
_lock_acquire(&fat_ctx->lock);
prepend_drive_to_path(fat_ctx, &path, NULL);
res = f_stat(path, &info);
_lock_release(&fat_ctx->lock);
if (res == FR_OK) {
if (((amode & W_OK) == W_OK) && ((info.fattrib & AM_RDO) == AM_RDO)) {
ret = -1;
errno = EACCES;
}
// There is no flag to test readable or executable: we assume that if
// it exists then it is readable and executable
} else {
ret = -1;
errno = ENOENT;
}
return ret;
}

View file

@ -18,6 +18,9 @@
#include <stdint.h>
#include <stddef.h>
#include <stdarg.h>
#include <unistd.h>
#include "freertos/FreeRTOS.h"
#include "freertos/semphr.h"
#include "esp_err.h"
#include <sys/types.h>
#include <sys/reent.h>
@ -160,6 +163,10 @@ typedef struct
int (*fsync_p)(void* ctx, int fd);
int (*fsync)(int fd);
};
union {
int (*access_p)(void* ctx, const char *path, int amode);
int (*access)(const char *path, int amode);
};
} esp_vfs_t;

View file

@ -0,0 +1,127 @@
// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
//
// 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 <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
#include "unity.h"
#include "driver/uart.h"
#include "esp_vfs.h"
#include "esp_vfs_dev.h"
#include "esp_vfs_fat.h"
#include "wear_levelling.h"
static wl_handle_t test_wl_handle;
TEST_CASE("Can use access() for UART", "[vfs]")
{
const char *uarts[] = {"/dev/uart/0", "/dev/uart/1", "/dev/uart/2"};
uart_driver_install(UART_NUM_0, 256, 0, 0, NULL, 0);
uart_driver_install(UART_NUM_1, 256, 0, 0, NULL, 0);
uart_driver_install(UART_NUM_2, 256, 0, 0, NULL, 0);
for (int i = 0; i < sizeof(uarts)/sizeof(uarts[0]); ++i) {
TEST_ASSERT_EQUAL_MESSAGE(access(uarts[i], F_OK), 0, uarts[i]);
TEST_ASSERT_EQUAL_MESSAGE(access(uarts[i], R_OK), 0, uarts[i]);
TEST_ASSERT_EQUAL_MESSAGE(access(uarts[i], W_OK), 0, uarts[i]);
TEST_ASSERT_EQUAL_MESSAGE(access(uarts[i], X_OK), -1, uarts[i]);
TEST_ASSERT_EQUAL_MESSAGE(errno, EACCES, uarts[i]);
TEST_ASSERT_EQUAL_MESSAGE(access(uarts[i], R_OK | W_OK), 0, uarts[i]);
TEST_ASSERT_EQUAL_MESSAGE(access(uarts[i], R_OK | X_OK), -1, uarts[i]);
TEST_ASSERT_EQUAL_MESSAGE(errno, EACCES, uarts[i]);
TEST_ASSERT_EQUAL_MESSAGE(access(uarts[i], W_OK | X_OK), -1, uarts[i]);
TEST_ASSERT_EQUAL_MESSAGE(errno, EACCES, uarts[i]);
TEST_ASSERT_EQUAL_MESSAGE(access(uarts[i], R_OK | W_OK | X_OK), -1, uarts[i]);
TEST_ASSERT_EQUAL_MESSAGE(errno, EACCES, uarts[i]);
}
TEST_ASSERT_EQUAL(access("/dev/uart/3", F_OK), -1);
TEST_ASSERT_EQUAL(errno, ENOENT);
uart_driver_delete(UART_NUM_0);
uart_driver_delete(UART_NUM_1);
uart_driver_delete(UART_NUM_2);
}
static inline void test_spi_flash_setup()
{
esp_vfs_fat_sdmmc_mount_config_t mount_config = {
.format_if_mount_failed = true,
.max_files = 5
};
TEST_ESP_OK(esp_vfs_fat_spiflash_mount("/spiflash", NULL, &mount_config, &test_wl_handle));
}
static inline void test_spi_flash_teardown()
{
TEST_ESP_OK(esp_vfs_fat_spiflash_unmount("/spiflash", test_wl_handle));
}
static inline void test_fatfs_create_file(const char *name)
{
int fd = open(name, O_WRONLY | O_CREAT | O_TRUNC);
TEST_ASSERT_NOT_EQUAL(fd, -1);
TEST_ASSERT_EQUAL(0, close(fd));
}
static inline void test_fatfs_delete_file(const char *name)
{
int ret = unlink(name);
TEST_ASSERT_EQUAL(ret, 0);
}
TEST_CASE("Can use access() for FATFS", "[vfs][fatfs][wear_levelling]")
{
const char *path = "/spiflash/access.txt";
test_spi_flash_setup();
{
int ret = access(path, F_OK);
if (ret != -1) {
// it wasn't deleted before so we delete it now to pass the test
// case the next time
test_fatfs_delete_file(path);
}
TEST_ASSERT_EQUAL(ret, -1);
TEST_ASSERT_EQUAL(errno, ENOENT);
}
test_fatfs_create_file(path);
TEST_ASSERT_EQUAL(access(path, F_OK), 0);
TEST_ASSERT_EQUAL(access(path, R_OK), 0);
TEST_ASSERT_EQUAL(access(path, W_OK), 0);
TEST_ASSERT_EQUAL(access(path, X_OK), 0);
TEST_ASSERT_EQUAL(access(path, R_OK | W_OK), 0);
TEST_ASSERT_EQUAL(access(path, R_OK | X_OK), 0);
TEST_ASSERT_EQUAL(access(path, W_OK | X_OK), 0);
TEST_ASSERT_EQUAL(access(path, R_OK | W_OK | X_OK), 0);
//TODO f_chmod the file and re-test the access rights (this requires
// f_chmod support to be implemented in VFS)
test_fatfs_delete_file(path);
TEST_ASSERT_EQUAL(access(path, F_OK), -1);
TEST_ASSERT_EQUAL(errno, ENOENT);
test_spi_flash_teardown();
}

View file

@ -623,3 +623,17 @@ int fsync(int fd)
CHECK_AND_CALL(ret, r, vfs, fsync, local_fd);
return ret;
}
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;
}

View file

@ -267,6 +267,27 @@ static int uart_fcntl(int fd, int cmd, va_list args)
return result;
}
static int uart_access(const char *path, int amode)
{
int ret = -1;
if (strcmp(path, "/0") == 0 || strcmp(path, "/1") == 0 || strcmp(path, "/2") == 0) {
if (F_OK == amode) {
ret = 0; //path exists
} else {
if ((((amode & R_OK) == R_OK) || ((amode & W_OK) == W_OK)) && ((amode & X_OK) != X_OK)) {
ret = 0; //path is readable and/or writable but not executable
} else {
errno = EACCES;
}
}
} else {
errno = ENOENT;
}
return ret;
}
void esp_vfs_dev_uart_register()
{
esp_vfs_t vfs = {
@ -276,7 +297,8 @@ void esp_vfs_dev_uart_register()
.fstat = &uart_fstat,
.close = &uart_close,
.read = &uart_read,
.fcntl = &uart_fcntl
.fcntl = &uart_fcntl,
.access = &uart_access,
};
ESP_ERROR_CHECK(esp_vfs_register("/dev/uart", &vfs, NULL));
}