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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
|
/*
* Copyright (c) 2006-2023, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2005-02-22 Bernard The first version.
* 2017-12-11 Bernard Use rt_free to instead of free in fd_is_open().
* 2018-03-20 Heyuanjie dynamic allocation FD
*/
#include <dfs.h>
#include <dfs_fs.h>
#include <dfs_dentry.h>
#include <dfs_file.h>
#include <dfs_mnt.h>
#include <rtservice.h>
#include "dfs_private.h"
#define DBG_TAG "DFS"
#define DBG_LVL DBG_INFO
#include <rtdbg.h>
#ifdef RT_USING_SMART
#include <lwp.h>
#endif
#ifdef DFS_USING_WORKDIR
char working_directory[DFS_PATH_MAX] = {"/"};
#endif
static rt_bool_t _dfs_init_ok = RT_FALSE;
/* device filesystem lock */
static struct rt_mutex fslock;
static struct rt_mutex fdlock;
static struct dfs_fdtable _fdtab = {0};
static int _fdt_slot_expand(struct dfs_fdtable *fdt, int fd)
{
int nr;
int index;
struct dfs_file **fds = NULL;
if (fd < fdt->maxfd)
{
return fd;
}
if (fd >= DFS_FD_MAX)
{
return -1;
}
nr = ((fd + 4) & ~3);
if (nr > DFS_FD_MAX)
{
nr = DFS_FD_MAX;
}
fds = (struct dfs_file **)rt_realloc(fdt->fds, nr * sizeof(struct dfs_file *));
if (!fds)
{
return -1;
}
/* clean the new allocated fds */
for (index = fdt->maxfd; index < nr; index++)
{
fds[index] = NULL;
}
fdt->fds = fds;
fdt->maxfd = nr;
return fd;
}
static int _fdt_slot_alloc(struct dfs_fdtable *fdt, int startfd)
{
int idx;
/* find an empty fd slot */
for (idx = startfd; idx < (int)fdt->maxfd; idx++)
{
if (fdt->fds[idx] == RT_NULL)
{
return idx;
}
}
idx = fdt->maxfd;
if (idx < startfd)
{
idx = startfd;
}
if (_fdt_slot_expand(fdt, idx) < 0)
{
return -1;
}
return idx;
}
static int _fdt_fd_alloc(struct dfs_fdtable *fdt, int startfd)
{
int idx;
idx = _fdt_slot_alloc(fdt, startfd);
return idx;
}
/**
* this function will lock device file system.
*
* @note please don't invoke it on ISR.
*/
rt_err_t dfs_lock(void)
{
rt_err_t result = -RT_EBUSY;
while (result == -RT_EBUSY)
{
result = rt_mutex_take(&fslock, RT_WAITING_FOREVER);
}
return result;
}
/**
* this function will lock device file system.
*
* @note please don't invoke it on ISR.
*/
void dfs_unlock(void)
{
rt_mutex_release(&fslock);
}
/** @addtogroup group_DFS
*
*
* @{
*/
rt_err_t dfs_file_lock(void)
{
rt_err_t result = -RT_EBUSY;
if (!_dfs_init_ok)
{
return -RT_ENOSYS;
}
while (result == -RT_EBUSY)
{
result = rt_mutex_take(&fdlock, RT_WAITING_FOREVER);
}
return result;
}
void dfs_file_unlock(void)
{
rt_mutex_release(&fdlock);
}
/**
* this function will initialize device file system.
*/
int dfs_init(void)
{
if (_dfs_init_ok)
{
LOG_E("DFS was already initialized.\n");
return 0;
}
/* create device filesystem lock */
rt_mutex_init(&fslock, "fslock", RT_IPC_FLAG_FIFO);
rt_mutex_init(&fdlock, "fdlock", RT_IPC_FLAG_FIFO);
/* clean fd table */
dfs_dentry_init();
_dfs_init_ok = RT_TRUE;
return 0;
}
INIT_PREV_EXPORT(dfs_init);
struct dfs_file* dfs_file_create(void)
{
struct dfs_file *file;
file = (struct dfs_file *)rt_calloc(1, sizeof(struct dfs_file));
if (file)
{
file->magic = DFS_FD_MAGIC;
file->ref_count = 1;
rt_mutex_init(&file->pos_lock, "fpos", RT_IPC_FLAG_PRIO);
}
return file;
}
void dfs_file_destroy(struct dfs_file *file)
{
rt_mutex_detach(&file->pos_lock);
if (file->mmap_context)
{
rt_free(file->mmap_context);
}
rt_free(file);
}
/**
* @ingroup group_Fd
* This function will allocate a file descriptor.
*
* @return -1 on failed or the allocated file descriptor.
*/
int fdt_fd_new(struct dfs_fdtable *fdt)
{
int idx = -1;
/* lock filesystem */
if (dfs_file_lock() != RT_EOK)
{
return -RT_ENOSYS;
}
/* find an empty fd entry */
idx = _fdt_fd_alloc(fdt, (fdt == &_fdtab) ? DFS_STDIO_OFFSET : 0);
/* can't find an empty fd entry */
if (idx < 0)
{
LOG_E("DFS fd new is failed! Could not found an empty fd entry.");
}
else if (!fdt->fds[idx])
{
struct dfs_file *file;
file = dfs_file_create();
if (file)
{
fdt->fds[idx] = file;
LOG_D("allocate a new fd @ %d", idx);
}
else
{
fdt->fds[idx] = RT_NULL;
idx = -1;
}
}
else
{
LOG_E("DFS not found an empty fds entry.");
idx = -1;
}
dfs_file_unlock();
return idx;
}
void fdt_fd_release(struct dfs_fdtable *fdt, int fd)
{
if (fd < fdt->maxfd)
{
struct dfs_file *file;
file = fdt_get_file(fdt, fd);
if (file && file->ref_count == 1)
{
dfs_file_destroy(file);
}
else
{
rt_atomic_sub(&(file->ref_count), 1);
}
fdt->fds[fd] = RT_NULL;
}
}
/**
* @ingroup group_Fd
*
* This function will return a file descriptor structure according to file
* descriptor.
*
* @return NULL on on this file descriptor or the file descriptor structure
* pointer.
*/
struct dfs_file *fdt_get_file(struct dfs_fdtable *fdt, int fd)
{
struct dfs_file *f;
if (fd < 0 || fd >= (int)fdt->maxfd)
{
return NULL;
}
f = fdt->fds[fd];
/* check file valid or not */
if ((f == NULL) || (f->magic != DFS_FD_MAGIC))
{
return NULL;
}
return f;
}
int fdt_fd_associate_file(struct dfs_fdtable *fdt, int fd, struct dfs_file *file)
{
int retfd = -1;
if (!file)
{
return retfd;
}
if (!fdt)
{
return retfd;
}
if (dfs_file_lock() != RT_EOK)
{
return -RT_ENOSYS;
}
/* check old fd */
if ((fd < 0) || (fd >= fdt->maxfd))
{
goto exit;
}
if (fdt->fds[fd])
{
goto exit;
}
/* inc ref_count */
rt_atomic_add(&(file->ref_count), 1);
fdt->fds[fd] = file;
retfd = fd;
exit:
dfs_file_unlock();
return retfd;
}
int fd_new(void)
{
struct dfs_fdtable *fdt;
fdt = dfs_fdtable_get();
return fdt_fd_new(fdt);
}
/**
* @ingroup group_Fd
*
* This function will put the file descriptor.
*/
void fd_release(int fd)
{
struct dfs_fdtable *fdt;
fdt = dfs_fdtable_get();
fdt_fd_release(fdt, fd);
}
struct dfs_file *fd_get(int fd)
{
struct dfs_fdtable *fdt;
fdt = dfs_fdtable_get();
return fdt_get_file(fdt, fd);
}
/**
* This function will get the file descriptor table of current process.
*/
struct dfs_fdtable *dfs_fdtable_get(void)
{
struct dfs_fdtable *fdt;
#ifdef RT_USING_SMART
struct rt_lwp *lwp = NULL;
rt_thread_t thread = rt_thread_self();
if (thread)
{
lwp = (struct rt_lwp *)thread->lwp;
}
if (lwp)
fdt = &lwp->fdt;
else
fdt = &_fdtab;
#else
fdt = &_fdtab;
#endif
return fdt;
}
#ifdef RT_USING_SMART
struct dfs_fdtable *dfs_fdtable_get_from_pid(int pid)
{
struct rt_lwp *lwp = RT_NULL;
struct dfs_fdtable *fdt = RT_NULL;
lwp_pid_lock_take();
lwp = lwp_from_pid_locked(pid);
if (lwp)
{
fdt = &lwp->fdt;
}
lwp_pid_lock_release();
return fdt;
}
#endif
struct dfs_fdtable *dfs_fdtable_get_global(void)
{
return &_fdtab;
}
/**
* @brief Dup the specified fd_src from fdt_src to fdt_dst.
*
* @param fdt_dst is the fd table for destination, if empty, use global (_fdtab).
*
* @param fdt_src is the fd table for source, if empty, use global (_fdtab).
*
* @param fd_src is the fd in the designate fdt_src table.
*
* @return -1 on failed or the allocated file descriptor.
*/
int dfs_fdtable_dup(struct dfs_fdtable *fdt_dst, struct dfs_fdtable *fdt_src, int fd_src)
{
int newfd = -1;
if (dfs_file_lock() != RT_EOK)
{
return -RT_ENOSYS;
}
if (fdt_src == NULL)
{
fdt_src = &_fdtab;
}
if (fdt_dst == NULL)
{
fdt_dst = &_fdtab;
}
/* check fd */
if ((fd_src < 0) || (fd_src >= fdt_src->maxfd))
{
goto _EXIT;
}
if (!fdt_src->fds[fd_src])
{
goto _EXIT;
}
/* get a new fd*/
newfd = fdt_fd_new(fdt_dst);
if (newfd >= 0)
{
fdt_dst->fds[newfd]->mode = fdt_src->fds[fd_src]->mode;
fdt_dst->fds[newfd]->flags = fdt_src->fds[fd_src]->flags;
fdt_dst->fds[newfd]->fops = fdt_src->fds[fd_src]->fops;
fdt_dst->fds[newfd]->dentry = dfs_dentry_ref(fdt_src->fds[fd_src]->dentry);
fdt_dst->fds[newfd]->vnode = fdt_src->fds[fd_src]->vnode;
fdt_dst->fds[newfd]->mmap_context = RT_NULL;
fdt_dst->fds[newfd]->data = fdt_src->fds[fd_src]->data;
/*
* dma-buf/socket fd is without dentry, so should used the vnode reference.
*/
if (!fdt_dst->fds[newfd]->dentry)
{
rt_atomic_add(&(fdt_dst->fds[newfd]->vnode->ref_count), 1);
}
}
_EXIT:
dfs_file_unlock();
return newfd;
}
/**
* @brief drop fd from the fd table.
*
* @param fdt is the fd table, if empty, use global (_fdtab).
*
* @param fd is the fd in the designate fd table.
*
* @return -1 on failed the drop file descriptor.
*/
int dfs_fdtable_drop_fd(struct dfs_fdtable *fdt, int fd)
{
int err = 0;
if (fdt == NULL)
{
fdt = &_fdtab;
}
if (dfs_file_lock() != RT_EOK)
{
return -RT_ENOSYS;
}
err = dfs_file_close(fdt->fds[fd]);
if (!err)
{
fdt_fd_release(fdt, fd);
}
dfs_file_unlock();
return err;
}
int dfs_dup(int oldfd, int startfd)
{
int newfd = -1;
struct dfs_fdtable *fdt = NULL;
if (dfs_file_lock() != RT_EOK)
{
return -RT_ENOSYS;
}
/* check old fd */
fdt = dfs_fdtable_get();
if ((oldfd < 0) || (oldfd >= fdt->maxfd))
{
rt_set_errno(-EBADF);
goto exit;
}
if (!fdt->fds[oldfd])
{
goto exit;
}
/* get a new fd */
newfd = _fdt_slot_alloc(fdt, startfd);
if (newfd >= 0)
{
fdt->fds[newfd] = fdt->fds[oldfd];
/* inc ref_count */
rt_atomic_add(&(fdt->fds[newfd]->ref_count), 1);
}
exit:
dfs_file_unlock();
return newfd;
}
/**
* @brief The fd in the current process dup to designate fd table.
*
* @param oldfd is the fd in current process.
*
* @param fdtab is the fd table to dup, if empty, use global (_fdtab).
*
* @return -1 on failed or the allocated file descriptor.
*/
int dfs_dup_to(int oldfd, struct dfs_fdtable *fdtab)
{
int newfd = -1;
struct dfs_fdtable *fdt = NULL;
if (dfs_file_lock() != RT_EOK)
{
return -RT_ENOSYS;
}
if (fdtab == NULL)
{
fdtab = &_fdtab;
}
/* check old fd */
fdt = dfs_fdtable_get();
if ((oldfd < 0) || (oldfd >= fdt->maxfd))
{
goto exit;
}
if (!fdt->fds[oldfd])
{
goto exit;
}
/* get a new fd*/
newfd = _fdt_slot_alloc(fdtab, DFS_STDIO_OFFSET);
if (newfd >= 0)
{
fdtab->fds[newfd] = fdt->fds[oldfd];
/* inc ref_count */
rt_atomic_add(&(fdtab->fds[newfd]->ref_count), 1);
}
exit:
dfs_file_unlock();
return newfd;
}
/**
* @brief The fd in the designate fd table dup to current process.
*
* @param oldfd is the fd in the designate fd table.
*
* @param fdtab is the fd table for oldfd, if empty, use global (_fdtab).
*
* @return -1 on failed or the allocated file descriptor.
*/
int dfs_dup_from(int oldfd, struct dfs_fdtable *fdtab)
{
int newfd = -1;
struct dfs_file *file;
if (dfs_file_lock() != RT_EOK)
{
return -RT_ENOSYS;
}
if (fdtab == NULL)
{
fdtab = &_fdtab;
}
/* check old fd */
if ((oldfd < 0) || (oldfd >= fdtab->maxfd))
{
goto exit;
}
if (!fdtab->fds[oldfd])
{
goto exit;
}
/* get a new fd*/
newfd = fd_new();
file = fd_get(newfd);
if (newfd >= 0 && file)
{
file->mode = fdtab->fds[oldfd]->mode;
file->flags = fdtab->fds[oldfd]->flags;
file->fops = fdtab->fds[oldfd]->fops;
file->dentry = dfs_dentry_ref(fdtab->fds[oldfd]->dentry);
file->vnode = fdtab->fds[oldfd]->vnode;
file->mmap_context = RT_NULL;
file->data = fdtab->fds[oldfd]->data;
}
dfs_file_close(fdtab->fds[oldfd]);
exit:
fdt_fd_release(fdtab, oldfd);
dfs_file_unlock();
return newfd;
}
#ifdef RT_USING_SMART
sysret_t sys_dup(int oldfd)
#else
int sys_dup(int oldfd)
#endif
{
int err = 0;
int newfd = dfs_dup(oldfd, (dfs_fdtable_get() == &_fdtab) ? DFS_STDIO_OFFSET : 0);
if(newfd < 0)
{
err = rt_get_errno();
}
#ifdef RT_USING_SMART
return err < 0 ? err : newfd;
#else
return err < 0 ? err : newfd;
#endif
}
rt_err_t sys_dup2(int oldfd, int newfd)
{
struct dfs_fdtable *fdt = NULL;
int ret = 0;
int retfd = -1;
if (dfs_file_lock() != RT_EOK)
{
return -RT_ENOSYS;
}
/* check old fd */
fdt = dfs_fdtable_get();
if ((oldfd < 0) || (oldfd >= fdt->maxfd))
{
goto exit;
}
if (!fdt->fds[oldfd])
{
goto exit;
}
if (newfd < 0)
{
goto exit;
}
if (newfd >= fdt->maxfd)
{
newfd = _fdt_slot_expand(fdt, newfd);
if (newfd < 0)
{
goto exit;
}
}
if (fdt->fds[newfd] == fdt->fds[oldfd])
{
/* ok, return newfd */
retfd = newfd;
goto exit;
}
if (fdt->fds[newfd])
{
ret = dfs_file_close(fdt->fds[newfd]);
if (ret < 0)
{
goto exit;
}
fd_release(newfd);
}
fdt->fds[newfd] = fdt->fds[oldfd];
/* inc ref_count */
rt_atomic_add(&(fdt->fds[newfd]->ref_count), 1);
retfd = newfd;
exit:
dfs_file_unlock();
return retfd;
}
/**
* this function will return a sub-path name under directory.
*
* @param directory the parent directory.
* @param filename the filename.
*
* @return the subdir pointer in filename
*/
const char *dfs_subdir(const char *directory, const char *filename)
{
const char *dir;
if (strlen(directory) == strlen(filename)) /* it's a same path */
return NULL;
dir = filename + strlen(directory);
if ((*dir != '/') && (dir != filename))
{
dir--;
}
return dir;
}
RTM_EXPORT(dfs_subdir);
/**
* this function will normalize a path according to specified parent directory
* and file name.
*
* @param directory the parent path
* @param filename the file name
*
* @return the built full file path (absolute path)
*/
char *dfs_normalize_path(const char *directory, const char *filename)
{
char *fullpath;
char *dst0, *dst, *src;
/* check parameters */
RT_ASSERT(filename != NULL);
#ifdef DFS_USING_WORKDIR
if (directory == NULL) /* shall use working directory */
{
#ifdef RT_USING_SMART
directory = lwp_getcwd();
#else
directory = &working_directory[0];
#endif
}
#else
if ((directory == NULL) && (filename[0] != '/'))
{
rt_kprintf(NO_WORKING_DIR);
return NULL;
}
#endif
if (filename[0] != '/') /* it's a absolute path, use it directly */
{
int path_len;
path_len = strlen(directory) + strlen(filename) + 2;
if (path_len > DFS_PATH_MAX)
{
return NULL;
}
fullpath = (char *)rt_malloc(path_len);
if (fullpath == NULL)
{
return NULL;
}
/* join path and file name */
rt_snprintf(fullpath, strlen(directory) + strlen(filename) + 2,
"%s/%s", directory, filename);
}
else
{
fullpath = rt_strdup(filename); /* copy string */
if (fullpath == NULL)
return NULL;
}
src = fullpath;
dst = fullpath;
dst0 = dst;
while (1)
{
char c = *src;
if (c == '.')
{
if (!src[1])
src++; /* '.' and ends */
else if (src[1] == '/')
{
/* './' case */
src += 2;
while ((*src == '/') && (*src != '\0'))
src++;
continue;
}
else if (src[1] == '.')
{
if (!src[2])
{
/* '..' and ends case */
src += 2;
goto up_one;
}
else if (src[2] == '/')
{
/* '../' case */
src += 3;
while ((*src == '/') && (*src != '\0'))
src++;
goto up_one;
}
}
}
/* copy up the next '/' and erase all '/' */
while ((c = *src++) != '\0' && c != '/')
*dst++ = c;
if (c == '/')
{
*dst++ = '/';
while (c == '/')
c = *src++;
src--;
}
else if (!c)
break;
continue;
up_one:
dst--;
if (dst < dst0)
{
rt_free(fullpath);
return NULL;
}
while (dst0 < dst && dst[-1] != '/')
dst--;
}
*dst = '\0';
/* remove '/' in the end of path if exist */
dst--;
if (dst > fullpath && (*dst == '/'))
*dst = '\0';
/* final check fullpath is not empty, for the special path of lwext "/.." */
if ('\0' == fullpath[0])
{
fullpath[0] = '/';
fullpath[1] = '\0';
}
return fullpath;
}
RTM_EXPORT(dfs_normalize_path);
#ifdef RT_USING_FINSH
#include <finsh.h>
int list_fd(void)
{
int index;
struct dfs_fdtable *fd_table;
fd_table = dfs_fdtable_get();
if (!fd_table) return -1;
rt_enter_critical();
rt_kprintf("fd type ref magic path\n");
rt_kprintf("-- ------ --- ----- ------\n");
for (index = 0; index < (int)fd_table->maxfd; index++)
{
struct dfs_file *file = fd_table->fds[index];
if (file && file->vnode)
{
rt_kprintf("%2d ", index);
if (file->vnode->type == FT_DIRECTORY) rt_kprintf("%-7.7s ", "dir");
else if (file->vnode->type == FT_REGULAR) rt_kprintf("%-7.7s ", "file");
else if (file->vnode->type == FT_SOCKET) rt_kprintf("%-7.7s ", "socket");
else if (file->vnode->type == FT_USER) rt_kprintf("%-7.7s ", "user");
else if (file->vnode->type == FT_DEVICE) rt_kprintf("%-7.7s ", "device");
else rt_kprintf("%-8.8s ", "unknown");
rt_kprintf("%3d ", file->ref_count);
rt_kprintf("%04x ", file->magic);
if (file->dentry)
{
rt_kprintf("%s%s\n", file->dentry->mnt->fullpath, file->dentry->pathname);
}
else
{
rt_kprintf("\n");
}
}
}
rt_exit_critical();
return 0;
}
MSH_CMD_EXPORT(list_fd, list file descriptor);
int dfs_fd_dump(int argc, char** argv)
{
int index;
if (dfs_file_lock() != RT_EOK)
{
return -RT_ENOSYS;
}
for (index = 0; index < _fdtab.maxfd; index++)
{
struct dfs_file *file = _fdtab.fds[index];
if (file)
{
char* fullpath = dfs_dentry_full_path(file->dentry);
if (fullpath)
{
printf("[%d] - %s, ref_count %zd\n", index,
fullpath, (size_t)rt_atomic_load(&(file->ref_count)));
rt_free(fullpath);
}
else
{
printf("[%d] - %s, ref_count %zd\n", index,
file->dentry->pathname, (size_t)rt_atomic_load(&(file->ref_count)));
}
}
}
dfs_file_unlock();
return 0;
}
MSH_CMD_EXPORT_ALIAS(dfs_fd_dump, fd_dump, fd dump);
#ifdef PKG_USING_DLOG
int dfs_dlog(int argc, char** argv)
{
if (argc == 2)
{
if (strcmp(argv[1], "on") == 0)
{
dlog_session_start();
dlog_participant("dfs");
dlog_participant("dfs_file");
dlog_participant("dentry");
dlog_participant("vnode");
dlog_participant("mnt");
dlog_participant("rom");
dlog_participant("devfs");
}
else if (strcmp(argv[1], "off") == 0)
{
dlog_session_stop();
}
}
return 0;
}
MSH_CMD_EXPORT(dfs_dlog, dfs dlog on|off);
#endif
#endif
/** @} */
|