1 /* Copyright 1996-1998,2000-2002,2005,2007-2009 Alain Knaff.
2 * This file is part of mtools.
4 * Mtools is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * Mtools is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with Mtools. If not, see <http://www.gnu.org/licenses/>.
18 * Renames/moves an MSDOS file
25 #include "sysincludes.h"
31 #include "nameclash.h"
36 * Preserve the file modification times after the fclose()
39 typedef struct Arg_t {
50 * Open the named file for read, create the cluster chain, return the
51 * directory structure or NULL on error.
53 static int renameit(dos_name_t *dosname,
56 direntry_t *targetEntry)
58 Arg_t *arg = (Arg_t *) arg0;
61 targetEntry->dir = arg->entry->dir;
62 dosnameToDirentry(dosname, &targetEntry->dir);
64 if(IS_DIR(targetEntry)) {
65 direntry_t *movedEntry;
67 /* get old direntry. It is important that we do this
68 * on the actual direntry which is stored in the file,
69 * and not on a copy, because we will modify it, and the
70 * modification should be visible at file
71 * de-allocation time */
72 movedEntry = getDirentry(arg->mp.File);
73 if(movedEntry->Dir != targetEntry->Dir) {
74 /* we are indeed moving it to a new directory */
77 /* we have a directory here. Change its parent link */
79 initializeDirentry(&subEntry, arg->mp.File);
81 switch(vfat_lookup(&subEntry, "..", 2, ACCEPT_DIR,
85 " Directory has no parent entry\n");
90 GET_DATA(targetEntry->Dir, 0, 0, 0, &fat);
91 if (fat == fat32RootCluster(targetEntry->Dir)) {
95 subEntry.dir.start[1] = (fat >> 8) & 0xff;
96 subEntry.dir.start[0] = fat & 0xff;
100 "Easy, isn't it? I wonder why DOS can't do this.\n");
105 wipeEntry(movedEntry);
107 /* free the old parent, allocate the new one. */
108 oldDir = movedEntry->Dir;
109 *movedEntry = *targetEntry;
110 COPY(targetEntry->Dir);
116 /* wipe out original entry */
117 wipeEntry(arg->mp.direntry);
123 static int rename_file(direntry_t *entry, MainParam_t *mp)
124 /* rename a messy DOS file to another messy DOS file */
129 const char *longname;
131 Arg_t * arg = (Arg_t *) (mp->arg);
134 targetDir = mp->targetDir;
136 if (targetDir == entry->Dir){
137 arg->ch.ignore_entry = -1;
138 arg->ch.source = entry->entry;
139 arg->ch.source_entry = entry->entry;
141 arg->ch.ignore_entry = -1;
145 longname = mpPickTargetName(mp);
147 result = mwrite_one(targetDir, longname, shortname,
148 renameit, (void *)arg, &arg->ch);
156 static int rename_directory(direntry_t *entry, MainParam_t *mp)
160 /* moves a DOS dir */
161 if(isSubdirOf(mp->targetDir, mp->File)) {
162 fprintf(stderr, "Cannot move directory ");
163 fprintPwd(stderr, entry,0);
164 fprintf(stderr, " into one of its own subdirectories (");
165 fprintPwd(stderr, getDirentry(mp->targetDir),0);
166 fprintf(stderr, ")\n");
170 if(entry->entry == -3) {
171 fprintf(stderr, "Cannot move a root directory: ");
172 fprintPwd(stderr, entry,0);
176 ret = rename_file(entry, mp);
183 static int rename_oldsyntax(direntry_t *entry, MainParam_t *mp)
187 const char *shortname, *longname;
189 Arg_t * arg = (Arg_t *) (mp->arg);
191 targetDir = entry->Dir;
193 arg->ch.ignore_entry = -1;
194 arg->ch.source = entry->entry;
195 arg->ch.source_entry = entry->entry;
198 if(!strcasecmp(mp->shortname, arg->fromname)){
199 longname = mp->longname;
200 shortname = mp->targetName;
203 longname = mp->targetName;
208 result = mwrite_one(targetDir, longname, shortname,
209 renameit, (void *)arg, &arg->ch);
217 static void usage(int ret) NORETURN;
218 static void usage(int ret)
221 "Mtools version %s, dated %s\n", mversion, mdate);
223 "Usage: %s [-vV] [-D clash_option] file targetfile\n", progname);
225 " %s [-vV] [-D clash_option] file [files...] target_directory\n",
230 void mmove(int argc, char **argv, int oldsyntax)
235 char longname[VBUFSIZE];
239 /* get command line options */
241 init_clash_handling(& arg.ch);
243 /* get command line options */
245 if(helpFlag(argc, argv))
247 while ((c = getopt(argc, argv, "i:vD:oh")) != EOF) {
250 set_cmd_line_image(optarg, 0);
252 case 'v': /* dummy option for mcopy */
256 handle_clash_options(&arg.ch, c);
259 if(handle_clash_options(&arg.ch, *optarg))
271 if (argc - optind < 2)
275 arg.mp.arg = (void *) &arg;
276 arg.mp.openflags = O_RDWR;
278 /* look for a default drive */
280 for(i=optind; i<argc; i++)
281 if(argv[i][0] && argv[i][1] == ':' ){
283 def_drive = toupper(argv[i][0]);
284 else if(def_drive != toupper(argv[i][0])){
286 "Cannot move files across different drives\n");
292 *(arg.mp.mcwd) = def_drive;
294 if (oldsyntax && (argc - optind != 2 || strpbrk(":/", argv[argc-1])))
298 ACCEPT_PLAIN | ACCEPT_DIR | DO_OPEN_DIRS | NO_DOTS | NO_UNIX;
301 target_lookup(&arg.mp, argv[argc-1]);
302 arg.mp.callback = rename_file;
303 arg.mp.dirCallback = rename_directory;
305 /* do not look up the target; it will be the same dir as the
307 arg.fromname = argv[optind];
308 if(arg.fromname[0] && arg.fromname[1] == ':')
310 arg.fromname = _basename(arg.fromname);
311 arg.mp.targetName = strdup(argv[argc-1]);
312 arg.mp.callback = rename_oldsyntax;
316 arg.mp.longname = longname;
319 arg.mp.shortname = shortname;
322 exit(main_loop(&arg.mp, argv + optind, argc - optind - 1));