diff options
author | Vladimir Ermakov <vooon341@gmail.com> | 2014-09-17 10:07:29 +0400 |
---|---|---|
committer | Vladimir Ermakov <vooon341@gmail.com> | 2014-09-17 10:07:29 +0400 |
commit | 60c63f48729f028ec9f5b6de93da91873d152bfd (patch) | |
tree | fdb267cb01c146b0aef45179eca10821857fdac9 /src/modules | |
parent | d8e0a22cbc59ed435519ad66a44b14b05ef9bbf9 (diff) | |
download | px4-firmware-60c63f48729f028ec9f5b6de93da91873d152bfd.tar.gz px4-firmware-60c63f48729f028ec9f5b6de93da91873d152bfd.tar.bz2 px4-firmware-60c63f48729f028ec9f5b6de93da91873d152bfd.zip |
FTP: Add new open command for write.
All open commands now return file size.
Diffstat (limited to 'src/modules')
-rw-r--r-- | src/modules/mavlink/mavlink_ftp.cpp | 38 | ||||
-rw-r--r-- | src/modules/mavlink/mavlink_ftp.h | 7 |
2 files changed, 23 insertions, 22 deletions
diff --git a/src/modules/mavlink/mavlink_ftp.cpp b/src/modules/mavlink/mavlink_ftp.cpp index 2a85c3702..b17036c8e 100644 --- a/src/modules/mavlink/mavlink_ftp.cpp +++ b/src/modules/mavlink/mavlink_ftp.cpp @@ -180,12 +180,16 @@ MavlinkFTP::_process_request(Request *req) errorCode = _workList(payload); break; - case kCmdOpenFile: - errorCode = _workOpen(payload, false); + case kCmdOpenFileRO: + errorCode = _workOpen(payload, O_RDONLY); break; case kCmdCreateFile: - errorCode = _workOpen(payload, true); + errorCode = _workOpen(payload, O_CREAT | O_EXCL | O_WRONLY); + break; + + case kCmdOpenFileWO: + errorCode = _workOpen(payload, O_CREAT | O_WRONLY); break; case kCmdReadFile: @@ -396,27 +400,27 @@ MavlinkFTP::_workList(PayloadHeader* payload) /// @brief Responds to an Open command MavlinkFTP::ErrorCode -MavlinkFTP::_workOpen(PayloadHeader* payload, bool create) +MavlinkFTP::_workOpen(PayloadHeader* payload, int oflag) { int session_index = _find_unused_session(); if (session_index < 0) { warnx("FTP: Open failed - out of sessions\n"); return kErrNoSessionsAvailable; } - + char *filename = _data_as_cstring(payload); - + uint32_t fileSize = 0; - if (!create) { - struct stat st; - if (stat(filename, &st) != 0) { + struct stat st; + if (stat(filename, &st) != 0) { + // fail only if requested open for read + if (oflag & O_RDONLY) return kErrFailErrno; - } - fileSize = st.st_size; + else + st.st_size = 0; } + fileSize = st.st_size; - int oflag = create ? (O_CREAT | O_EXCL | O_APPEND) : O_RDONLY; - int fd = ::open(filename, oflag); if (fd < 0) { return kErrFailErrno; @@ -424,12 +428,8 @@ MavlinkFTP::_workOpen(PayloadHeader* payload, bool create) _session_fds[session_index] = fd; payload->session = session_index; - if (create) { - payload->size = 0; - } else { - payload->size = sizeof(uint32_t); - *((uint32_t*)payload->data) = fileSize; - } + payload->size = sizeof(uint32_t); + *((uint32_t*)payload->data) = fileSize; return kErrNone; } diff --git a/src/modules/mavlink/mavlink_ftp.h b/src/modules/mavlink/mavlink_ftp.h index 657e2f855..1c873fe9f 100644 --- a/src/modules/mavlink/mavlink_ftp.h +++ b/src/modules/mavlink/mavlink_ftp.h @@ -89,13 +89,14 @@ public: kCmdTerminateSession, ///< Terminates open Read session kCmdResetSessions, ///< Terminates all open Read sessions kCmdListDirectory, ///< List files in <path> from <offset> - kCmdOpenFile, ///< Opens file at <path> for reading, returns <session> + kCmdOpenFileRO, ///< Opens file at <path> for reading, returns <session> kCmdReadFile, ///< Reads <size> bytes from <offset> in <session> kCmdCreateFile, ///< Creates file at <path> for writing, returns <session> - kCmdWriteFile, ///< Appends <size> bytes to file in <session> + kCmdWriteFile, ///< Writes <size> bytes to <offset> in <session> kCmdRemoveFile, ///< Remove file at <path> kCmdCreateDirectory, ///< Creates directory at <path> kCmdRemoveDirectory, ///< Removes Directory at <path>, must be empty + kCmdOpenFileWO, ///< Opens file at <path> for writing, returns <session> kRspAck = 128, ///< Ack response kRspNak ///< Nak response @@ -140,7 +141,7 @@ private: void _reply(Request *req); ErrorCode _workList(PayloadHeader *payload); - ErrorCode _workOpen(PayloadHeader *payload, bool create); + ErrorCode _workOpen(PayloadHeader *payload, int oflag); ErrorCode _workRead(PayloadHeader *payload); ErrorCode _workWrite(PayloadHeader *payload); ErrorCode _workTerminate(PayloadHeader *payload); |