]>
Commit | Line | Data |
---|---|---|
e3642c43 DL |
1 | /* |
2 | * lxc: linux Container library | |
3 | * | |
4 | * (C) Copyright IBM Corp. 2007, 2008 | |
5 | * | |
6 | * Authors: | |
7 | * Daniel Lezcano <dlezcano at fr.ibm.com> | |
8 | * | |
9 | * This library is free software; you can redistribute it and/or | |
10 | * modify it under the terms of the GNU Lesser General Public | |
11 | * License as published by the Free Software Foundation; either | |
12 | * version 2.1 of the License, or (at your option) any later version. | |
13 | * | |
14 | * This library is distributed in the hope that it will be useful, | |
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
17 | * Lesser General Public License for more details. | |
18 | * | |
19 | * You should have received a copy of the GNU Lesser General Public | |
20 | * License along with this library; if not, write to the Free Software | |
21 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
22 | */ | |
23 | ||
24 | #include <fcntl.h> | |
25 | #include <errno.h> | |
26 | #include <unistd.h> | |
27 | #include <sys/types.h> | |
28 | #include <sys/stat.h> | |
29 | #include <sys/mman.h> | |
30 | ||
31 | #include "log.h" | |
32 | ||
33 | lxc_log_define(lxc_utils, lxc); | |
34 | ||
35 | int lxc_copy_file(const char *srcfile, const char *dstfile) | |
36 | { | |
37 | void *srcaddr = NULL, *dstaddr; | |
38 | struct stat stat; | |
39 | int srcfd, dstfd, ret = -1; | |
40 | char c = '\0'; | |
41 | ||
42 | dstfd = open(dstfile, O_CREAT | O_EXCL | O_RDWR, 0600); | |
43 | if (dstfd < 0) { | |
44 | SYSERROR("failed to creat '%s'", dstfile); | |
45 | goto out; | |
46 | } | |
47 | ||
48 | srcfd = open(srcfile, O_RDONLY); | |
49 | if (srcfd < 0) { | |
50 | SYSERROR("failed to open '%s'", srcfile); | |
51 | goto err; | |
52 | } | |
53 | ||
54 | if (fstat(srcfd, &stat)) { | |
55 | SYSERROR("failed to stat '%s'", srcfile); | |
56 | goto err; | |
57 | } | |
58 | ||
59 | if (!stat.st_size) { | |
60 | INFO("copy '%s' which is an empty file", srcfile); | |
61 | ret = 0; | |
62 | goto out_close; | |
63 | } | |
64 | ||
65 | if (lseek(dstfd, stat.st_size - 1, SEEK_SET) < 0) { | |
66 | SYSERROR("failed to seek dest file '%s'", dstfile); | |
67 | goto err; | |
68 | } | |
69 | ||
70 | /* fixup length */ | |
71 | if (write(dstfd, &c, 1) < 0) { | |
72 | SYSERROR("failed to write to '%s'", dstfile); | |
73 | goto err; | |
74 | } | |
75 | ||
76 | srcaddr = mmap(NULL, stat.st_size, PROT_READ, MAP_SHARED, srcfd, 0L); | |
77 | if (srcaddr == MAP_FAILED) { | |
78 | SYSERROR("failed to mmap '%s'", srcfile); | |
79 | goto err; | |
80 | } | |
81 | ||
82 | dstaddr = mmap(NULL, stat.st_size, PROT_WRITE, MAP_SHARED, dstfd, 0L); | |
83 | if (dstaddr == MAP_FAILED) { | |
84 | SYSERROR("failed to mmap '%s'", dstfile); | |
85 | goto err; | |
86 | } | |
87 | ||
88 | ret = 0; | |
89 | ||
90 | memcpy(dstaddr, srcaddr, stat.st_size); | |
91 | ||
92 | munmap(dstaddr, stat.st_size); | |
93 | out_mmap: | |
94 | if (srcaddr) | |
95 | munmap(srcaddr, stat.st_size); | |
96 | out_close: | |
97 | close(dstfd); | |
98 | close(srcfd); | |
99 | out: | |
100 | return ret; | |
101 | err: | |
102 | unlink(dstfile); | |
103 | goto out_mmap; | |
104 | } |