aboutsummaryrefslogtreecommitdiff
path: root/src/fs.c
blob: a7f7888c36cbefe94fdc70a54fea9756f84866b0 (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
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
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <errno.h>
#include <limits.h>
#include <sys/stat.h>
#include <unistd.h>
#include <dirent.h>

#ifdef _WIN32
#include <windows.h>
#include <shlwapi.h>
#endif

#include "fs.h"

#ifdef _WIN32
#define mkdir(path, perm) mkdir(path)
#endif

static struct stat getStat(const char* path)
{
    // fill with 0s by default in the case stat fails
    struct stat sb = {0};

    // the return value signifies if stat failes (e.g. file not found)
    // unimportant for us if it fails it won't touch sb
    stat(path, &sb);

    return sb;
}

int isFile(const char* path)
{
    struct stat sb = getStat(path);

#ifndef _WIN32
    if (S_ISLNK(sb.st_mode))
    {
        char buf[PATH_MAX];
        readlink(path, buf, sizeof(buf));

        return isFile(buf);
    }
#endif
    return S_ISREG(sb.st_mode);
}

int isDir(const char* path)
{
    struct stat sb = getStat(path);

#ifndef _WIN32
    if (S_ISLNK(sb.st_mode))
    {
        char buf[PATH_MAX];
        readlink(path, buf, sizeof(buf));

        return isDir(buf);
    }
#endif
    return S_ISDIR(sb.st_mode);
}

int isRelativePath(const char* path)
{
    if (!path)
        return 0;
    else if (*path == *OS_PATH_SEP)
        return 0;
#ifdef _WIN32
    else if (!PathIsRelativeA(path))
        return 0;
#endif
    return 1;
}

int leavesRelativePath(const char* path)
{
    if (!path || !isRelativePath(path))
        return 0;

    int depth = 0;
    const char* head = path;
    const char* tail = head;

    while (*tail)
    {
        ++tail;
        if (*tail == *OS_PATH_SEP || *tail == '\0')
        {
            size_t size = (size_t)(tail-head);
            if (!size)
                continue;
            else if (!strncmp(head, "..", size))
                depth -= 1;
            else if (strncmp(head, ".", size))
                depth += 1;

            if (depth < 0)
                return 1;

            head = tail + 1;
        }
    }
    return 0;
}

char* normalizeUnixPath(char* path)
{
    char* head = path;
    if (head)
    {    
        while (*head)
        {
            if (*head == '/')
                *head = *OS_PATH_SEP;

            ++head;
        }
    }

    return path;
}

int makeDir(const char* path)
{
    char pathcpy[PATH_MAX];
    char *index;

    strncpy(pathcpy, path, PATH_MAX-1); // make a mutable copy of the path

    for(index = pathcpy+1; *index; ++index)
    {

        if (*index == *OS_PATH_SEP)
        {
            *index = '\0';

            if (mkdir(pathcpy, 0755) != 0)
            {
                if (errno != EEXIST)
                    return -1;
            }

            *index = *OS_PATH_SEP;
        }
    }

    return mkdir(path, 0755);
}

int removeDir(const char* path)
{
    DIR *d = opendir(path);
    size_t path_len = strlen(path);
    int r = -1;

    if (d) {
        struct dirent *p;

        r = 0;
        while (!r && (p = readdir(d))) {
            char *buf;
            size_t len;

            // Skip the names "." and ".." as we don't want to recurse on them.
            if (!strcmp(p->d_name, ".") || !strcmp(p->d_name, ".."))
                continue;

            len = path_len + strlen(p->d_name) + 2;
            buf = malloc(len);

            if (buf) {
                struct stat statbuf = {0};

                snprintf(buf, len, "%s/%s", path, p->d_name);
                if (!stat(buf, &statbuf)) {
                    if (S_ISDIR(statbuf.st_mode))
                        r = removeDir(buf);
#ifndef _WIN32
                    else if (S_ISLNK(statbuf.st_mode))
                        r = unlink(buf);
#endif
                    else
                        r = remove(buf);
                }
                else // it is very likely that we found a dangling symlink which is not detected by stat
                {
                    r = unlink(buf);
                }
                free(buf);
            }
        }
        closedir(d);
    }

    if (!r)
        r = rmdir(path);

    return r;
}