summaryrefslogtreecommitdiff
path: root/filesystem-posix.c
blob: e2ff82ea734448ac838f06b53fa0b83c92b60613 (plain)
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
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <dirent.h>
#include <errno.h>

// Does this file exist? Returns false for directories.
static bool fs_file_exists(char const *path) {
	struct stat statbuf = {0};
	if (stat(path, &statbuf) != 0)
		return false;
	return S_ISREG(statbuf.st_mode);
}

// Returns a NULL-terminated array of the files/directories in this directory, or NULL if the directory does not exist.
// When you're done with the file names, call free on each one, then on the array.
// NOTE: The files aren't returned in any particular order!
static char **fs_list_directory(char const *dirname) {
	char **ret = NULL;
	DIR *dir = opendir(dirname);
	if (dir) {
		struct dirent *ent;
		char **filenames = NULL;
		size_t nentries = 0;
		size_t filename_idx = 0;

		while (readdir(dir)) ++nentries;
		rewinddir(dir);
		filenames = (char **)calloc(nentries+1, sizeof *filenames);

		while ((ent = readdir(dir))) {
			char const *filename = ent->d_name;
			size_t len = strlen(filename);
			char *filename_copy = (char *)malloc(len+1);
			if (!filename_copy) break;
			strcpy(filename_copy, filename);
			if (filename_idx < nentries) // this could actually fail if someone creates files between calculating nentries and here. 
				filenames[filename_idx++] = filename_copy;
		}
		ret = filenames;
		closedir(dir);
	}
	return ret;
}

static int fs_mkdir(char const *path) {
	if (mkdir(path, 0755) == 0) {
		// directory created successfully 
		return 1;
	} else if (errno == EEXIST) {
		struct stat statbuf = {0};
		if (stat(path, &statbuf) == 0) {
			if (S_ISDIR(statbuf.st_mode)) {
				// already exists, and it's a directory 
				return 0;
			} else {
				// already exists, but not a directory 
				return -1;
			}
		} else {
			return -1;
		}
	} else {
		return -1;
	}
}