MCPcopy Create free account
hub / github.com/ArduPilot/ardupilot / attach_file

Method attach_file

libraries/StorageManager/StorageManager.cpp:344–400  ·  view source on GitHub ↗

attach a file to a storage region */

Source from the content-addressed store, hash-verified

342 attach a file to a storage region
343 */
344bool StorageAccess::attach_file(const char *filename, uint16_t size_kbyte)
345{
346 if (file != nullptr) {
347 // only one attach per boot
348 return false;
349 }
350 const uint32_t size = MIN(0xFFFFU, size_kbyte * 1024U);
351 auto *newfile = NEW_NOTHROW FileStorage;
352 if (newfile == nullptr) {
353 AP_BoardConfig::allocation_error("StorageFile");
354 }
355 ssize_t nread;
356
357 newfile->fd = AP::FS().open(filename, O_RDWR | O_CREAT);
358 if (newfile->fd == -1) {
359 goto fail;
360 }
361 newfile->buffer = NEW_NOTHROW uint8_t[size];
362 if (newfile->buffer == nullptr) {
363 AP_BoardConfig::allocation_error("StorageFile");
364 }
365 newfile->bufsize = size;
366 nread = AP::FS().read(newfile->fd, newfile->buffer, size);
367 if (nread == -1) {
368 goto fail;
369 }
370 if (nread == 0) {
371 // new file, copy storage from existing to allow users to
372 // start with existing mission
373 read_block(newfile->buffer, 0, total_size);
374 }
375 if (nread < int32_t(size)) {
376 if (AP::FS().write(newfile->fd, &newfile->buffer[nread], size-nread) != int32_t(size-nread)) {
377 goto fail;
378 }
379 if (AP::FS().fsync(newfile->fd) != 0) {
380 goto fail;
381 }
382 }
383
384 hal.scheduler->register_io_process(FUNCTOR_BIND_MEMBER(&StorageAccess::flush_file, void));
385
386 file = newfile;
387 total_size = newfile->bufsize;
388
389 return true;
390
391fail:
392 if (newfile->fd != -1) {
393 AP::FS().close(newfile->fd);
394 }
395 if (newfile->buffer != nullptr) {
396 delete[] newfile->buffer;
397 }
398 delete newfile;
399 return false;
400}
401

Callers 2

initMethod · 0.80
initMethod · 0.80

Calls 7

MINFunction · 0.85
openMethod · 0.45
readMethod · 0.45
writeMethod · 0.45
fsyncMethod · 0.45
register_io_processMethod · 0.45
closeMethod · 0.45

Tested by

no test coverage detected