Teach MultiNodeLogNamer to write files with a temporary suffix

Change-Id: I8bdc9afc2b97bf95fc417bb8a047ed810253bc57
diff --git a/aos/events/logging/log_namer.cc b/aos/events/logging/log_namer.cc
index fbdc292..06243b0 100644
--- a/aos/events/logging/log_namer.cc
+++ b/aos/events/logging/log_namer.cc
@@ -69,14 +69,23 @@
 
 MultiNodeLogNamer::MultiNodeLogNamer(std::string_view base_name,
                                      const Configuration *configuration,
-                                     const Node *node)
+                                     const Node *node,
+                                     std::string_view temp_suffix)
     : LogNamer(node),
       base_name_(base_name),
+      temp_suffix_(temp_suffix),
       configuration_(configuration),
       uuid_(UUID::Random()) {
   OpenDataWriter();
 }
 
+MultiNodeLogNamer::~MultiNodeLogNamer() {
+  if (!ran_out_of_space_) {
+    // This handles renaming temporary files etc.
+    Close();
+  }
+}
+
 void MultiNodeLogNamer::WriteHeader(
     aos::SizePrefixedFlatbufferDetachedBuffer<LogFileHeader> *header,
     const Node *node) {
@@ -210,6 +219,8 @@
         ran_out_of_space_ = true;
         data_writer.second.writer->acknowledge_out_of_space();
       }
+      RenameTempFile(data_writer.second.writer.get());
+      data_writer.second.writer.reset();
     }
   }
   if (data_writer_) {
@@ -218,6 +229,8 @@
       ran_out_of_space_ = true;
       data_writer_->acknowledge_out_of_space();
     }
+    RenameTempFile(data_writer_.get());
+    data_writer_.reset();
   }
 }
 
@@ -256,7 +269,7 @@
     // means they're probably going to run out of space and get stuck too.
     return;
   }
-  const std::string filename = absl::StrCat(base_name_, path);
+  const std::string filename = absl::StrCat(base_name_, path, temp_suffix_);
   if (!destination->get()) {
     all_filenames_.emplace_back(path);
     *destination = std::make_unique<DetachedBufferWriter>(
@@ -268,10 +281,31 @@
     ran_out_of_space_ = true;
     return;
   }
+  RenameTempFile(destination->get());
   all_filenames_.emplace_back(path);
   *destination->get() =
       DetachedBufferWriter(filename, std::make_unique<DummyEncoder>());
 }
 
+void MultiNodeLogNamer::RenameTempFile(DetachedBufferWriter *destination) {
+  if (temp_suffix_.empty()) {
+    return;
+  }
+  const std::string current_filename = std::string(destination->filename());
+  CHECK(current_filename.size() > temp_suffix_.size());
+  const std::string final_filename =
+      current_filename.substr(0, current_filename.size() - temp_suffix_.size());
+  const int result = rename(current_filename.c_str(), final_filename.c_str());
+  if (result != 0) {
+    if (errno == ENOSPC) {
+      ran_out_of_space_ = true;
+      return;
+    } else {
+      PLOG(FATAL) << "Renaming " << current_filename << " to " << final_filename
+                  << " failed";
+    }
+  }
+}
+
 }  // namespace logger
 }  // namespace aos
diff --git a/aos/events/logging/log_namer.h b/aos/events/logging/log_namer.h
index 583e605..d9601ae 100644
--- a/aos/events/logging/log_namer.h
+++ b/aos/events/logging/log_namer.h
@@ -122,9 +122,17 @@
 // Log namer which uses a config and a base name to name a bunch of files.
 class MultiNodeLogNamer : public LogNamer {
  public:
+  // If temp_suffix is set, then this will write files under names beginning
+  // with the specified suffix, and then rename them to the desired name after
+  // they are fully written.
+  //
+  // This is useful to enable incremental copying of the log files.
+  //
+  // Defaults to writing directly to the final filename.
   MultiNodeLogNamer(std::string_view base_name,
-                    const Configuration *configuration, const Node *node);
-  ~MultiNodeLogNamer() override = default;
+                    const Configuration *configuration, const Node *node,
+                    std::string_view temp_suffix = "");
+  ~MultiNodeLogNamer() override;
 
   std::string_view base_name() const { return base_name_; }
 
@@ -204,7 +212,10 @@
   void CreateBufferWriter(std::string_view path,
                           std::unique_ptr<DetachedBufferWriter> *destination);
 
+  void RenameTempFile(DetachedBufferWriter *destination);
+
   const std::string base_name_;
+  const std::string temp_suffix_;
   const Configuration *const configuration_;
   const UUID uuid_;