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
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
|
/*-
* SPDX-License-Identifier: BSD-3-Clause
*
* Copyright (c) 1989, 1993, 1994
* The Regents of the University of California. All rights reserved.
* Copyright (c) 2026
* Project Tick. All rights reserved.
*
* This code is derived from software contributed to Berkeley by
* Ken Smith of The State University of New York at Buffalo.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. 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.
* 3. Neither the name of the University 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 REGENTS 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 REGENTS 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 _POSIX_C_SOURCE 200809L
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/xattr.h>
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#ifndef O_CLOEXEC
#define O_CLOEXEC 0
#endif
#ifndef AT_NO_AUTOMOUNT
#define AT_NO_AUTOMOUNT 0
#endif
#define MV_EXIT_ERROR 1
#define MV_EXIT_USAGE 2
#define COPY_BUFFER_MIN (128U * 1024U)
#define COPY_BUFFER_MAX (2U * 1024U * 1024U)
struct mv_options {
bool force;
bool interactive;
bool no_clobber;
bool no_target_dir_follow;
bool verbose;
};
struct move_target {
bool treat_as_directory;
char *path;
};
static const char *progname;
static int append_child_basename(const char *directory, const char *source,
char **result);
static int apply_existing_target_policy(const struct mv_options *options,
const char *target, bool *skip_move);
static int apply_path_metadata(const char *target, const struct stat *source_sb,
bool nofollow, const char *source);
static size_t basename_len(const char *path);
static const char *basename_start(const char *path);
static int cleanup_failed_target(const char *target);
static int copy_directory_tree(const char *source, const char *target,
const struct stat *source_sb);
static int copy_file_data(int from_fd, int to_fd, const char *source,
const char *target);
static int copy_file_xattrs(int source_fd, int dest_fd, const char *source,
const char *target);
static int copy_move_fallback(const struct mv_options *options,
const char *source, const char *target,
const struct stat *source_sb, const struct stat *target_sb);
static int copy_node(const char *source, const char *target,
const struct stat *source_sb);
static int copy_non_regular(const char *source, const char *target,
const struct stat *source_sb);
static int copy_path_xattrs(const char *source, const char *target,
bool nofollow);
static int copy_regular_file(const char *source, const char *target,
const struct stat *source_sb);
static int determine_target_mode(const struct mv_options *options, int argc,
char *const argv[], struct move_target *target);
static char *dirname_dup(const char *path);
static void error_errno(const char *fmt, ...);
static void error_msg(const char *fmt, ...);
static int file_list_xattrs(int fd, const char *source, char **names_buf,
ssize_t *names_len);
static int handle_single_move(const struct mv_options *options,
const char *source, const char *target);
static int is_dir_symlink(const char *path);
static int is_mount_point(const char *path, const struct stat *source_sb,
bool *mount_point);
static char *join_path(const char *dir, const char *name);
static int path_list_xattrs(const char *source, bool nofollow,
char **names_buf, ssize_t *names_len);
static int preserve_timestamps_fd(int fd, const struct stat *source_sb,
const char *target);
static int preserve_timestamps_path(const char *target,
const struct stat *source_sb, bool nofollow);
static const char *program_name(const char *argv0);
static int prompt_overwrite(const char *target);
static int readlink_alloc(const char *path, char **targetp);
static int remove_source_tree(const char *path, const struct stat *sb);
static int remove_target_path(const char *target, const struct stat *target_sb);
static int set_fd_ownership_and_mode(int fd, const struct stat *source_sb,
const char *target);
static int set_path_ownership_and_mode(const char *target,
const struct stat *source_sb, bool nofollow);
static int set_xattr_loop(const char *source, const char *target, char *names,
ssize_t names_len, bool nofollow, int source_fd, int dest_fd);
static int should_prompt_for_permissions(const char *target);
static void usage(void);
static void *xmalloc(size_t size);
static void *xrealloc(void *ptr, size_t size);
static char *xstrdup(const char *text);
extern int mknod(const char *path, mode_t mode, dev_t dev);
static const char *
program_name(const char *argv0)
{
const char *name;
if (argv0 == NULL || argv0[0] == '\0')
return ("mv");
name = strrchr(argv0, '/');
return (name == NULL ? argv0 : name + 1);
}
static void
verror_message(bool with_errno, const char *fmt, va_list ap)
{
int saved_errno;
saved_errno = errno;
(void)fprintf(stderr, "%s: ", progname);
(void)vfprintf(stderr, fmt, ap);
if (with_errno)
(void)fprintf(stderr, ": %s", strerror(saved_errno));
(void)fputc('\n', stderr);
}
static void
error_errno(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
verror_message(true, fmt, ap);
va_end(ap);
}
static void
error_msg(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
verror_message(false, fmt, ap);
va_end(ap);
}
static void *
xmalloc(size_t size)
{
void *ptr;
ptr = malloc(size);
if (ptr == NULL) {
error_msg("out of memory");
exit(MV_EXIT_ERROR);
}
return (ptr);
}
static void *
xrealloc(void *ptr, size_t size)
{
void *newptr;
newptr = realloc(ptr, size);
if (newptr == NULL) {
error_msg("out of memory");
exit(MV_EXIT_ERROR);
}
return (newptr);
}
static char *
xstrdup(const char *text)
{
size_t len;
char *copy;
len = strlen(text) + 1;
copy = xmalloc(len);
memcpy(copy, text, len);
return (copy);
}
static const char *
basename_start(const char *path)
{
const char *end;
const char *start;
if (path[0] == '\0')
return (path);
end = path + strlen(path);
while (end > path && end[-1] == '/')
end--;
if (end == path)
return (path);
start = end;
while (start > path && start[-1] != '/')
start--;
return (start);
}
static size_t
basename_len(const char *path)
{
const char *end;
const char *start;
if (path[0] == '\0')
return (1);
end = path + strlen(path);
while (end > path && end[-1] == '/')
end--;
if (end == path)
return (1);
start = basename_start(path);
return ((size_t)(end - start));
}
static char *
dirname_dup(const char *path)
{
const char *end;
const char *slash;
size_t len;
char *dir;
if (path[0] == '\0')
return (xstrdup("."));
end = path + strlen(path);
while (end > path && end[-1] == '/')
end--;
if (end == path)
return (xstrdup("/"));
slash = end;
while (slash > path && slash[-1] != '/')
slash--;
if (slash == path)
return (xstrdup("."));
while (slash > path && slash[-1] == '/')
slash--;
if (slash == path)
return (xstrdup("/"));
len = (size_t)(slash - path);
dir = xmalloc(len + 1);
memcpy(dir, path, len);
dir[len] = '\0';
return (dir);
}
static char *
join_path(const char *dir, const char *name)
{
size_t dir_len;
size_t name_len;
bool need_sep;
char *path;
dir_len = strlen(dir);
name_len = strlen(name);
need_sep = dir_len != 0 && dir[dir_len - 1] != '/';
path = xmalloc(dir_len + (need_sep ? 1U : 0U) + name_len + 1U);
memcpy(path, dir, dir_len);
if (need_sep)
path[dir_len++] = '/';
memcpy(path + dir_len, name, name_len);
path[dir_len + name_len] = '\0';
return (path);
}
static int
append_child_basename(const char *directory, const char *source, char **result)
{
const char *base;
size_t base_len;
char *name;
base = basename_start(source);
base_len = basename_len(source);
name = xmalloc(base_len + 1);
memcpy(name, base, base_len);
name[base_len] = '\0';
*result = join_path(directory, name);
free(name);
return (0);
}
static void
usage(void)
{
(void)fprintf(stderr, "%s\n%s\n",
"usage: mv [-f | -i | -n] [-hv] source target",
" mv [-f | -i | -n] [-v] source ... directory");
exit(MV_EXIT_USAGE);
}
static int
prompt_overwrite(const char *target)
{
int ch;
int first;
(void)fprintf(stderr, "overwrite %s? (y/n [n]) ", target);
first = getchar();
ch = first;
while (ch != '\n' && ch != EOF)
ch = getchar();
return (first == 'y' || first == 'Y');
}
static int
should_prompt_for_permissions(const char *target)
{
struct stat sb;
if (!isatty(STDIN_FILENO))
return (0);
if (access(target, W_OK) == 0)
return (0);
if (stat(target, &sb) != 0)
return (0);
return (1);
}
static int
apply_existing_target_policy(const struct mv_options *options, const char *target,
bool *skip_move)
{
int prompt;
*skip_move = false;
if (options->force)
return (0);
if (options->no_clobber) {
if (options->verbose)
(void)printf("%s not overwritten\n", target);
*skip_move = true;
return (0);
}
prompt = options->interactive || should_prompt_for_permissions(target);
if (!prompt)
return (0);
if (!prompt_overwrite(target)) {
(void)fprintf(stderr, "not overwritten\n");
*skip_move = true;
}
return (0);
}
static int
readlink_alloc(const char *path, char **targetp)
{
size_t size;
ssize_t len;
char *buf;
size = 128;
buf = xmalloc(size);
for (;;) {
len = readlink(path, buf, size);
if (len < 0) {
free(buf);
return (-1);
}
if ((size_t)len < size) {
buf[len] = '\0';
*targetp = buf;
return (0);
}
size *= 2;
buf = xrealloc(buf, size);
}
}
static int
file_list_xattrs(int fd, const char *source, char **names_buf, ssize_t *names_len)
{
ssize_t len;
char *buf;
len = flistxattr(fd, NULL, 0);
if (len < 0) {
if (errno == ENOTSUP || errno == EOPNOTSUPP) {
*names_buf = NULL;
*names_len = 0;
return (0);
}
error_errno("list xattrs for %s", source);
return (-1);
}
if (len == 0) {
*names_buf = NULL;
*names_len = 0;
return (0);
}
buf = xmalloc((size_t)len);
if (flistxattr(fd, buf, (size_t)len) != len) {
error_errno("list xattrs for %s", source);
free(buf);
return (-1);
}
*names_buf = buf;
*names_len = len;
return (0);
}
static int
path_list_xattrs(const char *source, bool nofollow, char **names_buf, ssize_t *names_len)
{
ssize_t len;
char *buf;
if (nofollow)
len = llistxattr(source, NULL, 0);
else
len = listxattr(source, NULL, 0);
if (len < 0) {
if (errno == ENOTSUP || errno == EOPNOTSUPP) {
*names_buf = NULL;
*names_len = 0;
return (0);
}
error_errno("list xattrs for %s", source);
return (-1);
}
if (len == 0) {
*names_buf = NULL;
*names_len = 0;
return (0);
}
buf = xmalloc((size_t)len);
if ((nofollow ? llistxattr(source, buf, (size_t)len) :
listxattr(source, buf, (size_t)len)) != len) {
error_errno("list xattrs for %s", source);
free(buf);
return (-1);
}
*names_buf = buf;
*names_len = len;
return (0);
}
static int
set_xattr_loop(const char *source, const char *target, char *names,
ssize_t names_len, bool nofollow, int source_fd, int dest_fd)
{
char *name;
char *limit;
limit = names + names_len;
for (name = names; name < limit; name += strlen(name) + 1) {
ssize_t value_len;
void *value;
int set_result;
if (source_fd >= 0)
value_len = fgetxattr(source_fd, name, NULL, 0);
else if (nofollow)
value_len = lgetxattr(source, name, NULL, 0);
else
value_len = getxattr(source, name, NULL, 0);
if (value_len < 0) {
error_errno("read xattr '%s' from %s", name, source);
return (-1);
}
value = xmalloc(value_len == 0 ? 1U : (size_t)value_len);
if (value_len > 0) {
if (source_fd >= 0)
value_len = fgetxattr(source_fd, name, value,
(size_t)value_len);
else if (nofollow)
value_len = lgetxattr(source, name, value,
(size_t)value_len);
else
value_len = getxattr(source, name, value,
(size_t)value_len);
if (value_len < 0) {
error_errno("read xattr '%s' from %s", name, source);
free(value);
return (-1);
}
}
if (dest_fd >= 0)
set_result = fsetxattr(dest_fd, name, value,
(size_t)value_len, 0);
else if (nofollow)
set_result = lsetxattr(target, name, value,
(size_t)value_len, 0);
else
set_result = setxattr(target, name, value,
(size_t)value_len, 0);
if (set_result != 0) {
error_errno("set xattr '%s' on %s", name, target);
free(value);
return (-1);
}
free(value);
}
return (0);
}
static int
copy_file_xattrs(int source_fd, int dest_fd, const char *source, const char *target)
{
char *names;
ssize_t names_len;
int rc;
names = NULL;
names_len = 0;
rc = file_list_xattrs(source_fd, source, &names, &names_len);
if (rc != 0)
return (rc);
if (names_len == 0)
return (0);
rc = set_xattr_loop(source, target, names, names_len, false, source_fd,
dest_fd);
free(names);
return (rc);
}
static int
copy_path_xattrs(const char *source, const char *target, bool nofollow)
{
char *names;
ssize_t names_len;
int rc;
names = NULL;
names_len = 0;
rc = path_list_xattrs(source, nofollow, &names, &names_len);
if (rc != 0)
return (rc);
if (names_len == 0)
return (0);
rc = set_xattr_loop(source, target, names, names_len, nofollow, -1, -1);
free(names);
return (rc);
}
static int
preserve_timestamps_fd(int fd, const struct stat *source_sb, const char *target)
{
struct timespec times[2];
times[0] = source_sb->st_atim;
times[1] = source_sb->st_mtim;
if (futimens(fd, times) != 0) {
error_errno("set times on %s", target);
return (-1);
}
return (0);
}
static int
preserve_timestamps_path(const char *target, const struct stat *source_sb,
bool nofollow)
{
struct timespec times[2];
int flags;
times[0] = source_sb->st_atim;
times[1] = source_sb->st_mtim;
flags = nofollow ? AT_SYMLINK_NOFOLLOW : 0;
if (utimensat(AT_FDCWD, target, times, flags) != 0) {
error_errno("set times on %s", target);
return (-1);
}
return (0);
}
static int
set_fd_ownership_and_mode(int fd, const struct stat *source_sb, const char *target)
{
struct stat current_sb;
mode_t mode;
if (fstat(fd, ¤t_sb) != 0) {
error_errno("stat %s", target);
return (-1);
}
mode = source_sb->st_mode & 07777;
if (current_sb.st_uid != source_sb->st_uid ||
current_sb.st_gid != source_sb->st_gid) {
if (fchown(fd, source_sb->st_uid, source_sb->st_gid) != 0) {
if (errno != EPERM) {
error_errno("set owner on %s", target);
return (-1);
}
mode &= ~(S_ISUID | S_ISGID);
}
}
if ((current_sb.st_mode & 07777) != mode && fchmod(fd, mode) != 0) {
error_errno("set mode on %s", target);
return (-1);
}
return (0);
}
static int
set_path_ownership_and_mode(const char *target, const struct stat *source_sb,
bool nofollow)
{
struct stat current_sb;
mode_t mode;
int flags;
flags = nofollow ? AT_SYMLINK_NOFOLLOW : 0;
if ((nofollow ? lstat(target, ¤t_sb) : stat(target, ¤t_sb)) != 0) {
error_errno("stat %s", target);
return (-1);
}
mode = source_sb->st_mode & 07777;
if (current_sb.st_uid != source_sb->st_uid ||
current_sb.st_gid != source_sb->st_gid) {
if (fchownat(AT_FDCWD, target, source_sb->st_uid, source_sb->st_gid,
flags) != 0) {
if (errno != EPERM) {
error_errno("set owner on %s", target);
return (-1);
}
mode &= ~(S_ISUID | S_ISGID);
}
}
if (!nofollow && (current_sb.st_mode & 07777) != mode &&
fchmodat(AT_FDCWD, target, mode, 0) != 0) {
error_errno("set mode on %s", target);
return (-1);
}
return (0);
}
static int
apply_path_metadata(const char *target, const struct stat *source_sb,
bool nofollow, const char *source)
{
if (set_path_ownership_and_mode(target, source_sb, nofollow) != 0)
return (-1);
if (copy_path_xattrs(source, target, nofollow) != 0)
return (-1);
return (preserve_timestamps_path(target, source_sb, nofollow));
}
static size_t
copy_buffer_size(void)
{
long pages;
long pagesize;
size_t size;
pages = sysconf(_SC_PHYS_PAGES);
pagesize = sysconf(_SC_PAGESIZE);
if (pages > 0 && pagesize > 0) {
uint64_t total;
total = (uint64_t)pages * (uint64_t)pagesize;
if (total >= (uint64_t)(512U * 1024U * 1024U))
return (COPY_BUFFER_MAX);
}
size = COPY_BUFFER_MIN;
return (size);
}
static int
copy_file_data(int from_fd, int to_fd, const char *source, const char *target)
{
char *buffer;
size_t buffer_size;
buffer_size = copy_buffer_size();
buffer = xmalloc(buffer_size);
for (;;) {
ssize_t read_count;
char *outp;
read_count = read(from_fd, buffer, buffer_size);
if (read_count == 0)
break;
if (read_count < 0) {
if (errno == EINTR)
continue;
error_errno("read %s", source);
free(buffer);
return (-1);
}
outp = buffer;
while (read_count > 0) {
ssize_t write_count;
write_count = write(to_fd, outp, (size_t)read_count);
if (write_count < 0) {
if (errno == EINTR)
continue;
error_errno("write %s", target);
free(buffer);
return (-1);
}
outp += write_count;
read_count -= write_count;
}
}
free(buffer);
return (0);
}
static int
copy_regular_file(const char *source, const char *target, const struct stat *source_sb)
{
int source_fd;
int target_fd;
int saved_errno;
int rc;
source_fd = open(source, O_RDONLY | O_CLOEXEC | O_NOFOLLOW);
if (source_fd < 0) {
error_errno("%s", source);
return (-1);
}
target_fd = open(target, O_WRONLY | O_CREAT | O_EXCL | O_TRUNC | O_CLOEXEC,
0600);
if (target_fd < 0) {
error_errno("%s", target);
(void)close(source_fd);
return (-1);
}
rc = copy_file_data(source_fd, target_fd, source, target);
if (rc == 0 && set_fd_ownership_and_mode(target_fd, source_sb, target) != 0)
rc = -1;
if (rc == 0 && copy_file_xattrs(source_fd, target_fd, source, target) != 0)
rc = -1;
if (rc == 0 && preserve_timestamps_fd(target_fd, source_sb, target) != 0)
rc = -1;
saved_errno = errno;
if (close(target_fd) != 0 && rc == 0) {
error_errno("%s", target);
rc = -1;
saved_errno = errno;
}
(void)close(source_fd);
if (rc != 0) {
errno = saved_errno;
(void)unlink(target);
return (-1);
}
return (0);
}
static int
copy_non_regular(const char *source, const char *target, const struct stat *source_sb)
{
char *link_target;
if (S_ISLNK(source_sb->st_mode)) {
link_target = NULL;
if (readlink_alloc(source, &link_target) != 0) {
error_errno("readlink %s", source);
return (-1);
}
if (symlink(link_target, target) != 0) {
error_errno("symlink %s", target);
free(link_target);
return (-1);
}
free(link_target);
if (apply_path_metadata(target, source_sb, true, source) != 0) {
(void)unlink(target);
return (-1);
}
return (0);
}
if (S_ISFIFO(source_sb->st_mode)) {
if (mkfifo(target, source_sb->st_mode & 07777) != 0) {
error_errno("mkfifo %s", target);
return (-1);
}
if (apply_path_metadata(target, source_sb, false, source) != 0) {
(void)unlink(target);
return (-1);
}
return (0);
}
if (S_ISCHR(source_sb->st_mode) || S_ISBLK(source_sb->st_mode)) {
if (mknod(target, source_sb->st_mode, source_sb->st_rdev) != 0) {
error_errno("mknod %s", target);
return (-1);
}
if (apply_path_metadata(target, source_sb, false, source) != 0) {
(void)unlink(target);
return (-1);
}
return (0);
}
if (S_ISSOCK(source_sb->st_mode))
error_msg("cannot move socket across filesystems: %s", source);
else
error_msg("unsupported file type for cross-filesystem move: %s",
source);
return (-1);
}
static int
copy_directory_tree(const char *source, const char *target, const struct stat *source_sb)
{
DIR *dirp;
struct dirent *entry;
mode_t mode;
int rc;
mode = (source_sb->st_mode & 07777) | S_IRWXU;
if (mkdir(target, mode) != 0) {
error_errno("mkdir %s", target);
return (-1);
}
dirp = opendir(source);
if (dirp == NULL) {
error_errno("opendir %s", source);
(void)rmdir(target);
return (-1);
}
rc = 0;
while ((entry = readdir(dirp)) != NULL) {
struct stat child_sb;
char *child_source;
char *child_target;
if ((entry->d_name[0] == '.' && entry->d_name[1] == '\0') ||
(entry->d_name[0] == '.' && entry->d_name[1] == '.' &&
entry->d_name[2] == '\0'))
continue;
child_source = join_path(source, entry->d_name);
child_target = join_path(target, entry->d_name);
if (lstat(child_source, &child_sb) != 0) {
error_errno("%s", child_source);
free(child_source);
free(child_target);
rc = -1;
break;
}
if (copy_node(child_source, child_target, &child_sb) != 0) {
free(child_source);
free(child_target);
rc = -1;
break;
}
free(child_source);
free(child_target);
}
if (closedir(dirp) != 0 && rc == 0) {
error_errno("closedir %s", source);
rc = -1;
}
if (rc != 0)
return (-1);
if (apply_path_metadata(target, source_sb, false, source) != 0)
return (-1);
return (0);
}
static int
copy_node(const char *source, const char *target, const struct stat *source_sb)
{
if (S_ISREG(source_sb->st_mode))
return (copy_regular_file(source, target, source_sb));
if (S_ISDIR(source_sb->st_mode))
return (copy_directory_tree(source, target, source_sb));
return (copy_non_regular(source, target, source_sb));
}
static int
remove_source_tree(const char *path, const struct stat *sb)
{
DIR *dirp;
struct dirent *entry;
int rc;
if (!S_ISDIR(sb->st_mode)) {
if (unlink(path) != 0) {
error_errno("remove %s", path);
return (-1);
}
return (0);
}
dirp = opendir(path);
if (dirp == NULL) {
error_errno("opendir %s", path);
return (-1);
}
rc = 0;
while ((entry = readdir(dirp)) != NULL) {
struct stat child_sb;
char *child_path;
if ((entry->d_name[0] == '.' && entry->d_name[1] == '\0') ||
(entry->d_name[0] == '.' && entry->d_name[1] == '.' &&
entry->d_name[2] == '\0'))
continue;
child_path = join_path(path, entry->d_name);
if (lstat(child_path, &child_sb) != 0) {
error_errno("%s", child_path);
free(child_path);
rc = -1;
break;
}
if (remove_source_tree(child_path, &child_sb) != 0) {
free(child_path);
rc = -1;
break;
}
free(child_path);
}
if (closedir(dirp) != 0 && rc == 0) {
error_errno("closedir %s", path);
rc = -1;
}
if (rc != 0)
return (-1);
if (rmdir(path) != 0) {
error_errno("remove %s", path);
return (-1);
}
return (0);
}
static int
cleanup_failed_target(const char *target)
{
struct stat sb;
if (lstat(target, &sb) != 0) {
if (errno == ENOENT)
return (0);
error_errno("%s", target);
return (-1);
}
return (remove_source_tree(target, &sb));
}
static int
remove_target_path(const char *target, const struct stat *target_sb)
{
if (S_ISDIR(target_sb->st_mode)) {
if (rmdir(target) != 0) {
error_errno("remove %s", target);
return (-1);
}
return (0);
}
if (unlink(target) != 0) {
error_errno("remove %s", target);
return (-1);
}
return (0);
}
static int
is_mount_point(const char *path, const struct stat *source_sb, bool *mount_point)
{
char *parent;
struct stat parent_sb;
*mount_point = false;
parent = dirname_dup(path);
if (lstat(parent, &parent_sb) != 0) {
error_errno("%s", parent);
free(parent);
return (-1);
}
if (source_sb->st_dev != parent_sb.st_dev)
*mount_point = true;
if (!*mount_point && lstat(parent, &parent_sb) == 0 &&
source_sb->st_dev == parent_sb.st_dev &&
source_sb->st_ino == parent_sb.st_ino)
*mount_point = true;
free(parent);
return (0);
}
static int
copy_move_fallback(const struct mv_options *options, const char *source,
const char *target, const struct stat *source_sb, const struct stat *target_sb)
{
bool mount_point;
if (S_ISDIR(source_sb->st_mode)) {
if (is_mount_point(source, source_sb, &mount_point) != 0)
return (-1);
if (mount_point) {
error_msg("cannot move mount point across filesystems: %s",
source);
return (-1);
}
}
if (target_sb != NULL && remove_target_path(target, target_sb) != 0)
return (-1);
if (copy_node(source, target, source_sb) != 0) {
(void)cleanup_failed_target(target);
return (-1);
}
if (remove_source_tree(source, source_sb) != 0)
return (-1);
if (options->verbose)
(void)printf("%s -> %s\n", source, target);
return (0);
}
static int
handle_single_move(const struct mv_options *options, const char *source,
const char *target)
{
struct stat source_sb;
struct stat target_sb;
bool target_exists;
bool skip_move;
if (lstat(source, &source_sb) != 0) {
error_errno("%s", source);
return (MV_EXIT_ERROR);
}
target_exists = false;
if (lstat(target, &target_sb) == 0)
target_exists = true;
else if (errno != ENOENT) {
error_errno("%s", target);
return (MV_EXIT_ERROR);
}
if (target_exists) {
if (apply_existing_target_policy(options, target, &skip_move) != 0)
return (MV_EXIT_ERROR);
if (skip_move)
return (0);
if (S_ISDIR(source_sb.st_mode) && !S_ISDIR(target_sb.st_mode)) {
errno = ENOTDIR;
error_errno("%s", target);
return (MV_EXIT_ERROR);
}
if (!S_ISDIR(source_sb.st_mode) && S_ISDIR(target_sb.st_mode)) {
errno = EISDIR;
error_errno("%s", target);
return (MV_EXIT_ERROR);
}
}
if (rename(source, target) == 0) {
if (options->verbose)
(void)printf("%s -> %s\n", source, target);
return (0);
}
if (errno != EXDEV) {
error_errno("rename %s to %s", source, target);
return (MV_EXIT_ERROR);
}
if (copy_move_fallback(options, source, target, &source_sb,
target_exists ? &target_sb : NULL) != 0)
return (MV_EXIT_ERROR);
return (0);
}
static int
is_dir_symlink(const char *path)
{
struct stat lst;
struct stat st;
if (lstat(path, &lst) != 0)
return (0);
if (!S_ISLNK(lst.st_mode))
return (0);
if (stat(path, &st) != 0)
return (0);
return (S_ISDIR(st.st_mode) ? 1 : 0);
}
static int
determine_target_mode(const struct mv_options *options, int argc, char *const argv[],
struct move_target *target)
{
struct stat sb;
int stat_rc;
bool treat_as_directory;
treat_as_directory = false;
stat_rc = stat(argv[argc - 1], &sb);
if (!(options->no_target_dir_follow && argc == 2 &&
is_dir_symlink(argv[argc - 1])) &&
stat_rc == 0 && S_ISDIR(sb.st_mode))
treat_as_directory = true;
if (!treat_as_directory && argc > 2) {
if (stat_rc != 0 && errno != ENOENT)
error_errno("%s", argv[argc - 1]);
else
error_msg("%s is not a directory", argv[argc - 1]);
return (-1);
}
target->treat_as_directory = treat_as_directory;
target->path = argv[argc - 1];
return (0);
}
int
main(int argc, char *argv[])
{
struct move_target target;
struct mv_options options;
int ch;
int rval;
progname = program_name(argv[0]);
memset(&options, 0, sizeof(options));
while ((ch = getopt(argc, argv, "+fhinv")) != -1) {
switch (ch) {
case 'f':
options.force = true;
options.interactive = false;
options.no_clobber = false;
break;
case 'h':
options.no_target_dir_follow = true;
break;
case 'i':
options.interactive = true;
options.force = false;
options.no_clobber = false;
break;
case 'n':
options.no_clobber = true;
options.force = false;
options.interactive = false;
break;
case 'v':
options.verbose = true;
break;
default:
usage();
}
}
argc -= optind;
argv += optind;
if (argc < 2)
usage();
if (options.no_target_dir_follow && argc != 2)
usage();
if (determine_target_mode(&options, argc, argv, &target) != 0)
return (MV_EXIT_ERROR);
if (!target.treat_as_directory)
return (handle_single_move(&options, argv[0], target.path));
rval = 0;
for (int i = 0; i < argc - 1; i++) {
char *child_target;
append_child_basename(target.path, argv[i], &child_target);
if (handle_single_move(&options, argv[i], child_target) != 0)
rval = MV_EXIT_ERROR;
free(child_target);
}
return (rval);
}
|