libclod
C library for interacting with NBTs, region files, LOD data and other things.
Loading...
Searching...
No Matches
dir.c
1#include "../platform.h"
2#include "../../error.h"
3#include <unistd.h>
4#include <fcntl.h>
5#include <string.h>
6#include <errno.h>
7#include <stdio.h>
8
9enum clod_region_result dir_open(dir *d, const char *path, const struct clod_region_opts *opts) {
10 int fd;
11 if (opts->unix_fd)
12 fd = openat(opts->unix_fd, path && path[0] ? path : ".", O_DIRECTORY | O_RDONLY);
13 else
14 fd = open(path && path[0] ? path : ".", O_DIRECTORY | O_RDONLY);
15
16 if (fd < 0) {
17 return region_error(CLOD_REGION_INVALID_USAGE, "Opening directory: %s", strerror(errno));
18 }
19
20 *d = (dir)fd;
21 return CLOD_REGION_OK;
22}
23enum clod_region_result dir_rename(const dir d, const char *old_name, const char *new_name) {
24 if (renameat((int)(intptr_t)d, old_name, (int)(intptr_t)d, new_name)) {
25 return region_error(CLOD_REGION_INVALID_USAGE, "Renaming \"%s\" to \"%s\": %s",
26 old_name, new_name, strerror(errno));
27 }
28 return CLOD_REGION_OK;
29}
30enum clod_region_result dir_close(const dir d) {
31 if (close((int)(intptr_t)d)) {
32 return region_error(CLOD_REGION_INVALID_USAGE, "Closing directory: %s", strerror(errno));
33 }
34 return CLOD_REGION_OK;
35}
clod_region_result
Definition region.h:33
@ CLOD_REGION_INVALID_USAGE
Definition region.h:39
@ CLOD_REGION_OK
Definition region.h:35
Sized string helpers.