xref: /dragonfly/contrib/xz/src/xz/file_io.c (revision 92fc8b5c)
1 ///////////////////////////////////////////////////////////////////////////////
2 //
3 /// \file       file_io.c
4 /// \brief      File opening, unlinking, and closing
5 //
6 //  Author:     Lasse Collin
7 //
8 //  This file has been put into the public domain.
9 //  You can do whatever you want with this file.
10 //
11 ///////////////////////////////////////////////////////////////////////////////
12 
13 #include "private.h"
14 
15 #include <fcntl.h>
16 
17 #ifdef TUKLIB_DOSLIKE
18 #	include <io.h>
19 #else
20 static bool warn_fchown;
21 #endif
22 
23 #if defined(HAVE_FUTIMES) || defined(HAVE_FUTIMESAT) || defined(HAVE_UTIMES)
24 #	include <sys/time.h>
25 #elif defined(HAVE_UTIME)
26 #	include <utime.h>
27 #endif
28 
29 #include "tuklib_open_stdxxx.h"
30 
31 #ifndef O_BINARY
32 #	define O_BINARY 0
33 #endif
34 
35 #ifndef O_NOCTTY
36 #	define O_NOCTTY 0
37 #endif
38 
39 
40 /// If true, try to create sparse files when decompressing.
41 static bool try_sparse = true;
42 
43 #ifndef TUKLIB_DOSLIKE
44 /// File status flags of standard output. This is used by io_open_dest()
45 /// and io_close_dest().
46 static int stdout_flags = 0;
47 #endif
48 
49 
50 static bool io_write_buf(file_pair *pair, const uint8_t *buf, size_t size);
51 
52 
53 extern void
54 io_init(void)
55 {
56 	// Make sure that stdin, stdout, and and stderr are connected to
57 	// a valid file descriptor. Exit immediately with exit code ERROR
58 	// if we cannot make the file descriptors valid. Maybe we should
59 	// print an error message, but our stderr could be screwed anyway.
60 	tuklib_open_stdxxx(E_ERROR);
61 
62 #ifndef TUKLIB_DOSLIKE
63 	// If fchown() fails setting the owner, we warn about it only if
64 	// we are root.
65 	warn_fchown = geteuid() == 0;
66 #endif
67 
68 #ifdef __DJGPP__
69 	// Avoid doing useless things when statting files.
70 	// This isn't important but doesn't hurt.
71 	_djstat_flags = _STAT_INODE | _STAT_EXEC_EXT
72 			| _STAT_EXEC_MAGIC | _STAT_DIRSIZE;
73 #endif
74 
75 	return;
76 }
77 
78 
79 extern void
80 io_no_sparse(void)
81 {
82 	try_sparse = false;
83 	return;
84 }
85 
86 
87 /// \brief      Unlink a file
88 ///
89 /// This tries to verify that the file being unlinked really is the file that
90 /// we want to unlink by verifying device and inode numbers. There's still
91 /// a small unavoidable race, but this is much better than nothing (the file
92 /// could have been moved/replaced even hours earlier).
93 static void
94 io_unlink(const char *name, const struct stat *known_st)
95 {
96 #if defined(TUKLIB_DOSLIKE)
97 	// On DOS-like systems, st_ino is meaningless, so don't bother
98 	// testing it. Just silence a compiler warning.
99 	(void)known_st;
100 #else
101 	struct stat new_st;
102 
103 	// If --force was used, use stat() instead of lstat(). This way
104 	// (de)compressing symlinks works correctly. However, it also means
105 	// that xz cannot detect if a regular file foo is renamed to bar
106 	// and then a symlink foo -> bar is created. Because of stat()
107 	// instead of lstat(), xz will think that foo hasn't been replaced
108 	// with another file. Thus, xz will remove foo even though it no
109 	// longer is the same file that xz used when it started compressing.
110 	// Probably it's not too bad though, so this doesn't need a more
111 	// complex fix.
112 	const int stat_ret = opt_force
113 			? stat(name, &new_st) : lstat(name, &new_st);
114 
115 	if (stat_ret
116 #	ifdef __VMS
117 			// st_ino is an array, and we don't want to
118 			// compare st_dev at all.
119 			|| memcmp(&new_st.st_ino, &known_st->st_ino,
120 				sizeof(new_st.st_ino)) != 0
121 #	else
122 			// Typical POSIX-like system
123 			|| new_st.st_dev != known_st->st_dev
124 			|| new_st.st_ino != known_st->st_ino
125 #	endif
126 			)
127 		// TRANSLATORS: When compression or decompression finishes,
128 		// and xz is going to remove the source file, xz first checks
129 		// if the source file still exists, and if it does, does its
130 		// device and inode numbers match what xz saw when it opened
131 		// the source file. If these checks fail, this message is
132 		// shown, %s being the filename, and the file is not deleted.
133 		// The check for device and inode numbers is there, because
134 		// it is possible that the user has put a new file in place
135 		// of the original file, and in that case it obviously
136 		// shouldn't be removed.
137 		message_error(_("%s: File seems to have been moved, "
138 				"not removing"), name);
139 	else
140 #endif
141 		// There's a race condition between lstat() and unlink()
142 		// but at least we have tried to avoid removing wrong file.
143 		if (unlink(name))
144 			message_error(_("%s: Cannot remove: %s"),
145 					name, strerror(errno));
146 
147 	return;
148 }
149 
150 
151 /// \brief      Copies owner/group and permissions
152 ///
153 /// \todo       ACL and EA support
154 ///
155 static void
156 io_copy_attrs(const file_pair *pair)
157 {
158 	// Skip chown and chmod on Windows.
159 #ifndef TUKLIB_DOSLIKE
160 	// This function is more tricky than you may think at first.
161 	// Blindly copying permissions may permit users to access the
162 	// destination file who didn't have permission to access the
163 	// source file.
164 
165 	// Try changing the owner of the file. If we aren't root or the owner
166 	// isn't already us, fchown() probably doesn't succeed. We warn
167 	// about failing fchown() only if we are root.
168 	if (fchown(pair->dest_fd, pair->src_st.st_uid, -1) && warn_fchown)
169 		message_warning(_("%s: Cannot set the file owner: %s"),
170 				pair->dest_name, strerror(errno));
171 
172 	mode_t mode;
173 
174 	if (fchown(pair->dest_fd, -1, pair->src_st.st_gid)) {
175 		message_warning(_("%s: Cannot set the file group: %s"),
176 				pair->dest_name, strerror(errno));
177 		// We can still safely copy some additional permissions:
178 		// `group' must be at least as strict as `other' and
179 		// also vice versa.
180 		//
181 		// NOTE: After this, the owner of the source file may
182 		// get additional permissions. This shouldn't be too bad,
183 		// because the owner would have had permission to chmod
184 		// the original file anyway.
185 		mode = ((pair->src_st.st_mode & 0070) >> 3)
186 				& (pair->src_st.st_mode & 0007);
187 		mode = (pair->src_st.st_mode & 0700) | (mode << 3) | mode;
188 	} else {
189 		// Drop the setuid, setgid, and sticky bits.
190 		mode = pair->src_st.st_mode & 0777;
191 	}
192 
193 	if (fchmod(pair->dest_fd, mode))
194 		message_warning(_("%s: Cannot set the file permissions: %s"),
195 				pair->dest_name, strerror(errno));
196 #endif
197 
198 	// Copy the timestamps. We have several possible ways to do this, of
199 	// which some are better in both security and precision.
200 	//
201 	// First, get the nanosecond part of the timestamps. As of writing,
202 	// it's not standardized by POSIX, and there are several names for
203 	// the same thing in struct stat.
204 	long atime_nsec;
205 	long mtime_nsec;
206 
207 #	if defined(HAVE_STRUCT_STAT_ST_ATIM_TV_NSEC)
208 	// GNU and Solaris
209 	atime_nsec = pair->src_st.st_atim.tv_nsec;
210 	mtime_nsec = pair->src_st.st_mtim.tv_nsec;
211 
212 #	elif defined(HAVE_STRUCT_STAT_ST_ATIMESPEC_TV_NSEC)
213 	// BSD
214 	atime_nsec = pair->src_st.st_atimespec.tv_nsec;
215 	mtime_nsec = pair->src_st.st_mtimespec.tv_nsec;
216 
217 #	elif defined(HAVE_STRUCT_STAT_ST_ATIMENSEC)
218 	// GNU and BSD without extensions
219 	atime_nsec = pair->src_st.st_atimensec;
220 	mtime_nsec = pair->src_st.st_mtimensec;
221 
222 #	elif defined(HAVE_STRUCT_STAT_ST_UATIME)
223 	// Tru64
224 	atime_nsec = pair->src_st.st_uatime * 1000;
225 	mtime_nsec = pair->src_st.st_umtime * 1000;
226 
227 #	elif defined(HAVE_STRUCT_STAT_ST_ATIM_ST__TIM_TV_NSEC)
228 	// UnixWare
229 	atime_nsec = pair->src_st.st_atim.st__tim.tv_nsec;
230 	mtime_nsec = pair->src_st.st_mtim.st__tim.tv_nsec;
231 
232 #	else
233 	// Safe fallback
234 	atime_nsec = 0;
235 	mtime_nsec = 0;
236 #	endif
237 
238 	// Construct a structure to hold the timestamps and call appropriate
239 	// function to set the timestamps.
240 #if defined(HAVE_FUTIMENS)
241 	// Use nanosecond precision.
242 	struct timespec tv[2];
243 	tv[0].tv_sec = pair->src_st.st_atime;
244 	tv[0].tv_nsec = atime_nsec;
245 	tv[1].tv_sec = pair->src_st.st_mtime;
246 	tv[1].tv_nsec = mtime_nsec;
247 
248 	(void)futimens(pair->dest_fd, tv);
249 
250 #elif defined(HAVE_FUTIMES) || defined(HAVE_FUTIMESAT) || defined(HAVE_UTIMES)
251 	// Use microsecond precision.
252 	struct timeval tv[2];
253 	tv[0].tv_sec = pair->src_st.st_atime;
254 	tv[0].tv_usec = atime_nsec / 1000;
255 	tv[1].tv_sec = pair->src_st.st_mtime;
256 	tv[1].tv_usec = mtime_nsec / 1000;
257 
258 #	if defined(HAVE_FUTIMES)
259 	(void)futimes(pair->dest_fd, tv);
260 #	elif defined(HAVE_FUTIMESAT)
261 	(void)futimesat(pair->dest_fd, NULL, tv);
262 #	else
263 	// Argh, no function to use a file descriptor to set the timestamp.
264 	(void)utimes(pair->dest_name, tv);
265 #	endif
266 
267 #elif defined(HAVE_UTIME)
268 	// Use one-second precision. utime() doesn't support using file
269 	// descriptor either. Some systems have broken utime() prototype
270 	// so don't make this const.
271 	struct utimbuf buf = {
272 		.actime = pair->src_st.st_atime,
273 		.modtime = pair->src_st.st_mtime,
274 	};
275 
276 	// Avoid warnings.
277 	(void)atime_nsec;
278 	(void)mtime_nsec;
279 
280 	(void)utime(pair->dest_name, &buf);
281 #endif
282 
283 	return;
284 }
285 
286 
287 /// Opens the source file. Returns false on success, true on error.
288 static bool
289 io_open_src_real(file_pair *pair)
290 {
291 	// There's nothing to open when reading from stdin.
292 	if (pair->src_name == stdin_filename) {
293 		pair->src_fd = STDIN_FILENO;
294 #ifdef TUKLIB_DOSLIKE
295 		setmode(STDIN_FILENO, O_BINARY);
296 #endif
297 		return false;
298 	}
299 
300 	// Symlinks are not followed unless writing to stdout or --force
301 	// was used.
302 	const bool follow_symlinks = opt_stdout || opt_force;
303 
304 	// We accept only regular files if we are writing the output
305 	// to disk too. bzip2 allows overriding this with --force but
306 	// gzip and xz don't.
307 	const bool reg_files_only = !opt_stdout;
308 
309 	// Flags for open()
310 	int flags = O_RDONLY | O_BINARY | O_NOCTTY;
311 
312 #ifndef TUKLIB_DOSLIKE
313 	// If we accept only regular files, we need to be careful to avoid
314 	// problems with special files like devices and FIFOs. O_NONBLOCK
315 	// prevents blocking when opening such files. When we want to accept
316 	// special files, we must not use O_NONBLOCK, or otherwise we won't
317 	// block waiting e.g. FIFOs to become readable.
318 	if (reg_files_only)
319 		flags |= O_NONBLOCK;
320 #endif
321 
322 #if defined(O_NOFOLLOW)
323 	if (!follow_symlinks)
324 		flags |= O_NOFOLLOW;
325 #elif !defined(TUKLIB_DOSLIKE)
326 	// Some POSIX-like systems lack O_NOFOLLOW (it's not required
327 	// by POSIX). Check for symlinks with a separate lstat() on
328 	// these systems.
329 	if (!follow_symlinks) {
330 		struct stat st;
331 		if (lstat(pair->src_name, &st)) {
332 			message_error("%s: %s", pair->src_name,
333 					strerror(errno));
334 			return true;
335 
336 		} else if (S_ISLNK(st.st_mode)) {
337 			message_warning(_("%s: Is a symbolic link, "
338 					"skipping"), pair->src_name);
339 			return true;
340 		}
341 	}
342 #else
343 	// Avoid warnings.
344 	(void)follow_symlinks;
345 #endif
346 
347 	// Try to open the file. If we are accepting non-regular files,
348 	// unblock the caught signals so that open() can be interrupted
349 	// if it blocks e.g. due to a FIFO file.
350 	if (!reg_files_only)
351 		signals_unblock();
352 
353 	// Maybe this wouldn't need a loop, since all the signal handlers for
354 	// which we don't use SA_RESTART set user_abort to true. But it
355 	// doesn't hurt to have it just in case.
356 	do {
357 		pair->src_fd = open(pair->src_name, flags);
358 	} while (pair->src_fd == -1 && errno == EINTR && !user_abort);
359 
360 	if (!reg_files_only)
361 		signals_block();
362 
363 	if (pair->src_fd == -1) {
364 		// If we were interrupted, don't display any error message.
365 		if (errno == EINTR) {
366 			// All the signals that don't have SA_RESTART
367 			// set user_abort.
368 			assert(user_abort);
369 			return true;
370 		}
371 
372 #ifdef O_NOFOLLOW
373 		// Give an understandable error message if the reason
374 		// for failing was that the file was a symbolic link.
375 		//
376 		// Note that at least Linux, OpenBSD, Solaris, and Darwin
377 		// use ELOOP to indicate that O_NOFOLLOW was the reason
378 		// that open() failed. Because there may be
379 		// directories in the pathname, ELOOP may occur also
380 		// because of a symlink loop in the directory part.
381 		// So ELOOP doesn't tell us what actually went wrong,
382 		// and this stupidity went into POSIX-1.2008 too.
383 		//
384 		// FreeBSD associates EMLINK with O_NOFOLLOW and
385 		// Tru64 uses ENOTSUP. We use these directly here
386 		// and skip the lstat() call and the associated race.
387 		// I want to hear if there are other kernels that
388 		// fail with something else than ELOOP with O_NOFOLLOW.
389 		bool was_symlink = false;
390 
391 #	if defined(__FreeBSD__) || defined(__DragonFly__)
392 		if (errno == EMLINK)
393 			was_symlink = true;
394 
395 #	elif defined(__digital__) && defined(__unix__)
396 		if (errno == ENOTSUP)
397 			was_symlink = true;
398 
399 #	elif defined(__NetBSD__)
400 		// As of 2010-09-05, NetBSD doesn't document what errno is
401 		// used with O_NOFOLLOW. It is EFTYPE though, and I
402 		// understood that is very unlikely to change even though
403 		// it is undocumented.
404 		if (errno == EFTYPE)
405 			was_symlink = true;
406 
407 #	else
408 		if (errno == ELOOP && !follow_symlinks) {
409 			const int saved_errno = errno;
410 			struct stat st;
411 			if (lstat(pair->src_name, &st) == 0
412 					&& S_ISLNK(st.st_mode))
413 				was_symlink = true;
414 
415 			errno = saved_errno;
416 		}
417 #	endif
418 
419 		if (was_symlink)
420 			message_warning(_("%s: Is a symbolic link, "
421 					"skipping"), pair->src_name);
422 		else
423 #endif
424 			// Something else than O_NOFOLLOW failing
425 			// (assuming that the race conditions didn't
426 			// confuse us).
427 			message_error("%s: %s", pair->src_name,
428 					strerror(errno));
429 
430 		return true;
431 	}
432 
433 #ifndef TUKLIB_DOSLIKE
434 	// Drop O_NONBLOCK, which is used only when we are accepting only
435 	// regular files. After the open() call, we want things to block
436 	// instead of giving EAGAIN.
437 	if (reg_files_only) {
438 		flags = fcntl(pair->src_fd, F_GETFL);
439 		if (flags == -1)
440 			goto error_msg;
441 
442 		flags &= ~O_NONBLOCK;
443 
444 		if (fcntl(pair->src_fd, F_SETFL, flags))
445 			goto error_msg;
446 	}
447 #endif
448 
449 	// Stat the source file. We need the result also when we copy
450 	// the permissions, and when unlinking.
451 	if (fstat(pair->src_fd, &pair->src_st))
452 		goto error_msg;
453 
454 	if (S_ISDIR(pair->src_st.st_mode)) {
455 		message_warning(_("%s: Is a directory, skipping"),
456 				pair->src_name);
457 		goto error;
458 	}
459 
460 	if (reg_files_only) {
461 		if (!S_ISREG(pair->src_st.st_mode)) {
462 			message_warning(_("%s: Not a regular file, "
463 					"skipping"), pair->src_name);
464 			goto error;
465 		}
466 
467 		// These are meaningless on Windows.
468 #ifndef TUKLIB_DOSLIKE
469 		if (pair->src_st.st_mode & (S_ISUID | S_ISGID)) {
470 			// gzip rejects setuid and setgid files even
471 			// when --force was used. bzip2 doesn't check
472 			// for them, but calls fchown() after fchmod(),
473 			// and many systems automatically drop setuid
474 			// and setgid bits there.
475 			//
476 			// We accept setuid and setgid files if
477 			// --force was used. We drop these bits
478 			// explicitly in io_copy_attr().
479 			message_warning(_("%s: File has setuid or "
480 					"setgid bit set, skipping"),
481 					pair->src_name);
482 			goto error;
483 		}
484 
485 		if (pair->src_st.st_mode & S_ISVTX) {
486 			message_warning(_("%s: File has sticky bit "
487 					"set, skipping"),
488 					pair->src_name);
489 			goto error;
490 		}
491 
492 		if (pair->src_st.st_nlink > 1) {
493 			message_warning(_("%s: Input file has more "
494 					"than one hard link, "
495 					"skipping"), pair->src_name);
496 			goto error;
497 		}
498 #endif
499 	}
500 
501 	return false;
502 
503 error_msg:
504 	message_error("%s: %s", pair->src_name, strerror(errno));
505 error:
506 	(void)close(pair->src_fd);
507 	return true;
508 }
509 
510 
511 extern file_pair *
512 io_open_src(const char *src_name)
513 {
514 	if (is_empty_filename(src_name))
515 		return NULL;
516 
517 	// Since we have only one file open at a time, we can use
518 	// a statically allocated structure.
519 	static file_pair pair;
520 
521 	pair = (file_pair){
522 		.src_name = src_name,
523 		.dest_name = NULL,
524 		.src_fd = -1,
525 		.dest_fd = -1,
526 		.src_eof = false,
527 		.dest_try_sparse = false,
528 		.dest_pending_sparse = 0,
529 	};
530 
531 	// Block the signals, for which we have a custom signal handler, so
532 	// that we don't need to worry about EINTR.
533 	signals_block();
534 	const bool error = io_open_src_real(&pair);
535 	signals_unblock();
536 
537 	return error ? NULL : &pair;
538 }
539 
540 
541 /// \brief      Closes source file of the file_pair structure
542 ///
543 /// \param      pair    File whose src_fd should be closed
544 /// \param      success If true, the file will be removed from the disk if
545 ///                     closing succeeds and --keep hasn't been used.
546 static void
547 io_close_src(file_pair *pair, bool success)
548 {
549 	if (pair->src_fd != STDIN_FILENO && pair->src_fd != -1) {
550 #ifdef TUKLIB_DOSLIKE
551 		(void)close(pair->src_fd);
552 #endif
553 
554 		// If we are going to unlink(), do it before closing the file.
555 		// This way there's no risk that someone replaces the file and
556 		// happens to get same inode number, which would make us
557 		// unlink() wrong file.
558 		//
559 		// NOTE: DOS-like systems are an exception to this, because
560 		// they don't allow unlinking files that are open. *sigh*
561 		if (success && !opt_keep_original)
562 			io_unlink(pair->src_name, &pair->src_st);
563 
564 #ifndef TUKLIB_DOSLIKE
565 		(void)close(pair->src_fd);
566 #endif
567 	}
568 
569 	return;
570 }
571 
572 
573 static bool
574 io_open_dest_real(file_pair *pair)
575 {
576 	if (opt_stdout || pair->src_fd == STDIN_FILENO) {
577 		// We don't modify or free() this.
578 		pair->dest_name = (char *)"(stdout)";
579 		pair->dest_fd = STDOUT_FILENO;
580 #ifdef TUKLIB_DOSLIKE
581 		setmode(STDOUT_FILENO, O_BINARY);
582 #endif
583 	} else {
584 		pair->dest_name = suffix_get_dest_name(pair->src_name);
585 		if (pair->dest_name == NULL)
586 			return true;
587 
588 		// If --force was used, unlink the target file first.
589 		if (opt_force && unlink(pair->dest_name) && errno != ENOENT) {
590 			message_error(_("%s: Cannot remove: %s"),
591 					pair->dest_name, strerror(errno));
592 			free(pair->dest_name);
593 			return true;
594 		}
595 
596 		// Open the file.
597 		const int flags = O_WRONLY | O_BINARY | O_NOCTTY
598 				| O_CREAT | O_EXCL;
599 		const mode_t mode = S_IRUSR | S_IWUSR;
600 		pair->dest_fd = open(pair->dest_name, flags, mode);
601 
602 		if (pair->dest_fd == -1) {
603 			message_error("%s: %s", pair->dest_name,
604 					strerror(errno));
605 			free(pair->dest_name);
606 			return true;
607 		}
608 	}
609 
610 	// If this really fails... well, we have a safe fallback.
611 	if (fstat(pair->dest_fd, &pair->dest_st)) {
612 #if defined(__VMS)
613 		pair->dest_st.st_ino[0] = 0;
614 		pair->dest_st.st_ino[1] = 0;
615 		pair->dest_st.st_ino[2] = 0;
616 #elif !defined(TUKLIB_DOSLIKE)
617 		pair->dest_st.st_dev = 0;
618 		pair->dest_st.st_ino = 0;
619 #endif
620 #ifndef TUKLIB_DOSLIKE
621 	} else if (try_sparse && opt_mode == MODE_DECOMPRESS) {
622 		// When writing to standard output, we need to be extra
623 		// careful:
624 		//  - It may be connected to something else than
625 		//    a regular file.
626 		//  - We aren't necessarily writing to a new empty file
627 		//    or to the end of an existing file.
628 		//  - O_APPEND may be active.
629 		//
630 		// TODO: I'm keeping this disabled for DOS-like systems
631 		// for now. FAT doesn't support sparse files, but NTFS
632 		// does, so maybe this should be enabled on Windows after
633 		// some testing.
634 		if (pair->dest_fd == STDOUT_FILENO) {
635 			if (!S_ISREG(pair->dest_st.st_mode))
636 				return false;
637 
638 			const int flags = fcntl(STDOUT_FILENO, F_GETFL);
639 			if (flags == -1)
640 				return false;
641 
642 			if (flags & O_APPEND) {
643 				// Creating a sparse file is not possible
644 				// when O_APPEND is active (it's used by
645 				// shell's >> redirection). As I understand
646 				// it, it is safe to temporarily disable
647 				// O_APPEND in xz, because if someone
648 				// happened to write to the same file at the
649 				// same time, results would be bad anyway
650 				// (users shouldn't assume that xz uses any
651 				// specific block size when writing data).
652 				//
653 				// The write position may be something else
654 				// than the end of the file, so we must fix
655 				// it to start writing at the end of the file
656 				// to imitate O_APPEND.
657 				if (lseek(STDOUT_FILENO, 0, SEEK_END) == -1)
658 					return false;
659 
660 				if (fcntl(STDOUT_FILENO, F_SETFL,
661 						stdout_flags & ~O_APPEND))
662 					return false;
663 
664 				// Remember the flags so that io_close_dest()
665 				// can restore them.
666 				stdout_flags = flags;
667 
668 			} else if (lseek(STDOUT_FILENO, 0, SEEK_CUR)
669 					!= pair->dest_st.st_size) {
670 				// Writing won't start exactly at the end
671 				// of the file. We cannot use sparse output,
672 				// because it would probably corrupt the file.
673 				return false;
674 			}
675 		}
676 
677 		pair->dest_try_sparse = true;
678 #endif
679 	}
680 
681 	return false;
682 }
683 
684 
685 extern bool
686 io_open_dest(file_pair *pair)
687 {
688 	signals_block();
689 	const bool ret = io_open_dest_real(pair);
690 	signals_unblock();
691 	return ret;
692 }
693 
694 
695 /// \brief      Closes destination file of the file_pair structure
696 ///
697 /// \param      pair    File whose dest_fd should be closed
698 /// \param      success If false, the file will be removed from the disk.
699 ///
700 /// \return     Zero if closing succeeds. On error, -1 is returned and
701 ///             error message printed.
702 static bool
703 io_close_dest(file_pair *pair, bool success)
704 {
705 #ifndef TUKLIB_DOSLIKE
706 	// If io_open_dest() has disabled O_APPEND, restore it here.
707 	if (stdout_flags != 0) {
708 		assert(pair->dest_fd == STDOUT_FILENO);
709 
710 		const int fail = fcntl(STDOUT_FILENO, F_SETFL, stdout_flags);
711 		stdout_flags = 0;
712 
713 		if (fail) {
714 			message_error(_("Error restoring the O_APPEND flag "
715 					"to standard output: %s"),
716 					strerror(errno));
717 			return true;
718 		}
719 	}
720 #endif
721 
722 	if (pair->dest_fd == -1 || pair->dest_fd == STDOUT_FILENO)
723 		return false;
724 
725 	if (close(pair->dest_fd)) {
726 		message_error(_("%s: Closing the file failed: %s"),
727 				pair->dest_name, strerror(errno));
728 
729 		// Closing destination file failed, so we cannot trust its
730 		// contents. Get rid of junk:
731 		io_unlink(pair->dest_name, &pair->dest_st);
732 		free(pair->dest_name);
733 		return true;
734 	}
735 
736 	// If the operation using this file wasn't successful, we git rid
737 	// of the junk file.
738 	if (!success)
739 		io_unlink(pair->dest_name, &pair->dest_st);
740 
741 	free(pair->dest_name);
742 
743 	return false;
744 }
745 
746 
747 extern void
748 io_close(file_pair *pair, bool success)
749 {
750 	// Take care of sparseness at the end of the output file.
751 	if (success && pair->dest_try_sparse
752 			&& pair->dest_pending_sparse > 0) {
753 		// Seek forward one byte less than the size of the pending
754 		// hole, then write one zero-byte. This way the file grows
755 		// to its correct size. An alternative would be to use
756 		// ftruncate() but that isn't portable enough (e.g. it
757 		// doesn't work with FAT on Linux; FAT isn't that important
758 		// since it doesn't support sparse files anyway, but we don't
759 		// want to create corrupt files on it).
760 		if (lseek(pair->dest_fd, pair->dest_pending_sparse - 1,
761 				SEEK_CUR) == -1) {
762 			message_error(_("%s: Seeking failed when trying "
763 					"to create a sparse file: %s"),
764 					pair->dest_name, strerror(errno));
765 			success = false;
766 		} else {
767 			const uint8_t zero[1] = { '\0' };
768 			if (io_write_buf(pair, zero, 1))
769 				success = false;
770 		}
771 	}
772 
773 	signals_block();
774 
775 	// Copy the file attributes. We need to skip this if destination
776 	// file isn't open or it is standard output.
777 	if (success && pair->dest_fd != -1 && pair->dest_fd != STDOUT_FILENO)
778 		io_copy_attrs(pair);
779 
780 	// Close the destination first. If it fails, we must not remove
781 	// the source file!
782 	if (io_close_dest(pair, success))
783 		success = false;
784 
785 	// Close the source file, and unlink it if the operation using this
786 	// file pair was successful and we haven't requested to keep the
787 	// source file.
788 	io_close_src(pair, success);
789 
790 	signals_unblock();
791 
792 	return;
793 }
794 
795 
796 extern size_t
797 io_read(file_pair *pair, io_buf *buf_union, size_t size)
798 {
799 	// We use small buffers here.
800 	assert(size < SSIZE_MAX);
801 
802 	uint8_t *buf = buf_union->u8;
803 	size_t left = size;
804 
805 	while (left > 0) {
806 		const ssize_t amount = read(pair->src_fd, buf, left);
807 
808 		if (amount == 0) {
809 			pair->src_eof = true;
810 			break;
811 		}
812 
813 		if (amount == -1) {
814 			if (errno == EINTR) {
815 				if (user_abort)
816 					return SIZE_MAX;
817 
818 				continue;
819 			}
820 
821 			message_error(_("%s: Read error: %s"),
822 					pair->src_name, strerror(errno));
823 
824 			// FIXME Is this needed?
825 			pair->src_eof = true;
826 
827 			return SIZE_MAX;
828 		}
829 
830 		buf += (size_t)(amount);
831 		left -= (size_t)(amount);
832 	}
833 
834 	return size - left;
835 }
836 
837 
838 extern bool
839 io_pread(file_pair *pair, io_buf *buf, size_t size, off_t pos)
840 {
841 	// Using lseek() and read() is more portable than pread() and
842 	// for us it is as good as real pread().
843 	if (lseek(pair->src_fd, pos, SEEK_SET) != pos) {
844 		message_error(_("%s: Error seeking the file: %s"),
845 				pair->src_name, strerror(errno));
846 		return true;
847 	}
848 
849 	const size_t amount = io_read(pair, buf, size);
850 	if (amount == SIZE_MAX)
851 		return true;
852 
853 	if (amount != size) {
854 		message_error(_("%s: Unexpected end of file"),
855 				pair->src_name);
856 		return true;
857 	}
858 
859 	return false;
860 }
861 
862 
863 static bool
864 is_sparse(const io_buf *buf)
865 {
866 	assert(IO_BUFFER_SIZE % sizeof(uint64_t) == 0);
867 
868 	for (size_t i = 0; i < ARRAY_SIZE(buf->u64); ++i)
869 		if (buf->u64[i] != 0)
870 			return false;
871 
872 	return true;
873 }
874 
875 
876 static bool
877 io_write_buf(file_pair *pair, const uint8_t *buf, size_t size)
878 {
879 	assert(size < SSIZE_MAX);
880 
881 	while (size > 0) {
882 		const ssize_t amount = write(pair->dest_fd, buf, size);
883 		if (amount == -1) {
884 			if (errno == EINTR) {
885 				if (user_abort)
886 					return -1;
887 
888 				continue;
889 			}
890 
891 			// Handle broken pipe specially. gzip and bzip2
892 			// don't print anything on SIGPIPE. In addition,
893 			// gzip --quiet uses exit status 2 (warning) on
894 			// broken pipe instead of whatever raise(SIGPIPE)
895 			// would make it return. It is there to hide "Broken
896 			// pipe" message on some old shells (probably old
897 			// GNU bash).
898 			//
899 			// We don't do anything special with --quiet, which
900 			// is what bzip2 does too. If we get SIGPIPE, we
901 			// will handle it like other signals by setting
902 			// user_abort, and get EPIPE here.
903 			if (errno != EPIPE)
904 				message_error(_("%s: Write error: %s"),
905 					pair->dest_name, strerror(errno));
906 
907 			return true;
908 		}
909 
910 		buf += (size_t)(amount);
911 		size -= (size_t)(amount);
912 	}
913 
914 	return false;
915 }
916 
917 
918 extern bool
919 io_write(file_pair *pair, const io_buf *buf, size_t size)
920 {
921 	assert(size <= IO_BUFFER_SIZE);
922 
923 	if (pair->dest_try_sparse) {
924 		// Check if the block is sparse (contains only zeros). If it
925 		// sparse, we just store the amount and return. We will take
926 		// care of actually skipping over the hole when we hit the
927 		// next data block or close the file.
928 		//
929 		// Since io_close() requires that dest_pending_sparse > 0
930 		// if the file ends with sparse block, we must also return
931 		// if size == 0 to avoid doing the lseek().
932 		if (size == IO_BUFFER_SIZE) {
933 			if (is_sparse(buf)) {
934 				pair->dest_pending_sparse += size;
935 				return false;
936 			}
937 		} else if (size == 0) {
938 			return false;
939 		}
940 
941 		// This is not a sparse block. If we have a pending hole,
942 		// skip it now.
943 		if (pair->dest_pending_sparse > 0) {
944 			if (lseek(pair->dest_fd, pair->dest_pending_sparse,
945 					SEEK_CUR) == -1) {
946 				message_error(_("%s: Seeking failed when "
947 						"trying to create a sparse "
948 						"file: %s"), pair->dest_name,
949 						strerror(errno));
950 				return true;
951 			}
952 
953 			pair->dest_pending_sparse = 0;
954 		}
955 	}
956 
957 	return io_write_buf(pair, buf->u8, size);
958 }
959