xaizek / vifm (License: GPLv2+) (since 2018-12-07)
Vifm is a file manager with curses interface, which provides Vi[m]-like environment for managing objects within file systems, extended with some useful ideas from mutt.
<root> / src / io / ior.c (f698a12ed87a479ec78a363ff61c69d386049935) (12KiB) (mode 100644) [raw]
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
/* vifm
 * Copyright (C) 2013 xaizek.
 *
 * 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 2 of the License, 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 "ior.h"

#include <sys/stat.h> /* stat */
#include <unistd.h> /* unlink() */

#include <errno.h> /* EEXIST EISDIR ENOTEMPTY EXDEV errno */
#include <stddef.h> /* NULL */
#include <stdio.h> /* remove() snprintf() */
#include <stdlib.h> /* free() */
#include <string.h> /* strlen() */

#include "../compat/fs_limits.h"
#include "../compat/os.h"
#include "../utils/fs.h"
#include "../utils/log.h"
#include "../utils/path.h"
#include "../utils/str.h"
#include "../utils/utils.h"
#include "../background.h"
#include "private/ioc.h"
#include "private/ioe.h"
#include "private/ioeta.h"
#include "private/traverser.h"
#include "ioc.h"
#include "iop.h"

static VisitResult rm_visitor(const char full_path[], VisitAction action,
		void *param);
static VisitResult cp_visitor(const char full_path[], VisitAction action,
		void *param);
static IoRes mv_by_copy(io_args_t *args, int confirmed);
static IoRes mv_replacing_all(io_args_t *args);
static IoRes mv_replacing_files(io_args_t *args);
static int is_file(const char path[]);
static VisitResult mv_visitor(const char full_path[], VisitAction action,
		void *param);
static VisitResult cp_mv_visitor(const char full_path[], VisitAction action,
		void *param, int cp);
static VisitResult vr_from_io_res(IoRes result);

IoRes
ior_rm(io_args_t *args)
{
	const char *const path = args->arg1.path;
	return traverse(path, &rm_visitor, args);
}

/* Implementation of traverse() visitor for subtree removal.  Returns 0 on
 * success, otherwise non-zero is returned. */
static VisitResult
rm_visitor(const char full_path[], VisitAction action, void *param)
{
	io_args_t *const rm_args = param;
	VisitResult result = VR_OK;

	if(io_cancelled(rm_args))
	{
		return VR_CANCELLED;
	}

	switch(action)
	{
		case VA_DIR_ENTER:
			/* Do nothing, directories are removed on leaving them. */
			result = VR_OK;
			break;
		case VA_FILE:
			{
				io_args_t args = {
					.arg1.path = full_path,

					.cancellation = rm_args->cancellation,
					.estim = rm_args->estim,

					.result = rm_args->result,
				};

				result = vr_from_io_res(iop_rmfile(&args));
				rm_args->result = args.result;
				break;
			}
		case VA_DIR_LEAVE:
			{
				io_args_t args = {
					.arg1.path = full_path,

					.cancellation = rm_args->cancellation,
					.estim = rm_args->estim,

					.result = rm_args->result,
				};

				result = vr_from_io_res(iop_rmdir(&args));
				rm_args->result = args.result;
				break;
			}
	}

	return result;
}

IoRes
ior_cp(io_args_t *args)
{
	const char *const src = args->arg1.src;
	const char *const dst = args->arg2.dst;

	if(is_in_subtree(dst, src, 0))
	{
		(void)ioe_errlst_append(&args->result.errors, src, IO_ERR_UNKNOWN,
				"Can't copy parent path into subpath");
		return IO_RES_FAILED;
	}

	if(args->arg3.crs == IO_CRS_REPLACE_ALL)
	{
		io_args_t rm_args = {
			.arg1.path = dst,

			.cancellation = args->cancellation,
			.estim = args->estim,

			.result = args->result,
		};

		const IoRes result = ior_rm(&rm_args);
		args->result = rm_args.result;
		if(result != IO_RES_SUCCEEDED)
		{
			if(result == IO_RES_FAILED && !io_cancelled(args))
			{
				(void)ioe_errlst_append(&args->result.errors, dst, IO_ERR_UNKNOWN,
						"Failed to remove");
			}
			return result;
		}
	}

	return traverse(src, &cp_visitor, args);
}

/* Implementation of traverse() visitor for subtree copying.  Returns 0 on
 * success, otherwise non-zero is returned. */
static VisitResult
cp_visitor(const char full_path[], VisitAction action, void *param)
{
	return cp_mv_visitor(full_path, action, param, 1);
}

