Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Path: blob/master/libraries/AP_Filesystem/AP_Filesystem_ESP32.cpp
Views: 1798
/*1This program is free software: you can redistribute it and/or modify2it under the terms of the GNU General Public License as published by3the Free Software Foundation, either version 3 of the License, or4(at your option) any later version.5This program is distributed in the hope that it will be useful,6but WITHOUT ANY WARRANTY; without even the implied warranty of7MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the8GNU General Public License for more details.9You should have received a copy of the GNU General Public License10along with this program. If not, see <http://www.gnu.org/licenses/>.11*/1213/*14ArduPilot filesystem interface for esp32 systems15*/16#include "AP_Filesystem.h"17#include <AP_HAL/AP_HAL.h>1819#if AP_FILESYSTEM_ESP32_ENABLED2021#define FSDEBUG 02223#include <utime.h>2425extern const AP_HAL::HAL& hal;2627int AP_Filesystem_ESP32::open(const char *fname, int flags, bool allow_absolute_paths)28{29#if FSDEBUG30printf("DO open %s \n", fname);31#endif32// we automatically add O_CLOEXEC as we always want it for ArduPilot FS usage33return ::open(fname, flags | O_TRUNC | O_CLOEXEC, 0666);34}3536int AP_Filesystem_ESP32::close(int fd)37{38#if FSDEBUG39printf("DO close \n");40#endif41return ::close(fd);42}4344int32_t AP_Filesystem_ESP32::read(int fd, void *buf, uint32_t count)45{46#if FSDEBUG47printf("DO read \n");48#endif49return ::read(fd, buf, count);50}5152int32_t AP_Filesystem_ESP32::write(int fd, const void *buf, uint32_t count)53{54#if FSDEBUG55printf("DO write \n");56#endif57return ::write(fd, buf, count);58}5960int AP_Filesystem_ESP32::fsync(int fd)61{62#if FSDEBUG63printf("DO fsync \n");64#endif65return ::fsync(fd);66}6768int32_t AP_Filesystem_ESP32::lseek(int fd, int32_t offset, int seek_from)69{70#if FSDEBUG71printf("DO lseek \n");72#endif73return ::lseek(fd, offset, seek_from);74}7576int AP_Filesystem_ESP32::stat(const char *pathname, struct stat *stbuf)77{78#if FSDEBUG79printf("DO stat %s \n", pathname);80#endif81return ::stat(pathname, stbuf);82}8384int AP_Filesystem_ESP32::unlink(const char *pathname)85{86#if FSDEBUG87printf("DO unlink %s \n", pathname);88#endif89return ::unlink(pathname);90}9192int AP_Filesystem_ESP32::rename(const char *oldpath, const char *newpath)93{94#if FSDEBUG95printf("DO rename %s \n", oldpath, newpath);96#endif97return ::rename(oldpath, newpath);98}99100int AP_Filesystem_ESP32::mkdir(const char *pathname)101{102#if FSDEBUG103printf("DO mkdir %s \n", pathname);104#endif105return ::mkdir(pathname, 0777);106}107108void* AP_Filesystem_ESP32::opendir(const char *pathname)109{110#if FSDEBUG111printf("DO opendir %s \n", pathname);112#endif113114return (void*)::opendir(pathname);115// return NULL;116}117118struct dirent *AP_Filesystem_ESP32::readdir(void *dirp)119{120#if FSDEBUG121printf("DO readdir \n");122#endif123return ::readdir((DIR*)dirp);124// return NULL;125}126127int AP_Filesystem_ESP32::closedir(void *dirp)128{129#if FSDEBUG130printf("DO closedir \n");131#endif132133return ::closedir((DIR*)dirp);134// return 0;135}136137// return free disk space in bytes138int64_t AP_Filesystem_ESP32::disk_free(const char *path)139{140141#if FSDEBUG142printf("DO free disk %s \n", path);143#endif144FATFS *fs;145DWORD fre_clust, fre_sect;146147/* Get volume information and free clusters of sdcard */148auto res = f_getfree("/SDCARD/", &fre_clust, &fs);149if (res) {150return -1;151}152153/* Get total sectors and free sectors */154fre_sect = fre_clust * fs->csize;155156int64_t tmp_free_bytes = fre_sect * FF_SS_SDCARD;157158return tmp_free_bytes;159}160161// return total disk space in bytes162int64_t AP_Filesystem_ESP32::disk_space(const char *path)163{164#if FSDEBUG165printf("DO usage disk %s \n", path);166#endif167FATFS *fs;168DWORD fre_clust, tot_sect;169170/* Get volume information and free clusters of sdcard */171auto res = f_getfree("/SDCARD/", &fre_clust, &fs);172if (res) {173return -1;174}175176/* Get total sectors and free sectors */177tot_sect = (fs->n_fatent - 2) * fs->csize;178179int64_t tmp_total_bytes = tot_sect * FF_SS_SDCARD;180181return tmp_total_bytes;182}183184/*185set mtime on a file186*/187bool AP_Filesystem_ESP32::set_mtime(const char *filename, const uint32_t mtime_sec)188{189190#if FSDEBUG191printf("DO time %s \n", filename);192#endif193struct utimbuf times {};194times.actime = mtime_sec;195times.modtime = mtime_sec;196197return utime(filename, ×) == 0;198}199200#endif // AP_FILESYSTEM_ESP32_ENABLED201202203