forked from e2tools/e2tools
-
Notifications
You must be signed in to change notification settings - Fork 3
/
mv.c
546 lines (494 loc) · 14.2 KB
/
mv.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
/* $Header: /home/ksheff/src/e2tools/RCS/mv.c,v 0.1 2002/03/21 09:03:25 ksheff Exp $ */
/*
* mv.c
*
* Copyright (C) 2002 Keith W Sheffield. This file may be redistributed
* under the terms of the GNU Public License.
*
*/
/* Description */
/*
* Module to move/rename files
*
*/
/*
* $Log: mv.c,v $
* Revision 0.1 2002/03/21 09:03:25 ksheff
* initial revision.
*
*/
/* Feature Test Switches */
/* Headers */
#include "e2tools.h"
/* Macros */
#define USAGE "Usage: e2mv [-vfs] source1 [... sourceN] destination\n"
/* Local Prototypes */
static long
do_swap(int force, int verbose, int curidx, int argc, char **argv);
/* Name: do_mv()
*
* Description:
*
* This function reads the command line arguments and moves or renames files
* in an ext2fs file system
*
* Algorithm:
*
* Read any command line switches
* Get the first source file specification
* If we are performing a file swap, call do_swap()
* Open the file system
* Get the destination and determine if it is a directory
* If not, then get the destination's directory and basename
* Also check that the number of source files are no more than one
* For each source file
* Get the directory and basename of the source file
* Determine the inode number for the source file
* Create the link
* Unlink the original source file.
*
* Global Variables:
*
* None
*
* Arguments:
*
* int argc; The number of arguments
* char *argv[]; The command line arguments
*
* Return Values:
*
* 0 - the file was move successfully
* an error occurred.
*
* Author: Keith W. Sheffield
* Date: 03/20/2002
*
* Modification History:
*
* MM/DD/YY Name Description
*/
long
do_mv(int argc, char *argv[])
{
int verbose=0;
int force=0;
int swap_files=0;
int errcnt=0;
char *cur_filesys = NULL;
ext2_filsys fs = NULL;
ext2_ino_t root;
ext2_ino_t srcd;
ext2_ino_t destd;
ext2_ino_t source_file;
char *src_dir;
char *dest_dir;
char *src_name;
char *dest_name;
char *result_name;
long retval;
int c;
int curidx;
#ifdef HAVE_OPTRESET
optreset = 1; /* Makes BSD getopt happy */
#endif
while ((c = getopt(argc, argv, "vfs")) != EOF)
{
switch (c)
{
case 'v':
verbose = 1;
break;
case 'f':
force = E2T_FORCE;
break;
case 's':
swap_files = 1;
break;
default:
errcnt++;
break;
}
}
curidx = optind;
force |= E2T_DO_MV;
if (errcnt || argc < curidx+2)
{
fputs(USAGE, stderr);
return(1);
}
if (swap_files)
return(do_swap(force, verbose, curidx, argc, argv));
cur_filesys = argv[curidx++];
if (NULL == (src_dir = strchr(cur_filesys, ':')))
{
fprintf(stderr, "Invalid file specification: %s\n", cur_filesys);
return(1);
}
*src_dir++ = '\0';
if ((retval = open_filesystem(cur_filesys, &fs, &root, 1)))
{
return retval;
}
/* get the destination directory */
dest_name = NULL;
if (strcmp(dest_dir = argv[argc-1], ".") != 0)
{
/* check to see if the file name already exists in the current
* directory and also see if it is a directory.
*/
if ((retval = ext2fs_namei(fs, root, root, dest_dir, &destd)) ||
(retval = ext2fs_check_directory(fs, destd)))
{
if (retval != EXT2_ET_FILE_NOT_FOUND &&
retval != EXT2_ET_NO_DIRECTORY)
{
fprintf(stderr, "%s\n",error_message(retval));
ext2fs_close(fs);
return(retval);
}
/* ok, so it's either not there or it's not a directory, so
* get the real destination directory and file name.
*/
if (curidx+1 < argc)
{
fprintf(stderr, "%s must be a directory!\n", dest_dir);
ext2fs_close(fs);
return(1);
}
if (get_file_parts(fs, root, dest_dir, &destd, &dest_dir,
&dest_name))
{
ext2fs_close(fs);
return(-1);
}
}
else /* we have a directory!!! */
dest_name = NULL;
}
else
{
destd = root;
dest_name = NULL;
}
do
{
/* move to the source directory */
if (get_file_parts(fs, root, src_dir, &srcd, &src_dir, &src_name))
{
ext2fs_close(fs);
return(-1);
}
/* get the inode number for the source file */
if ((retval = ext2fs_namei(fs, srcd, srcd, src_name, &source_file)))
{
fprintf(stderr, "%s: source file %s\n",error_message(retval),
src_name);
ext2fs_close(fs);
return(retval);
}
result_name = (dest_name) ? dest_name : src_name;
/* now create the link */
if ((retval = create_hard_link(fs, destd, source_file, result_name,
force)))
{
fprintf(stderr, "Error renaming %s/%s as %s/%s\n",
((src_dir == NULL) ? "." : src_dir), src_name,
((dest_dir == NULL) ? "." : dest_dir), result_name);
ext2fs_close(fs);
return(1);
}
if ((retval = ext2fs_unlink(fs, srcd, src_name, 0, 0)))
{
fprintf(stderr, "%s - %s\n", src_name, error_message(retval));
ext2fs_close(fs);
return(retval);
}
if (verbose)
fprintf(stderr, "moved %s/%s as %s/%s\n",
((src_dir == NULL) ? "." : src_dir), src_name,
((dest_dir == NULL) ? "." : dest_dir), result_name);
src_dir = argv[curidx++];
}
while (curidx < argc);
ext2fs_close(fs);
return(0);
} /* end of do_mv */
/* Name: get_file_parts()
*
* Description:
*
* This function returns each of the following file 'parts': directory name,
* base name, inode number of the directory
*
* Algorithm:
*
* Use the root directory as the current working directory
* Find the last / in the full pathname
* If none are found, set the basename to the full pathname,
* and the directory to NULL
* Otherwise,
* Separate the basename from the directory
* Change the working directory
* Set the return pointers.
*
* Global Variables:
*
* None.
*
* Arguments:
*
* ext2_filsys fs; the filesystem being used
* ext2_ino_t root; the root directory of the filesystem
* char *pathname; the full pathname of the file
* ext2_ino_t *dir_ino; The inode number of the directory
* char **dir_name; the directory the file is in
* char **base_name; The basename of the file
*
* Return Values:
*
* 0 - retrieved the information ok
* otherwise the error code of what went wrong
*
* Author: Keith W. Sheffield
* Date: 03/21/2002
*
* Modification History:
*
* MM/DD/YY Name Description
*
*/
long
get_file_parts(ext2_filsys fs, ext2_ino_t root, char *pathname,
ext2_ino_t *dir_ino, char **dir_name, char **base_name)
{
char *fname;
long retval;
/* move to the source directory */
*dir_name = pathname;
*dir_ino = root;
if (NULL == (fname = strrchr(pathname, '/')))
{
fname = pathname;
*dir_name = NULL;
}
else
{
*fname++ = '\0';
if ((*pathname != '\0' && strcmp(pathname, ".") != 0) &&
(retval = change_cwd(fs, root, dir_ino, pathname)))
{
fprintf(stderr, "Error changing to directory %s\n",
pathname);
return(retval);
}
}
*base_name = fname;
return(0);
} /* end of get_file_parts */
/* Name: do_swap()
*
* Description:
*
* This function swaps the names of two files and optionally assigns the
* first file a new name:
*
* file1 file2 file3
*
* After the operation, file1 will reference the file that was originally file2, and file3 will reference the what used to be file1.
*
* Algorithm:
*
* check input parameters
* Get the directory and inode numbers for the first two files
* If a third file exists
* Get the directory info for the file
* Rename the first file to the third
* Rename the 2nd file to the first
* Otherwise
* Remove the first file from its directory
* Rename the 2nd file to the first
* Add the first file back as the 2nd file.
*
* Global Variables:
*
* None.
*
* Arguments:
*
* int force; Flag indicating if existing files are to be removed
* int verbose; Flag to print lots of output
* int curidx; The current index in the command line args
* int argc; The total number of arguments
* char **argv; Pointer to an array of argument strings.
*
* Return Values:
*
* 0 - the operation was successful
* otherwise the error code of what went wrong.
*
* Author: Keith W. Sheffield
* Date: 03/21/2002
*
* Modification History:
*
* MM/DD/YY Name Description
*
*/
static long
do_swap(int force, int verbose, int curidx, int argc, char **argv)
{
char *cur_filesys = NULL;
ext2_filsys fs = NULL;
ext2_ino_t root;
ext2_ino_t file1_dirno;
ext2_ino_t file2_dirno;
ext2_ino_t file3_dirno;
ext2_ino_t file1_no;
ext2_ino_t file2_no;
char *file1_dir;
char *file2_dir;
char *file3_dir;
char *file1_name;
char *file2_name;
char *file3_name;
long retval;
if (curidx + 2 > argc)
{
fputs(USAGE, stderr);
return(1);
}
cur_filesys = argv[curidx++];
if (NULL == (file1_dir = strchr(cur_filesys, ':')))
{
fprintf(stderr, "Invalid file specification: %s\n", cur_filesys);
return(1);
}
*file1_dir++ = '\0';
if ((retval = open_filesystem(cur_filesys, &fs, &root, 1)))
{
return retval;
}
/* move to the file 1 directory */
if (get_file_parts(fs, root, file1_dir, &file1_dirno, &file1_dir,
&file1_name))
{
ext2fs_close(fs);
return(-1);
}
/* get the inode number for the file 1 file */
if ((retval = ext2fs_namei(fs, file1_dirno, file1_dirno, file1_name,
&file1_no)))
{
fprintf(stderr, "%s: file 1 file %s\n",error_message(retval),
file1_name);
ext2fs_close(fs);
return(retval);
}
/* move to the file 2 directory */
if (get_file_parts(fs, root, argv[curidx++], &file2_dirno, &file2_dir,
&file2_name))
{
ext2fs_close(fs);
return(-1);
}
/* get the inode number for the file 2 file */
if ((retval = ext2fs_namei(fs, file2_dirno, file2_dirno, file2_name,
&file2_no)))
{
fprintf(stderr, "%s: file 2 file %s\n",error_message(retval),
file2_name);
ext2fs_close(fs);
return(retval);
}
if (curidx < argc)
{
/* move to the file 3 directory */
if (get_file_parts(fs, root, argv[curidx++], &file3_dirno, &file3_dir,
&file3_name))
{
ext2fs_close(fs);
return(-1);
}
/* now move the first file to the 3rd */
if ((retval = create_hard_link(fs, file3_dirno, file1_no, file3_name,
force)))
{
fprintf(stderr, "Error renaming %s/%s as %s/%s\n",
((file1_dir == NULL) ? "." : file1_dir), file1_name,
((file3_dir == NULL) ? "." : file3_dir), file3_name);
ext2fs_close(fs);
return(1);
}
if ((retval = ext2fs_unlink(fs, file1_dirno, file1_name, 0, 0)))
{
fprintf(stderr, "%s - %s\n", file1_name, error_message(retval));
ext2fs_close(fs);
return(retval);
}
/* now move the 2nd file to the 1st */
if ((retval = create_hard_link(fs, file1_dirno, file2_no, file1_name,
force)))
{
fprintf(stderr, "Error renaming %s/%s as %s/%s\n",
((file2_dir == NULL) ? "." : file2_dir), file2_name,
((file1_dir == NULL) ? "." : file1_dir), file1_name);
ext2fs_close(fs);
return(1);
}
if ((retval = ext2fs_unlink(fs, file2_dirno, file2_name, 0, 0)))
{
fprintf(stderr, "%s - %s\n", file2_name, error_message(retval));
ext2fs_close(fs);
return(retval);
}
if (verbose)
fprintf(stderr, "renamed file %s/%s as %s/%s\n"
"renamed file %s/%s as %s/%s\n",
((file1_dir == NULL) ? "." : file1_dir), file1_name,
((file3_dir == NULL) ? "." : file3_dir), file3_name,
((file2_dir == NULL) ? "." : file2_dir), file2_name,
((file1_dir == NULL) ? "." : file1_dir), file1_name);
}
else
{
/* now remove the first file */
if ((retval = ext2fs_unlink(fs, file1_dirno, file1_name, 0, 0)))
{
fprintf(stderr, "%s - %s\n", file1_name, error_message(retval));
ext2fs_close(fs);
return(retval);
}
/* now move the 2nd file to the 1st */
if ((retval = create_hard_link(fs, file1_dirno, file2_no, file1_name,
force)))
{
fprintf(stderr, "Error renaming %s/%s as %s/%s\n",
((file2_dir == NULL) ? "." : file2_dir), file2_name,
((file1_dir == NULL) ? "." : file1_dir), file1_name);
ext2fs_close(fs);
return(1);
}
if ((retval = ext2fs_unlink(fs, file2_dirno, file2_name, 0, 0)))
{
fprintf(stderr, "%s - %s\n", file2_name, error_message(retval));
ext2fs_close(fs);
return(retval);
}
if ((retval = create_hard_link(fs, file2_dirno, file1_no, file2_name,
force)))
{
fprintf(stderr, "Error renaming %s/%s as %s/%s\n",
((file1_dir == NULL) ? "." : file1_dir), file1_name,
((file2_dir == NULL) ? "." : file2_dir), file2_name);
ext2fs_close(fs);
return(1);
}
if (verbose)
fprintf(stderr, "swapped files %s/%s <-> %s/%s\n",
((file1_dir == NULL) ? "." : file1_dir), file1_name,
((file2_dir == NULL) ? "." : file2_dir), file2_name);
}
ext2fs_close(fs);
return(0);
} /* end of do_swap */