forked from joan2937/lg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lgFile.c
317 lines (250 loc) · 6.6 KB
/
lgFile.c
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
/*
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.
In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
For more information, please refer to <http://unlicense.org/>
*/
#include <stdlib.h>
#include <glob.h>
#include <string.h>
#include <fnmatch.h>
#include <ctype.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include "lgpio.h"
#include "rgpiod.h"
#include "lgDbg.h"
#include "lgHdl.h"
typedef struct
{
int16_t fd;
uint32_t mode;
} lgFileObj_t, *lgFileObj_p;
static void _fileClose(lgFileObj_p file)
{
if (file) close(file->fd);
}
int lgFileOpen(char *file, int mode)
{
int fd=-1;
int handle, oflag, omode;
struct stat statbuf;
lgFileObj_p h;
LG_DBG(LG_DEBUG_TRACE, "file=%s mode=%d", file, mode);
if ( (mode < LG_FILE_MIN) ||
(mode > LG_FILE_MAX) ||
((mode & LG_FILE_RW) == 0) )
PARAM_ERROR(LG_BAD_FILE_MODE, "bad mode (%d)", mode);
if ((mode > 3) && ((mode & LG_FILE_WRITE) == 0))
PARAM_ERROR(LG_NO_FILE_ACCESS, "no permission to write file (%s)", file);
omode = 0;
oflag = 0;
if (mode & LG_FILE_APPEND)
{
oflag |= O_APPEND;
}
if (mode & LG_FILE_CREATE)
{
oflag |= O_CREAT;
omode |= (S_IRUSR|S_IWUSR);
}
if (mode & LG_FILE_TRUNC)
{
oflag |= O_TRUNC;
}
switch(mode&LG_FILE_RW)
{
case LG_FILE_READ:
fd = open(file, O_RDONLY|oflag, omode);
break;
case LG_FILE_WRITE:
fd = open(file, O_WRONLY|oflag, omode);
break;
case LG_FILE_RW:
fd = open(file, O_RDWR|oflag, omode);
break;
}
if (fd == -1)
{
return LG_FILE_OPEN_FAILED;
}
else
{
if (stat(file, &statbuf) == 0)
{
if (S_ISDIR(statbuf.st_mode))
{
close(fd);
PARAM_ERROR(LG_FILE_IS_A_DIR, "file is a directory (%s)", file);
}
}
}
handle = lgHdlAlloc(
LG_HDL_TYPE_FILE, sizeof(lgFileObj_t), (void**)&h, _fileClose);
if (handle < 0)
{
close(fd);
return LG_NO_MEMORY;
}
h->fd = fd;
h->mode = mode;
return handle;
}
int lgFileClose(int handle)
{
int status;
lgFileObj_p h;
LG_DBG(LG_DEBUG_TRACE, "handle=%d", handle);
status = lgHdlGetLockedObj(handle, LG_HDL_TYPE_FILE, (void **)&h);
if (status == LG_OKAY)
{
status = lgHdlFree(handle, LG_HDL_TYPE_FILE);
lgHdlUnlock(handle);
}
return status;
}
int lgFileWrite(int handle, char *buf, int count)
{
int w;
int status;
lgFileObj_p h;
LG_DBG(LG_DEBUG_TRACE, "handle=%d count=%d [%s]",
handle, count, lgDbgBuf2Str(count, buf));
if (!count)
PARAM_ERROR(LG_BAD_FILE_PARAM, "bad count (%d)", count);
status = lgHdlGetLockedObj(handle, LG_HDL_TYPE_FILE, (void **)&h);
if (status == LG_OKAY)
{
if (h->mode & LG_FILE_WRITE)
{
w = write(h->fd, buf, count);
if (w != count)
{
if (w == -1)
{
LG_DBG(LG_DEBUG_FILE, "write failed with errno %d", errno);
}
status = LG_BAD_FILE_WRITE;
}
}
else
{
LG_DBG(LG_DEBUG_FILE, "file not opened for write");
status = LG_FILE_NOT_WOPEN;
}
lgHdlUnlock(handle);
}
return status;
}
int lgFileRead(int handle, char *buf, int count)
{
int status;
lgFileObj_p h;
LG_DBG(LG_DEBUG_TRACE, "handle=%d count=%d buf=%p", handle, count, buf);
if (!count)
PARAM_ERROR(LG_BAD_FILE_PARAM, "bad count (%d)", count);
status = lgHdlGetLockedObj(handle, LG_HDL_TYPE_FILE, (void **)&h);
if (status == LG_OKAY)
{
if (h->mode & LG_FILE_READ)
{
status = read(h->fd, buf, count);
if (status == -1)
{
LG_DBG(LG_DEBUG_FILE, "read failed with errno %d", errno);
status = LG_BAD_FILE_READ;
}
else
{
buf[status] = 0;
}
}
else
{
LG_DBG(LG_DEBUG_FILE, "file not opened for read");
status = LG_FILE_NOT_ROPEN;
}
lgHdlUnlock(handle);
}
return status;
}
int lgFileSeek(int handle, int32_t seekOffset, int seekFrom)
{
int whence;
int status;
lgFileObj_p h;
LG_DBG(LG_DEBUG_TRACE, "handle=%d offset=%d from=%d",
handle, seekOffset, seekFrom);
switch (seekFrom)
{
case LG_FROM_START:
whence = SEEK_SET;
break;
case LG_FROM_CURRENT:
whence = SEEK_CUR;
break;
case LG_FROM_END:
whence = SEEK_END;
break;
default:
PARAM_ERROR(LG_BAD_FILE_SEEK, "bad seek from (%d)", seekFrom);
}
status = lgHdlGetLockedObj(handle, LG_HDL_TYPE_FILE, (void **)&h);
if (status == LG_OKAY)
{
status = lseek(h->fd, seekOffset, whence);
if (status == -1)
{
LG_DBG(LG_DEBUG_FILE, "seek failed with errno %d", errno);
status = LG_BAD_FILE_SEEK;
}
lgHdlUnlock(handle);
}
return status;
}
int lgFileList(char *fpat, char *buf, int count)
{
int len, bufpos;
glob_t pglob;
int i;
LG_DBG(LG_DEBUG_TRACE, "fpat=%s count=%d buf=%p", fpat, count, buf);
bufpos = 0;
if (glob(fpat, GLOB_MARK, NULL, &pglob) == 0)
{
for (i=0; i<pglob.gl_pathc; i++)
{
len = strlen(pglob.gl_pathv[i]);
if ((bufpos + len + 1) < count)
{
strcpy(buf+bufpos, pglob.gl_pathv[i]);
bufpos += len;
buf[bufpos++] = '\n';
}
}
}
else
{
bufpos = LG_NO_FILE_MATCH;
}
globfree(&pglob);
return bufpos;
}