This repository was archived by the owner on Aug 27, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathvcfilesys.c
More file actions
1132 lines (899 loc) · 29 KB
/
vcfilesys.c
File metadata and controls
1132 lines (899 loc) · 29 KB
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
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Copyright (c) 2012, Broadcom Europe Ltd
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of the copyright holder nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#define VCOS_LOG_CATEGORY (&hostfs_log_cat)
#ifndef _LARGEFILE_SOURCE
#define _LARGEFILE_SOURCE
#endif
#ifndef _LARGEFILE64_SOURCE
#define _LARGEFILE64_SOURCE
#endif
#define _FILE_OFFSET_BITS 64 /* So we get lseek and lseek64 */
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <ctype.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/statfs.h>
#include <fcntl.h>
#include <errno.h>
#include <assert.h>
#include <ctype.h>
#include <limits.h>
#if defined(__GLIBC__) && !defined( __USE_FILE_OFFSET64 )
#error "__USE_FILE_OFFSET64 isn't defined"
#endif
#include "interface/vcos/vcos.h"
/* Some hackery to prevent a clash with the Linux type of the same name */
#define dirent fs_dirent
#include "vcfilesys_defs.h"
#include "vchost.h"
#undef dirent
#include <dirent.h>
#include "vc_fileservice_defs.h"
VCOS_LOG_CAT_T hostfs_log_cat;
/******************************************************************************
Global data.
******************************************************************************/
/******************************************************************************
Local types and defines.
******************************************************************************/
//#define DEBUG_LEVEL 1
#define DEBUG_MINOR(...) vcos_log_info(__VA_ARGS__)
#define DEBUG_MAJOR(...) vcos_log_warn(__VA_ARGS__)
/* Define a wrapper for the native directory handle which includes the path
* to that directory (needed to retrieve size and attributes via stat()).
*/
struct fs_dir
{
DIR *dhandle;
int pathlen;
char pathbuf[PATH_MAX];
};
/*
* The media player on the Videocore may be asked to open a file on the Host that
* is in fact a FIFO. We need to note when a FIFO has been opened so that we
* can fake out some FIFO seeks that the Videocore may perform, hence the following
* types and variables.
*/
typedef struct
{
int is_fifo; // non-zero if file is a FIFO
uint64_t read_offset; // read offset into file
} file_info_t;
#define FILE_INFO_TABLE_CHUNK_LEN 20
/******************************************************************************
Static data.
******************************************************************************/
static file_info_t *p_file_info_table = NULL;
static int file_info_table_len = 0;
/******************************************************************************
Static functions.
******************************************************************************/
static void backslash_to_slash( char *s );
/******************************************************************************
Global functions.
******************************************************************************/
/******************************************************************************
NAME
vc_hostfs_init
SYNOPSIS
void vc_hostfs_init(void)
FUNCTION
Initialises the host to accept requests from Videocore
RETURNS
void
******************************************************************************/
void vc_hostfs_init(void)
{
// This hostfs module is not thread safe - it allocaes a block
// of memory and uses it without any kind of locking.
//
// It offers no advantage of stdio, and so most clients should
// not use it. Arguably FILESYS should use it in order to get
// the FIFO support.
const char *thread_name = vcos_thread_get_name(vcos_thread_current());
if (strcmp(thread_name, "FILESYS") != 0 && strcmp(thread_name, "HFilesys") != 0)
{
fprintf(stderr,"%s: vc_hostfs is deprecated. Please use stdio\n",
vcos_thread_get_name(vcos_thread_current()));
}
vcos_log_register("hostfs", &hostfs_log_cat);
DEBUG_MINOR("init");
// Allocate memory for the file info table
p_file_info_table = (file_info_t *)calloc( FILE_INFO_TABLE_CHUNK_LEN, sizeof( file_info_t ) );
assert( p_file_info_table != NULL );
if (p_file_info_table)
{
file_info_table_len = FILE_INFO_TABLE_CHUNK_LEN;
}
}
/** Terminate this library. Clean up resources.
*/
void vc_hostfs_exit(void)
{
vcos_log_unregister(&hostfs_log_cat);
if (p_file_info_table)
{
free(p_file_info_table);
p_file_info_table = NULL;
}
}
/******************************************************************************
NAME
vc_hostfs_close
SYNOPSIS
int vc_hostfs_close(int fildes)
FUNCTION
Deallocates the file descriptor to a file.
RETURNS
Successful completion: 0
Otherwise: -1
******************************************************************************/
int vc_hostfs_close(int fildes)
{
DEBUG_MINOR("vc_hostfs_close(%d)", fildes);
return close(fildes);
}
/******************************************************************************
NAME
vc_hostfs_lseek
SYNOPSIS
long vc_hostfs_lseek(int fildes, long offset, int whence)
FUNCTION
Sets the file pointer associated with the open file specified by fildes. If
the file is a FIFO (Linux does not support seeking on a FIFO) then, for the
benefit of the Videocore streaming file handlers which do a number of null seeks,
that is, seeks to the current position, the return value is faked without an
actual seek being done.
RETURNS
Successful completion: offset
Otherwise: -1
******************************************************************************/
long vc_hostfs_lseek(int fildes, long offset, int whence)
{
return (long) vc_hostfs_lseek64( fildes, (int64_t) offset, whence);
}
/******************************************************************************
NAME
vc_hostfs_lseek64
SYNOPSIS
int64_t vc_hostfs_lseek64(int fildes, int64_t offset, int whence)
FUNCTION
Sets the file pointer associated with the open file specified by fildes. If
the file is a FIFO (Linux does not support seeking on a FIFO) then, for the
benefit of the Videocore streaming file handlers which do a number of null seeks,
that is, seeks to the current position, the return value is faked without an
actual seek being done.
RETURNS
Successful completion: offset
Otherwise: -1
******************************************************************************/
int64_t vc_hostfs_lseek64(int fildes, int64_t offset, int whence)
{
DEBUG_MINOR("vc_hostfs_lseek(%d,%" PRId64 ",%d)", fildes, offset, whence);
if (fildes >= file_info_table_len)
{
// File descriptor not in table, so this is an error
DEBUG_MAJOR("vc_hostfs_lseek: invalid fildes %d", fildes);
return -1;
}
else
{
// There is entry in the file info table for this file descriptor, so go
// ahead and handle the seek
int64_t read_offset = p_file_info_table[fildes].read_offset;
if (p_file_info_table[fildes].is_fifo)
{
// The Videocore is attempting to seek on a FIFO. FIFOs don't support seeking
// but, for the benefit of certain Videocore "streaming" file handlers, we
// will fake limited FIFO seek functionality by computing where a seek
// would take us to
if (whence == SEEK_SET)
{
read_offset = offset;
}
else if (whence == SEEK_CUR)
{
read_offset += offset;
}
else
{
// seeking to the end of FIFO makes no sense, so this is an error
DEBUG_MAJOR("vc_hostfs_lseek(%d,%lld,%d): SEEK_END not supported on FIFO", fildes, (long long)offset, whence);
return -1;
}
}
else
{
// File is not a FIFO, so do the seek
read_offset = lseek64(fildes, offset, whence);
}
p_file_info_table[fildes].read_offset = read_offset;
DEBUG_MINOR("vc_hostfs_lseek returning %" PRId64 ")", read_offset);
return read_offset;
}
}
/******************************************************************************
NAME
vc_hostfs_open
SYNOPSIS
int vc_hostfs_open(const char *path, int vc_oflag)
FUNCTION
Establishes a connection between a file and a file descriptor. For the benefit
of faking out seeks on a FIFO, we will need to keep track of the read offset for
all reads, and to facilitate this each opened file is given an entry in a local
file info table.
RETURNS
Successful completion: file descriptor
Otherwise: -1
******************************************************************************/
int vc_hostfs_open(const char *inPath, int vc_oflag)
{
char *path = strdup( inPath );
//char *s;
int flags = 0, ret=errno;
struct stat fileStat;
// Replace all '\' with '/'
backslash_to_slash( path );
#if 0
s = path + strlen( path );
if (( s - path ) >= 4 )
{
if ( strcasecmp( &s[ -4 ], ".vll" ) == 0 )
{
// The Videocore is asking for a .vll file. Since it isn't consistent with
// the case, we convert .vll files to all lowercase.
"vc_hostfs_open: '%s'", path ;
s--; // backup to the last character (*s is on the '\0')
while (( s >= path ) && ( *s != '/' ))
{
*s = tolower( *s );
s--;
}
}
}
#endif
DEBUG_MINOR("vc_hostfs_open: '%s'", path);
flags = O_RDONLY;
if (vc_oflag & VC_O_WRONLY) flags = O_WRONLY;
if (vc_oflag & VC_O_RDWR) flags = O_RDWR;
if (vc_oflag & VC_O_APPEND) flags |= O_APPEND;
if (vc_oflag & VC_O_CREAT) flags |= O_CREAT;
if (vc_oflag & VC_O_TRUNC) flags |= O_TRUNC;
if (vc_oflag & VC_O_EXCL) flags |= O_EXCL;
//while (*path == '\\') path++; // do not want initial '\'
if (flags & O_CREAT)
ret = open(path, flags, S_IRUSR | S_IWUSR );
else
ret = open(path, flags );
if (ret < 0 )
{
DEBUG_MINOR("vc_hostfs_open(%s,%d) = %d", path, vc_oflag, ret);
}
else
{
DEBUG_MINOR("vc_hostfs_open(%s,%d) = %d", path, vc_oflag, ret);
}
// If the file was successfully open then initialize its entry in
// the file info table. If necessary, we expand the size of the table
if (ret >= 0)
{
// File was successfully opened
if (ret >= file_info_table_len)
{
file_info_t *p_new_file_info_table = p_file_info_table;
int new_file_info_table_len = file_info_table_len;
// try and allocate a bigger buffer for the file info table
new_file_info_table_len += FILE_INFO_TABLE_CHUNK_LEN;
p_new_file_info_table = calloc( (size_t)new_file_info_table_len, sizeof( file_info_t ) );
if (p_new_file_info_table == NULL)
{
// calloc failed
DEBUG_MAJOR("vc_hostfs_open: file_info_table calloc failed");
assert( 0 );
}
else
{
// calloc successful, so copy data from previous buffer to new buffer,
// free previous buffer and update ptr and len info
memcpy( p_new_file_info_table, p_file_info_table, sizeof( file_info_t ) * file_info_table_len );
free( p_file_info_table );
p_file_info_table = p_new_file_info_table;
file_info_table_len = new_file_info_table_len;
}
}
assert( ret < file_info_table_len );
{
// initialize this file's entry in the file info table
p_file_info_table[ret].is_fifo = 0;
p_file_info_table[ret].read_offset = 0;
}
// Check whether the file is a FIFO. A FIFO does not support seeking
// but we will fake, to the extent supported by the buffered file system
// on the Videocore, limited FIFO seek functionality. This is for the benefit
// of certain Videocore "streaming" file handlers.
if (fstat( ret, &fileStat ) != 0)
{
DEBUG_MINOR("vc_hostfs_open: fstat failed: %s", strerror(errno));
}
else if (S_ISFIFO( fileStat.st_mode ))
{
// file is a FIFO, so note its fildes for future reference
p_file_info_table[ret].is_fifo = 1;
DEBUG_MINOR("vc_hostfs_open: file with fildes %d is a FIFO", ret);
}
}
free( path );
return ret;
}
/******************************************************************************
NAME
vc_hostfs_read
SYNOPSIS
int vc_hostfs_read(int fildes, void *buf, unsigned int nbyte)
FUNCTION
Attempts to read nbyte bytes from the file associated with the file
descriptor, fildes, into the buffer pointed to by buf. For the benefit
of faking out seeks on a FIFO, we keep track of the read offset for all
reads.
RETURNS
Successful completion: number of bytes read
Otherwise: -1
******************************************************************************/
int vc_hostfs_read(int fildes, void *buf, unsigned int nbyte)
{
if (fildes >= file_info_table_len)
{
// File descriptor not in table, so this is an error
DEBUG_MAJOR("vc_hostfs_read(%d,%p,%u): invalid fildes", fildes, buf, nbyte);
return -1;
}
else
{
// There is entry in the file info table for this file descriptor, so go
// ahead and handle the read
int ret = (int) read(fildes, buf, nbyte);
DEBUG_MINOR("vc_hostfs_read(%d,%p,%u) = %d", fildes, buf, nbyte, ret);
if (ret > 0)
{
p_file_info_table[fildes].read_offset += (long) ret;
}
return ret;
}
}
/******************************************************************************
NAME
vc_hostfs_write
SYNOPSIS
int vc_hostfs_write(int fildes, const void *buf, unsigned int nbyte)
FUNCTION
Attempts to write nbyte bytes from the buffer pointed to by buf to file
associated with the file descriptor, fildes.
RETURNS
Successful completion: number of bytes written
Otherwise: -1
******************************************************************************/
int vc_hostfs_write(int fildes, const void *buf, unsigned int nbyte)
{
int ret = (int) write(fildes, buf, nbyte);
DEBUG_MINOR("vc_hostfs_write(%d,%p,%u) = %d", fildes, buf, nbyte, ret);
return ret;
}
/******************************************************************************
NAME
vc_hostfs_closedir
SYNOPSIS
int vc_hostfs_closedir(void *dhandle)
FUNCTION
Ends a directory list iteration.
RETURNS
Successful completion: 0
Otherwise: -1
******************************************************************************/
int vc_hostfs_closedir(void *dhandle)
{
struct fs_dir *fsdir = (struct fs_dir *)dhandle;
int ret = -1;
DEBUG_MINOR( "vc_hostfs_closedir(%p)", dhandle );
if (dhandle && fsdir->dhandle)
{
(void)closedir(fsdir->dhandle);
fsdir->dhandle = NULL;
free(fsdir);
ret = 0;
}
return ret;
}
/******************************************************************************
NAME
vc_hostfs_format
SYNOPSIS
int vc_hostfs_format(const char *path)
FUNCTION
Formats the physical file system that contains path.
RETURNS
Successful completion: 0
Otherwise: -1
******************************************************************************/
int vc_hostfs_format(const char *path)
{
DEBUG_MINOR("vc_hostfs_format: '%s' not implemented", path);
return -1;
}
/******************************************************************************
NAME
vc_hostfs_freespace
SYNOPSIS
int vc_hostfs_freespace(const char *path)
FUNCTION
Returns the amount of free space on the physical file system that contains
path.
RETURNS
Successful completion: free space
Otherwise: -1
******************************************************************************/
int vc_hostfs_freespace(const char *inPath)
{
int ret;
int64_t freeSpace = vc_hostfs_freespace64( inPath );
// Saturate return value (need this in case we have a large file system)
if (freeSpace > (int64_t) INT_MAX)
{
ret = INT_MAX;
}
else
{
ret = (int) freeSpace;
}
return ret;
}
/******************************************************************************
NAME
vc_hostfs_freespace
SYNOPSIS
int vc_hostfs_freespace(const char *path)
FUNCTION
Returns the amount of free space on the physical file system that contains
path.
RETURNS
Successful completion: free space
Otherwise: -1
******************************************************************************/
int64_t vc_hostfs_freespace64(const char *inPath)
{
char *path = strdup( inPath );
int64_t ret;
struct statfs fsStat;
// Replace all '\' with '/'
backslash_to_slash( path );
ret = (int64_t) statfs( path, &fsStat );
if (ret == 0)
{
ret = fsStat.f_bsize * fsStat.f_bavail;
}
else
{
ret = -1;
}
DEBUG_MINOR( "vc_hostfs_freespace64 for '%s' returning %" PRId64 "", path, ret );
free( path );
return ret;
}
/******************************************************************************
NAME
vc_hostfs_get_attr
SYNOPSIS
int vc_hostfs_get_attr(const char *path, fattributes_t *attr)
FUNCTION
Gets the file/directory attributes.
RETURNS
Successful completion: 0
Otherwise: -1
******************************************************************************/
int vc_hostfs_get_attr(const char *path, fattributes_t *attr)
{
struct stat sb;
DEBUG_MINOR("vc_hostfs_get_attr: '%s'", path );
*attr = 0;
if ( stat( path, &sb ) == 0 )
{
if ( S_ISDIR( sb.st_mode ))
{
*attr |= ATTR_DIRENT;
}
if (( sb.st_mode & S_IWUSR ) == 0 )
{
*attr |= ATTR_RDONLY;
}
return 0;
}
return -1;
}
/******************************************************************************
NAME
vc_hostfs_mkdir
SYNOPSIS
int vc_hostfs_mkdir(const char *path)
FUNCTION
Creates a new directory named by the pathname pointed to by path.
RETURNS
Successful completion: 0
Otherwise: -1
******************************************************************************/
int vc_hostfs_mkdir(const char *path)
{
DEBUG_MINOR( "vc_hostfs_mkdir: '%s'", path );
if ( mkdir( path, 0777 ) == 0 )
{
return 0;
}
return -1;
}
/******************************************************************************
NAME
vc_hostfs_opendir
SYNOPSIS
void *vc_hostfs_opendir(const char *dirname)
FUNCTION
Starts a directory list iteration of sub-directories.
RETURNS
Successful completion: dhandle (pointer)
Otherwise: NULL
******************************************************************************/
void *vc_hostfs_opendir(const char *dirname)
{
struct fs_dir *fsdir = NULL;
DEBUG_MINOR( "vc_hostfs_opendir: '%s'", dirname );
if (dirname && dirname[0])
{
fsdir = (struct fs_dir *)malloc(sizeof(struct fs_dir));
if (fsdir)
{
DIR *dhandle;
int len = strlen(dirname);
memcpy(fsdir->pathbuf, dirname, len);
backslash_to_slash(fsdir->pathbuf);
/* Remove any trailing slashes */
while (fsdir->pathbuf[len - 1] == '/')
len--;
fsdir->pathbuf[len] = '\0';
dhandle = opendir(fsdir->pathbuf);
DEBUG_MINOR( "opendir: '%s' = %p", fsdir->pathbuf, dhandle );
if (dhandle)
{
fsdir->pathlen = len;
fsdir->dhandle = dhandle;
}
else
{
free(fsdir);
fsdir = NULL;
}
}
}
return fsdir;
}
/******************************************************************************
NAME
vc_hostfs_readdir_r
SYNOPSIS
struct dirent *vc_hostfs_readdir_r(void *dhandle, struct dirent *result)
FUNCTION
Fills in the passed result structure with details of the directory entry
at the current psition in the directory stream specified by the argument
dhandle, and positions the directory stream at the next entry. If the last
sub-directory has been reached it ends the iteration and begins a new one
for files in the directory.
RETURNS
Successful completion: result
End of directory stream: NULL
******************************************************************************/
struct fs_dirent *vc_hostfs_readdir_r(void *dhandle, struct fs_dirent *result)
{
struct fs_dir *fsdir = (struct fs_dir *)dhandle;
DEBUG_MINOR( "vc_hostfs_readdir_r(%p)", fsdir );
if (fsdir && result)
{
struct dirent *dent;
while ((dent = readdir(fsdir->dhandle)) != NULL)
{
struct stat statbuf;
int ret;
/* Append the filename, and stat the resulting path */
fsdir->pathbuf[fsdir->pathlen] = '/';
vcos_safe_strcpy(fsdir->pathbuf, dent->d_name, sizeof(fsdir->pathbuf), fsdir->pathlen + 1);
ret = stat(fsdir->pathbuf, &statbuf);
fsdir->pathbuf[fsdir->pathlen] = '\0';
if (ret == 0)
{
vcos_safe_strcpy(result->d_name, dent->d_name, sizeof(result->d_name), 0);
result->d_size = (statbuf.st_size <= 0xffffffff) ? (unsigned int)statbuf.st_size : 0xffffffff;
result->d_attrib = ATTR_NORMAL;
if ((statbuf.st_mode & S_IWUSR) == 0)
result->d_attrib |= ATTR_RDONLY;
if (statbuf.st_mode & S_IFDIR)
result->d_attrib |= ATTR_DIRENT;
result->d_creatime = statbuf.st_ctime;
result->d_modtime = statbuf.st_mtime;
DEBUG_MINOR( "vc_hostfs_readdir_r() = '%s', %x, %x", result->d_name, result->d_size, result->d_attrib );
break;
}
}
if (!dent)
{
DEBUG_MINOR( "vc_hostfs_readdir_r() = NULL" );
rewinddir(fsdir->dhandle);
result = NULL;
}
}
else
{
result = NULL;
}
return result;
}
/******************************************************************************
NAME
vc_hostfs_remove
SYNOPSIS
int vc_hostfs_remove(const char *path)
FUNCTION
Removes a file or a directory. A directory must be empty before it can be
deleted.
RETURNS
Successful completion: 0
Otherwise: -1
******************************************************************************/
int vc_hostfs_remove(const char *path)
{
char *pathbuf = strdup(path);
int ret = -1;
DEBUG_MINOR( "vc_hostfs_remove: '%s'", path );
if (pathbuf)
{
backslash_to_slash(pathbuf);
if ( unlink( pathbuf ) == 0 )
ret = 0;
}
free(pathbuf);
return ret;
}
/******************************************************************************
NAME
vc_hostfs_rename
SYNOPSIS
int vc_hostfs_rename(const char *old, const char *new)
FUNCTION
Changes the name of a file. The old and new pathnames must be on the same
physical file system.
RETURNS
Successful completion: 0
Otherwise: -1
******************************************************************************/
int vc_hostfs_rename(const char *old, const char *new)
{
char *oldbuf = strdup(old);
char *newbuf = strdup(new);
int ret = -1;
DEBUG_MINOR( "vc_hostfs_rename: '%s' to '%s'", old, new );
if (oldbuf && newbuf)
{
backslash_to_slash(oldbuf);
backslash_to_slash(newbuf);
if ( rename( oldbuf, newbuf ) == 0 )
ret = 0;
}
if (oldbuf)
free(oldbuf);
if (newbuf)
free(newbuf);
return ret;
}
/******************************************************************************
NAME
vc_hostfs_set_attr
SYNOPSIS
int vc_hostfs_set_attr(const char *path, fattributes_t attr)
FUNCTION
Sets file/directory attributes.
RETURNS
Successful completion: 0
Otherwise: -1
******************************************************************************/
int vc_hostfs_set_attr(const char *path, fattributes_t attr)
{
char *pathbuf = strdup(path);
int ret = -1;
DEBUG_MINOR( "vc_hostfs_set_attr: '%s', %x", path, attr );
if (pathbuf)
{
mode_t mode = 0;
struct stat sb;
backslash_to_slash(pathbuf);
if ( stat( path, &sb ) == 0 )
{
mode = sb.st_mode;
if ( attr & ATTR_RDONLY )
{
mode &= ~S_IWUSR;
}
else
{
mode |= S_IWUSR;
}
/* coverity[toctou] Not doing anything security-relevant here,
* so the race condition is harmless */
if ( chmod( path, mode ) == 0 )
ret = 0;
}
}
if (pathbuf)
free(pathbuf);
return ret;
}
/******************************************************************************
NAME
vc_hostfs_setend
SYNOPSIS
int vc_hostfs_setend(int fildes)
FUNCTION
Truncates file at current position.
RETURNS
Successful completion: 0
Otherwise: -1
******************************************************************************/
int vc_hostfs_setend(int filedes)
{
off_t currPosn;
if (( currPosn = lseek( filedes, 0, SEEK_CUR )) != (off_t)-1 )
{
if ( ftruncate( filedes, currPosn ) == 0 )
{
return 0;
}
}
return -1;
}
/******************************************************************************
NAME
vc_hostfs_totalspace64
SYNOPSIS
int64_t vc_hostfs_totalspace64(const char *path)
FUNCTION
Returns the total amount of space on the physical file system that contains
path.
RETURNS
Successful completion: total space
Otherwise: -1
******************************************************************************/
int64_t vc_hostfs_totalspace64(const char *inPath)
{
char *path = strdup( inPath );
int64_t ret = -1;
struct statfs fsStat;