18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci#include "util/copyfile.h"
38c2ecf20Sopenharmony_ci#include "util/namespaces.h"
48c2ecf20Sopenharmony_ci#include <internal/lib.h>
58c2ecf20Sopenharmony_ci#include <sys/mman.h>
68c2ecf20Sopenharmony_ci#include <sys/stat.h>
78c2ecf20Sopenharmony_ci#include <errno.h>
88c2ecf20Sopenharmony_ci#include <fcntl.h>
98c2ecf20Sopenharmony_ci#include <stdio.h>
108c2ecf20Sopenharmony_ci#include <stdlib.h>
118c2ecf20Sopenharmony_ci#include <string.h>
128c2ecf20Sopenharmony_ci#include <unistd.h>
138c2ecf20Sopenharmony_ci
148c2ecf20Sopenharmony_cistatic int slow_copyfile(const char *from, const char *to, struct nsinfo *nsi)
158c2ecf20Sopenharmony_ci{
168c2ecf20Sopenharmony_ci	int err = -1;
178c2ecf20Sopenharmony_ci	char *line = NULL;
188c2ecf20Sopenharmony_ci	size_t n;
198c2ecf20Sopenharmony_ci	FILE *from_fp, *to_fp;
208c2ecf20Sopenharmony_ci	struct nscookie nsc;
218c2ecf20Sopenharmony_ci
228c2ecf20Sopenharmony_ci	nsinfo__mountns_enter(nsi, &nsc);
238c2ecf20Sopenharmony_ci	from_fp = fopen(from, "r");
248c2ecf20Sopenharmony_ci	nsinfo__mountns_exit(&nsc);
258c2ecf20Sopenharmony_ci	if (from_fp == NULL)
268c2ecf20Sopenharmony_ci		goto out;
278c2ecf20Sopenharmony_ci
288c2ecf20Sopenharmony_ci	to_fp = fopen(to, "w");
298c2ecf20Sopenharmony_ci	if (to_fp == NULL)
308c2ecf20Sopenharmony_ci		goto out_fclose_from;
318c2ecf20Sopenharmony_ci
328c2ecf20Sopenharmony_ci	while (getline(&line, &n, from_fp) > 0)
338c2ecf20Sopenharmony_ci		if (fputs(line, to_fp) == EOF)
348c2ecf20Sopenharmony_ci			goto out_fclose_to;
358c2ecf20Sopenharmony_ci	err = 0;
368c2ecf20Sopenharmony_ciout_fclose_to:
378c2ecf20Sopenharmony_ci	fclose(to_fp);
388c2ecf20Sopenharmony_ci	free(line);
398c2ecf20Sopenharmony_ciout_fclose_from:
408c2ecf20Sopenharmony_ci	fclose(from_fp);
418c2ecf20Sopenharmony_ciout:
428c2ecf20Sopenharmony_ci	return err;
438c2ecf20Sopenharmony_ci}
448c2ecf20Sopenharmony_ci
458c2ecf20Sopenharmony_ciint copyfile_offset(int ifd, loff_t off_in, int ofd, loff_t off_out, u64 size)
468c2ecf20Sopenharmony_ci{
478c2ecf20Sopenharmony_ci	void *ptr;
488c2ecf20Sopenharmony_ci	loff_t pgoff;
498c2ecf20Sopenharmony_ci
508c2ecf20Sopenharmony_ci	pgoff = off_in & ~(page_size - 1);
518c2ecf20Sopenharmony_ci	off_in -= pgoff;
528c2ecf20Sopenharmony_ci
538c2ecf20Sopenharmony_ci	ptr = mmap(NULL, off_in + size, PROT_READ, MAP_PRIVATE, ifd, pgoff);
548c2ecf20Sopenharmony_ci	if (ptr == MAP_FAILED)
558c2ecf20Sopenharmony_ci		return -1;
568c2ecf20Sopenharmony_ci
578c2ecf20Sopenharmony_ci	while (size) {
588c2ecf20Sopenharmony_ci		ssize_t ret = pwrite(ofd, ptr + off_in, size, off_out);
598c2ecf20Sopenharmony_ci		if (ret < 0 && errno == EINTR)
608c2ecf20Sopenharmony_ci			continue;
618c2ecf20Sopenharmony_ci		if (ret <= 0)
628c2ecf20Sopenharmony_ci			break;
638c2ecf20Sopenharmony_ci
648c2ecf20Sopenharmony_ci		size -= ret;
658c2ecf20Sopenharmony_ci		off_in += ret;
668c2ecf20Sopenharmony_ci		off_out += ret;
678c2ecf20Sopenharmony_ci	}
688c2ecf20Sopenharmony_ci	munmap(ptr, off_in + size);
698c2ecf20Sopenharmony_ci
708c2ecf20Sopenharmony_ci	return size ? -1 : 0;
718c2ecf20Sopenharmony_ci}
728c2ecf20Sopenharmony_ci
738c2ecf20Sopenharmony_cistatic int copyfile_mode_ns(const char *from, const char *to, mode_t mode,
748c2ecf20Sopenharmony_ci			    struct nsinfo *nsi)
758c2ecf20Sopenharmony_ci{
768c2ecf20Sopenharmony_ci	int fromfd, tofd;
778c2ecf20Sopenharmony_ci	struct stat st;
788c2ecf20Sopenharmony_ci	int err;
798c2ecf20Sopenharmony_ci	char *tmp = NULL, *ptr = NULL;
808c2ecf20Sopenharmony_ci	struct nscookie nsc;
818c2ecf20Sopenharmony_ci
828c2ecf20Sopenharmony_ci	nsinfo__mountns_enter(nsi, &nsc);
838c2ecf20Sopenharmony_ci	err = stat(from, &st);
848c2ecf20Sopenharmony_ci	nsinfo__mountns_exit(&nsc);
858c2ecf20Sopenharmony_ci	if (err)
868c2ecf20Sopenharmony_ci		goto out;
878c2ecf20Sopenharmony_ci	err = -1;
888c2ecf20Sopenharmony_ci
898c2ecf20Sopenharmony_ci	/* extra 'x' at the end is to reserve space for '.' */
908c2ecf20Sopenharmony_ci	if (asprintf(&tmp, "%s.XXXXXXx", to) < 0) {
918c2ecf20Sopenharmony_ci		tmp = NULL;
928c2ecf20Sopenharmony_ci		goto out;
938c2ecf20Sopenharmony_ci	}
948c2ecf20Sopenharmony_ci	ptr = strrchr(tmp, '/');
958c2ecf20Sopenharmony_ci	if (!ptr)
968c2ecf20Sopenharmony_ci		goto out;
978c2ecf20Sopenharmony_ci	ptr = memmove(ptr + 1, ptr, strlen(ptr) - 1);
988c2ecf20Sopenharmony_ci	*ptr = '.';
998c2ecf20Sopenharmony_ci
1008c2ecf20Sopenharmony_ci	tofd = mkstemp(tmp);
1018c2ecf20Sopenharmony_ci	if (tofd < 0)
1028c2ecf20Sopenharmony_ci		goto out;
1038c2ecf20Sopenharmony_ci
1048c2ecf20Sopenharmony_ci	if (st.st_size == 0) { /* /proc? do it slowly... */
1058c2ecf20Sopenharmony_ci		err = slow_copyfile(from, tmp, nsi);
1068c2ecf20Sopenharmony_ci		if (!err && fchmod(tofd, mode))
1078c2ecf20Sopenharmony_ci			err = -1;
1088c2ecf20Sopenharmony_ci		goto out_close_to;
1098c2ecf20Sopenharmony_ci	}
1108c2ecf20Sopenharmony_ci
1118c2ecf20Sopenharmony_ci	if (fchmod(tofd, mode))
1128c2ecf20Sopenharmony_ci		goto out_close_to;
1138c2ecf20Sopenharmony_ci
1148c2ecf20Sopenharmony_ci	nsinfo__mountns_enter(nsi, &nsc);
1158c2ecf20Sopenharmony_ci	fromfd = open(from, O_RDONLY);
1168c2ecf20Sopenharmony_ci	nsinfo__mountns_exit(&nsc);
1178c2ecf20Sopenharmony_ci	if (fromfd < 0)
1188c2ecf20Sopenharmony_ci		goto out_close_to;
1198c2ecf20Sopenharmony_ci
1208c2ecf20Sopenharmony_ci	err = copyfile_offset(fromfd, 0, tofd, 0, st.st_size);
1218c2ecf20Sopenharmony_ci
1228c2ecf20Sopenharmony_ci	close(fromfd);
1238c2ecf20Sopenharmony_ciout_close_to:
1248c2ecf20Sopenharmony_ci	close(tofd);
1258c2ecf20Sopenharmony_ci	if (!err)
1268c2ecf20Sopenharmony_ci		err = link(tmp, to);
1278c2ecf20Sopenharmony_ci	unlink(tmp);
1288c2ecf20Sopenharmony_ciout:
1298c2ecf20Sopenharmony_ci	free(tmp);
1308c2ecf20Sopenharmony_ci	return err;
1318c2ecf20Sopenharmony_ci}
1328c2ecf20Sopenharmony_ci
1338c2ecf20Sopenharmony_ciint copyfile_ns(const char *from, const char *to, struct nsinfo *nsi)
1348c2ecf20Sopenharmony_ci{
1358c2ecf20Sopenharmony_ci	return copyfile_mode_ns(from, to, 0755, nsi);
1368c2ecf20Sopenharmony_ci}
1378c2ecf20Sopenharmony_ci
1388c2ecf20Sopenharmony_ciint copyfile_mode(const char *from, const char *to, mode_t mode)
1398c2ecf20Sopenharmony_ci{
1408c2ecf20Sopenharmony_ci	return copyfile_mode_ns(from, to, mode, NULL);
1418c2ecf20Sopenharmony_ci}
1428c2ecf20Sopenharmony_ci
1438c2ecf20Sopenharmony_ciint copyfile(const char *from, const char *to)
1448c2ecf20Sopenharmony_ci{
1458c2ecf20Sopenharmony_ci	return copyfile_mode(from, to, 0755);
1468c2ecf20Sopenharmony_ci}
147