summaryrefslogtreecommitdiff
path: root/Userland/Services/FileOperation/main.cpp
blob: 6e174e747189af6893f9dfdf237de02ef274dee4 (plain)
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
/*
 * Copyright (c) 2021, Andreas Kling <kling@serenityos.org>
 * Copyright (c) 2021, Sam Atkins <atkinssj@serenityos.org>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#include <AK/Format.h>
#include <AK/LexicalPath.h>
#include <LibCore/ArgsParser.h>
#include <LibCore/DirIterator.h>
#include <LibCore/File.h>
#include <sched.h>
#include <sys/stat.h>
#include <unistd.h>

struct WorkItem {
    enum class Type {
        CreateDirectory,
        DeleteDirectory,
        CopyFile,
        MoveFile,
        DeleteFile,
    };
    Type type;
    String source;
    String destination;
    off_t size;
};

static int perform_copy(Vector<StringView> const& sources, String const& destination);
static int perform_move(Vector<StringView> const& sources, String const& destination);
static int perform_delete(Vector<StringView> const& sources);
static int execute_work_items(Vector<WorkItem> const& items);
static void report_error(String message);
static void report_warning(String message);
static ErrorOr<NonnullRefPtr<Core::File>> open_destination_file(String const& destination);
static String deduplicate_destination_file_name(String const& destination);

int main(int argc, char** argv)
{
    String operation;
    Vector<StringView> paths;

    Core::ArgsParser args_parser;
    args_parser.add_positional_argument(operation, "Operation: either 'Copy', 'Move' or 'Delete'", "operation", Core::ArgsParser::Required::Yes);
    args_parser.add_positional_argument(paths, "Source paths, followed by a destination if applicable", "paths", Core::ArgsParser::Required::Yes);
    args_parser.parse(argc, argv);

    if (operation == "Delete")
        return perform_delete(paths);

    String destination = paths.take_last();
    if (paths.is_empty()) {
        report_warning("At least one source and destination are required");
        return 1;
    }

    if (operation == "Copy")
        return perform_copy(paths, destination);
    if (operation == "Move")
        return perform_move(paths, destination);

    report_warning(String::formatted("Unknown operation '{}'", operation));
    return 0;
}

static void report_error(String message)
{
    outln("ERROR {}", message);
}

static void report_warning(String message)
{
    outln("WARN {}", message);
}

static bool collect_copy_work_items(String const& source, String const& destination, Vector<WorkItem>& items)
{
    struct stat st = {};
    if (lstat(source.characters(), &st) < 0) {
        auto original_errno = errno;
        report_error(String::formatted("stat: {}", strerror(original_errno)));
        return false;
    }

    if (!S_ISDIR(st.st_mode)) {
        // It's a file.
        items.append(WorkItem {
            .type = WorkItem::Type::CopyFile,
            .source = source,
            .destination = LexicalPath::join(destination, LexicalPath::basename(source)).string(),
            .size = st.st_size,
        });
        return true;
    }

    // It's a directory.
    items.append(WorkItem {
        .type = WorkItem::Type::CreateDirectory,
        .source = {},
        .destination = LexicalPath::join(destination, LexicalPath::basename(source)).string(),
        .size = 0,
    });

    Core::DirIterator dt(source, Core::DirIterator::SkipParentAndBaseDir);
    while (dt.has_next()) {
        auto name = dt.next_path();
        if (!collect_copy_work_items(
                LexicalPath::join(source, name).string(),
                LexicalPath::join(destination, LexicalPath::basename(source)).string(),
                items)) {
            return false;
        }
    }

    return true;
}

int perform_copy(Vector<StringView> const& sources, String const& destination)
{
    Vector<WorkItem> items;

    for (auto& source : sources) {
        if (!collect_copy_work_items(source, destination, items))
            return 1;
    }

    return execute_work_items(items);
}

static bool collect_move_work_items(String const& source, String const& destination, Vector<WorkItem>& items)
{
    struct stat st = {};
    if (lstat(source.characters(), &st) < 0) {
        auto original_errno = errno;
        report_error(String::formatted("stat: {}", strerror(original_errno)));
        return false;
    }

    if (!S_ISDIR(st.st_mode)) {
        // It's a file.
        items.append(WorkItem {
            .type = WorkItem::Type::MoveFile,
            .source = source,
            .destination = LexicalPath::join(destination, LexicalPath::basename(source)).string(),
            .size = st.st_size,
        });
        return true;
    }

    // It's a directory.
    items.append(WorkItem {
        .type = WorkItem::Type::CreateDirectory,
        .source = {},
        .destination = LexicalPath::join(destination, LexicalPath::basename(source)).string(),
        .size = 0,
    });

    Core::DirIterator dt(source, Core::DirIterator::SkipParentAndBaseDir);
    while (dt.has_next()) {
        auto name = dt.next_path();
        if (!collect_move_work_items(
                LexicalPath::join(source, name).string(),
                LexicalPath::join(destination, LexicalPath::basename(source)).string(),
                items)) {
            return false;
        }
    }

    items.append(WorkItem {
        .type = WorkItem::Type::DeleteDirectory,
        .source = source,
        .destination = {},
        .size = 0,
    });

    return true;
}

int perform_move(Vector<StringView> const& sources, String const& destination)
{
    Vector<WorkItem> items;

    for (auto& source : sources) {
        if (!collect_move_work_items(source, destination, items))
            return 1;
    }

    return execute_work_items(items);
}

static bool collect_delete_work_items(String const& source, Vector<WorkItem>& items)
{
    struct stat st = {};
    if (lstat(source.characters(), &st) < 0) {
        auto original_errno = errno;
        report_error(String::formatted("stat: {}", strerror(original_errno)));
        return false;
    }

    if (!S_ISDIR(st.st_mode)) {
        // It's a file.
        items.append(WorkItem {
            .type = WorkItem::Type::DeleteFile,
            .source = source,
            .destination = {},
            .size = st.st_size,
        });
        return true;
    }

    // It's a directory.
    Core::DirIterator dt(source, Core::DirIterator::SkipParentAndBaseDir);
    while (dt.has_next()) {
        auto name = dt.next_path();
        if (!collect_delete_work_items(LexicalPath::join(source, name).string(), items))
            return false;
    }

    items.append(WorkItem {
        .type = WorkItem::Type::DeleteDirectory,
        .source = source,
        .destination = {},
        .size = 0,
    });

    return true;
}

int perform_delete(Vector<StringView> const& sources)
{
    Vector<WorkItem> items;

    for (auto& source : sources) {
        if (!collect_delete_work_items(source, items))
            return 1;
    }

    return execute_work_items(items);
}

int execute_work_items(Vector<WorkItem> const& items)
{
    off_t total_work_bytes = 0;
    for (auto& item : items)
        total_work_bytes += item.size;

    off_t executed_work_bytes = 0;

    for (size_t i = 0; i < items.size(); ++i) {
        auto& item = items[i];
        off_t item_done = 0;
        auto print_progress = [&] {
            outln("PROGRESS {} {} {} {} {} {} {}", i, items.size(), executed_work_bytes, total_work_bytes, item_done, item.size, item.source);
        };

        auto copy_file = [&](String const& source, String const& destination) {
            auto source_file_or_error = Core::File::open(source, Core::OpenMode::ReadOnly);
            if (source_file_or_error.is_error()) {
                report_warning(String::formatted("Failed to open {} for reading: {}", source, source_file_or_error.error()));
                return false;
            }
            // FIXME: When the file already exists, let the user choose the next action instead of renaming it by default.
            auto destination_file_or_error = open_destination_file(destination);
            if (destination_file_or_error.is_error()) {
                report_warning(String::formatted("Failed to open {} for write: {}", destination, destination_file_or_error.error()));
                return false;
            }
            auto& source_file = *source_file_or_error.value();
            auto& destination_file = *destination_file_or_error.value();

            while (true) {
                print_progress();
                auto buffer = source_file.read(65536);
                if (buffer.is_empty())
                    break;
                if (!destination_file.write(buffer)) {
                    report_warning(String::formatted("Failed to write to destination file: {}", destination_file.error_string()));
                    return false;
                }
                item_done += buffer.size();
                executed_work_bytes += buffer.size();
                print_progress();
                // FIXME: Remove this once the kernel is smart enough to schedule other threads
                //        while we're doing heavy I/O. Right now, copying a large file will totally
                //        starve the rest of the system.
                sched_yield();
            }
            print_progress();
            return true;
        };

        switch (item.type) {

        case WorkItem::Type::CreateDirectory: {
            outln("MKDIR {}", item.destination);
            // FIXME: Support deduplication like open_destination_file() when the directory already exists.
            if (mkdir(item.destination.characters(), 0755) < 0 && errno != EEXIST) {
                auto original_errno = errno;
                report_error(String::formatted("mkdir: {}", strerror(original_errno)));
                return 1;
            }
            break;
        }

        case WorkItem::Type::DeleteDirectory: {
            if (rmdir(item.source.characters()) < 0) {
                auto original_errno = errno;
                report_error(String::formatted("rmdir: {}", strerror(original_errno)));
                return 1;
            }
            break;
        }

        case WorkItem::Type::CopyFile: {
            if (!copy_file(item.source, item.destination))
                return 1;

            break;
        }

        case WorkItem::Type::MoveFile: {
            String destination = item.destination;
            while (true) {
                if (rename(item.source.characters(), destination.characters()) == 0) {
                    item_done += item.size;
                    executed_work_bytes += item.size;
                    print_progress();
                    break;
                }
                auto original_errno = errno;

                if (original_errno == EEXIST) {
                    destination = deduplicate_destination_file_name(destination);
                    continue;
                }

                if (original_errno != EXDEV) {
                    report_warning(String::formatted("Failed to move {}: {}", item.source, strerror(original_errno)));
                    return 1;
                }

                // EXDEV means we have to copy the file data and then remove the original
                if (!copy_file(item.source, item.destination))
                    return 1;

                if (unlink(item.source.characters()) < 0) {
                    auto original_errno = errno;
                    report_error(String::formatted("unlink: {}", strerror(original_errno)));
                    return 1;
                }
                break;
            }

            break;
        }

        case WorkItem::Type::DeleteFile: {
            if (unlink(item.source.characters()) < 0) {
                auto original_errno = errno;
                report_error(String::formatted("unlink: {}", strerror(original_errno)));
                return 1;
            }

            item_done += item.size;
            executed_work_bytes += item.size;
            print_progress();

            break;
        }

        default:
            VERIFY_NOT_REACHED();
        }
    }

    outln("FINISH");
    return 0;
}

ErrorOr<NonnullRefPtr<Core::File>> open_destination_file(String const& destination)
{
    auto destination_file_or_error = Core::File::open(destination, (Core::OpenMode)(Core::OpenMode::WriteOnly | Core::OpenMode::Truncate | Core::OpenMode::MustBeNew));
    if (destination_file_or_error.is_error() && destination_file_or_error.error().code() == EEXIST) {
        return open_destination_file(deduplicate_destination_file_name(destination));
    }
    return destination_file_or_error;
}

String deduplicate_destination_file_name(String const& destination)
{
    LexicalPath destination_path(destination);
    auto title_without_counter = destination_path.title();
    size_t next_counter = 1;

    auto last_hyphen_index = title_without_counter.find_last('-');
    if (last_hyphen_index.has_value()) {
        auto counter_string = title_without_counter.substring_view(*last_hyphen_index + 1);
        auto last_counter = counter_string.to_uint();
        if (last_counter.has_value()) {
            next_counter = *last_counter + 1;
            title_without_counter = title_without_counter.substring_view(0, *last_hyphen_index);
        }
    }

    StringBuilder basename;
    basename.appendff("{}-{}", title_without_counter, next_counter);
    if (!destination_path.extension().is_empty()) {
        basename.append(".");
        basename.append(destination_path.extension());
    }

    return LexicalPath::join(destination_path.dirname(), basename.to_string()).string();
}