forked from RT-Thread/rt-thread
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdfs_file.c
1094 lines (940 loc) · 24.4 KB
/
dfs_file.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
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) 2006-2021, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2005-02-22 Bernard The first version.
* 2011-12-08 Bernard Merges rename patch from iamcacy.
* 2015-05-27 Bernard Fix the fd clear issue.
* 2019-01-24 Bernard Remove file repeatedly open check.
*/
#include <dfs.h>
#include <dfs_file.h>
#include <dfs_private.h>
#include <unistd.h>
#define DFS_FNODE_HASH_NR 128
struct dfs_vnode_mgr
{
struct rt_mutex lock;
rt_list_t head[DFS_FNODE_HASH_NR];
};
static struct dfs_vnode_mgr dfs_fm;
void dfs_fm_lock(void)
{
rt_mutex_take(&dfs_fm.lock, RT_WAITING_FOREVER);
}
void dfs_fm_unlock(void)
{
rt_mutex_release(&dfs_fm.lock);
}
void dfs_vnode_mgr_init(void)
{
int i = 0;
rt_mutex_init(&dfs_fm.lock, "dfs_mgr", RT_IPC_FLAG_PRIO);
for (i = 0; i < DFS_FNODE_HASH_NR; i++)
{
rt_list_init(&dfs_fm.head[i]);
}
}
/* BKDR Hash Function */
static unsigned int bkdr_hash(const char *str)
{
unsigned int seed = 131; // 31 131 1313 13131 131313 etc..
unsigned int hash = 0;
while (*str)
{
hash = hash * seed + (*str++);
}
return (hash % DFS_FNODE_HASH_NR);
}
static struct dfs_vnode *dfs_vnode_find(const char *path, rt_list_t **hash_head)
{
struct dfs_vnode *vnode = NULL;
int hash = bkdr_hash(path);
rt_list_t *hh;
hh = dfs_fm.head[hash].next;
if (hash_head)
{
*hash_head = &dfs_fm.head[hash];
}
while (hh != &dfs_fm.head[hash])
{
vnode = rt_container_of(hh, struct dfs_vnode, list);
if (rt_strcmp(path, vnode->fullpath) == 0)
{
/* found */
return vnode;
}
hh = hh->next;
}
return NULL;
}
/**
* @addtogroup FileApi
* @{
*/
/**
* This function will return whether this file has been opend.
*
* @param pathname the file path name.
*
* @return 0 on file has been open successfully, -1 on open failed.
*/
int dfs_file_is_open(const char *pathname)
{
char *fullpath = NULL;
struct dfs_vnode *vnode = NULL;
int ret = 0;
fullpath = dfs_normalize_path(NULL, pathname);
dfs_fm_lock();
vnode = dfs_vnode_find(fullpath, NULL);
if (vnode)
{
ret = 1;
}
dfs_fm_unlock();
rt_free(fullpath);
return ret;
}
/**
* this function will open a file which specified by path with specified flags.
*
* @param fd the file descriptor pointer to return the corresponding result.
* @param path the specified file path.
* @param flags the flags for open operator.
*
* @return 0 on successful, -1 on failed.
*/
int dfs_file_open(struct dfs_file *fd, const char *path, int flags)
{
struct dfs_filesystem *fs;
char *fullpath;
int result;
struct dfs_vnode *vnode = NULL;
rt_list_t *hash_head;
/* parameter check */
if (fd == NULL)
return -EINVAL;
/* make sure we have an absolute path */
fullpath = dfs_normalize_path(NULL, path);
if (fullpath == NULL)
{
return -ENOMEM;
}
LOG_D("open file:%s", fullpath);
dfs_fm_lock();
/* vnode find */
vnode = dfs_vnode_find(fullpath, &hash_head);
if (vnode)
{
vnode->ref_count++;
fd->pos = 0;
fd->vnode = vnode;
dfs_fm_unlock();
rt_free(fullpath); /* release path */
}
else
{
/* find filesystem */
fs = dfs_filesystem_lookup(fullpath);
if (fs == NULL)
{
dfs_fm_unlock();
rt_free(fullpath); /* release path */
return -ENOENT;
}
vnode = rt_calloc(1, sizeof(struct dfs_vnode));
if (!vnode)
{
dfs_fm_unlock();
rt_free(fullpath); /* release path */
return -ENOMEM;
}
vnode->ref_count = 1;
LOG_D("open in filesystem:%s", fs->ops->name);
vnode->fs = fs; /* set file system */
vnode->fops = fs->ops->fops; /* set file ops */
/* initialize the fd item */
vnode->type = FT_REGULAR;
vnode->flags = 0;
if (!(fs->ops->flags & DFS_FS_FLAG_FULLPATH))
{
if (dfs_subdir(fs->path, fullpath) == NULL)
vnode->path = rt_strdup("/");
else
vnode->path = rt_strdup(dfs_subdir(fs->path, fullpath));
LOG_D("Actual file path: %s", vnode->path);
}
else
{
vnode->path = fullpath;
}
vnode->fullpath = fullpath;
/* specific file system open routine */
if (vnode->fops->open == NULL)
{
dfs_fm_unlock();
/* clear fd */
if (vnode->path != vnode->fullpath)
{
rt_free(vnode->fullpath);
}
rt_free(vnode->path);
rt_free(vnode);
return -ENOSYS;
}
fd->pos = 0;
fd->vnode = vnode;
/* insert vnode to hash */
rt_list_insert_after(hash_head, &vnode->list);
}
fd->flags = flags;
if ((result = vnode->fops->open(fd)) < 0)
{
vnode->ref_count--;
if (vnode->ref_count == 0)
{
/* remove from hash */
rt_list_remove(&vnode->list);
/* clear fd */
if (vnode->path != vnode->fullpath)
{
rt_free(vnode->fullpath);
}
rt_free(vnode->path);
fd->vnode = NULL;
rt_free(vnode);
}
dfs_fm_unlock();
LOG_D("%s open failed", fullpath);
return result;
}
fd->flags |= DFS_F_OPEN;
if (flags & O_DIRECTORY)
{
fd->vnode->type = FT_DIRECTORY;
fd->flags |= DFS_F_DIRECTORY;
}
dfs_fm_unlock();
LOG_D("open successful");
return 0;
}
/**
* this function will close a file descriptor.
*
* @param fd the file descriptor to be closed.
*
* @return 0 on successful, -1 on failed.
*/
int dfs_file_close(struct dfs_file *fd)
{
struct dfs_vnode *vnode = NULL;
int result = 0;
if (fd == NULL)
{
return -ENXIO;
}
if (fd->ref_count == 1)
{
dfs_fm_lock();
vnode = fd->vnode;
if (vnode->ref_count <= 0)
{
dfs_fm_unlock();
return -ENXIO;
}
if (vnode->fops->close != NULL)
{
result = vnode->fops->close(fd);
}
/* close fd error, return */
if (result < 0)
{
dfs_fm_unlock();
return result;
}
if (vnode->ref_count == 1)
{
/* remove from hash */
rt_list_remove(&vnode->list);
fd->vnode = NULL;
if (vnode->path != vnode->fullpath)
{
rt_free(vnode->fullpath);
}
rt_free(vnode->path);
rt_free(vnode);
}
dfs_fm_unlock();
}
return result;
}
/**
* this function will perform a io control on a file descriptor.
*
* @param fd the file descriptor.
* @param cmd the command to send to file descriptor.
* @param args the argument to send to file descriptor.
*
* @return 0 on successful, -1 on failed.
*/
int dfs_file_ioctl(struct dfs_file *fd, int cmd, void *args)
{
if (fd == NULL)
{
return -EINVAL;
}
/* regular file system fd */
if (fd->vnode->type == FT_REGULAR || fd->vnode->type == FT_DEVICE)
{
switch (cmd)
{
case F_GETFL:
return fd->flags; /* return flags */
case F_SETFL:
{
int flags = (int)(rt_base_t)args;
int mask = O_NONBLOCK | O_APPEND;
flags &= mask;
fd->flags &= ~mask;
fd->flags |= flags;
}
return 0;
}
}
if (fd->vnode->fops->ioctl != NULL)
{
return fd->vnode->fops->ioctl(fd, cmd, args);
}
return -ENOSYS;
}
/**
* this function will read specified length data from a file descriptor to a
* buffer.
*
* @param fd the file descriptor.
* @param buf the buffer to save the read data.
* @param len the length of data buffer to be read.
*
* @return the actual read data bytes or 0 on end of file or failed.
*/
int dfs_file_read(struct dfs_file *fd, void *buf, size_t len)
{
int result = 0;
if (fd == NULL)
{
return -EINVAL;
}
if (fd->vnode->fops->read == NULL)
{
return -ENOSYS;
}
if ((result = fd->vnode->fops->read(fd, buf, len)) < 0)
{
fd->flags |= DFS_F_EOF;
}
return result;
}
/**
* this function will fetch directory entries from a directory descriptor.
*
* @param fd the directory descriptor.
* @param dirp the dirent buffer to save result.
* @param nbytes the available room in the buffer.
*
* @return the read dirent, others on failed.
*/
int dfs_file_getdents(struct dfs_file *fd, struct dirent *dirp, size_t nbytes)
{
/* parameter check */
if (fd == NULL)
{
return -EINVAL;
}
if (fd->vnode->type != FT_DIRECTORY)
{
return -EINVAL;
}
if (fd->vnode->fops->getdents != NULL)
{
return fd->vnode->fops->getdents(fd, dirp, nbytes);
}
return -ENOSYS;
}
/**
* this function will unlink (remove) a specified path file from file system.
*
* @param path the specified path file to be unlinked.
*
* @return 0 on successful, -1 on failed.
*/
int dfs_file_unlink(const char *path)
{
int result;
char *fullpath;
struct dfs_filesystem *fs;
/* Make sure we have an absolute path */
fullpath = dfs_normalize_path(NULL, path);
if (fullpath == NULL)
{
return -EINVAL;
}
/* Check whether file is already open */
if (dfs_file_is_open(fullpath))
{
result = -EBUSY;
goto __exit;
}
/* get filesystem */
if ((fs = dfs_filesystem_lookup(fullpath)) == NULL)
{
result = -ENOENT;
goto __exit;
}
if (fs->ops->unlink != NULL)
{
if (!(fs->ops->flags & DFS_FS_FLAG_FULLPATH))
{
if (dfs_subdir(fs->path, fullpath) == NULL)
result = fs->ops->unlink(fs, "/");
else
result = fs->ops->unlink(fs, dfs_subdir(fs->path, fullpath));
}
else
result = fs->ops->unlink(fs, fullpath);
}
else result = -ENOSYS;
__exit:
rt_free(fullpath);
return result;
}
/**
* this function will write some specified length data to file system.
*
* @param fd the file descriptor.
* @param buf the data buffer to be written.
* @param len the data buffer length
*
* @return the actual written data length.
*/
int dfs_file_write(struct dfs_file *fd, const void *buf, size_t len)
{
if (fd == NULL)
{
return -EINVAL;
}
if (fd->vnode->fops->write == NULL)
{
return -ENOSYS;
}
return fd->vnode->fops->write(fd, buf, len);
}
/**
* this function will flush buffer on a file descriptor.
*
* @param fd the file descriptor.
*
* @return 0 on successful, -1 on failed.
*/
int dfs_file_flush(struct dfs_file *fd)
{
if (fd == NULL)
return -EINVAL;
if (fd->vnode->fops->flush == NULL)
return -ENOSYS;
return fd->vnode->fops->flush(fd);
}
/**
* this function will seek the offset for specified file descriptor.
*
* @param fd the file descriptor.
* @param offset the offset to be sought.
*
* @return the current position after seek.
*/
int dfs_file_lseek(struct dfs_file *fd, off_t offset)
{
int result;
if (fd == NULL)
return -EINVAL;
if (fd->vnode->fops->lseek == NULL)
return -ENOSYS;
result = fd->vnode->fops->lseek(fd, offset);
/* update current position */
if (result >= 0)
fd->pos = result;
return result;
}
/**
* this function will get file information.
*
* @param path the file path.
* @param buf the data buffer to save stat description.
*
* @return 0 on successful, -1 on failed.
*/
int dfs_file_stat(const char *path, struct stat *buf)
{
int result;
char *fullpath;
struct dfs_filesystem *fs;
fullpath = dfs_normalize_path(NULL, path);
if (fullpath == NULL)
{
return -1;
}
if ((fs = dfs_filesystem_lookup(fullpath)) == NULL)
{
LOG_E("can't find mounted filesystem on this path:%s", fullpath);
rt_free(fullpath);
return -ENOENT;
}
if ((fullpath[0] == '/' && fullpath[1] == '\0') ||
(dfs_subdir(fs->path, fullpath) == NULL))
{
/* it's the root directory */
buf->st_dev = 0;
buf->st_mode = S_IRUSR | S_IRGRP | S_IROTH |
S_IWUSR | S_IWGRP | S_IWOTH;
buf->st_mode |= S_IFDIR | S_IXUSR | S_IXGRP | S_IXOTH;
buf->st_size = 0;
buf->st_mtime = 0;
/* release full path */
rt_free(fullpath);
return RT_EOK;
}
else
{
if (fs->ops->stat == NULL)
{
rt_free(fullpath);
LOG_E("the filesystem didn't implement this function");
return -ENOSYS;
}
/* get the real file path and get file stat */
if (fs->ops->flags & DFS_FS_FLAG_FULLPATH)
result = fs->ops->stat(fs, fullpath, buf);
else
result = fs->ops->stat(fs, dfs_subdir(fs->path, fullpath), buf);
}
rt_free(fullpath);
return result;
}
/**
* this function will rename an old path name to a new path name.
*
* @param oldpath the old path name.
* @param newpath the new path name.
*
* @return 0 on successful, -1 on failed.
*/
int dfs_file_rename(const char *oldpath, const char *newpath)
{
int result = RT_EOK;
struct dfs_filesystem *oldfs = NULL, *newfs = NULL;
char *oldfullpath = NULL, *newfullpath = NULL;
newfullpath = NULL;
oldfullpath = NULL;
oldfullpath = dfs_normalize_path(NULL, oldpath);
if (oldfullpath == NULL)
{
result = -ENOENT;
goto __exit;
}
if (dfs_file_is_open((const char *)oldfullpath))
{
result = -EBUSY;
goto __exit;
}
newfullpath = dfs_normalize_path(NULL, newpath);
if (newfullpath == NULL)
{
result = -ENOENT;
goto __exit;
}
oldfs = dfs_filesystem_lookup(oldfullpath);
newfs = dfs_filesystem_lookup(newfullpath);
if (oldfs == newfs)
{
if (oldfs->ops->rename == NULL)
{
result = -ENOSYS;
}
else
{
if (oldfs->ops->flags & DFS_FS_FLAG_FULLPATH)
result = oldfs->ops->rename(oldfs, oldfullpath, newfullpath);
else
/* use sub directory to rename in file system */
result = oldfs->ops->rename(oldfs,
dfs_subdir(oldfs->path, oldfullpath),
dfs_subdir(newfs->path, newfullpath));
}
}
else
{
result = -EXDEV;
}
__exit:
if (oldfullpath)
{
rt_free(oldfullpath);
}
if (newfullpath)
{
rt_free(newfullpath);
}
/* not at same file system, return EXDEV */
return result;
}
/**
* this function is will cause the regular file referenced by fd
* to be truncated to a size of precisely length bytes.
*
* @param fd the file descriptor.
* @param length the length to be truncated.
*
* @return the status of truncated.
*/
int dfs_file_ftruncate(struct dfs_file *fd, off_t length)
{
int result;
/* fd is null or not a regular file system fd, or length is invalid */
if (fd == NULL || fd->vnode->type != FT_REGULAR || length < 0)
return -EINVAL;
if (fd->vnode->fops->ioctl == NULL)
return -ENOSYS;
result = fd->vnode->fops->ioctl(fd, RT_FIOFTRUNCATE, (void*)&length);
/* update current size */
if (result == 0)
fd->vnode->size = length;
return result;
}
int dfs_file_mmap2(struct dfs_file *fd, struct dfs_mmap2_args *mmap2)
{
int ret = 0;
if (fd && mmap2)
{
if (fd->vnode->type != FT_DEVICE || !fd->vnode->fops->ioctl)
{
rt_set_errno(EINVAL);
}
else if (fd->vnode->type == FT_DEVICE && fd->vnode->fops->ioctl)
{
ret = fd->vnode->fops->ioctl(fd, RT_FIOMMAP2, mmap2);
if (ret != 0)
{
ret = ret > 0? ret : -ret;
rt_set_errno(ret);
}
}
}
return ret;
}
#ifdef RT_USING_FINSH
#include <finsh.h>
void ls(const char *pathname)
{
struct dfs_file fd;
struct dirent dirent;
struct stat stat;
int length;
char *fullpath, *path;
fullpath = NULL;
if (pathname == NULL)
{
#ifdef DFS_USING_WORKDIR
/* open current working directory */
path = rt_strdup(working_directory);
#else
path = rt_strdup("/");
#endif
if (path == NULL)
return ; /* out of memory */
}
else
{
path = (char *)pathname;
}
fd_init(&fd);
/* list directory */
if (dfs_file_open(&fd, path, O_DIRECTORY) == 0)
{
rt_kprintf("Directory %s:\n", path);
do
{
rt_memset(&dirent, 0, sizeof(struct dirent));
length = dfs_file_getdents(&fd, &dirent, sizeof(struct dirent));
if (length > 0)
{
rt_memset(&stat, 0, sizeof(struct stat));
/* build full path for each file */
fullpath = dfs_normalize_path(path, dirent.d_name);
if (fullpath == NULL)
break;
if (dfs_file_stat(fullpath, &stat) == 0)
{
rt_kprintf("%-20s", dirent.d_name);
if (S_ISDIR(stat.st_mode))
{
rt_kprintf("%-25s\n", "<DIR>");
}
else
{
rt_kprintf("%-25lu\n", (unsigned long)stat.st_size);
}
}
else
rt_kprintf("BAD file: %s\n", dirent.d_name);
rt_free(fullpath);
}
}
while (length > 0);
dfs_file_close(&fd);
}
else
{
rt_kprintf("No such directory\n");
}
if (pathname == NULL)
rt_free(path);
}
FINSH_FUNCTION_EXPORT(ls, list directory contents);
void rm(const char *filename)
{
if (dfs_file_unlink(filename) < 0)
{
rt_kprintf("Delete %s failed\n", filename);
}
}
FINSH_FUNCTION_EXPORT(rm, remove files or directories);
void cat(const char *filename)
{
struct dfs_file fd;
int length = 0;
char buffer[81];
fd_init(&fd);
if (dfs_file_open(&fd, filename, O_RDONLY) < 0)
{
rt_kprintf("Open %s failed\n", filename);
return;
}
do
{
rt_memset(buffer, 0x0, sizeof(buffer));
length = dfs_file_read(&fd, (void *)buffer, sizeof(buffer) - 1);
if (length > 0)
{
buffer[length] = '\0';
rt_device_t out_device = rt_console_get_device();
rt_device_write(out_device, 0, (void *)buffer, sizeof(buffer));
}
} while (length > 0);
rt_kprintf("\n");
dfs_file_close(&fd);
}
FINSH_FUNCTION_EXPORT(cat, print file);
#ifdef DFS_USING_POSIX
#define BUF_SZ 4096
static void copyfile(const char *src, const char *dst)
{
struct dfs_file fd;
struct dfs_file src_fd;
rt_uint8_t *block_ptr;
rt_int32_t read_bytes;
block_ptr = (rt_uint8_t *)rt_malloc(BUF_SZ);
if (block_ptr == NULL)
{
rt_kprintf("out of memory\n");
return;
}
fd_init(&src_fd);
if (dfs_file_open(&src_fd, src, O_RDONLY) < 0)
{
rt_free(block_ptr);
rt_kprintf("Read %s failed\n", src);
return;
}
fd_init(&fd);
if (dfs_file_open(&fd, dst, O_WRONLY | O_CREAT) < 0)
{
rt_free(block_ptr);
dfs_file_close(&src_fd);
rt_kprintf("Write %s failed\n", dst);
return;
}
do
{
read_bytes = dfs_file_read(&src_fd, block_ptr, BUF_SZ);
if (read_bytes > 0)
{
int length;
length = dfs_file_write(&fd, block_ptr, read_bytes);
if (length != read_bytes)
{
/* write failed. */
rt_kprintf("Write file data failed, errno=%d\n", length);
break;
}
}
}
while (read_bytes > 0);
dfs_file_close(&src_fd);
dfs_file_close(&fd);
rt_free(block_ptr);
}
extern int mkdir(const char *path, mode_t mode);
static void copydir(const char *src, const char *dst)
{
struct dirent dirent;
struct stat stat;
int length;
struct dfs_file cpfd;
if (dfs_file_open(&cpfd, src, O_DIRECTORY) < 0)
{
rt_kprintf("open %s failed\n", src);
return ;
}
do
{
rt_memset(&dirent, 0, sizeof(struct dirent));
length = dfs_file_getdents(&cpfd, &dirent, sizeof(struct dirent));
if (length > 0)
{
char *src_entry_full = NULL;
char *dst_entry_full = NULL;
if (strcmp(dirent.d_name, "..") == 0 || strcmp(dirent.d_name, ".") == 0)
continue;
/* build full path for each file */
if ((src_entry_full = dfs_normalize_path(src, dirent.d_name)) == NULL)
{
rt_kprintf("out of memory!\n");
break;
}
if ((dst_entry_full = dfs_normalize_path(dst, dirent.d_name)) == NULL)
{
rt_kprintf("out of memory!\n");
rt_free(src_entry_full);
break;
}
rt_memset(&stat, 0, sizeof(struct stat));
if (dfs_file_stat(src_entry_full, &stat) != 0)
{
rt_kprintf("open file: %s failed\n", dirent.d_name);
continue;
}
if (S_ISDIR(stat.st_mode))
{
mkdir(dst_entry_full, 0);
copydir(src_entry_full, dst_entry_full);
}
else
{
copyfile(src_entry_full, dst_entry_full);
}
rt_free(src_entry_full);
rt_free(dst_entry_full);
}
}
while (length > 0);
dfs_file_close(&cpfd);
}
static const char *_get_path_lastname(const char *path)
{
char *ptr;
if ((ptr = (char *)strrchr(path, '/')) == NULL)
return path;
/* skip the '/' then return */
return ++ptr;
}
void copy(const char *src, const char *dst)
{
#define FLAG_SRC_TYPE 0x03