|
153 | 153 | (defn unlink! [file-name] |
154 | 154 | (ignore (unlink file-name))) |
155 | 155 |
|
| 156 | + (private rename-) |
| 157 | + (hidden rename-) |
| 158 | + (register rename- (Fn [(Ptr CChar) (Ptr CChar)] Int) "rename") |
| 159 | + |
| 160 | + (doc rename |
| 161 | + "Renames a file or directory from `old-name` to `new-name`." |
| 162 | + "Returns an integer indicating success (0) or failure (-1)." |
| 163 | + "Wraps `rename` from the C standard library.") |
| 164 | + (defn rename [old-name new-name] |
| 165 | + (rename- (String.cstr old-name) (String.cstr new-name))) |
| 166 | + |
| 167 | + (doc rename! |
| 168 | + "Renames a file via [`rename`](#rename), but discards the result," |
| 169 | + "making this function appropriate for use as a side effect in" |
| 170 | + "`do` forms.") |
| 171 | + (defn rename! [old-name new-name] |
| 172 | + (ignore (rename old-name new-name))) |
| 173 | + |
156 | 174 | (doc fseek |
157 | 175 | "Sets the position indicator of a [FILE](#file) based on a given" |
158 | 176 | "reference position and offset. The position indicator will be set to an" |
|
204 | 222 | "given [FILE](#file)." |
205 | 223 | "Wraps `ferror` from the C standard library.") |
206 | 224 | (register ferror (Fn [(Ptr FILE)] Bool) "ferror") |
| 225 | + |
| 226 | + (doc list-dir |
| 227 | + "Returns an array containing the names of the entries in the directory" |
| 228 | + "designated by `path`, skipping `.` and `..`.") |
| 229 | + (register list-dir (Fn [&String] (Array String)) "IO_Raw_list_MINUS_dir") |
207 | 230 | ) |
208 | 231 |
|
| 232 | + (doc file-exists? "Checks if a file exists at the given path.") |
| 233 | + (register file-exists? (Fn [&String] Bool)) |
| 234 | + |
| 235 | + (doc dir-exists? "Checks if a directory exists at the given path.") |
| 236 | + (register dir-exists? (Fn [&String] Bool)) |
| 237 | + |
| 238 | + (doc list-dir |
| 239 | + "Returns an array containing the names of the entries in the directory" |
| 240 | + "designated by `path`, skipping `.` and `..`." |
| 241 | + "" |
| 242 | + "Returns a (Result (Array String) String) indicating success or failure.") |
| 243 | + (defn list-dir [path] |
| 244 | + (let [res (IO.Raw.list-dir path)] |
| 245 | + (if (Int.= (Array.length &res) -1) |
| 246 | + (Result.Error (fmt "Failed to list directory '%s': %s" path &(System.error-text))) |
| 247 | + (Result.Success res)))) |
| 248 | + |
209 | 249 | (doc println |
210 | 250 | "Prints a String ref to [`stdout`](#stdout), appends a newline.") |
211 | 251 | (register println (Fn [(Ref String)] ())) |
|
329 | 369 | (Result.Success true) |
330 | 370 | (Result.Error (fmt "only %d of %d bytes were written" bytes-written bytes2write))))))) |
331 | 371 |
|
| 372 | + (doc move-file |
| 373 | + "Moves or renames a file or directory from `old-name` to `new-name`." |
| 374 | + "" |
| 375 | + "Returns a (Result Bool String) indicating success or failure.") |
| 376 | + (defn move-file [old-name new-name] |
| 377 | + (if (Int.= (IO.Raw.rename old-name new-name) 0) |
| 378 | + (Result.Success true) |
| 379 | + (Result.Error (fmt "Failed to move '%s' to '%s': %s" old-name new-name &(System.error-text))))) |
| 380 | + |
| 381 | + (doc remove-file |
| 382 | + "Deletes a file from the filesystem." |
| 383 | + "" |
| 384 | + "Returns a (Result Bool String) indicating success or failure.") |
| 385 | + (defn remove-file [filename] |
| 386 | + (if (Int.= (IO.Raw.unlink filename) 0) |
| 387 | + (Result.Success true) |
| 388 | + (Result.Error (fmt "Failed to remove file '%s': %s" filename &(System.error-text))))) |
| 389 | + |
| 390 | + (doc copy-file |
| 391 | + "Copies a file from `src` to `dest`. Overwrites `dest` if it already exists." |
| 392 | + "" |
| 393 | + "Returns a (Result Bool String) indicating success or failure.") |
| 394 | + (defn copy-file [src dest] |
| 395 | + (Result.and-then (read-file src) |
| 396 | + &(fn [content] (write-file &content dest)))) |
| 397 | + |
332 | 398 | (private getenv-) |
333 | 399 | (hidden getenv-) |
334 | 400 | (doc getenv- "gets the value of an environment variable (thin wrapper for the C standard library)") |
|
0 commit comments