zhangmeng
2024-04-22 16935f4aebffdd1b6580b844391a0aa0f4f3012b
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
//
// Copyright 2020 Staysail Systems, Inc. <info@staysail.tech>
// Copyright 2018 Capitar IT Group BV <info@capitar.com>
//
// This software is supplied under the terms of the MIT License, a
// copy of which should be located in the distribution where this
// file was obtained (LICENSE.txt).  A copy of the license may also be
// found online at https://opensource.org/licenses/MIT.
//
 
#include "core/nng_impl.h"
 
#ifdef NNG_PLATFORM_POSIX
 
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
 
// Some systems -- Android -- have BSD flock but not POSIX lockf.
#if defined(NNG_HAVE_FLOCK) && !defined(NNG_HAVE_LOCKF)
#include <sys/file.h>
#endif
 
// File support.
 
static int
nni_plat_make_parent_dirs(const char *path)
{
    char *dup;
    char *p;
    int   rv;
 
    // creates everything up until the last component.
    if ((dup = nni_strdup(path)) == NULL) {
        return (NNG_ENOMEM);
    }
    p = dup;
    while ((p = strchr(p, '/')) != NULL) {
        if (p != dup) {
            *p = '\0';
            rv = mkdir(dup, S_IRWXU);
            *p = '/';
            if ((rv != 0) && (errno != EEXIST)) {
                rv = nni_plat_errno(errno);
                nni_strfree(dup);
                return (rv);
            }
        }
 
        // collapse grouped "/" characters
        while (*p == '/') {
            p++;
        }
    }
    nni_strfree(dup);
    return (0);
}
 
// nni_plat_file_put writes the named file, with the provided data,
// and the given size.  If the file already exists it is overwritten.
// The permissions on the file should be limited to read and write
// access by the entity running the application only.
int
nni_plat_file_put(const char *name, const void *data, size_t len)
{
    FILE *f;
    int   rv = 0;
 
    // It is possible that the name contains a directory path
    // that does not exist.  In this case we try to create the
    // entire tree.
    if (strchr(name, '/') != NULL) {
        if ((rv = nni_plat_make_parent_dirs(name)) != 0) {
            return (rv);
        }
    }
 
    if ((f = fopen(name, "wb")) == NULL) {
        return (nni_plat_errno(errno));
    }
    if (fwrite(data, 1, len, f) != len) {
        rv = nni_plat_errno(errno);
        (void) unlink(name);
    }
    (void) fclose(f);
    return (rv);
}
 
// nni_plat_file_get reads the entire named file, allocating storage
// to receive the data and returning the data and the size in the
// reference arguments.
int
nni_plat_file_get(const char *name, void **datap, size_t *lenp)
{
    FILE *      f;
    struct stat st;
    int         rv = 0;
    size_t      len;
    void *      data;
 
    if ((f = fopen(name, "rb")) == NULL) {
        return (nni_plat_errno(errno));
    }
 
    if (stat(name, &st) != 0) {
        rv = nni_plat_errno(errno);
        (void) fclose(f);
        return (rv);
    }
 
    len = st.st_size;
    if (len > 0) {
        if ((data = nni_alloc(len)) == NULL) {
            rv = NNG_ENOMEM;
            goto done;
        }
        if (fread(data, 1, len, f) != len) {
            rv = nni_plat_errno(errno);
            nni_free(data, len);
            goto done;
        }
    } else {
        data = NULL;
    }
    *datap = data;
    *lenp  = len;
done:
    (void) fclose(f);
    return (rv);
}
 
// nni_plat_file_delete deletes the named file or directory.
int
nni_plat_file_delete(const char *name)
{
    if (rmdir(name) == 0) {
        return (0);
    }
    if ((errno == ENOTDIR) && (unlink(name) == 0)) {
        return (0);
    }
    if (errno == ENOENT) {
        return (0);
    }
    return (nni_plat_errno(errno));
}
 
int
nni_plat_file_type(const char *name, int *typep)
{
    struct stat sbuf;
 
    if (stat(name, &sbuf) != 0) {
        return (nni_plat_errno(errno));
    }
    switch (sbuf.st_mode & S_IFMT) {
    case S_IFREG:
        *typep = NNI_PLAT_FILE_TYPE_FILE;
        break;
    case S_IFDIR:
        *typep = NNI_PLAT_FILE_TYPE_DIR;
        break;
    default:
        *typep = NNI_PLAT_FILE_TYPE_OTHER;
        break;
    }
    return (0);
}
 
