06c8aef60440017fb9bbe0b18f1cfda97fe2c210
[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 static inline bool
40 verifySource(int fd, struct stat const *exp_stat)
41 {
42   struct stat           st;
43   
44   return (fstat(fd, &st)!=-1 &&
45           st.st_dev==exp_stat->st_dev &&
46           st.st_ino==exp_stat->st_ino);
47 }
48
49 static inline bool
50 copyLnk(char const *src, char const *dst)
51 {
52   ssize_t       len = 1024;
53   for (;;) {
54     char        buf[len];
55     ssize_t     l;
56     l = readlink(src, buf, len-1);
57     if (l==-1) return false;
58     if (l>=len-1) {
59       len *= 2;
60       continue;
61     }
62     buf[l] = '\0';
63
64     return (symlink(buf, dst)!=-1);
65   }
66 }
67
68 static jmp_buf                  bus_error_restore;
69 static volatile sig_atomic_t    bus_error;
70
71 static void
72 handlerSIGBUS(int UNUSED num)
73 {
74   bus_error = 1;
75   longjmp(bus_error_restore, 1);
76 }
77
78 static void
79 copyMem(void *dst_v, void const *src_v, size_t len_v)
80 {
81 #if 1
82   int           *dst = dst_v;
83   int const     *src = src_v;
84   size_t        len  = len_v / sizeof(int);
85   size_t        rest = len_v - sizeof(int)*len;
86   size_t        i=0;
87
88   for (; i<len; ++i) {
89     if (*src != 0) *dst = *src;
90     ++dst;
91     ++src;
92   }
93
94   char          *dst_c = (void *)(dst);
95   char const    *src_c = (void const *)(src);
96
97   for (i=0; i<rest; ++i) {
98     if (*src_c != 0) *dst_c = *src_c;
99     ++dst_c;
100     ++src_c;
101   }
102 #else
103   memcpy(dst_v, src_v, len_v);
104 #endif  
105 }
106
107 static UNUSED bool
108 copyMMap(int in_fd, int out_fd)
109 {
110   off_t                 in_len   = lseek(in_fd, 0, SEEK_END);
111   void const            *in_buf  = 0;
112   void                  *out_buf = 0;
113   
114   loff_t                in_size  = 0;
115   loff_t                out_size = 0;
116   bool                  res      = false;
117
118   if (in_len==-1) return false;
119   if (in_len>0 &&
120       (lseek(out_fd, in_len-1, SEEK_SET)==-1 ||
121        write(out_fd, "\0",     1)!=1))          // create sparse file
122     return false;
123   
124   bus_error = 0;
125   if (setjmp(bus_error_restore)==0) {
126     off_t               offset = 0;
127
128     while (offset < in_len) {
129       in_size = in_len - offset;
130       if (in_size > MMAP_BLOCKSIZE) in_size = MMAP_BLOCKSIZE;
131       
132       in_buf   = mmap(0, in_size,  PROT_READ,  MAP_SHARED, in_fd,  offset);
133       if (in_buf==0)  goto out;
134
135       out_size = in_size;
136       out_buf  = mmap(0, out_size, PROT_WRITE, MAP_SHARED,  out_fd, offset);
137       if (out_buf==0) goto out;
138
139       offset  += in_size;
140       madvise(const_cast(void *)(in_buf),  in_size,  MADV_SEQUENTIAL);
141       madvise(out_buf,                     out_size, MADV_SEQUENTIAL);
142
143       copyMem(out_buf, in_buf, in_size);
144
145       munmap(const_cast(void *)(in_buf),  in_size);  in_buf  = 0;
146       munmap(out_buf,                     out_size); out_buf = 0;
147     }
148
149     res = true;
150   }
151
152   out:
153   if (in_buf!=0)  munmap(const_cast(void *)(in_buf),  in_size);
154   if (out_buf!=0) munmap(out_buf,                     out_size);
155
156   return res;
157 }
158
159 static inline bool
160 copyReg(char const *src, struct stat const *src_stat,
161         char const *dst)
162 {
163   int           in_fd  = open(src, O_RDONLY|O_NOCTTY|O_NONBLOCK|O_NOFOLLOW|O_LARGEFILE);
164   int           out_fd = in_fd==-1 ? -1 : open(dst, O_RDWR|O_CREAT|O_EXCL, 0200);
165   bool          res    = false;
166   
167   if (in_fd==-1 || out_fd==-1 ||
168       !verifySource(in_fd, src_stat)) goto err;
169
170 #if 0  
171   for (;;) {
172     char        buf[2048];
173     ssize_t     l = read(in_fd, buf, sizeof buf);
174     if (l==-1) goto err;
175     if (l==0)  break;
176     if (!WwriteAll(out_fd, buf, l, 0)) goto err;
177   }
178
179   res = true;
180 #else
181   void          (*old_handler)(int) = signal(SIGBUS, handlerSIGBUS);
182
183   res = copyMMap(in_fd, out_fd);
184
185   signal(SIGBUS, old_handler);
186 #endif
187
188   err:
189   if (out_fd!=-1 && close(out_fd)==-1) res=false;
190   if (in_fd!=-1  && close(in_fd)==-1)  res=false;
191   return res;
192 }
193
194 static inline bool
195 copyNode(char const UNUSED *src, struct stat const *src_stat,
196          char const *dst)
197 {
198   return mknod(dst, src_stat->st_mode & (S_IFMT|S_IWUSR),
199                src_stat->st_rdev)!=-1;
200 }
201
202 static inline bool
203 copyDir(char const UNUSED *src, struct stat const UNUSED *src_stat,
204         char const *dst)
205 {
206   return mkdir(dst, 0700)!=-1;
207 }
208
209 static inline bool
210 setModes(char const *path, struct stat const *st)
211 {
212   return (lchown(path, st->st_uid, st->st_gid)!=-1 &&
213           (S_ISLNK(st->st_mode) || chmod(path, st->st_mode)!=-1));
214 }
215
216
217 bool
218 Unify_copy(char const *src, struct stat const *src_stat,
219            char const *dst)
220 {
221   // skip sockets
222   // TODO: message
223   if (S_ISSOCK(src_stat->st_mode))
224     return true;
225   
226   return
227     (((S_ISLNK (src_stat->st_mode) && copyLnk (src, dst)) ||
228       (S_ISREG (src_stat->st_mode) && copyReg (src, src_stat, dst)) ||
229       (S_ISDIR (src_stat->st_mode) && copyDir (src, src_stat, dst)) ||
230       ((S_ISBLK (src_stat->st_mode) ||
231         S_ISCHR (src_stat->st_mode) || 
232         S_ISFIFO(src_stat->st_mode)) && copyNode(src, src_stat, dst))
233       ) &&
234      setModes(dst, src_stat) &&
235      Unify_setTime(dst, src_stat));
236 }