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
//
// Copyright 2018 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"
 
int
nni_file_put(const char *name, const void *data, size_t sz)
{
    return (nni_plat_file_put(name, data, sz));
}
 
int
nni_file_get(const char *name, void **datap, size_t *szp)
{
    return (nni_plat_file_get(name, datap, szp));
}
 
int
nni_file_delete(const char *name)
{
    return (nni_plat_file_delete(name));
}
 
bool
nni_file_is_file(const char *name)
{
    int ft;
    if ((nni_file_type(name, &ft) == 0) && (ft == NNI_FILE_TYPE_FILE)) {
        return (true);
    }
    return (false);
}
 
bool
nni_file_is_dir(const char *name)
{
    int ft;
    if ((nni_file_type(name, &ft) == 0) && (ft == NNI_FILE_TYPE_DIR)) {
        return (true);
    }
    return (false);
}
 
struct walkdata {
    nni_file_walker fn;
    void *          arg;
};
 
static int
plat_walker(const char *name, void *arg)
{
    struct walkdata *w = arg;
    int              rv;
 
    rv = w->fn(name, w->arg);
    switch (rv) {
    case NNI_FILE_WALK_CONTINUE:
        return (NNI_PLAT_FILE_WALK_CONTINUE);
    case NNI_FILE_WALK_STOP:
        return (NNI_PLAT_FILE_WALK_STOP);
    case NNI_FILE_WALK_PRUNE_CHILD:
        return (NNI_PLAT_FILE_WALK_PRUNE_CHILD);
    case NNI_FILE_WALK_PRUNE_SIB:
        return (NNI_PLAT_FILE_WALK_PRUNE_SIB);
    }
    // We treat any other value as a stop condition.  The program
    // is returning something invalid.
    return (NNI_PLAT_FILE_WALK_STOP);
}
 
int
nni_file_walk(const char *name, nni_file_walker walker, void *arg, int flags)
{
    struct walkdata w;
    int             wflags = 0;
 
    w.fn  = walker;
    w.arg = arg;
 
    if (flags & NNI_FILE_WALK_FILES_ONLY) {
        wflags |= NNI_PLAT_FILE_WALK_FILES_ONLY;
    }
    if (flags & NNI_FILE_WALK_SHALLOW) {
        wflags |= NNI_PLAT_FILE_WALK_SHALLOW;
    }
 
    return (nni_plat_file_walk(name, plat_walker, &w, wflags));
}
 
int
nni_file_type(const char *name, int *ftype)
{
    int rv;
    int t;
 
    if ((rv = nni_plat_file_type(name, &t)) != 0) {
        return (rv);
    }
 
    switch (t) {
    case NNI_PLAT_FILE_TYPE_FILE:
        *ftype = NNI_FILE_TYPE_FILE;
        break;
    case NNI_PLAT_FILE_TYPE_DIR:
        *ftype = NNI_FILE_TYPE_DIR;
        break;
    default:
        *ftype = NNI_FILE_TYPE_OTHER;
        break;
    }
    return (0);
}
 
char *
nni_file_join(const char *dir, const char *file)
{
    return (nni_plat_join_dir(dir, file));
}
 
const char *
nni_file_basename(const char *path)
{
    return (nni_plat_file_basename(path));
}
 
struct nni_file_lockh {
    nni_plat_flock lk;
};
 
int
nni_file_lock(const char *path, nni_file_lockh **hp)
{
    nni_file_lockh *h;
    int             rv;
    if ((h = NNI_ALLOC_STRUCT(h)) == NULL) {
        return (NNG_ENOMEM);
    }
    rv = nni_plat_file_lock(path, &h->lk);
    if (rv != 0) {
        NNI_FREE_STRUCT(h);
        return (rv);
    }
    *hp = h;
    return (0);
}
 
void
nni_file_unlock(nni_file_lockh *h)
{
    nni_plat_file_unlock(&h->lk);
    NNI_FREE_STRUCT(h);
}