From 5526997d45410e1dda80eaeb613dca2de10afd8a Mon Sep 17 00:00:00 2001 From: Lucas De Marchi Date: Tue, 27 Mar 2018 23:42:42 -0700 Subject: [PATCH] AP_HAL_Linux: Storage: prefer custom storage If ardupilot was started with --storage-directory option, use that directory to save/load parameters. --- libraries/AP_HAL_Linux/Storage.cpp | 30 ++++++++++++++++++++++++------ 1 file changed, 24 insertions(+), 6 deletions(-) diff --git a/libraries/AP_HAL_Linux/Storage.cpp b/libraries/AP_HAL_Linux/Storage.cpp index 01eb20c750..7ac342c9d7 100644 --- a/libraries/AP_HAL_Linux/Storage.cpp +++ b/libraries/AP_HAL_Linux/Storage.cpp @@ -20,7 +20,7 @@ using namespace Linux; // name the storage file after the sketch so you can use the same board // card for ArduCopter and ArduPlane -#define STORAGE_FILE HAL_BOARD_STORAGE_DIRECTORY "/" SKETCHNAME ".stg" +#define STORAGE_FILE SKETCHNAME ".stg" extern const AP_HAL::HAL& hal; @@ -93,12 +93,30 @@ static int mkdir_p(const char *path, int len, mode_t mode) void Storage::_storage_create(void) { - mkdir_p(HAL_BOARD_STORAGE_DIRECTORY, strlen(HAL_BOARD_STORAGE_DIRECTORY), 0777); - unlink(STORAGE_FILE); - int fd = open(STORAGE_FILE, O_RDWR|O_CREAT|O_CLOEXEC, 0666); - if (fd == -1) { - AP_HAL::panic("Failed to create " STORAGE_FILE); + const char *dpath = HAL_BOARD_STORAGE_DIRECTORY, *p; + int dfd = -1; + + p = hal.util->get_custom_storage_directory(); + if (p) { + dpath = p; } + + mkdir_p(dpath, strlen(dpath), 0777); + dfd = open(dpath, O_RDWR|O_CLOEXEC); + if (dfd < 0) { + AP_HAL::panic("Error opening storage directory: %s\n", dpath); + } + + unlinkat(dfd, STORAGE_FILE, 0); + int fd = openat(dfd, STORAGE_FILE, O_RDWR|O_CREAT|O_CLOEXEC, 0666); + + close(dfd); + + if (fd == -1) { + AP_HAL::panic("Failed to create %s/%s", HAL_BOARD_STORAGE_DIRECTORY, + STORAGE_FILE); + } + for (uint16_t loc=0; loc