-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfileHandles.c
638 lines (559 loc) · 16.1 KB
/
fileHandles.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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
//
// Created by paul on 8/31/22.
//
#define _GNU_SOURCE
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <string.h>
#include <time.h>
#include <sys/stat.h>
#include <uci.h>
#include "logStuff.h"
#include "utils.h"
#include "ucifs.h"
#include "fileHandles.h"
#include "uci2libelektra.h"
typedef struct sFileHandle {
tFileHandle * next;
struct stat st;
const char * path;
tHash pathHash;
char * contents;
int buildCount; // for 'mark/sweep' GC. will be set from buildCounter during a populateRoot.
tBool dirty;
} tFileHandle;
typedef struct sMountPoint {
time_t lastUpdated; // last time the root dir was populated
int buildCounter; // part of a 'mark/sweep' algo to maintain the root dir
struct sFileHandle * rootFiles;
struct stat rootStat;
} tMountPoint;
/**
* @brief
* @param path
* @param mode
* @return
*/
tFileHandle * newFH( const char * path, int mode )
{
tFileHandle * result = NULL;
if ( path != NULL && *path != '\0' )
{
logDebug( " new fh for \'%s\'", path );
result = calloc( 1, sizeof( tFileHandle ) );
if ( result != NULL )
{
result->path = strdup( path );
result->pathHash = hashString( result->path );
// GNU's definitions of the attributes (http://www.gnu.org/software/libc/manual/html_node/Attribute-Meanings.html):
// st_uid: The user ID of the file’s owner.
// st_gid: The group ID of the file.
// st_atime: This is the last access time for the file.
// st_mtime: This is the time of the last modification to the contents of the file.
// st_mode: Specifies the mode of the file. This includes file type information (see Testing File Type)
// and the file permission bits (see Permission Bits).
// st_nlink: The number of hard links to the file. This count keeps track of how many directories have
// entries for this file. If the count is ever decremented to zero, then the file itself is
// discarded as soon as no process still holds it open. Symbolic links are not counted in the
// total.
// st_size: This specifies the size of a regular file in bytes. For files that are really devices this
// field isn’t usually meaningful. For symbolic links this specifies the length of the file
// name the link refers to.
if ( mode == 0 )
{
mode = S_IFREG | S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH; // 0644
}
result->st.st_mode = mode;
result->st.st_nlink = 1;
result->st.st_size = 1024;
time_t now = time(NULL);
result->st.st_atime = now; // The last "a"ccess of the file
result->st.st_mtime = now; // The last "m"odification of the file
result->st.st_ctime = now; // The last "c"hange of the attributes of the file (it's new)
tMountPoint * mountPoint = (tMountPoint *)getPrivateData();
if ( mountPoint != NULL )
{
result->st.st_uid = mountPoint->rootStat.st_uid;
result->st.st_gid = mountPoint->rootStat.st_gid;
mountPoint->rootStat.st_mtime = now; // we have "m"odified the root directory
mountPoint->rootStat.st_ctime = now; // also "c"hanged the attributes of the root directory
/* add it to the current list of files in the root dir */
result->next = mountPoint->rootFiles;
mountPoint->rootFiles = result;
}
}
}
return result;
}
/**
* @brief
* @param path
* @return
*/
tFileHandle * findFH( const char * path )
{
tFileHandle * result = NULL;
tMountPoint * mountPoint = (tMountPoint *)getPrivateData();
if ( mountPoint != NULL)
{
/* make sure the root cache is populated & up-to-date */
populateRoot( mountPoint );
tHash hash = hashString( path );
for ( result = mountPoint->rootFiles; result != NULL; result = result->next )
{
if ( hash == result->pathHash )
{
/* found an existing matching entry, so exit loop prematurely */
break;
}
}
}
return result;
}
/**
* @brief
* @param fh
* @param path
* @return
*/
tFileHandle * getFH( tFileHandle * fh, const char * path )
{
if ( path == NULL || *path == '\0' )
{
logError( "supplied path is empty" );
return NULL;
}
/* if fh is NULL, search through the root files for a
* match to the path. Often the case for doGetAttr() */
if ( fh == NULL )
{
fh = findFH( path );
}
#ifdef DEBUG
if ( fh == NULL)
{
logError("no matching UCI file handle found");
}
else if ( fh->path == NULL)
{
logError( "fh->path is null" );
free( fh );
fh = NULL;
}
else if ( strcmp( fh->path, path ) != 0 )
{
logError( "path and fh->path do not match" );
free( (void *)fh->path );
free( fh );
fh = NULL;
}
#endif
return fh;
}
/**
* @brief
* @param fh
* @return
*/
tFileHandle * nextFH( tFileHandle * fh )
{
if ( fh != NULL )
{
fh = fh->next;
}
else
{
tMountPoint * mountPoint = (tMountPoint *)getPrivateData();
fh = mountPoint->rootFiles;
}
return fh;
}
/**
* @brief
* @param fh
* @return
*/
const char * getFHpath( tFileHandle * fh )
{
return fh->path;
}
/**
* @brief
* @param fh
* @return
*/
struct stat * getFHstat( tFileHandle * fh )
{
return &fh->st;
}
/**
* @brief
* @param fh
* @param buffer
* @param size
* @param offset
* @return
*/
ssize_t readFH( tFileHandle * fh, char *buffer, size_t size, off_t offset)
{
ssize_t remaining = fh->st.st_size - offset;
if ( remaining < 0 )
remaining = 0;
ssize_t length = (ssize_t)size;
// do we have enough data to satisfy the whole request?
if ( length > remaining )
{
// no, so trim it down to what remains
length = remaining;
}
if ( length > 0 )
{
memcpy( buffer, &fh->contents[offset], length );
}
else {
length = -1; // no more data to read - 'end of file'
}
return length;
}
/**
* @brief
* @param fh
* @param buffer
* @param size
* @param offset
* @return
*/
ssize_t writeFH( tFileHandle * fh, const char *buffer, size_t size, off_t offset )
{
ssize_t end = (ssize_t)(offset + size);
char * contents = fh->contents;
if ( contents == NULL )
{
logDebug( " calloc %ld bytes", end );
contents = calloc( end, sizeof(byte) );
fh->st.st_size = end;
}
else if ( end > fh->st.st_size)
{
logDebug( " realloc to %ld bytes", end );
contents = realloc( contents, end );
fh->st.st_size = end;
}
if ( contents == NULL )
{
logError( "failed to allocate memory for write");
size = 0;
}
else
{
fh->contents = contents; // in case realloc() moved the block, or it's newly calloc'd.
fh->st.st_mtime = time(NULL); // The last "m"odification of the contents of the file/directory
fh->dirty = 1;
memcpy( &contents[offset], buffer, size );
}
return (ssize_t)size;
}
/**
* @brief
* @param fh
* @param offset
* @return
*/
int truncateFH( tFileHandle * fh, off_t offset )
{
fh->st.st_mtime = time(NULL); // The last "m"odification of the contents of the file
if ( offset == 0 )
{
if ( fh->contents != NULL )
{
free( fh->contents );
fh->contents = NULL;
}
fh->st.st_size = 0;
}
else if ( offset > 0 )
{
if ( fh->contents == NULL )
{
fh->contents = calloc( offset, sizeof( byte ));
}
else {
fh->contents = realloc( fh->contents, offset );
}
fh->st.st_size = offset;
}
else {
logError( "attempted to truncate using a negative offset: %ld", offset );
}
return 0;
}
/**
* @brief
* @param fh
* @return
*/
int populateFH( tFileHandle * fh )
{
int result = -EINVAL;
if ( fh != NULL )
{
/* ToDo: populate the contents from LibElektra */
/* ToDo: set the ctime & mtime from the 'last changed' timestamps of the enclosed UCI values */
if ( fh->contents != NULL)
{
free( fh->contents );
fh->contents = NULL;
fh->st.st_size = 0;
}
/* ToDo: until we wire up LibElekta, set the contents to something */
int len = asprintf( (char **)&fh->contents, "Placeholder for %s\n", fh->path );
if ( len < 0 )
{
result = -errno;
logError( "unable to populate %s", fh->path );
}
else {
fh->st.st_size = len;
result = 0;
}
}
return result;
}
/**
* @brief
* @param fh
* @return
*/
int parseFH( tFileHandle * fh )
{
int result = -EINVAL;
if ( fh != NULL)
{
if ( fh->contents != NULL )
{
if ( fh->st.st_size > 0 )
{
logDebug( "contents of %s:", fh->path );
logTextBlock( kLogDebug, fh->contents, fh->st.st_size );
/* use libuci to parse the contents into UCI structures */
struct uci_context * ctx = uci_alloc_context();
if ( ctx != NULL )
{
const char *name = fh->path;
if (*name == '/') ++name;
struct uci_package * package = NULL;
FILE * contentStream = fmemopen( fh->contents, fh->st.st_size, "r");
result = uci_import( ctx, contentStream, name, &package, false);
if ( result != 0 )
{
char * errStr;
uci_get_errorstr( ctx, &errStr, "" );
logError( " problem importing %s: %s", name, errStr );
}
else
{
uci2elektra( ctx );
}
uci_free_context(ctx);
}
}
fh->dirty = 0;
#if 0
/* ToDDo: drop the contents, we're done parsing it */
free( fh->contents );
fh->contents = NULL;
fh->st.st_size = 0;
#endif
}
result = 0;
}
return result;
}
/**
* @brief
* @param fh
*/
void releaseFH( tFileHandle * fh )
{
if ( fh != NULL )
{
if ( fh->path != NULL )
{
free( (void *)fh->path );
fh->path = NULL;
}
if ( fh->contents != NULL )
{
free( fh->contents );
fh->contents = NULL;
}
free( fh );
}
}
/**
* @brief
* @param mountPoint
* @return
*/
int releaseRoot( tMountPoint * mountPoint )
{
int result = 0;
if ( mountPoint != NULL )
{
tFileHandle * fh = mountPoint->rootFiles;
mountPoint->rootFiles = NULL;
mountPoint->rootStat.st_nlink = 0;
while ( fh != NULL )
{
tFileHandle * next = fh->next;
releaseFH( fh );
fh = next;
}
free( mountPoint );
}
return result;
}
/**
* @brief
* @param fh
* @param st
* @return
*/
int getFileAttributes( tFileHandle * fh, struct stat * st )
{
int result = -EINVAL;
if ( fh != NULL )
{
result = populateFH( fh );
fh->st.st_atime = time(NULL); // The last "a"ccess of the file/directory is right now
memcpy( st, (const void *)&fh->st, sizeof( struct stat ));
}
return result;
}
/**
* @brief
* @param mountPoint
* @param st
* @return
*/
int getDirAttributes( tMountPoint * mountPoint, struct stat * st )
{
int result = -EINVAL;
if ( mountPoint != NULL )
{
result = populateRoot( mountPoint );
mountPoint->rootStat.st_atime = time(NULL); // The last "a"ccess of the file/directory is right now
memcpy( st, (const void *)&mountPoint->rootStat, sizeof( struct stat ));
}
return result;
}
/**
* @brief
* @param path
* @return
*/
int isDirectory( const char * path )
{
/* the only directory is at the root of the mount */
return ( path[0] == '/' && path[1] == '\0' );
}
/**
* @brief
* Note: this is called *really* early, mountPoint can't be retrieved until _after_ this has returned
* @param uid
* @param gid
* @return
*/
tMountPoint * initRoot( uid_t uid, gid_t gid )
{
tMountPoint * mountPoint = calloc( 1, sizeof( tMountPoint ));
if ( mountPoint != NULL)
{
mountPoint->rootStat.st_uid = uid;
mountPoint->rootStat.st_gid = gid;
}
else {
logError( " failed to allocate mountPoint structure" );
}
return mountPoint;
}
/**
* @brief
* @param mountPoint
* @return
*/
int populateRoot( tMountPoint * mountPoint )
{
int result = 0;
if ( mountPoint == NULL )
{
logError("mountPoint structure is absent");
return -EFAULT;
}
time_t now = time(NULL);
/* if we recently populated, then assume it's unlikely
* anything has changed, and reuse what we just built */
time_t age = now - mountPoint->lastUpdated;
if ( age < 5 )
{
return 0;
}
logDebug( "root cache is %lu secs old, so rebuild", age );
mountPoint->lastUpdated = now;
mountPoint->rootStat.st_mode = S_IFDIR | S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH; // 0644
mountPoint->rootStat.st_size = 1024;
mountPoint->rootStat.st_atime = time(NULL); // The last "a"ccess of the file/directory is right now
mountPoint->rootStat.st_mtime = time(NULL); // The last "m"odification of the file/directory is right now
mountPoint->rootStat.st_ctime = time(NULL); // The last "c"hange of the file/directory is right now
int buildCount = ++mountPoint->buildCounter;
/* iterate through the current list of UCI files, marking the ones that still
* exist with the new buildCount, and adding new ones */
tFileHandle * fh;
const char * path;
int i;
for ( i = 0; (path = iterateUCIfiles( i )) != NULL; ++i )
{
fh = findFH( path );
if ( fh == NULL )
{
// did not find a matching entry in the list, so create a new one and add it
fh = newFH( path, 0 );
if (fh != NULL)
{
/* fill in the contents */
populateFH( fh );
}
}
if ( fh != NULL )
{
/* mark fh as 'seen' by updating the buildCount */
fh->buildCount = buildCount;
}
}
mountPoint->rootStat.st_nlink = i + 2; /* +2 to include '.' and '..' entries */
/* now scan the list and remove anything that wasn't just marked with the new buildCount */
fh = mountPoint->rootFiles;
tFileHandle ** prev = &mountPoint->rootFiles;
while ( fh != NULL )
{
/* preserve the pointer to the address holding the 'next' field used
* at the end of this loop to set 'prev' after it has been used */
tFileHandle ** nextprev = &fh->next;
if ( fh->buildCount != buildCount )
{
/* a stale buildCount value means it's a 'dead' entry - i.e. a LibElektra entry that
* is no longer being returned by iterateUCIfiles(). So unlink and dispose of it */
logDebug( "remove \'%s\'", fh->path );
*prev = fh->next; /* 'unhook' this fh from the linked list */
releaseFH( fh );
nextprev = prev; /* in case we need to also remove the next fh in the list */
}
/* on first iteration, prev points at mountPoint->rootFiles, subsequent
* iterations it points at the fh->next field of the _previous_ fh */
fh = *prev; /* pick up the next fh */
prev = nextprev; /* point at the 'next' field in the _previous_ fh */
}
return result;
}