IoRes
ior_mv(io_args_t *args)
{
	const char *const src = args->arg1.src;
	const char *const dst = args->arg2.dst;
	const IoCrs crs = args->arg3.crs;
	const io_confirm confirm = args->confirm;
	int confirmed = 0;

	if(crs == IO_CRS_FAIL && path_exists(dst, NODEREF) &&
			!is_case_change(src, dst))
	{
		(void)ioe_errlst_append(&args->result.errors, dst, EEXIST,
				"Destination path already exists");
		return IO_RES_FAILED;
	}

	if(crs == IO_CRS_APPEND_TO_FILES)
	{
		if(!is_file(src))
		{
			(void)ioe_errlst_append(&args->result.errors, src, EISDIR,
					"Can't append when source is not a file");
			return IO_RES_FAILED;
		}
		if(!is_file(dst))
		{
			(void)ioe_errlst_append(&args->result.errors, dst, EISDIR,
					"Can't append when destination is not a file");
			return IO_RES_FAILED;
		}
	}
	else if(crs == IO_CRS_REPLACE_FILES && path_exists(dst, DEREF))
	{
		/* Ask user whether to overwrite destination file. */
		if(confirm != NULL && !confirm(args, src, dst))
		{
			return IO_RES_SKIPPED;
		}
		confirmed = 1;
	}

	if(os_rename(src, dst) == 0)
	{
		ioeta_update(args->estim, src, dst, 1, 0);
		return IO_RES_SUCCEEDED;
	}

	int error = errno;

	int cross_fs = (error == EXDEV);
#ifndef _WIN32
	/* At least SSHFS fails to propagate EXDEV and reports EPERM.  So we try to do
	 * the copy across mounts anyway, which might actually work despite the error
	 * code. */
	cross_fs |= (error == EPERM || error == EACCES);
#endif
	if(cross_fs)
	{
		return mv_by_copy(args, confirmed);
	}

	int overwrite = (error == EISDIR || error == ENOTEMPTY || error == EEXIST);
#ifdef _WIN32
	/* For MXE builds running in Wine. */
	overwrite |= (error == EPERM || error == EACCES);
#endif
	if(overwrite)
	{
		if(crs == IO_CRS_REPLACE_ALL)
		{
			return mv_replacing_all(args);
		}

		if(crs == IO_CRS_REPLACE_FILES ||
				(!has_atomic_file_replace() && crs == IO_CRS_APPEND_TO_FILES))
		{
			return mv_replacing_files(args);
		}
	}

	(void)ioe_errlst_append(&args->result.errors, src, error,
			"Rename operation failed");
	return (error == 0 ? IO_RES_SUCCEEDED : IO_RES_FAILED);
}

/* Performs a manual move: copy followed by deletion.  Returns status. */
static IoRes
mv_by_copy(io_args_t *args, int confirmed)
{
	/* Do not ask for confirmation second time. */
	const io_confirm confirm = args->confirm;
	args->confirm = (confirmed ? NULL : confirm);

	IoRes result = ior_cp(args);

	args->confirm = confirm;

	/* When result is success, there still might be errors if they were
	 * ignored by the user.  Do not delete source in this case, some files
	 * might be missing at the destination. */
	if(result == IO_RES_SUCCEEDED && args->result.errors.error_count == 0)
	{
		io_args_t rm_args = {
			.arg1.path = args->arg1.src,

			.cancellation = args->cancellation,
			.estim = args->estim,

			.result = args->result,
		};

		/* Disable progress reporting for this "secondary" operation. */
		const int silent = ioeta_silent_on(rm_args.estim);
		result = ior_rm(&rm_args);
		args->result = rm_args.result;
		ioeta_silent_set(rm_args.estim, silent);
	}
	return result;
}

/* Performs a move after deleting target first.  Returns status. */
static IoRes
mv_replacing_all(io_args_t *args)
{
	const char *const src = args->arg1.src;
	const char *const dst = args->arg2.dst;

	io_args_t rm_args = {
		.arg1.path = dst,

		.cancellation = args->cancellation,
		.estim = args->estim,

		.result = args->result,
	};

	/* Ask user whether to overwrite destination file. */
	if(args->confirm != NULL && !args->confirm(args, src, dst))
	{
		return IO_RES_SUCCEEDED;
	}

	IoRes result = ior_rm(&rm_args);
	args->result = rm_args.result;
	if(result != IO_RES_SUCCEEDED)
	{
		if(result == IO_RES_FAILED && !io_cancelled(args))
		{
			(void)ioe_errlst_append(&args->result.errors, dst, IO_ERR_UNKNOWN,
					"Failed to remove");
		}
		return result;
	}

	if(os_rename(src, dst) != 0)
	{
		(void)ioe_errlst_append(&args->result.errors, src, errno,
				"Rename operation failed");
		return IO_RES_FAILED;
	}
	return IO_RES_SUCCEEDED;
}

/* Performs a merging move (for directories).  Returns status. */
static IoRes
mv_replacing_files(io_args_t *args)
{
	const char *const src = args->arg1.src;
	const char *const dst = args->arg2.dst;

	if(!has_atomic_file_replace() && is_file(dst))
	{
		io_args_t rm_args = {
			.arg1.path = dst,

			.cancellation = args->cancellation,
			.estim = args->estim,

			.result = args->result,
		};

		const IoRes result = iop_rmfile(&rm_args);
		args->result = rm_args.result;
		if(result != IO_RES_SUCCEEDED)
		{
			if(result == IO_RES_FAILED && !io_cancelled(args))
			{
				(void)ioe_errlst_append(&args->result.errors, dst, IO_ERR_UNKNOWN,
						"Failed to remove");
			}
			return result;
		}
	}

	return traverse(src, &mv_visitor, args);
}

