NAH 1.0.6
Native Application Host - Library API Reference
Loading...
Searching...
No Matches
platform.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <optional>
4#include <string>
5#include <vector>
6#include <cstdint>
7#include <unordered_map>
8
9namespace nah {
10
11// ============================================================================
12// Platform Detection
13// ============================================================================
14
15enum class Platform {
16 Linux,
17 macOS,
18 Windows,
20};
21
23
24// ============================================================================
25// Binary Section Reading (per SPEC L1599-L1627)
26// ============================================================================
27
28// Read the NAH manifest section from a binary file
29// Returns the raw bytes of the manifest section, or empty on failure
31 bool ok = false;
32 std::string error;
33 std::vector<uint8_t> data;
34};
35
36// Read the NAH manifest section from a binary file
37// Platform-specific:
38// - macOS: __NAH,__manifest section in Mach-O
39// - Linux: .nah_manifest section in ELF
40// - Windows: .nah section in PE/COFF
42
43// Read the NAH manifest section from binary data in memory
45
46// ============================================================================
47// Atomic File Operations (per SPEC L569-L577)
48// ============================================================================
49
51 bool ok = false;
52 std::string error;
53};
54
55// Write content atomically using temp file + fsync + rename + fsync(dir)
56AtomicWriteResult atomic_write_file(const std::string& path, const std::string& content);
57AtomicWriteResult atomic_write_file(const std::string& path, const std::vector<uint8_t>& content);
58
59// Create a directory atomically (mkdir with fsync on parent)
61
62// Update symlink atomically (remove + create with fsync on parent)
63AtomicWriteResult atomic_update_symlink(const std::string& link_path, const std::string& target);
64
65// ============================================================================
66// Path Utilities
67// ============================================================================
68
69// Convert a path to use forward slashes (portable format)
70// NAH uses forward slashes internally for cross-platform consistency
71// in tar archives, manifests, and all stored paths.
72std::string to_portable_path(const std::string& path);
73
74// Get the directory containing a file path
75std::string get_parent_directory(const std::string& path);
76
77// Get the filename from a path
78std::string get_filename(const std::string& path);
79
80// Join path components
81std::string join_path(const std::string& base, const std::string& rel);
82
83// Check if a path exists
84bool path_exists(const std::string& path);
85
86// Check if a path is a directory
87bool is_directory(const std::string& path);
88
89// Check if a path is a regular file
90bool is_regular_file(const std::string& path);
91
92// Check if a path is a symlink
93bool is_symlink(const std::string& path);
94
95// Read symlink target
96std::optional<std::string> read_symlink(const std::string& path);
97
98// List directory entries
99std::vector<std::string> list_directory(const std::string& path);
100
101// Create parent directories recursively
102bool create_directories(const std::string& path);
103
104// Remove a directory recursively
105bool remove_directory(const std::string& path);
106
107// Remove a file
108bool remove_file(const std::string& path);
109
110// Copy a file
111bool copy_file(const std::string& src, const std::string& dst);
112
113// ============================================================================
114// Environment
115// ============================================================================
116
117// Get an environment variable
118std::optional<std::string> get_env(const std::string& name);
119
120// Get all environment variables as a map
121std::unordered_map<std::string, std::string> get_all_env();
122
123// Get current timestamp as RFC3339 string
125
126// Generate a UUID string
127std::string generate_uuid();
128
129} // namespace nah
Result type for fallible operations.
Definition nahhost.hpp:109
std::string to_portable_path(const std::string &path)
AtomicWriteResult atomic_create_directory(const std::string &path)
std::optional< std::string > get_env(const std::string &name)
bool remove_directory(const std::string &path)
std::unordered_map< std::string, std::string > get_all_env()
std::string get_parent_directory(const std::string &path)
bool copy_file(const std::string &src, const std::string &dst)
std::vector< std::string > list_directory(const std::string &path)
bool remove_file(const std::string &path)
AtomicWriteResult atomic_update_symlink(const std::string &link_path, const std::string &target)
bool create_directories(const std::string &path)
std::optional< std::string > read_symlink(const std::string &path)
std::string get_filename(const std::string &path)
std::string get_current_timestamp()
std::string join_path(const std::string &base, const std::string &rel)
bool is_regular_file(const std::string &path)
Platform
Definition platform.hpp:15
bool is_directory(const std::string &path)
AtomicWriteResult atomic_write_file(const std::string &path, const std::string &content)
SectionReadResult read_manifest_section(const std::string &binary_path)
std::string generate_uuid()
bool is_symlink(const std::string &path)
Platform get_current_platform()
bool path_exists(const std::string &path)
std::vector< uint8_t > data
Definition platform.hpp:33