Cthulhu  0.2.10
Cthulhu compiler collection
fs.c
Go to the documentation of this file.
1 // SPDX-License-Identifier: LGPL-3.0-only
2 
3 #include "os/os.h"
4 #include "os_common.h"
5 
6 #include "base/panic.h"
7 #include "base/util.h"
8 
9 #include <sys/stat.h>
10 #include <unistd.h>
11 #include <stdio.h>
12 #include <errno.h>
13 #include <string.h>
14 
16 os_error_t os_file_delete(const char *path)
17 {
18  CTASSERT(path != NULL);
19 
20  if (remove(path) != 0)
21  {
22  return errno;
23  }
24 
25  return 0;
26 }
27 
29 os_error_t os_dir_create(const char *path)
30 {
31  CTASSERT(path != NULL);
32 
33  // TODO: this is a bit of a hack to avoid a bug in mkdir_recursive
34  // on linux it will pass an empty string into mkdir because of the leading `/`
35  if (ctu_strlen(path) == 0)
36  {
37  return eOsSuccess;
38  }
39 
40  if (mkdir(path, S_IRWXU | S_IRWXG | S_IRWXO) != 0)
41  {
42  if (errno == EEXIST)
43  {
44  return eOsExists;
45  }
46  }
47 
48  return errno;
49 }
50 
52 os_error_t os_dir_delete(const char *path)
53 {
54  CTASSERT(path != NULL);
55 
56  if (rmdir(path) != 0)
57  {
58  return errno;
59  }
60 
61  return errno;
62 }
63 
65 os_dirent_t os_dirent_type(const char *path)
66 {
67  CTASSERT(path != NULL);
68 
69  struct stat sb;
70  if (stat(path, &sb) != 0)
71  {
72  if (errno == ENOENT)
73  {
74  return eOsNodeNone;
75  }
76 
77  return eOsNodeError;
78  }
79 
80  if (sb.st_mode & S_IFDIR)
81  {
82  return eOsNodeDir;
83  }
84  if (sb.st_mode & S_IFREG)
85  {
86  return eOsNodeFile;
87  }
88 
89  return eOsNodeNone;
90 }
91 
93 size_t os_cwd_get_string(char *buffer, size_t size)
94 {
95  if (size == 0)
96  {
97  // caller is asking for the size of the buffer
98  CTASSERT(buffer == NULL);
99  return impl_maxname();
100  }
101 
102  CTASSERT(buffer != NULL);
103 
104  if (getcwd(buffer, size) == NULL)
105  {
106  return 0;
107  }
108 
109  return ctu_strlen(buffer);
110 }
CT_NODISCARD size_t size
Definition: scan.h:128
os_dirent_t
directory entry type
Definition: core.h:56
#define STA_DECL
sal2 annotation on function implementations to copy annotations from the declaration
CT_NODISCARD CT_PUREFN CT_BASE_API size_t ctu_strlen(const char *str)
get the length of a string not including the null terminator equivalent to strlen but with safety che...
Definition: util.c:87
#define CTASSERT(expr)
assert a condition, prints the condition as a message
Definition: panic.h:130
STA_DECL size_t os_cwd_get_string(char *buffer, size_t size)
Definition: fs.c:93
STA_DECL os_error_t os_dir_create(const char *path)
check if a directory exists
Definition: fs.c:29
STA_DECL os_dirent_t os_dirent_type(const char *path)
get the type of a paths inode entry
Definition: fs.c:65
STA_DECL os_error_t os_file_delete(const char *path)
delete a file
Definition: fs.c:16
STA_DECL os_error_t os_dir_delete(const char *path)
delete a directory
Definition: fs.c:52
CT_LOCAL size_t impl_maxname(void)
Definition: init.c:13
@ eOsExists
Definition: posix.h:26
@ eOsSuccess
Definition: posix.h:24