d39057a63348b19d54ded3d4aa19a9ca66d498aa
[util-vserver.git] / util-vserver / lib_internal / unify-copy.c
1 // $Id$    --*- c -*--
2
3 // Copyright (C) 2004 Enrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
4 //  
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; version 2 of the License.
8 //  
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 // GNU General Public License for more details.
13 //  
14 // You should have received a copy of the GNU General Public License
15 // along with this program; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17
18
19 #ifdef HAVE_CONFIG_H
20 #  include <config.h>
21 #endif
22
23 #include "unify.h"
24 #include "util.h"
25
26 #include <unistd.h>
27 #include <errno.h>
28 #include <fcntl.h>
29 #include <setjmp.h>
30 #include <signal.h>
31 #include <sys/stat.h>
32 #include <sys/mman.h>
33
34 #define ENSC_WRAPPERS_IO        1
35 #include <wrappers.h>
36
37 #define MMAP_BLOCKSIZE          (16 * 1024*1024)
38
39 #ifndef   TESTSUITE_COPY_CODE
40 #  define TESTSUITE_COPY_CODE   do { } while (false)
41 #endif
42
43 static inline bool
44 verifySource(int fd, struct stat const *exp_stat)
45 {
46   struct stat           st;
47   
48   return (fstat(fd, &st)!=-1 &&
49           st.st_dev==exp_stat->st_dev &&
50           st.st_ino==exp_stat->st_ino);
51 }
52
53 static inline bool
54 copyLnk(char const *src, char const *dst)
55 {
56   ssize_t       len = 1024;
57   for (;;) {
58     char        buf[len];
59     ssize_t     l;
60     l = readlink(src, buf, len-1);
61     if (l==-1) return false;
62     if (l>=len-1) {
63       len *= 2;
64       continue;
65     }
66     buf[l] = '\0';
67
68     return (symlink(buf, dst)!=-1);
69   }
70 }
71
72 static sigjmp_buf               bus_error_restore;
73 static volatile sig_atomic_t    bus_error;
74
75 static void
76 handlerSIGBUS(int UNUSED num)
77 {
78   bus_error = 1;
79   siglongjmp(bus_error_restore, 1);
80 }
81
82 static void
83 copyMem(void *dst_v, void const *src_v, size_t len_v)
84 {
85 #if 1
86   int           *dst = dst_v;
87   int const     *src = src_v;
88   size_t        len  = len_v / sizeof(int);
89   size_t        rest = len_v - sizeof(int)*len;
90   size_t        i=0;
91
92   for (; i<len; ++i) {
93     if (*src != 0) *dst = *src;
94     ++dst;
95     ++src;
96   }
97
98   char          *dst_c = (void *)(dst);
99   char const    *src_c = (void const *)(src);
100
101   for (i=0; i<rest; ++i) {
102     if (*src_c != 0) *dst_c = *src_c;
103     ++dst_c;
104     ++src_c;
105   }
106 #else
107   memcpy(dst_v, src_v, len_v);
108 #endif  
109 }
110
111 static UNUSED bool
112 copyMMap(int in_fd, int out_fd)
113 {
114   off_t                 in_len   = lseek(in_fd, 0, SEEK_END);
115   volatile void const   *in_buf  = 0;
116   volatile void         *out_buf = 0;
117   
118   volatile loff_t       in_size  = 0;
119   loff_t                out_size = 0;
120   volatile bool         res      = false;
121
122   if (in_len==-1) return false;
123   if (in_len>0 &&
124       (lseek(out_fd, in_len-1, SEEK_SET)==-1 ||
125        write(out_fd, "\0",     1)!=1))          // create sparse file
126     return false;
127   
128   bus_error = 0;
129   if (sigsetjmp(bus_error_restore, 1)==0) {
130     off_t               offset = 0;
131
132     while (offset < in_len) {
133       in_size = in_len - offset;
134       if (in_size > MMAP_BLOCKSIZE) in_size = MMAP_BLOCKSIZE;
135       
136       in_buf   = mmap(0, in_size,  PROT_READ,  MAP_SHARED, in_fd,  offset);
137       if (in_buf==0)  goto out;
138
139       out_size = in_size;
140       out_buf  = mmap(0, out_size, PROT_WRITE, MAP_SHARED, out_fd, offset);
141       if (out_buf==0) goto out;
142
143       offset  += in_size;
144       madvise(const_cast(void *)(in_buf),  in_size, MADV_SEQUENTIAL);
145       madvise((void *)out_buf,            out_size, MADV_SEQUENTIAL);
146
147       TESTSUITE_COPY_CODE;
148       copyMem((void *)out_buf, (void *)in_buf, in_size);
149
150       munmap(const_cast(void *)(in_buf),  in_size); in_buf  = 0;
151       munmap((void*)out_buf,             out_size); out_buf = 0;
152     }
153
154     res = true;
155   }
156
157   out:
158   if (in_buf!=0)  munmap(const_cast(void *)(in_buf),  in_size);
159   if (out_buf!=0) munmap((void *)out_buf,            out_size);
160
161   return res;
162 }
163
164 static inline bool
165 copyReg(char const *src, struct stat const *src_stat,
166         char const *dst)
167 {
168   int           in_fd  = open(src, O_RDONLY|O_NOCTTY|O_NONBLOCK|O_NOFOLLOW|O_LARGEFILE);
169   int           out_fd = in_fd==-1 ? -1 : open(dst, O_RDWR|O_CREAT|O_EXCL, 0200);
170   bool          res    = false;
171   
172   if (in_fd==-1 || out_fd==-1 ||
173       !verifySource(in_fd, src_stat)) goto err;
174
175 #if 0  
176   for (;;) {
177     char        buf[2048];
178     ssize_t     l = read(in_fd, buf, sizeof buf);
179     if (l==-1) goto err;
180     if (l==0)  break;
181     if (!WwriteAll(out_fd, buf, l, 0)) goto err;
182   }
183
184   res = true;
185 #else
186   void          (*old_handler)(int) = signal(SIGBUS, handlerSIGBUS);
187
188   res = copyMMap(in_fd, out_fd);
189
190   signal(SIGBUS, old_handler);
191 #endif
192
193   err:
194   if (out_fd!=-1 && close(out_fd)==-1) res=false;
195   if (in_fd!=-1  && close(in_fd)==-1)  res=false;
196   return res;
197 }
198
199 static inline bool
200 copyNode(char const UNUSED *src, struct stat const *src_stat,
201          char const *dst)
202 {
203   return mknod(dst, src_stat->st_mode & (S_IFMT|S_IWUSR),
204                src_stat->st_rdev)!=-1;
205 }
206
207 static inline bool
208 copyDir(char const UNUSED *src, struct stat const UNUSED *src_stat,
209         char const *dst)
210 {
211   return mkdir(dst, 0700)!=-1;
212 }
213
214 static inline bool
215 setModes(char const *path, struct stat const *st)
216 {
217   return (lchown(path, st->st_uid, st->st_gid)!=-1 &&
218           (S_ISLNK(st->st_mode) || chmod(path, st->st_mode)!=-1));
219 }
220
221
222 bool
223 Unify_copy(char const *src, struct stat const *src_stat,
224            char const *dst)
225 {
226   // skip sockets
227   // TODO: message
228   if (S_ISSOCK(src_stat->st_mode))
229     return true;
230   
231   return
232     (((S_ISLNK (src_stat->st_mode) && copyLnk (src, dst)) ||
233       (S_ISREG (src_stat->st_mode) && copyReg (src, src_stat, dst)) ||
234       (S_ISDIR (src_stat->st_mode) && copyDir (src, src_stat, dst)) ||
235       ((S_ISBLK (src_stat->st_mode) ||
236         S_ISCHR (src_stat->st_mode) || 
237         S_ISFIFO(src_stat->st_mode)) && copyNode(src, src_stat, dst))
238       ) &&
239      setModes(dst, src_stat) &&
240      Unify_setTime(dst, src_stat));
241 }