3 // Copyright (C) 2004 Enrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
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.
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.
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.
34 #define ENSC_WRAPPERS_IO 1
37 #define MMAP_BLOCKSIZE (16 * 1024*1024)
40 verifySource(int fd, struct stat const *exp_stat)
44 return (fstat(fd, &st)!=-1 &&
45 st.st_dev==exp_stat->st_dev &&
46 st.st_ino==exp_stat->st_ino);
50 copyLnk(char const *src, char const *dst)
56 l = readlink(src, buf, len-1);
57 if (l==-1) return false;
64 return (symlink(buf, dst)!=-1);
68 static jmp_buf bus_error_restore;
69 static volatile sig_atomic_t bus_error;
72 handlerSIGBUS(int UNUSED num)
75 longjmp(bus_error_restore, 1);
79 copyMem(void *dst_v, void const *src_v, size_t len_v)
83 int const *src = src_v;
84 size_t len = len_v / sizeof(int);
85 size_t rest = len_v - sizeof(int)*len;
89 if (*src != 0) *dst = *src;
94 char *dst_c = (void *)(dst);
95 char const *src_c = (void const *)(src);
97 for (i=0; i<rest; ++i) {
98 if (*src_c != 0) *dst_c = *src_c;
103 memcpy(dst_v, src_v, len_v);
108 copyMMap(int in_fd, int out_fd)
110 off_t in_len = lseek(in_fd, 0, SEEK_END);
111 void const *in_buf = 0;
118 if (in_len==-1) return false;
120 (lseek(out_fd, in_len-1, SEEK_SET)==-1 ||
121 write(out_fd, "\0", 1)!=1)) // create sparse file
125 if (setjmp(bus_error_restore)==0) {
128 while (offset < in_len) {
129 in_size = in_len - offset;
130 if (in_size > MMAP_BLOCKSIZE) in_size = MMAP_BLOCKSIZE;
132 in_buf = mmap(0, in_size, PROT_READ, MAP_SHARED, in_fd, offset);
133 if (in_buf==0) goto out;
136 out_buf = mmap(0, out_size, PROT_WRITE, MAP_SHARED, out_fd, offset);
137 if (out_buf==0) goto out;
140 madvise(const_cast(void *)(in_buf), in_size, MADV_SEQUENTIAL);
141 madvise(out_buf, out_size, MADV_SEQUENTIAL);
143 copyMem(out_buf, in_buf, in_size);
145 munmap(const_cast(void *)(in_buf), in_size); in_buf = 0;
146 munmap(out_buf, out_size); out_buf = 0;
153 if (in_buf!=0) munmap(const_cast(void *)(in_buf), in_size);
154 if (out_buf!=0) munmap(out_buf, out_size);
160 copyReg(char const *src, struct stat const *src_stat,
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);
167 if (in_fd==-1 || out_fd==-1 ||
168 !verifySource(in_fd, src_stat)) goto err;
173 ssize_t l = read(in_fd, buf, sizeof buf);
176 if (!WwriteAll(out_fd, buf, l, 0)) goto err;
181 void (*old_handler)(int) = signal(SIGBUS, handlerSIGBUS);
183 res = copyMMap(in_fd, out_fd);
185 signal(SIGBUS, old_handler);
189 if (out_fd!=-1 && close(out_fd)==-1) res=false;
190 if (in_fd!=-1 && close(in_fd)==-1) res=false;
195 copyNode(char const UNUSED *src, struct stat const *src_stat,
198 return mknod(dst, src_stat->st_mode & (S_IFMT|S_IWUSR),
199 src_stat->st_rdev)!=-1;
203 copyDir(char const UNUSED *src, struct stat const UNUSED *src_stat,
206 return mkdir(dst, 0700)!=-1;
210 setModes(char const *path, struct stat const *st)
212 return (lchown(path, st->st_uid, st->st_gid)!=-1 &&
213 (S_ISLNK(st->st_mode) || chmod(path, st->st_mode)!=-1));
218 Unify_copy(char const *src, struct stat const *src_stat,
223 if (S_ISSOCK(src_stat->st_mode))
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))
234 setModes(dst, src_stat) &&
235 Unify_setTime(dst, src_stat));