Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 #include "util/copyfile.h"
0003 #include "util/namespaces.h"
0004 #include <internal/lib.h>
0005 #include <sys/mman.h>
0006 #include <sys/stat.h>
0007 #include <errno.h>
0008 #include <fcntl.h>
0009 #include <stdio.h>
0010 #include <stdlib.h>
0011 #include <string.h>
0012 #include <unistd.h>
0013 
0014 static int slow_copyfile(const char *from, const char *to, struct nsinfo *nsi)
0015 {
0016     int err = -1;
0017     char *line = NULL;
0018     size_t n;
0019     FILE *from_fp, *to_fp;
0020     struct nscookie nsc;
0021 
0022     nsinfo__mountns_enter(nsi, &nsc);
0023     from_fp = fopen(from, "r");
0024     nsinfo__mountns_exit(&nsc);
0025     if (from_fp == NULL)
0026         goto out;
0027 
0028     to_fp = fopen(to, "w");
0029     if (to_fp == NULL)
0030         goto out_fclose_from;
0031 
0032     while (getline(&line, &n, from_fp) > 0)
0033         if (fputs(line, to_fp) == EOF)
0034             goto out_fclose_to;
0035     err = 0;
0036 out_fclose_to:
0037     fclose(to_fp);
0038     free(line);
0039 out_fclose_from:
0040     fclose(from_fp);
0041 out:
0042     return err;
0043 }
0044 
0045 int copyfile_offset(int ifd, loff_t off_in, int ofd, loff_t off_out, u64 size)
0046 {
0047     void *ptr;
0048     loff_t pgoff;
0049 
0050     pgoff = off_in & ~(page_size - 1);
0051     off_in -= pgoff;
0052 
0053     ptr = mmap(NULL, off_in + size, PROT_READ, MAP_PRIVATE, ifd, pgoff);
0054     if (ptr == MAP_FAILED)
0055         return -1;
0056 
0057     while (size) {
0058         ssize_t ret = pwrite(ofd, ptr + off_in, size, off_out);
0059         if (ret < 0 && errno == EINTR)
0060             continue;
0061         if (ret <= 0)
0062             break;
0063 
0064         size -= ret;
0065         off_in += ret;
0066         off_out += ret;
0067     }
0068     munmap(ptr, off_in + size);
0069 
0070     return size ? -1 : 0;
0071 }
0072 
0073 static int copyfile_mode_ns(const char *from, const char *to, mode_t mode,
0074                 struct nsinfo *nsi)
0075 {
0076     int fromfd, tofd;
0077     struct stat st;
0078     int err;
0079     char *tmp = NULL, *ptr = NULL;
0080     struct nscookie nsc;
0081 
0082     nsinfo__mountns_enter(nsi, &nsc);
0083     err = stat(from, &st);
0084     nsinfo__mountns_exit(&nsc);
0085     if (err)
0086         goto out;
0087     err = -1;
0088 
0089     /* extra 'x' at the end is to reserve space for '.' */
0090     if (asprintf(&tmp, "%s.XXXXXXx", to) < 0) {
0091         tmp = NULL;
0092         goto out;
0093     }
0094     ptr = strrchr(tmp, '/');
0095     if (!ptr)
0096         goto out;
0097     ptr = memmove(ptr + 1, ptr, strlen(ptr) - 1);
0098     *ptr = '.';
0099 
0100     tofd = mkstemp(tmp);
0101     if (tofd < 0)
0102         goto out;
0103 
0104     if (st.st_size == 0) { /* /proc? do it slowly... */
0105         err = slow_copyfile(from, tmp, nsi);
0106         if (!err && fchmod(tofd, mode))
0107             err = -1;
0108         goto out_close_to;
0109     }
0110 
0111     if (fchmod(tofd, mode))
0112         goto out_close_to;
0113 
0114     nsinfo__mountns_enter(nsi, &nsc);
0115     fromfd = open(from, O_RDONLY);
0116     nsinfo__mountns_exit(&nsc);
0117     if (fromfd < 0)
0118         goto out_close_to;
0119 
0120     err = copyfile_offset(fromfd, 0, tofd, 0, st.st_size);
0121 
0122     close(fromfd);
0123 out_close_to:
0124     close(tofd);
0125     if (!err)
0126         err = link(tmp, to);
0127     unlink(tmp);
0128 out:
0129     free(tmp);
0130     return err;
0131 }
0132 
0133 int copyfile_ns(const char *from, const char *to, struct nsinfo *nsi)
0134 {
0135     return copyfile_mode_ns(from, to, 0755, nsi);
0136 }
0137 
0138 int copyfile_mode(const char *from, const char *to, mode_t mode)
0139 {
0140     return copyfile_mode_ns(from, to, mode, NULL);
0141 }
0142 
0143 int copyfile(const char *from, const char *to)
0144 {
0145     return copyfile_mode(from, to, 0755);
0146 }