static int
nni_plat_file_walk_inner(const char *name, nni_plat_file_walker walkfn,
    void *arg, int flags, bool *stop)
{
    DIR *dir;
 
    if ((dir = opendir(name)) == NULL) {
        return (nni_plat_errno(errno));
    }
    for (;;) {
        int            rv;
        struct dirent *ent;
        struct stat    sbuf;
        char *         path;
        int            walkrv;
 
        if ((ent = readdir(dir)) == NULL) {
            closedir(dir);
            return (0);
        }
        // Skip "." and ".."  -- we would like to skip all
        // directories, but that would require checking full
        // paths.
        if ((strcmp(ent->d_name, ".") == 0) ||
            (strcmp(ent->d_name, "..") == 0)) {
            continue;
        }
        if ((rv = nni_asprintf(&path, "%s/%s", name, ent->d_name)) !=
            0) {
            closedir(dir);
            return (rv);
        }
        if (stat(path, &sbuf) != 0) {
            if (errno == ENOENT) { // deleted while walking
                continue;
            }
            rv = nni_plat_errno(errno);
            nni_strfree(path);
            closedir(dir);
            return (rv);
        }
        if (flags & NNI_PLAT_FILE_WALK_FILES_ONLY) {
            if ((sbuf.st_mode & S_IFMT) == S_IFREG) {
                walkrv = walkfn(path, arg);
            } else {
                walkrv = NNI_PLAT_FILE_WALK_CONTINUE;
            }
        } else {
            walkrv = walkfn(path, arg);
        }
 
        if (walkrv == NNI_PLAT_FILE_WALK_STOP) {
            *stop = true;
        }
 
        if ((!*stop) && (rv != NNI_PLAT_FILE_WALK_PRUNE_CHILD) &&
            ((flags & NNI_PLAT_FILE_WALK_SHALLOW) == 0) &&
            ((sbuf.st_mode & S_IFMT) == S_IFDIR)) {
            rv = nni_plat_file_walk_inner(
                path, walkfn, arg, flags, stop);
            if (rv != 0) {
                nni_strfree(path);
                closedir(dir);
                return (rv);
            }
        }
 
        nni_strfree(path);
 
        if ((walkrv == NNI_PLAT_FILE_WALK_PRUNE_SIB) || (*stop)) {
            break;
        }
    }
    closedir(dir);
    return (0);
}
 
int
nni_plat_file_walk(
    const char *name, nni_plat_file_walker walkfn, void *arg, int flags)
{
    bool stop = false;
 
    return (nni_plat_file_walk_inner(name, walkfn, arg, flags, &stop));
}
 
const char *
nni_plat_file_basename(const char *path)
{
    const char *end;
    if ((end = strrchr(path, '/')) != NULL) {
        return (end + 1);
    }
    return (path);
}
 
int
nni_plat_file_lock(const char *path, nni_plat_flock *lk)
{
    int fd;
    int rv;
    if ((fd = open(path, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR)) < 0) {
        return (nni_plat_errno(errno));
    }
#ifdef NNG_HAVE_LOCKF
    rv = lockf(fd, F_TLOCK, 0);
#elif defined NNG_HAVE_FLOCK
    rv = flock(fd, LOCK_EX | LOCK_NB);
#else
    // We don't have locking support.  This means you live dangerously.
    // For example, ZeroTier cannot be sure that nothing else is using
    // the same configuration file.  If you're here, its probably an
    // embedded scenario, and we can live with it.
    rv = 0;
#endif
    if (rv < 0) {
        int rv = errno;
        close(fd);
        if (rv == EAGAIN) {
            return (NNG_EBUSY);
        }
        return (nni_plat_errno(rv));
    }
    lk->fd = fd;
    return (0);
}
 
void
nni_plat_file_unlock(nni_plat_flock *lk)
{
    int fd = lk->fd;
    lk->fd = -1;
    (void) close(fd);
}
 
char *
nni_plat_temp_dir(void)
{
    char *temp;
 
    // POSIX says $TMPDIR is required.
    if ((temp = getenv("TMPDIR")) != NULL) {
        return (nni_strdup(temp));
    }
    return (nni_strdup("/tmp"));
}
 
char *
nni_plat_join_dir(const char *prefix, const char *suffix)
{
    char *result;
 
    if (nni_asprintf(&result, "%s/%s", prefix, suffix) == 0) {
        return (result);
    }
    return (NULL);
}
 
#endif // NNG_PLATFORM_POSIX