/* Checks that path points to a file or symbolic link.  Returns non-zero if so,
 * otherwise zero is returned. */
static int
is_file(const char path[])
{
	return !is_dir(path)
	    || (is_symlink(path) && get_symlink_type(path) != SLT_UNKNOWN);
}

/* Implementation of traverse() visitor for subtree moving.  Returns 0 on
 * success, otherwise non-zero is returned. */
static VisitResult
mv_visitor(const char full_path[], VisitAction action, void *param)
{
	return cp_mv_visitor(full_path, action, param, 0);
}

/* Generic implementation of traverse() visitor for subtree copying/moving.
 * Returns 0 on success, otherwise non-zero is returned. */
static VisitResult
cp_mv_visitor(const char full_path[], VisitAction action, void *param, int cp)
{
	io_args_t *const cp_args = param;
	const char *dst_full_path;
	char *free_me = NULL;
	VisitResult result = VR_OK;
	const char *rel_part;

	if(io_cancelled(cp_args))
	{
		return VR_CANCELLED;
	}

	/* TODO: come up with something better than this. */
	rel_part = full_path + strlen(cp_args->arg1.src);
	dst_full_path = (rel_part[0] == '\0')
	              ? cp_args->arg2.dst
	              : (free_me = join_paths(cp_args->arg2.dst, rel_part));

	switch(action)
	{
		case VA_DIR_ENTER:
			if(cp_args->arg3.crs != IO_CRS_REPLACE_FILES || !is_dir(dst_full_path))
			{
				io_args_t args = {
					.arg1.path = dst_full_path,

					/* Temporary fake rights so we can add files to the directory. */
					.arg3.mode = 0700,

					.cancellation = cp_args->cancellation,
					.estim = cp_args->estim,

					.result = cp_args->result,
				};

				result = vr_from_io_res(iop_mkdir(&args));
				cp_args->result = args.result;
			}
			break;
		case VA_FILE:
			{
				io_args_t args = {
					.arg1.src = full_path,
					.arg2.dst = dst_full_path,
					.arg3.crs = cp_args->arg3.crs,
					/* It's safe to always use fast file cloning on moving files. */
					.arg4.fast_file_cloning = cp ? cp_args->arg4.fast_file_cloning : 1,
					.arg4.data_sync = cp_args->arg4.data_sync,

					.cancellation = cp_args->cancellation,
					.confirm = cp_args->confirm,
					.estim = cp_args->estim,

					.result = cp_args->result,
				};

				result = vr_from_io_res(cp ? iop_cp(&args) : ior_mv(&args));
				cp_args->result = args.result;
				break;
			}
		case VA_DIR_LEAVE:
			{
				struct stat st;

				if(cp_args->arg3.crs == IO_CRS_REPLACE_FILES && !cp)
				{
					io_args_t rm_args = {
						.arg1.path = full_path,

						.cancellation = cp_args->cancellation,
						.estim = cp_args->estim,

						.result = cp_args->result,
					};

					result = vr_from_io_res(iop_rmdir(&rm_args));
				}
				else if(os_stat(full_path, &st) == 0)
				{
					result = (os_chmod(dst_full_path, st.st_mode & 07777) == 0)
									? VR_OK
									: VR_ERROR;
					if(result == VR_ERROR)
					{
						(void)ioe_errlst_append(&cp_args->result.errors, dst_full_path,
								errno, "Failed to setup directory permissions");
					}
					clone_attribs(dst_full_path, full_path, &st);
				}
				else
				{
					(void)ioe_errlst_append(&cp_args->result.errors, full_path, errno,
							"Failed to stat() source directory");
					result = VR_ERROR;
				}
				break;
			}
	}

	free(free_me);

	return result;
}

/* Turns IoRes into VisitResult.  Returns VisitResult. */
static VisitResult
vr_from_io_res(IoRes result)
{
	switch(result)
	{
		case IO_RES_SUCCEEDED:
		case IO_RES_SKIPPED:
			return VR_OK;
		case IO_RES_FAILED:
			return VR_ERROR;
		case IO_RES_ABORTED:
			return VR_CANCELLED;
	}

	return VR_ERROR;
}

/* vim: set tabstop=2 softtabstop=2 shiftwidth=2 noexpandtab cinoptions-=(0 : */
/* vim: set cinoptions+=t0 filetype=c : */
Hints

Before first commit, do not forget to setup your git environment:
git config --global user.name "your_name_here"
git config --global user.email "your@email_here"

Clone this repository using HTTP(S):
git clone https://code.reversed.top/user/xaizek/vifm

Clone this repository using ssh (do not forget to upload a key first):
git clone ssh://rocketgit@code.reversed.top/user/xaizek/vifm

You are allowed to anonymously push to this repository.
This means that your pushed commits will automatically be transformed into a pull request:
... clone the repository ...
... make some changes and some commits ...
git push origin master