-
Notifications
You must be signed in to change notification settings - Fork 217
/
osapi-filesys.c
816 lines (698 loc) · 26.2 KB
/
osapi-filesys.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
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
/*
* NASA Docket No. GSC-18,370-1, and identified as "Operating System Abstraction Layer"
*
* Copyright (c) 2019 United States Government as represented by
* the Administrator of the National Aeronautics and Space Administration.
* All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* \file osapi-filesys.c
* \ingroup shared
* \author [email protected]
*
* This file contains some of the OS APIs abstraction layer code
* that is shared/common across all OS-specific implementations.
*
*/
/****************************************************************************************
INCLUDE FILES
***************************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <ctype.h>
/*
* User defined include files
*/
#include "os-shared-filesys.h"
#include "os-shared-idmap.h"
#include "os-shared-common.h"
enum
{
LOCAL_NUM_OBJECTS = OS_MAX_FILE_SYSTEMS,
LOCAL_OBJID_TYPE = OS_OBJECT_TYPE_OS_FILESYS
};
/*
* Internal filesystem state table entries
*/
OS_filesys_internal_record_t OS_filesys_table[LOCAL_NUM_OBJECTS];
/*
* A string that should be the prefix of RAM disk volume names, which
* provides a hint that the file system refers to a RAM disk.
*
* If multiple RAM disks are required then these can be numbered,
* e.g. RAM0, RAM1, etc.
*/
const char OS_FILESYS_RAMDISK_VOLNAME_PREFIX[] = "RAM";
/*----------------------------------------------------------------
*
* Function: OS_FileSysFilterFree
*
* Purpose: Local helper routine, not part of OSAL API.
* Iterator function to match only the free/open entries
*
* Returns: true if the entry is free, false if it is in use
*
*-----------------------------------------------------------------*/
bool OS_FileSysFilterFree(void *ref, const OS_object_token_t *token, const OS_common_record_t *obj)
{
return !OS_ObjectIdDefined(obj->active_id);
}
/*----------------------------------------------------------------
*
* Function: OS_FileSys_FindVirtMountPoint
*
* Purpose: Local helper routine, not part of OSAL API.
* Checks if the filesys table index matches the "virtual_mountpt" field.
* Function is Compatible with the Search object lookup routine
*
* Returns: true if the entry matches, false if it does not match
*
*-----------------------------------------------------------------*/
bool OS_FileSys_FindVirtMountPoint(void *ref, const OS_object_token_t *token, const OS_common_record_t *obj)
{
OS_filesys_internal_record_t *filesys;
const char * target = (const char *)ref;
size_t mplen;
filesys = OS_OBJECT_TABLE_GET(OS_filesys_table, *token);
if ((filesys->flags & OS_FILESYS_FLAG_IS_MOUNTED_VIRTUAL) == 0)
{
return false;
}
mplen = OS_strnlen(filesys->virtual_mountpt, sizeof(filesys->virtual_mountpt));
/*
* The virtual_mountpt member should be a substring of the search target.
* If this matches a basic substring check then it may be match
*/
if (mplen == 0 || mplen >= sizeof(filesys->virtual_mountpt) ||
strncmp(target, filesys->virtual_mountpt, mplen) != 0)
{
/* not a substring, so not a match */
return false;
}
/*
* Confirm that the substring ends at either a directory separator
* or the end of string (so exact mount points also match).
*
* For instance consider a virtual_mountpt of /mnt/abc and searching
* for target=/mnt/abcd - this should return false in that case.
*/
return (target[mplen] == '/' || target[mplen] == 0);
} /* end OS_FileSys_FindVirtMountPoint */
/*----------------------------------------------------------------
*
* Function: OS_FileSys_Initialize
*
* Purpose: Local helper routine, not part of OSAL API.
* Implements Common code between the mkfs and initfs calls -
* mkfs passes the "should_format" as true and initfs passes as false.
*
* Returns: OS_SUCCESS on creating the disk, or appropriate error code.
*
*-----------------------------------------------------------------*/
int32 OS_FileSys_Initialize(char *address, const char *fsdevname, const char *fsvolname, size_t blocksize,
osal_blockcount_t numblocks, bool should_format)
{
OS_filesys_internal_record_t *filesys;
int32 return_code;
OS_object_token_t token;
/*
* Check parameters
*
* Note "address" is not checked, because in certain configurations it can be validly null.
*/
OS_CHECK_STRING(fsdevname, sizeof(filesys->device_name), OS_FS_ERR_PATH_TOO_LONG);
OS_CHECK_STRING(fsvolname, sizeof(filesys->volume_name), OS_FS_ERR_PATH_TOO_LONG);
/* check names are not empty strings */
if (fsdevname[0] == 0 || fsvolname[0] == 0)
{
return OS_FS_ERR_PATH_INVALID;
}
return_code = OS_ObjectIdAllocateNew(LOCAL_OBJID_TYPE, fsdevname, &token);
if (return_code == OS_SUCCESS)
{
filesys = OS_OBJECT_TABLE_GET(OS_filesys_table, token);
/* Reset the table entry and save the name */
OS_OBJECT_INIT(token, filesys, device_name, fsdevname);
/* populate the VolumeName and BlockSize ahead of the Impl call,
* so the implementation can reference this info if necessary */
filesys->blocksize = blocksize;
filesys->numblocks = numblocks;
filesys->address = address;
strncpy(filesys->volume_name, fsvolname, sizeof(filesys->volume_name) - 1);
/*
* Determine basic type of filesystem, if not already known
*
* if either an address was supplied, or if the volume name
* contains the string "RAM" then it is a RAM disk. Otherwise
* leave the type as UNKNOWN and let the implementation decide.
*/
if (filesys->fstype == OS_FILESYS_TYPE_UNKNOWN &&
(filesys->address != NULL || strncmp(filesys->volume_name, OS_FILESYS_RAMDISK_VOLNAME_PREFIX,
sizeof(OS_FILESYS_RAMDISK_VOLNAME_PREFIX) - 1) == 0))
{
filesys->fstype = OS_FILESYS_TYPE_VOLATILE_DISK;
}
return_code = OS_FileSysStartVolume_Impl(&token);
if (return_code == OS_SUCCESS)
{
/*
* The "mkfs" call also formats the device.
* this is the primary difference between mkfs and initfs.
*/
if (should_format)
{
return_code = OS_FileSysFormatVolume_Impl(&token);
}
if (return_code == OS_SUCCESS)
{
filesys->flags |= OS_FILESYS_FLAG_IS_READY;
}
else
{
/*
* To avoid leaving in an intermediate state,
* this also stops the volume if formatting failed.
* Cast to void to repress analysis warnings for
* ignored return value.
*/
(void)OS_FileSysStopVolume_Impl(&token);
}
}
/* Check result, finalize record, and unlock global table. */
return_code = OS_ObjectIdFinalizeNew(return_code, &token, NULL);
}
return return_code;
} /* end OS_FileSys_Initialize */
/****************************************************************************************
INITIALIZATION
***************************************************************************************/
/*----------------------------------------------------------------
*
* Function: OS_FileSysAPI_Init
*
* Purpose: Local helper routine, not part of OSAL API.
*
*-----------------------------------------------------------------*/
int32 OS_FileSysAPI_Init(void)
{
int32 return_code = OS_SUCCESS;
memset(OS_filesys_table, 0, sizeof(OS_filesys_table));
return return_code;
} /* end OS_FileSysAPI_Init */
/*----------------------------------------------------------------
*
* Function: OS_FileSysAddFixedMap
*
* Purpose: Implemented per public OSAL API
* See description in API and header file for detail
*
*-----------------------------------------------------------------*/
int32 OS_FileSysAddFixedMap(osal_id_t *filesys_id, const char *phys_path, const char *virt_path)
{
OS_filesys_internal_record_t *filesys;
int32 return_code;
OS_object_token_t token;
const char * dev_name;
/*
* Validate inputs
*/
OS_CHECK_POINTER(filesys_id);
OS_CHECK_STRING(phys_path, sizeof(filesys->system_mountpt), OS_FS_ERR_PATH_TOO_LONG);
OS_CHECK_PATHNAME(virt_path);
/*
* Generate a dev name by taking the basename of the phys_path.
*/
dev_name = strrchr(phys_path, '/');
if (dev_name == NULL)
{
dev_name = phys_path;
}
else
{
++dev_name;
}
if (memchr(dev_name, 0, sizeof(filesys->volume_name)) == NULL)
{
return OS_ERR_NAME_TOO_LONG;
}
return_code = OS_ObjectIdAllocateNew(LOCAL_OBJID_TYPE, dev_name, &token);
if (return_code == OS_SUCCESS)
{
filesys = OS_OBJECT_TABLE_GET(OS_filesys_table, token);
/* Reset the table entry and save the name */
OS_OBJECT_INIT(token, filesys, device_name, dev_name);
strncpy(filesys->volume_name, dev_name, sizeof(filesys->volume_name) - 1);
strncpy(filesys->system_mountpt, phys_path, sizeof(filesys->system_mountpt) - 1);
strncpy(filesys->virtual_mountpt, virt_path, sizeof(filesys->virtual_mountpt) - 1);
/*
* mark the entry that it is a fixed disk
*/
filesys->fstype = OS_FILESYS_TYPE_FS_BASED;
filesys->flags = OS_FILESYS_FLAG_IS_FIXED;
/*
* The "mount" implementation is required as it will
* create the mountpoint if it does not already exist
*/
return_code = OS_FileSysStartVolume_Impl(&token);
if (return_code == OS_SUCCESS)
{
filesys->flags |= OS_FILESYS_FLAG_IS_READY;
return_code = OS_FileSysMountVolume_Impl(&token);
}
if (return_code == OS_SUCCESS)
{
/*
* mark the entry that it is a fixed disk
*/
filesys->flags |= OS_FILESYS_FLAG_IS_MOUNTED_SYSTEM | OS_FILESYS_FLAG_IS_MOUNTED_VIRTUAL;
}
/* Check result, finalize record, and unlock global table. */
return_code = OS_ObjectIdFinalizeNew(return_code, &token, filesys_id);
}
return return_code;
} /* end OS_FileSysAddFixedMap */
/*----------------------------------------------------------------
*
* Function: OS_mkfs
*
* Purpose: Implemented per public OSAL API
* See description in API and header file for detail
*
*-----------------------------------------------------------------*/
int32 OS_mkfs(char *address, const char *devname, const char *volname, size_t blocksize, osal_blockcount_t numblocks)
{
int32 return_code;
return_code = OS_FileSys_Initialize(address, devname, volname, blocksize, numblocks, true);
if (return_code == OS_ERR_INCORRECT_OBJ_STATE || return_code == OS_ERR_NO_FREE_IDS)
{
/*
* This is the historic filesystem-specific error code generated when
* attempting to mkfs()/initfs() on a filesystem that was
* already initialized, or of there were no free slots in the table.
*
* This code preserved just in case application code was checking for it.
*/
return_code = OS_FS_ERR_DEVICE_NOT_FREE;
}
return return_code;
} /* end OS_mkfs */
/*----------------------------------------------------------------
*
* Function: OS_rmfs
*
* Purpose: Implemented per public OSAL API
* See description in API and header file for detail
*
*-----------------------------------------------------------------*/
int32 OS_rmfs(const char *devname)
{
int32 return_code;
OS_object_token_t token;
/* Check parameters */
OS_CHECK_PATHNAME(devname);
return_code = OS_ObjectIdGetByName(OS_LOCK_MODE_EXCLUSIVE, LOCAL_OBJID_TYPE, devname, &token);
if (return_code == OS_SUCCESS)
{
/*
* NOTE: It is likely that if the file system is mounted,
* this call to stop the volume will fail.
*
* It would be prudent to first check the flags to ensure that
* the filesystem is unmounted first, but this would break
* compatibility with the existing unit tests.
*/
return_code = OS_FileSysStopVolume_Impl(&token);
/* Free the entry in the master table */
return_code = OS_ObjectIdFinalizeDelete(return_code, &token);
}
else
{
return_code = OS_ERR_NAME_NOT_FOUND;
}
return return_code;
} /* end OS_rmfs */
/*----------------------------------------------------------------
*
* Function: OS_initfs
*
* Purpose: Implemented per public OSAL API
* See description in API and header file for detail
*
*-----------------------------------------------------------------*/
int32 OS_initfs(char *address, const char *devname, const char *volname, size_t blocksize, osal_blockcount_t numblocks)
{
int32 return_code;
return_code = OS_FileSys_Initialize(address, devname, volname, blocksize, numblocks, false);
if (return_code == OS_ERR_INCORRECT_OBJ_STATE || return_code == OS_ERR_NO_FREE_IDS)
{
/*
* This is the historic filesystem-specific error code generated when
* attempting to mkfs()/initfs() on a filesystem that was
* already initialized, or of there were no free slots in the table.
*
* This code preserved just in case application code was checking for it.
*/
return_code = OS_FS_ERR_DEVICE_NOT_FREE;
}
return return_code;
} /* end OS_initfs */
/*----------------------------------------------------------------
*
* Function: OS_mount
*
* Purpose: Implemented per public OSAL API
* See description in API and header file for detail
*
*-----------------------------------------------------------------*/
int32 OS_mount(const char *devname, const char *mountpoint)
{
int32 return_code;
OS_object_token_t token;
OS_filesys_internal_record_t *filesys;
/* Check parameters */
OS_CHECK_STRING(devname, sizeof(filesys->device_name), OS_FS_ERR_PATH_TOO_LONG);
OS_CHECK_STRING(mountpoint, sizeof(filesys->virtual_mountpt), OS_FS_ERR_PATH_TOO_LONG);
return_code = OS_ObjectIdGetByName(OS_LOCK_MODE_GLOBAL, LOCAL_OBJID_TYPE, devname, &token);
if (return_code == OS_SUCCESS)
{
filesys = OS_OBJECT_TABLE_GET(OS_filesys_table, token);
/*
* READY flag should be set (mkfs/initfs must have been called on this FS)
* MOUNTED SYSTEM/VIRTUAL should always be unset.
*
* FIXED flag _should_ always be unset (these don't support mount/unmount)
* but to support abstraction this is not enforced.
*/
if ((filesys->flags & ~OS_FILESYS_FLAG_IS_FIXED) != OS_FILESYS_FLAG_IS_READY)
{
/* mount() cannot be used on this file system at this time */
return_code = OS_ERR_INCORRECT_OBJ_STATE;
}
else if (filesys->system_mountpt[0] == 0)
{
/*
* The system mount point should be a non-empty string.
*/
return_code = OS_FS_ERR_PATH_INVALID;
}
else
{
return_code = OS_FileSysMountVolume_Impl(&token);
}
if (return_code == OS_SUCCESS)
{
/* mark as mounted in the local table.
* For now this does both sides (system and virtual) */
filesys->flags |= OS_FILESYS_FLAG_IS_MOUNTED_SYSTEM | OS_FILESYS_FLAG_IS_MOUNTED_VIRTUAL;
strncpy(filesys->virtual_mountpt, mountpoint, sizeof(filesys->virtual_mountpt) - 1);
filesys->virtual_mountpt[sizeof(filesys->virtual_mountpt) - 1] = 0;
}
OS_ObjectIdRelease(&token);
}
if (return_code != OS_SUCCESS)
{
return_code = OS_ERR_NAME_NOT_FOUND;
}
return return_code;
} /* end OS_mount */
/*----------------------------------------------------------------
*
* Function: OS_unmount
*
* Purpose: Implemented per public OSAL API
* See description in API and header file for detail
*
*-----------------------------------------------------------------*/
int32 OS_unmount(const char *mountpoint)
{
int32 return_code;
OS_object_token_t token;
OS_filesys_internal_record_t *filesys;
/* Check parameters */
OS_CHECK_STRING(mountpoint, sizeof(filesys->virtual_mountpt), OS_FS_ERR_PATH_TOO_LONG);
return_code = OS_ObjectIdGetBySearch(OS_LOCK_MODE_GLOBAL, LOCAL_OBJID_TYPE, OS_FileSys_FindVirtMountPoint,
(void *)mountpoint, &token);
if (return_code == OS_SUCCESS)
{
filesys = OS_OBJECT_TABLE_GET(OS_filesys_table, token);
/*
* FIXED flag should always be unset (these don't support mount/unmount at all)
* READY flag should be set (mkfs/initfs must have been called on this FS)
* MOUNTED SYSTEM/VIRTUAL should always be unset.
*
* The FIXED flag is not enforced to support abstraction.
*/
if ((filesys->flags & ~OS_FILESYS_FLAG_IS_FIXED) !=
(OS_FILESYS_FLAG_IS_READY | OS_FILESYS_FLAG_IS_MOUNTED_SYSTEM | OS_FILESYS_FLAG_IS_MOUNTED_VIRTUAL))
{
/* unmount() cannot be used on this file system at this time */
return_code = OS_ERR_INCORRECT_OBJ_STATE;
}
else
{
return_code = OS_FileSysUnmountVolume_Impl(&token);
}
if (return_code == OS_SUCCESS)
{
/* mark as mounted in the local table.
* For now this does both sides (system and virtual) */
filesys->flags &= ~(OS_FILESYS_FLAG_IS_MOUNTED_SYSTEM | OS_FILESYS_FLAG_IS_MOUNTED_VIRTUAL);
}
OS_ObjectIdRelease(&token);
}
if (return_code != OS_SUCCESS)
{
return_code = OS_ERR_NAME_NOT_FOUND;
}
return return_code;
} /* end OS_unmount */
/*----------------------------------------------------------------
*
* Function: OS_FileSysStatVolume
*
* Purpose: Implemented per public OSAL API
* See description in API and header file for detail
*
*-----------------------------------------------------------------*/
int32 OS_FileSysStatVolume(const char *name, OS_statvfs_t *statbuf)
{
int32 return_code;
OS_object_token_t token;
/* Check parameters */
OS_CHECK_PATHNAME(name);
OS_CHECK_POINTER(statbuf);
return_code = OS_ObjectIdGetBySearch(OS_LOCK_MODE_GLOBAL, LOCAL_OBJID_TYPE, OS_FileSys_FindVirtMountPoint,
(void *)name, &token);
if (return_code == OS_SUCCESS)
{
return_code = OS_FileSysStatVolume_Impl(&token, statbuf);
OS_ObjectIdRelease(&token);
}
return return_code;
} /* end OS_FileSysStatVolume */
/*----------------------------------------------------------------
*
* Function: OS_chkfs
*
* Purpose: Implemented per public OSAL API
* See description in API and header file for detail
*
*-----------------------------------------------------------------*/
int32 OS_chkfs(const char *name, bool repair)
{
OS_object_token_t token;
int32 return_code;
/* Check parameters */
OS_CHECK_PATHNAME(name);
/* Get a reference lock, as a filesystem check could take some time. */
return_code = OS_ObjectIdGetBySearch(OS_LOCK_MODE_REFCOUNT, LOCAL_OBJID_TYPE, OS_FileSys_FindVirtMountPoint,
(void *)name, &token);
if (return_code == OS_SUCCESS)
{
return_code = OS_FileSysCheckVolume_Impl(&token, repair);
OS_ObjectIdRelease(&token);
}
return return_code;
} /* end OS_chkfs */
/*----------------------------------------------------------------
*
* Function: OS_FS_GetPhysDriveName
*
* Purpose: Implemented per public OSAL API
* See description in API and header file for detail
*
*-----------------------------------------------------------------*/
int32 OS_FS_GetPhysDriveName(char *PhysDriveName, const char *MountPoint)
{
OS_object_token_t token;
int32 return_code;
OS_filesys_internal_record_t *filesys;
/* Check parameters */
OS_CHECK_PATHNAME(MountPoint);
OS_CHECK_POINTER(PhysDriveName);
/* Get a reference lock, as a filesystem check could take some time. */
return_code = OS_ObjectIdGetBySearch(OS_LOCK_MODE_GLOBAL, LOCAL_OBJID_TYPE, OS_FileSys_FindVirtMountPoint,
(void *)MountPoint, &token);
if (return_code == OS_SUCCESS)
{
filesys = OS_OBJECT_TABLE_GET(OS_filesys_table, token);
if ((filesys->flags & OS_FILESYS_FLAG_IS_MOUNTED_SYSTEM) != 0)
{
strncpy(PhysDriveName, filesys->system_mountpt, OS_FS_PHYS_NAME_LEN - 1);
PhysDriveName[OS_FS_PHYS_NAME_LEN - 1] = 0;
}
else
{
return_code = OS_ERR_INCORRECT_OBJ_STATE;
}
OS_ObjectIdRelease(&token);
}
else
{
return_code = OS_ERR_NAME_NOT_FOUND;
}
return return_code;
} /* end OS_FS_GetPhysDriveName */
/*----------------------------------------------------------------
*
* Function: OS_GetFsInfo
*
* Purpose: Implemented per public OSAL API
* See description in API and header file for detail
*
*-----------------------------------------------------------------*/
int32 OS_GetFsInfo(os_fsinfo_t *filesys_info)
{
OS_object_iter_t iter;
/* Check parameters */
OS_CHECK_POINTER(filesys_info);
memset(filesys_info, 0, sizeof(*filesys_info));
filesys_info->MaxFds = OS_MAX_NUM_OPEN_FILES;
filesys_info->MaxVolumes = OS_MAX_FILE_SYSTEMS;
OS_ObjectIdIteratorInit(OS_FileSysFilterFree, NULL, OS_OBJECT_TYPE_OS_STREAM, &iter);
while (OS_ObjectIdIteratorGetNext(&iter))
{
++filesys_info->FreeFds;
}
OS_ObjectIdIteratorDestroy(&iter);
OS_ObjectIdIteratorInit(OS_FileSysFilterFree, NULL, OS_OBJECT_TYPE_OS_FILESYS, &iter);
while (OS_ObjectIdIteratorGetNext(&iter))
{
++filesys_info->FreeVolumes;
}
OS_ObjectIdIteratorDestroy(&iter);
return (OS_SUCCESS);
} /* end OS_GetFsInfo */
/*----------------------------------------------------------------
*
* Function: OS_TranslatePath
*
* Purpose: Implemented per public OSAL API
* See description in API and header file for detail
*
*-----------------------------------------------------------------*/
int32 OS_TranslatePath(const char *VirtualPath, char *LocalPath)
{
OS_object_token_t token;
int32 return_code;
const char * name_ptr;
OS_filesys_internal_record_t *filesys;
size_t SysMountPointLen;
size_t VirtPathLen;
size_t VirtPathBegin;
/*
** Check to see if the path pointers are NULL
*/
/* Check parameters */
OS_CHECK_POINTER(VirtualPath);
OS_CHECK_POINTER(LocalPath);
/*
** Check length
*/
VirtPathLen = OS_strnlen(VirtualPath, OS_MAX_PATH_LEN);
if (VirtPathLen >= OS_MAX_PATH_LEN)
{
return OS_FS_ERR_PATH_TOO_LONG;
}
/* checks to see if there is a '/' somewhere in the path */
name_ptr = strrchr(VirtualPath, '/');
if (name_ptr == NULL)
{
return OS_FS_ERR_PATH_INVALID;
}
/* strrchr returns a pointer to the last '/' char, so we advance one char */
name_ptr = name_ptr + 1;
if (memchr(name_ptr, 0, OS_MAX_FILE_NAME) == NULL)
{
return OS_FS_ERR_NAME_TOO_LONG;
}
SysMountPointLen = 0;
VirtPathBegin = VirtPathLen;
/*
** All valid Virtual paths must start with a '/' character
*/
if (VirtualPath[0] != '/')
{
return OS_FS_ERR_PATH_INVALID;
}
/* Get a reference lock, as a filesystem check could take some time. */
return_code = OS_ObjectIdGetBySearch(OS_LOCK_MODE_GLOBAL, LOCAL_OBJID_TYPE, OS_FileSys_FindVirtMountPoint,
(void *)VirtualPath, &token);
if (return_code != OS_SUCCESS)
{
return_code = OS_FS_ERR_PATH_INVALID;
}
else
{
filesys = OS_OBJECT_TABLE_GET(OS_filesys_table, token);
if ((filesys->flags & OS_FILESYS_FLAG_IS_MOUNTED_SYSTEM) != 0)
{
SysMountPointLen = OS_strnlen(filesys->system_mountpt, sizeof(filesys->system_mountpt));
VirtPathBegin = OS_strnlen(filesys->virtual_mountpt, sizeof(filesys->virtual_mountpt));
if (SysMountPointLen < OS_MAX_LOCAL_PATH_LEN)
{
memcpy(LocalPath, filesys->system_mountpt, SysMountPointLen);
}
}
else
{
return_code = OS_ERR_INCORRECT_OBJ_STATE;
}
OS_ObjectIdRelease(&token);
}
if (return_code == OS_SUCCESS)
{
if (VirtPathLen < VirtPathBegin)
{
return_code = OS_FS_ERR_PATH_INVALID;
}
else
{
VirtPathLen -= VirtPathBegin;
if ((SysMountPointLen + VirtPathLen) < OS_MAX_LOCAL_PATH_LEN)
{
memcpy(&LocalPath[SysMountPointLen], &VirtualPath[VirtPathBegin], VirtPathLen);
LocalPath[SysMountPointLen + VirtPathLen] = 0;
}
else
{
return_code = OS_FS_ERR_PATH_TOO_LONG;
}
}
}
return return_code;
} /* end OS_TranslatePath */