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_backend.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.56This program is distributed in the hope that it will be useful,7but WITHOUT ANY WARRANTY; without even the implied warranty of8MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the9GNU General Public License for more details.1011You should have received a copy of the GNU General Public License12along with this program. If not, see <http://www.gnu.org/licenses/>.13*/1415#include <AP_HAL/AP_HAL.h>16#include "AP_Filesystem.h"1718extern const AP_HAL::HAL& hal;1920/*21Load a file's contents into memory. Returned object must be `delete`d to free22the data. The data is guaranteed to be null-terminated such that it can be23treated as a string.24*/25FileData *AP_Filesystem_Backend::load_file(const char *filename)26{27struct stat st;28if (stat(filename, &st) != 0) {29return nullptr;30}31FileData *fd = NEW_NOTHROW FileData(this);32if (fd == nullptr) {33return nullptr;34}35// add one byte for null termination; ArduPilot's malloc will zero it.36void *data = malloc(st.st_size+1);37if (data == nullptr) {38delete fd;39return nullptr;40}41int d = open(filename, O_RDONLY);42if (d == -1) {43free(data);44delete fd;45return nullptr;46}47if (read(d, data, st.st_size) != st.st_size) {48close(d);49free(data);50delete fd;51return nullptr;52}53close(d);54fd->length = st.st_size; // length does not include our added termination55fd->data = (const uint8_t *)data;56return fd;57}5859/*60unload a FileData object61*/62void AP_Filesystem_Backend::unload_file(FileData *fd)63{64if (fd->data != nullptr) {65free(const_cast<uint8_t *>(fd->data));66fd->data = nullptr;67}68}6970// return true if file operations are allowed71bool AP_Filesystem_Backend::file_op_allowed(void) const72{73if (!hal.util->get_soft_armed() || !hal.scheduler->in_main_thread()) {74return true;75}76return false;77}7879/*80destructor for FileData81*/82FileData::~FileData()83{84if (backend != nullptr) {85((AP_Filesystem_Backend *)backend)->unload_file(this);86}87}88899091