-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnsh_fscmds.c
1915 lines (1579 loc) · 44.7 KB
/
nsh_fscmds.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
/****************************************************************************
* apps/nshlib/nsh_fscmds.c
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. The
* ASF licenses this file to you 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.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <sys/types.h>
#include <stdint.h>
#include <stdbool.h>
#include <sys/stat.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <dirent.h>
#include <limits.h>
#include <libgen.h>
#include <errno.h>
#include <debug.h>
#if !defined(CONFIG_DISABLE_MOUNTPOINT)
# include <sys/mount.h>
# include <sys/boardctl.h>
# include <nuttx/drivers/ramdisk.h>
# ifdef CONFIG_DEV_LOOP
# include <sys/ioctl.h>
# include <nuttx/fs/loop.h>
# endif
# ifdef CONFIG_FS_SMARTFS
# include "fsutils/mksmartfs.h"
# endif
# ifdef CONFIG_SMART_DEV_LOOP
# include <sys/ioctl.h>
# include <nuttx/fs/smart.h>
# endif
# ifdef CONFIG_NFS
# include <sys/socket.h>
# include <netinet/in.h>
# include <nuttx/fs/nfs.h>
# endif
# ifdef CONFIG_RAMLOG_SYSLOG
# include <nuttx/syslog/ramlog.h>
# endif
#endif
#ifdef CONFIG_FSUTILS_MKFATFS
# include "fsutils/mkfatfs.h"
#endif
#include "nsh.h"
#include "nsh_console.h"
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
#define LSFLAGS_SIZE 1
#define LSFLAGS_LONG 2
#define LSFLAGS_RECURSIVE 4
/****************************************************************************
* Private Functions
****************************************************************************/
/****************************************************************************
* Name: nsh_getdirpath
****************************************************************************/
#if !defined(CONFIG_NSH_DISABLE_LS) || !defined(CONFIG_NSH_DISABLE_CP)
static char *nsh_getdirpath(FAR struct nsh_vtbl_s *vtbl,
FAR const char *path, FAR const char *file)
{
/* Handle the case where all that is left is '/' */
if (strcmp(path, "/") == 0)
{
snprintf(vtbl->iobuffer, IOBUFFERSIZE, "/%s", file);
}
else
{
snprintf(vtbl->iobuffer, IOBUFFERSIZE, "%s/%s", path, file);
}
vtbl->iobuffer[PATH_MAX] = '\0';
return strdup(vtbl->iobuffer);
}
#endif
/****************************************************************************
* Name: ls_specialdir
****************************************************************************/
static inline int ls_specialdir(const char *dir)
{
/* '.' and '..' directories are not listed like normal directories */
return (strcmp(dir, ".") == 0 || strcmp(dir, "..") == 0);
}
/****************************************************************************
* Name: ls_handler
****************************************************************************/
#if !defined(CONFIG_NSH_DISABLE_LS)
static int ls_handler(FAR struct nsh_vtbl_s *vtbl, FAR const char *dirpath,
FAR struct dirent *entryp, FAR void *pvarg)
{
unsigned int lsflags = (unsigned int)((uintptr_t)pvarg);
#ifdef CONFIG_PSEUDOFS_SOFTLINKS
bool isdir = false;
#endif
int ret;
/* Check if any options will require that we stat the file */
if ((lsflags & (LSFLAGS_SIZE | LSFLAGS_LONG)) != 0)
{
struct stat buf;
/* stat the file */
if (entryp != NULL)
{
FAR char *fullpath = nsh_getdirpath(vtbl, dirpath, entryp->d_name);
ret = stat(fullpath, &buf);
free(fullpath);
}
else
{
/* NULL entry signifies that we are running ls on a single file */
ret = stat(dirpath, &buf);
}
if (ret != 0)
{
nsh_error(vtbl, g_fmtcmdfailed, "ls", "stat", NSH_ERRNO);
return ERROR;
}
if ((lsflags & LSFLAGS_LONG) != 0)
{
char details[] = "----------";
#ifdef CONFIG_PSEUDOFS_SOFTLINKS
if (S_ISLNK(buf.st_mode))
{
details[0] = 'l'; /* Takes precedence over type of the target */
isdir = S_ISDIR(buf.st_mode);
}
else
#endif
if (S_ISBLK(buf.st_mode))
{
details[0] = 'b';
}
else if (S_ISCHR(buf.st_mode))
{
details[0] = 'c';
}
else if (S_ISDIR(buf.st_mode))
{
details[0] = 'd';
}
#ifdef CONFIG_MTD
else if (S_ISMTD(buf.st_mode))
{
details[0] = 'f';
}
#endif
#ifdef CONFIG_FS_SHM
else if (S_ISSHM(buf.st_mode))
{
details[0] = 'h';
}
#endif
#ifndef CONFIG_DISABLE_MQUEUE
else if (S_ISMQ(buf.st_mode))
{
details[0] = 'm';
}
#endif
#ifdef CONFIG_NET
else if (S_ISSOCK(buf.st_mode))
{
details[0] = 'n';
}
#endif
#ifdef CONFIG_FS_NAMED_SEMAPHORES
else if (S_ISSEM(buf.st_mode))
{
details[0] = 's';
}
#endif
else if (!S_ISREG(buf.st_mode))
{
details[0] = '?';
}
if ((buf.st_mode & S_IRUSR) != 0)
{
details[1] = 'r';
}
if ((buf.st_mode & S_IWUSR) != 0)
{
details[2] = 'w';
}
if ((buf.st_mode & S_IXUSR) != 0)
{
details[3] = 'x';
}
if ((buf.st_mode & S_IRGRP) != 0)
{
details[4] = 'r';
}
if ((buf.st_mode & S_IWGRP) != 0)
{
details[5] = 'w';
}
if ((buf.st_mode & S_IXGRP) != 0)
{
details[6] = 'x';
}
if ((buf.st_mode & S_IROTH) != 0)
{
details[7] = 'r';
}
if ((buf.st_mode & S_IWOTH) != 0)
{
details[8] = 'w';
}
if ((buf.st_mode & S_IXOTH) != 0)
{
details[9] = 'x';
}
nsh_output(vtbl, " %s", details);
}
if ((lsflags & LSFLAGS_SIZE) != 0)
{
nsh_output(vtbl, "%8d", buf.st_size);
}
}
/* Then provide the filename that is common to normal and verbose output */
if (entryp != NULL)
{
nsh_output(vtbl, " %s", entryp->d_name);
#ifdef CONFIG_PSEUDOFS_SOFTLINKS
if (DIRENT_ISLINK(entryp->d_type))
{
FAR char *fullpath;
ssize_t len;
/* Get the target of the symbolic link */
fullpath = nsh_getdirpath(vtbl, dirpath, entryp->d_name);
len = readlink(fullpath, vtbl->iobuffer, IOBUFFERSIZE);
free(fullpath);
if (len < 0)
{
nsh_error(vtbl, g_fmtcmdfailed, "ls", "readlink", NSH_ERRNO);
return ERROR;
}
if (isdir)
{
nsh_output(vtbl, "/ ->%s\n", vtbl->iobuffer);
}
else
{
nsh_output(vtbl, " ->%s\n", vtbl->iobuffer);
}
}
else
#endif
if (DIRENT_ISDIRECTORY(entryp->d_type) &&
!ls_specialdir(entryp->d_name))
{
nsh_output(vtbl, "/\n");
}
else
{
nsh_output(vtbl, "\n");
}
}
else
{
/* A single file */
nsh_output(vtbl, " %s\n", dirpath);
}
return OK;
}
#endif
/****************************************************************************
* Name: ls_recursive
****************************************************************************/
#if !defined(CONFIG_NSH_DISABLE_LS)
static int ls_recursive(FAR struct nsh_vtbl_s *vtbl, const char *dirpath,
struct dirent *entryp, void *pvarg)
{
int ret = OK;
/* Is this entry a directory (not a special directories, e.g. . and ..)? */
if (DIRENT_ISDIRECTORY(entryp->d_type) && !ls_specialdir(entryp->d_name))
{
/* Yes.. */
FAR char *newpath;
newpath = nsh_getdirpath(vtbl, dirpath, entryp->d_name);
/* List the directory contents */
nsh_output(vtbl, "%s:\n", newpath);
/* Traverse the directory */
ret = nsh_foreach_direntry(vtbl, "ls", newpath, ls_handler, pvarg);
if (ret == 0)
{
/* Then recurse to list each directory within the directory */
ret = nsh_foreach_direntry(vtbl, "ls", newpath, ls_recursive,
pvarg);
free(newpath);
}
}
return ret;
}
#endif /* !CONFIG_NSH_DISABLE_LS */
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: cmd_basename
****************************************************************************/
#ifndef CONFIG_NSH_DISABLE_BASENAME
int cmd_basename(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv)
{
FAR char *filename;
/* Usage: basename <path> [<suffix>]
*
* NOTE: basename() may modify path.
*/
filename = basename(argv[1]);
if (argc > 2)
{
FAR char *suffix = argv[2];
int nndx;
int sndx;
/* Check for any trailing sub-string */
nndx = strlen(filename);
sndx = strlen(suffix);
nndx -= sndx;
if (nndx > 0 && strcmp(&filename[nndx], suffix) == 0)
{
filename[nndx] = '\0';
}
}
/* And output the resulting basename */
nsh_output(vtbl, "%s\n", filename);
return OK;
}
#endif
/****************************************************************************
* Name: cmd_dirname
****************************************************************************/
#ifndef CONFIG_NSH_DISABLE_DIRNAME
int cmd_dirname(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv)
{
FAR char *filename;
/* Usage: dirname <path>
*
* NOTE: basename() may modify path.
*/
filename = dirname(argv[1]);
nsh_output(vtbl, "%s\n", filename);
return OK;
}
#endif
/****************************************************************************
* Name: cmd_cat
****************************************************************************/
#ifndef CONFIG_NSH_DISABLE_CAT
int cmd_cat(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv)
{
FAR char *fullpath;
int i;
int ret = OK;
/* Loop for each file name on the command line */
for (i = 1; i < argc && ret == OK; i++)
{
/* Get the fullpath to the file */
fullpath = nsh_getfullpath(vtbl, argv[i]);
if (fullpath == NULL)
{
nsh_error(vtbl, g_fmtcmdoutofmemory, argv[0]);
ret = ERROR;
}
else
{
/* Dump the file to the console */
ret = nsh_catfile(vtbl, argv[0], fullpath);
/* Free the allocated full path */
nsh_freefullpath(fullpath);
}
}
return ret;
}
#endif
/****************************************************************************
* Name: cmd_dmesg
****************************************************************************/
#if defined(CONFIG_RAMLOG_SYSLOG) && !defined(CONFIG_NSH_DISABLE_DMESG)
int cmd_dmesg(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv)
{
return nsh_catfile(vtbl, argv[0], CONFIG_SYSLOG_DEVPATH);
}
#endif
/****************************************************************************
* Name: cmd_cp
****************************************************************************/
#ifndef CONFIG_NSH_DISABLE_CP
int cmd_cp(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv)
{
struct stat buf;
FAR char *srcpath = NULL;
FAR char *destpath = NULL;
FAR char *allocpath = NULL;
int oflags = O_WRONLY | O_CREAT | O_TRUNC;
int rdfd;
int wrfd;
int ret = ERROR;
/* Get the full path to the source file */
srcpath = nsh_getfullpath(vtbl, argv[1]);
if (srcpath == NULL)
{
nsh_error(vtbl, g_fmtcmdoutofmemory, argv[0]);
goto errout;
}
/* Open the source file for reading */
rdfd = open(srcpath, O_RDONLY);
if (rdfd < 0)
{
nsh_error(vtbl, g_fmtcmdfailed, argv[0], "open", NSH_ERRNO);
goto errout_with_srcpath;
}
/* Get the full path to the destination file or directory */
destpath = nsh_getfullpath(vtbl, argv[2]);
if (destpath == NULL)
{
nsh_error(vtbl, g_fmtcmdoutofmemory, argv[0]);
goto errout_with_rdfd;
}
/* Check if the destination is a directory */
ret = stat(destpath, &buf);
if (ret == 0)
{
/* Something exists here... is it a directory? */
if (S_ISDIR(buf.st_mode))
{
/* Yes, it is a directory.
* Remove any trailing '/' characters from the path
*/
nsh_trimdir(destpath);
/* Construct the full path to the new file */
allocpath = nsh_getdirpath(vtbl, destpath, basename(argv[1]));
if (!allocpath)
{
nsh_error(vtbl, g_fmtcmdoutofmemory, argv[0]);
goto errout_with_destpath;
}
/* Open then dest for writing */
nsh_freefullpath(destpath);
destpath = allocpath;
}
else if (!S_ISREG(buf.st_mode))
{
/* Maybe it is a driver? */
oflags = O_WRONLY;
}
}
/* Check if the destination does not match the source */
if (strcmp(destpath, srcpath) == 0)
{
nsh_error(vtbl, g_fmtsyntax, argv[0]);
goto errout_with_allocpath;
}
/* Now open the destination */
wrfd = open(destpath, oflags, 0666);
if (wrfd < 0)
{
nsh_error(vtbl, g_fmtcmdfailed, argv[0], "open", NSH_ERRNO);
goto errout_with_allocpath;
}
/* Now copy the file */
for (; ; )
{
int nbytesread;
int nbyteswritten;
do
{
nbytesread = read(rdfd, vtbl->iobuffer, IOBUFFERSIZE);
if (nbytesread == 0)
{
/* End of file */
ret = OK;
goto errout_with_wrfd;
}
else if (nbytesread < 0)
{
/* EINTR is not an error (but will still stop the copy) */
if (errno == EINTR)
{
nsh_error(vtbl, g_fmtsignalrecvd, argv[0]);
}
else
{
/* Read error */
nsh_error(vtbl, g_fmtcmdfailed, argv[0], "read",
NSH_ERRNO);
}
goto errout_with_wrfd;
}
}
while (nbytesread <= 0);
do
{
nbyteswritten = write(wrfd, vtbl->iobuffer, nbytesread);
if (nbyteswritten >= 0)
{
nbytesread -= nbyteswritten;
}
else
{
/* EINTR is not an error (but will still stop the copy) */
if (errno == EINTR)
{
nsh_error(vtbl, g_fmtsignalrecvd, argv[0]);
}
else
{
/* Read error */
nsh_error(vtbl, g_fmtcmdfailed, argv[0], "write",
NSH_ERRNO);
}
goto errout_with_wrfd;
}
}
while (nbytesread > 0);
}
errout_with_wrfd:
close(wrfd);
errout_with_allocpath:
if (allocpath)
{
free(allocpath);
}
errout_with_destpath:
if (destpath && !allocpath)
{
nsh_freefullpath(destpath);
}
errout_with_rdfd:
close(rdfd);
errout_with_srcpath:
if (srcpath)
{
nsh_freefullpath(srcpath);
}
errout:
return ret;
}
#endif
/****************************************************************************
* Name: cmd_losetup
****************************************************************************/
#ifndef CONFIG_DISABLE_MOUNTPOINT
# if defined(CONFIG_DEV_LOOP) && !defined(CONFIG_NSH_DISABLE_LOSETUP)
int cmd_losetup(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv)
{
FAR char *loopdev = NULL;
FAR char *filepath = NULL;
struct losetup_s setup;
bool teardown = false;
bool readonly = false;
off_t offset = 0;
bool badarg = false;
int ret = ERROR;
int option;
int fd;
/* Get the losetup options: Two forms are supported:
*
* losetup -d <loop-device>
* losetup [-o <offset>] [-r] <loop-device> <filename>
*
* NOTE that the -o and -r options are accepted with the -d option, but
* will be ignored.
*/
while ((option = getopt(argc, argv, "d:o:r")) != ERROR)
{
switch (option)
{
case 'd':
loopdev = nsh_getfullpath(vtbl, optarg);
teardown = true;
break;
case 'o':
offset = atoi(optarg);
break;
case 'r':
readonly = true;
break;
case '?':
default:
nsh_error(vtbl, g_fmtarginvalid, argv[0]);
badarg = true;
break;
}
}
/* If a bad argument was encountered,
* then return without processing the command
*/
if (badarg)
{
goto errout_with_paths;
}
/* If this is not a tear down operation, then additional command line
* parameters are required.
*/
if (!teardown)
{
/* There must be two arguments on the command line after the options */
if (optind + 1 < argc)
{
loopdev = nsh_getfullpath(vtbl, argv[optind]);
optind++;
filepath = nsh_getfullpath(vtbl, argv[optind]);
optind++;
}
else
{
nsh_error(vtbl, g_fmtargrequired, argv[0]);
goto errout_with_paths;
}
}
/* There should be nothing else on the command line */
if (optind < argc)
{
nsh_error(vtbl, g_fmttoomanyargs, argv[0]);
goto errout_with_paths;
}
/* Open the loop device */
fd = open("/dev/loop", O_RDONLY);
if (fd < 0)
{
nsh_error(vtbl, g_fmtcmdfailed, argv[0], "open", NSH_ERRNO);
goto errout_with_paths;
}
/* Perform the teardown operation */
if (teardown)
{
/* Tear down the loop device. */
ret = ioctl(fd, LOOPIOC_TEARDOWN, (unsigned long)((uintptr_t)loopdev));
if (ret < 0)
{
nsh_error(vtbl, g_fmtcmdfailed, argv[0], "ioctl", NSH_ERRNO);
goto errout_with_fd;
}
}
else
{
/* Set up the loop device */
setup.devname = loopdev; /* The loop block device to be created */
setup.filename = filepath; /* The file or character device to use */
setup.sectsize = 512; /* The sector size to use with the block device */
setup.offset = offset; /* An offset that may be applied to the device */
setup.readonly = readonly; /* True: Read access will be supported only */
ret = ioctl(fd, LOOPIOC_SETUP, (unsigned long)((uintptr_t)&setup));
if (ret < 0)
{
nsh_error(vtbl, g_fmtcmdfailed, argv[0], "ioctl", NSH_ERRNO);
goto errout_with_fd;
}
}
ret = OK;
/* Free resources */
errout_with_fd:
close(fd);
errout_with_paths:
if (loopdev)
{
free(loopdev);
}
if (filepath)
{
free(filepath);
}
return ret;
}
#endif
#endif
/****************************************************************************
* Name: cmd_losmart
****************************************************************************/
#ifndef CONFIG_DISABLE_MOUNTPOINT
# if defined(CONFIG_SMART_DEV_LOOP) && !defined(CONFIG_NSH_DISABLE_LOSMART)
int cmd_losmart(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv)
{
FAR char *loopdev = NULL;
FAR char *filepath = NULL;
struct smart_losetup_s setup;
bool teardown = false;
bool readonly = false;
int minor = -1;
int erasesize = -1;
int sectsize = -1;
off_t offset = 0;
bool badarg = false;
int ret = ERROR;
int option;
int fd;
/* Get the losetup options: Two forms are supported:
*
* losmart -d <loop-device>
* losmart [-m minor-number] [-o <offset>] [-e erasesize] [-s sectsize]
* [-r] <filename>
*
* NOTE that the -o and -r options are accepted with the -d option, but
* will be ignored.
*/
while ((option = getopt(argc, argv, "d:e:m:o:rs:")) != ERROR)
{
switch (option)
{
case 'd':
loopdev = nsh_getfullpath(vtbl, optarg);
teardown = true;
break;
case 'e':
erasesize = atoi(optarg);
break;
case 'm':
minor = atoi(optarg);
break;
case 'o':
offset = atoi(optarg);
break;
case 'r':
readonly = true;
break;
case 's':
sectsize = atoi(optarg);
break;
case '?':
default:
nsh_error(vtbl, g_fmtarginvalid, argv[0]);
badarg = true;
break;
}
}
/* If a bad argument was encountered,
* then return without processing the command
*/
if (badarg)
{
goto errout_with_paths;
}
/* If this is not a tear down operation, then additional command line
* parameters are required.
*/
if (!teardown)
{
/* There must be two arguments on the command line after the options */
if (optind < argc)
{
filepath = nsh_getfullpath(vtbl, argv[optind]);
optind++;
}
else
{
nsh_error(vtbl, g_fmtargrequired, argv[0]);
goto errout_with_paths;
}
}
/* There should be nothing else on the command line */
if (optind < argc)
{
nsh_error(vtbl, g_fmttoomanyargs, argv[0]);
goto errout_with_paths;
}
/* Open the loop device */
fd = open("/dev/smart", O_RDONLY);
if (fd < 0)
{
nsh_error(vtbl, g_fmtcmdfailed, argv[0], "open", NSH_ERRNO);
goto errout_with_paths;
}
/* Perform the teardown operation */
if (teardown)
{
/* Tear down the loop device. */
ret = ioctl(fd, SMART_LOOPIOC_TEARDOWN,
(unsigned long)((uintptr_t) loopdev));
if (ret < 0)
{
nsh_error(vtbl, g_fmtcmdfailed, argv[0], "ioctl", NSH_ERRNO);
goto errout_with_fd;
}
}
else
{
/* Set up the loop device */
setup.minor = minor; /* The loop block device to be created */
setup.filename = filepath; /* The file or character device to use */
setup.sectsize = sectsize; /* The sector size to use with the block device */
setup.erasesize = erasesize; /* The sector size to use with the block device */
setup.offset = offset; /* An offset that may be applied to the device */
setup.readonly = readonly; /* True: Read access will be supported only */
ret = ioctl(fd, SMART_LOOPIOC_SETUP,
(unsigned long)((uintptr_t)&setup));
if (ret < 0)
{
nsh_error(vtbl, g_fmtcmdfailed, argv[0], "ioctl", NSH_ERRNO);
goto errout_with_fd;
}
}
ret = OK;
/* Free resources */
errout_with_fd:
close(fd);
errout_with_paths:
if (loopdev)
{
free(loopdev);
}
if (filepath)
{
free(filepath);