4
* Copyright (c) 2008 Arnaud Ysmal. All Rights Reserved.
6
* Redistribution and use in source and binary forms, with or without
7
* modification, are permitted provided that the following conditions
9
* 1. Redistributions of source code must retain the above copyright
10
* notice, this list of conditions and the following disclaimer.
11
* 2. Redistributions in binary form must reproduce the above copyright
12
* notice, this list of conditions and the following disclaimer in the
13
* documentation and/or other materials provided with the distribution.
15
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
16
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18
* DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29
#include <sys/syslimits.h>
37
#include <rump/ukfs.h>
39
#include <fsu_mount.h>
41
#define FSU_MV_INTERACTIVE (0x01)
42
#define FSU_MV_VERBOSE (FSU_MV_INTERACTIVE<<1)
46
static int fsu_mv_parse_arg(int *, char ***);
47
static int fsu_mv(struct ukfs *, const char *, const char *, int);
48
static int is_user_ok(const char *);
49
static int move_to_dir(struct ukfs *, const char *, const char *, int);
50
static int move_to_file(struct ukfs *, const char *, const char *, int);
51
static void usage(void);
53
int main(int, char *[]);
56
main(int argc, char *argv[])
58
int cur_arg, flags = 0, rv;
62
FSU_MOUNT(argc, argv, ukfs);
64
flags = fsu_mv_parse_arg(&argc, &argv);
65
if (flags == -1 || argc < 2) {
70
for (rv = 0, cur_arg = 0; cur_arg < argc-1; cur_arg++)
71
rv |= fsu_mv(ukfs, argv[cur_arg], argv[argc-1], flags);
77
fsu_mv_parse_arg(int *argc, char ***argv)
82
while ((rv = getopt(*argc, *argv, "iv")) != -1) {
85
flags |= FSU_MV_INTERACTIVE;
88
flags |= FSU_MV_VERBOSE;
103
fsu_mv(struct ukfs *fs, const char *from, const char *to, int flags)
106
struct stat file_stat;
108
rv = ukfs_stat(fs, to, &file_stat);
109
if (rv == -1 || !S_ISDIR(file_stat.st_mode))
110
return move_to_file(fs, from, to, flags);
112
return move_to_dir(fs, from, to, flags);
116
is_user_ok(const char *to)
120
printf("Overwrite %s? ", to);
121
if ((ch = getchar()) != EOF && ch != '\n')
122
while ((ch2 = getchar()) != EOF && ch2 != '\n')
125
if (ch != 'y' && ch != 'Y')
131
move_to_dir(struct ukfs *fs, const char *from, const char *to, int flags)
135
char path[PATH_MAX + 1];
136
struct stat file_stat;
140
rv = strlcpy(path, to, PATH_MAX + 1);
141
if (rv != (int)len) {
146
rv = ukfs_stat(fs, from, &file_stat);
152
p = strrchr(from, '/');
158
rv = strlcat(path, p, PATH_MAX + 1);
160
warn("%s/%s", path, p);
164
if (flags & FSU_MV_VERBOSE)
165
printf("%s -> %s\n", from, path);
167
if (flags & FSU_MV_INTERACTIVE) {
168
rv = ukfs_stat(fs, path, &file_stat);
169
if (rv != -1 && !is_user_ok(to))
173
rv = ukfs_rename(fs, from, path);
182
move_to_file(struct ukfs *fs, const char *from, const char *to, int flags)
185
struct stat file_stat;
187
if (flags & FSU_MV_VERBOSE)
188
printf("%s -> %s\n", from, to);
190
if (flags & FSU_MV_INTERACTIVE) {
191
rv = ukfs_stat(fs, to, &file_stat);
192
if (rv != -1 && !is_user_ok(to))
195
rv = ukfs_rename(fs, from, to);
197
warn("%s or %s", from, to);
207
fprintf(stderr, "usage: \t%s %s [-iv] source target\n"
208
" \t%s %s [-iv] source ... directory\n",
209
getprogname(), fsu_mount_usage(),
210
getprogname(), fsu_mount_usage());