1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
//! Copyright © 2025-2026 Wenze Wei. All Rights Reserved.
//!
//! This file is part of Ri.
//! The Ri project belongs to the Dunimd Team.
//!
//! Licensed under the Apache License, Version 2.0 (the "License");
//! You may not use this file except in compliance with the License.
//! You may obtain a copy of the License at
//!
//! http://www.apache.org/licenses/LICENSE-2.0
//!
//! Unless required by applicable law or agreed to in writing, software
//! distributed under the License is distributed on an "AS IS" BASIS,
//! WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//! See the License for the specific language governing permissions and
//! limitations under the License.
//! # File System Module C API
//!
//! This module provides C language bindings for Ri's file system abstraction layer. The file
//! system module delivers cross-platform file and directory operations with unified interfaces
//! across different operating systems. This C API enables C/C++ applications to leverage Ri's
//! file handling capabilities including path manipulation, file I/O operations, directory
//! traversal, symbolic link management, and file metadata operations.
//!
//! ## Module Architecture
//!
//! The file system module provides a single primary component:
//!
//! - **RiFileSystem**: Unified file system abstraction providing portable file and directory
//! operations. The abstraction layer normalizes platform differences while preserving access
//! to platform-specific features when needed. The file system object provides methods for
//! reading, writing, creating, deleting, and managing files and directories across Windows,
//! Linux, and macOS platforms.
//!
//! ## Cross-Platform Design
//!
//! The file system module implements comprehensive cross-platform compatibility:
//!
//! - **Path Representation**: Uses abstract path representation that normalizes platform-specific
//! path separators, conventions, and edge cases. Supports both Windows-style paths (C:\)
//! and Unix-style paths (/). Path operations handle relative and absolute paths uniformly.
//!
//! - **File Operations**: Provides consistent file I/O semantics across platforms including
//! atomic file operations, proper handling of file locks, and consistent error semantics.
//! Supports both blocking and asynchronous file operations through the Tokio integration.
//!
//! - **Directory Operations**: Cross-platform directory traversal, creation, and management.
//! Handles differences in directory structures, permissions, and special directories
//! across operating systems.
//!
//! - **Symbolic Links**: Proper handling of symbolic links on platforms that support them.
//! Detects link loops, resolves link targets, and provides control over link resolution.
//!
//! - **Metadata Access**: Uniform interface to file metadata including size, timestamps,
//! permissions, and file type information. Handles platform-specific metadata differences.
//!
//! ## Supported Operations
//!
//! The file system module provides comprehensive file and directory operations:
//!
//! - **Path Operations**: Join paths, normalize paths, resolve relative paths to absolute,
//! extract components (filename, extension, parent directory), and check path properties.
//!
//! - **File I/O**: Open files for reading, writing, or appending with various sharing modes.
//! Read and write operations with configurable buffering. Support for memory-mapped files
//! for large file operations.
//!
//! - **Directory Operations**: Create directories (including nested paths), list directory
//! contents, iterate directories recursively, remove directories (with or without contents).
//!
//! - **File Management**: Copy files (with optional overwrite), move/rename files, delete
//! files, check file existence, and create temporary files.
//!
//! - **Metadata Operations**: Get file size, access/modification/creation times, file
//! permissions, and file type (regular file, directory, symlink, etc.).
//!
//! - **Permission Management**: Get and set file permissions (Unix mode bits, Windows ACLs).
//! Handle permission propagation and default permissions for new files.
//!
//! ## File Operations Modes
//!
//! Files can be opened with various modes controlling access and behavior:
//!
//! - **Read Mode**: Open file for reading only. Multiple readers allowed concurrently.
//!
//! - **Write Mode**: Open file for writing. Truncates existing file by default.
//! Exclusive access for writing.
//!
//! - **Append Mode**: Open file for writing at end only. Multiple appenders allowed.
//! Preserves existing content.
//!
//! - **Create Mode**: Create file if it doesn't exist. Fail if file exists.
//!
//! - **Truncate Mode**: Truncate file to zero length when opened.
//!
//! - **Binary Mode**: Open file for binary data (no text translation).
//!
//! - **Text Mode**: Open file for text data with platform-specific line ending handling.
//!
//! ## Performance Characteristics
//!
//! File operations have the following performance profiles:
//!
//! - File open: O(1) typically, O(log n) for deep directory traversal
//! - Sequential read: O(n) where n is bytes read, optimized by OS caching
//! - Random read: O(1) per read operation, may cause disk seeks
//! - Directory listing: O(n) where n is directory entry count
//! - Metadata queries: O(1) for cached metadata, O(log n) otherwise
//!
//! ## Asynchronous Operations
//!
//! The file system module supports asynchronous operations through Tokio integration:
//!
//! - Async file I/O for high-concurrency scenarios
//! - Non-blocking directory iteration
//! - Cancellation support for long-running operations
//! - Proper integration with async/await patterns
//!
//! ## Memory Management
//!
//! All C API objects use opaque pointers with manual memory management:
//!
//! - Constructor functions allocate new instances on the heap
//! - Destructor functions must be called to release memory
//! - File handles must be properly closed after use
//! - Path strings must be freed after use
//!
//! ## Thread Safety
//!
//! The underlying implementations provide:
//!
//! - File handles are not thread-safe (use synchronization for concurrent access)
//! - File system instance is thread-safe for metadata queries
//! - Path resolution operations are thread-safe
//! - Consider using separate handles for concurrent file operations
//!
//! ## Error Handling
//!
//! File operations return error codes with optional messages:
//!
//! - Error codes follow standard POSIX conventions where possible
//! - Platform-specific errors are mapped to portable error codes
//! - Detailed error messages available for debugging
//! - Permission errors distinguished from other access errors
//!
//! ## Usage Example
//!
//! ```c
//! // Create file system instance with automatic root detection
//! RiFileSystem* fs = ri_fs_new_auto();
//! if (fs == NULL) {
//! fprintf(stderr, "Failed to create file system\n");
//! return ERROR_FILESYSTEM;
//! }
//!
//! // Read file contents
//! char* content = NULL;
//! size_t size = 0;
//! int result = ri_fs_read_file(fs, "/path/to/file.txt", &content, &size);
//!
//! if (result == 0 && content != NULL) {
//! printf("Read %zu bytes: %.*s\n", size, (int)size, content);
//! ri_fs_string_free(content);
//! }
//!
//! // Write to file
//! const char* data = "Hello, World!";
//! result = ri_fs_write_file(fs, "/path/to/output.txt", data, strlen(data));
//!
//! if (result != 0) {
//! fprintf(stderr, "Failed to write file: %s\n", ri_fs_last_error(fs));
//! }
//!
//! // List directory contents
//! char** entries = NULL;
//! size_t entry_count = 0;
//! result = ri_fs_list_dir(fs, "/path/to/directory", &entries, &entry_count);
//!
//! if (result == 0) {
//! for (size_t i = 0; i < entry_count; i++) {
//! printf(" %s\n", entries[i]);
//! ri_fs_string_free(entries[i]);
//! }
//! free(entries);
//! }
//!
//! // Cleanup
//! ri_fs_free(fs);
//! ```
//!
//! ## Dependencies
//!
//! This module depends on the following Ri components:
//!
//! - `crate::fs`: Rust file system module implementation
//! - `crate::prelude`: Common types and traits
//!
//! ## Feature Flags
//!
//! The file system module is always enabled as it provides fundamental infrastructure
//! for file operations in Ri applications.
use crateRiFileSystem;
c_wrapper!;
/// Creates a new RiFileSystem instance with automatic root directory detection.
///
/// Initializes a file system abstraction with automatic detection of the root directory
/// and appropriate platform configuration. The created instance can perform all
/// supported file operations on the local file system.
///
/// # Returns
///
/// Pointer to newly allocated RiFileSystem on success, or NULL if:
/// - Memory allocation fails
/// - Root directory detection fails
/// - Platform initialization fails
///
/// # Automatic Detection
///
/// The function detects and configures:
///
/// - **Root Directory**: System root (/ on Unix, C:\ on Windows)
/// - **Current Directory**: Process current working directory
/// - **Temporary Directory**: Platform temp directory location
/// - **Home Directory**: User home directory
/// - **Path Separators**: Configured based on platform
/// - **Case Sensitivity**: Determined by underlying file system
///
/// # Initial Capabilities
///
/// A newly created file system instance can:
///
/// - Perform all path operations
/// - Access files in any accessible location
/// - Create and manipulate files and directories
/// - Query file metadata
///
/// # Usage Pattern
///
/// ```c
/// RiFileSystem* fs = ri_fs_new_auto();
/// if (fs == NULL) {
/// fprintf(stderr, "File system initialization failed\n");
/// return ERROR_INIT;
/// }
///
/// // Use file system operations...
///
/// ri_fs_free(fs);
/// ```
///
/// # Platform-Specific Notes
///
/// - **Windows**: Uses backslash path separator, supports UNC paths
/// - **Linux/macOS**: Uses forward slash path separator, case-sensitive
/// - **All Platforms**: Supports both relative and absolute paths
pub extern "C"
c_destructor!;