summaryrefslogtreecommitdiff
path: root/mu/mu-cmd-move.cc
blob: c0c2ecc4aaecb0b104baf0e37c8396cefdd521c5 (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
/*
** Copyright (C) 2023-2026 Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>
**
** This program is free software; you can redistribute it and/or modify it
** under the terms of the GNU General Public License as published by the
** Free Software Foundation; either version 3, or (at your option) any
** later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software Foundation,
** Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
**
*/

#include "config.h"
#include "mu-cmd.hh"

#include "mu-store.hh"
#include "mu-maildir.hh"
#include "message/mu-message-file.hh"

 #include <unistd.h>

using namespace Mu;

Result<void>
Mu::mu_cmd_move(Mu::Store& store, const Options& opts)
{
	const auto& src{opts.move.src};
	if (::access(src.c_str(), R_OK) != 0 || determine_dtype(src) != DT_REG)
		return Err(Error::Code::InvalidArgument,
			   "Source is not a readable file");

	auto id{store.find_message_id(src)};
	if (!id)
		return Err(Error{Error::Code::InvalidArgument,
				"Source file is not present in database"}
			.add_hint("Perhaps run mu index?"));

	std::string dest{opts.move.dest};
	Option<const std::string&> dest_path;
	if (dest.empty() && opts.move.flags.empty())
		return Err(Error::Code::InvalidArgument,
			   "Must have at least one of destination and flags");
	else if (!dest.empty()) {
		const auto mdirs{store.maildirs()};
		if (!seq_some(mdirs, [&](auto &&d){ return d == dest;}))
			return Err(Error{Error::Code::InvalidArgument,
					"No maildir '{}' in store", dest}
				.add_hint("Try 'mu mkdir'"));
		else
			dest_path = dest;
	}

	auto old_flags{flags_from_path(src)};
	if (!old_flags)
		return Err(Error::Code::InvalidArgument, "failed to determine old flags");

	Flags new_flags{*old_flags};
	if (!opts.move.flags.empty()) {
		if (auto&& nflags{flags_from_expr(to_string_view(opts.move.flags),
						  *old_flags)}; !nflags)
			return Err(Error::Code::InvalidArgument, "Invalid flags");
		else
			new_flags = flags_maildir_file(*nflags);

		if (any_of(new_flags & Flags::New) && new_flags != Flags::New)
			return Err(Error{Error::Code::File,
					"the New flag cannot be combined with others"}
				.add_hint("See the mu-move manpage"));
	}

	Store::MoveOptions move_opts{};
	if (opts.move.change_name)
		move_opts |= Store::MoveOptions::ChangeName;
	if (opts.move.update_dups)
		move_opts |= Store::MoveOptions::DupFlags;
	if (opts.move.dry_run)
		move_opts |= Store::MoveOptions::DryRun;

	auto id_paths = store.move_message(*id, dest_path, new_flags, move_opts);
	if (!id_paths)
		return Err(std::move(id_paths.error()));

	for (const auto&[_id, path]: *id_paths)
		mu_println("{}", path);

	return Ok();
}



#ifdef BUILD_TESTS
/*
 * Tests.
 *
 */

#include "utils/mu-test-utils.hh"

static void
test_move_dry_run()
{
	allow_warnings();

	TempDir tdir;
	const auto dbpath{runtime_path(RuntimePath::XapianDb, tdir.path())};

	auto res = run_command0({CP_PROGRAM, "-r", MU_TESTMAILDIR, tdir.path()});
	assert_valid_command(res);

	const auto testpath{join_paths(tdir.path(), "testdir")};
	const auto src{join_paths(testpath, "cur", "1220863042.12663_1.mindcrime!2,S")};
	{
		auto store = Store::make_new(dbpath, testpath, {});
		assert_valid_result(store);
		g_assert_true(store->indexer().start({}, true/*block*/));
	}

	// make a message 'New'
	{
		auto res = run_command0({MU_PROGRAM, "move", "--muhome", tdir.path(), src,
				"--flags", "N", "--dry-run"});
		assert_valid_command(res);

		auto dst{join_paths(testpath, "new", "1220863042.12663_1.mindcrime")};
		assert_equal(res->standard_out, dst + '\n');

		g_assert_true(::access(dst.c_str(), F_OK) != 0);
		g_assert_true(::access(src.c_str(), F_OK) == 0);
	}

	// change some flags
	{
		auto res = run_command0({MU_PROGRAM, "move", "--muhome", tdir.path(), src,
				"--flags", "FP", "--dry-run"});
		assert_valid_command(res);

		auto dst{join_paths(testpath, "cur", "1220863042.12663_1.mindcrime!2,FP")};
		assert_equal(res->standard_out, dst + '\n');
	}

	// change some relative flag
	{
		auto res = run_command0({MU_PROGRAM, "move", "--muhome", tdir.path(), src,
				"--flags", "+F", "--dry-run"});
		assert_valid_command(res);

		auto dst{join_paths(testpath, "cur", "1220863042.12663_1.mindcrime!2,FS")};
		assert_equal(res->standard_out, dst + '\n');
	}

	{
		auto res = run_command0({MU_PROGRAM, "move", "--muhome", tdir.path(), src,
				"--flags", "-S+P+T", "--dry-run"});
		assert_valid_command(res);

		auto dst{join_paths(testpath, "cur", "1220863042.12663_1.mindcrime!2,PT")};
		assert_equal(res->standard_out, dst + '\n');
	}

	// move to different maildir without flags; should retain old flags
	for (auto& o : {"o1", "o2"})
		assert_valid_result(maildir_mkdir(join_paths(tdir.path(), "testdir", o)));

	{
		auto res = run_command0({MU_PROGRAM, "move", "--muhome", tdir.path(), src,
				"/o1", "--dry-run"});
		assert_valid_command(res);
		// src has ,S flag; moving without --flags should retain it
		assert_equal(res->standard_out,
			     join_paths(testpath,
					"o1/cur", "1220863042.12663_1.mindcrime!2,S") + "\n");
	}

	// change maildir

	{
		auto res = run_command0({MU_PROGRAM, "move", "--muhome", tdir.path(), src,
				"/o1", "--flags", "-S+F", "--dry-run"});
		assert_valid_command(res);
		assert_equal(res->standard_out,
			     join_paths(testpath,
					"o1/cur", "1220863042.12663_1.mindcrime!2,F") + "\n");
	}

	// change-dups; first create some dups and index them.
	assert_valid_result(run_command0({CP_PROGRAM, src, join_paths(testpath, "o1/cur")}));
	assert_valid_result(run_command0({CP_PROGRAM, src, join_paths(testpath, "o2/cur")}));
	{
		auto store = Store::make(dbpath, Store::Options::Writable);
		assert_valid_result(store);
		g_assert_true(store->indexer().start({}, true/*block*/));
	}

	// change some flags + update dups
	{
		auto res = run_command0({MU_PROGRAM, "move", "--muhome", tdir.path(), src,
				"--flags", "-S+S+T+R", "--update-dups", "--dry-run"});
		assert_valid_command(res);

		auto p{join_paths(testpath, "cur", "1220863042.12663_1.mindcrime!2,RST")};
		auto p1{join_paths(testpath, "o1", "cur", "1220863042.12663_1.mindcrime!2,RS")};
		auto p2{join_paths(testpath, "o2", "cur", "1220863042.12663_1.mindcrime!2,RS")};

		assert_equal(res->standard_out, mu_format("{}\n{}\n{}\n", p, p1, p2));
	}
}


static void
test_move_real()
{
	allow_warnings();

	TempDir tdir;
	const auto dbpath{runtime_path(RuntimePath::XapianDb, tdir.path())};

	auto res = run_command0({CP_PROGRAM, "-r", MU_TESTMAILDIR, tdir.path()});
	assert_valid_command(res);

	const auto testpath{join_paths(tdir.path(), "testdir")};
	const auto src{join_paths(testpath, "cur", "1220863042.12663_1.mindcrime!2,S")};
	{
		auto store = Store::make_new(dbpath, testpath, {});
		assert_valid_result(res);
		g_assert_true(store->indexer().start({}, true/*block*/));
	}

	{
		auto res = run_command0({MU_PROGRAM, "move", "--muhome", tdir.path(), src,
				"--flags", "N"});
		assert_valid_command(res);
		auto dst{join_paths(testpath, "new", "1220863042.12663_1.mindcrime")};
		g_assert_true(::access(dst.c_str(), F_OK) == 0);
		g_assert_true(::access(src.c_str(), F_OK) != 0);
	}

	// change flags, maildir, update-dups
	// change-dups; first create some dups and index them.
	const auto src2{join_paths(testpath, "cur", "1305664394.2171_402.cthulhu!2,")};
	for (auto& o : {"o1", "o2", "o3"})
		assert_valid_result(maildir_mkdir(join_paths(tdir.path(), "testdir", o)));
	assert_valid_result(run_command0({CP_PROGRAM, src2, join_paths(testpath, "o1/cur")}));
	assert_valid_result(run_command0({CP_PROGRAM, src2, join_paths(testpath, "o2/new")}));
	{
		auto store = Store::make(dbpath, Store::Options::Writable);
		assert_valid_result(store);
		g_assert_true(store->indexer().start({}, true/*block*/));
	}

	auto res2 = run_command0({MU_PROGRAM, "move", "--muhome", tdir.path(), src2, "/o3",
			"--flags", "-S+S+T+R", "--update-dups", "--change-name"});
	assert_valid_command(res2);

	auto store = Store::make(dbpath, Store::Options::Writable);
	assert_valid_result(store);
	g_assert_true(store->indexer().start({}, true/*block*/));

	for (auto&& f: split(res2->standard_out, "\n")) {
		//mu_println(">> {}", f);
		if (f.length() > 2)
			g_assert_true(::access(f.c_str(), F_OK) == 0);
	}
}


static void
test_move_retain_non_file_flags()
{
	g_test_bug("2831");

	allow_warnings();

	TempDir tdir;
	const auto dbpath{runtime_path(RuntimePath::XapianDb, tdir.path())};

	auto res = run_command0({CP_PROGRAM, "-r", MU_TESTMAILDIR, tdir.path()});
	assert_valid_command(res);

	const auto testpath{join_paths(tdir.path(), "testdir")};
	const auto src{join_paths(testpath, "cur", "multimime!2,FS")};

	Store::Id id{};
	{
		auto store = Store::make_new(dbpath, testpath, {});
		assert_valid_result(store);
		const auto idopt{store->add_message(src, true)};
		g_assert_true(!!idopt);
		id = *idopt;
		const auto msg{store->find_message(id)};
		g_assert_true(!!msg);
		g_assert_true(msg->flags() == (Flags::Seen | Flags::Flagged | Flags::HasAttachment));
	}

	// make a message 'New'
	const auto dst{join_paths(testpath, "new", "multimime")};
	{
		auto res = run_command0({MU_PROGRAM, "--debug", "move", "--muhome",
				tdir.path(), src, "--flags", "N"});
		assert_valid_command(res);
		assert_equal(res->standard_out, dst + '\n');
		// double-check it really moved.
		g_assert_true(::access(dst.c_str(), F_OK) == 0);
		g_assert_true(::access(src.c_str(), F_OK) != 0);
	}

	// re-open db
	{
		const auto store = Store::make(dbpath);
		assert_valid_result(store);

		const auto newid{store->find_message_id(dst)};
		g_assert_true(!!newid);

		const auto msg{store->find_message(*newid)};
		g_assert_true(!!msg);

		// check the old flags
		const auto flags = msg->flags();
		assert_equal(to_string(flags), to_string(Flags::New | Flags::Unread |
							 Flags::HasAttachment));
	}
}

int
main(int argc, char* argv[])
{
	mu_test_init(&argc, &argv);

	g_test_add_func("/cmd/move/dry-run", test_move_dry_run);
	g_test_add_func("/cmd/move/real", test_move_real);
	g_test_add_func("/cmd/move/retain-non-file-flags",
			test_move_retain_non_file_flags);

	return g_test_run();

}
#endif /*BUILD_TESTS*/