Skip to content
Repos

DevPlaceApp

C++ 768 B17 lines Opens in site

Directory Raw Download

#pragma once
// Spec: shell.md section 5 (DIFF: UserDefaults/Keychain become files). Verify before write: content is built in
// memory, written to "<path>.tmp" with the requested mode, closed, then renamed over the target (atomic).
#include <optional>
#include <string>

namespace dp {

class AtomicFile {
 public:
  // false when the content exceeds maxBytes or any step fails (temp file removed, target untouched).
  static bool write(const std::string& path, const std::string& content, unsigned mode, std::size_t maxBytes);
  // nullopt when missing, unreadable or larger than maxBytes (sets *tooLarge when it was the size).
  static std::optional<std::string> read(const std::string& path, std::size_t maxBytes, bool* tooLarge = nullptr);
};

}  // namespace dp
33.8ms