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 0x10000000
38 #define MMAP_BLOCKSIZE 0x4000
41 verifySource(int fd, struct stat const *exp_stat)
45 return (fstat(fd, &st)!=-1 &&
46 st.st_dev==exp_stat->st_dev &&
47 st.st_ino==exp_stat->st_ino);
51 copyLnk(char const *src, char const *dst)
57 l = readlink(src, buf, len-1);
58 if (l==-1) return false;
65 return (symlink(buf, dst)!=-1);
69 static jmp_buf bus_error_restore;
70 static volatile sig_atomic_t bus_error;
73 handlerSIGBUS(int UNUSED num)
76 longjmp(bus_error_restore, 1);
80 copyMMap(int in_fd, int out_fd)
82 off_t in_len = lseek(in_fd, 0, SEEK_END);
83 void volatile const *in_buf = 0;
84 void volatile *out_buf = 0;
86 volatile loff_t in_size = 0;
87 volatile loff_t out_size = 0;
88 volatile bool res = false;
90 if (in_len==-1) return false;
92 (lseek(out_fd, in_len-1, SEEK_SET)==-1 ||
93 write(out_fd, "\0", 1)!=1)) // create sparse file
97 if (setjmp(bus_error_restore)==0) {
100 while (offset < in_len) {
101 in_size = in_len - offset;
102 if (in_size > MMAP_BLOCKSIZE) in_size = MMAP_BLOCKSIZE;
104 in_buf = mmap(0, in_size, PROT_READ, MAP_SHARED, in_fd, offset);
105 if (in_buf==0) goto out;
108 out_buf = mmap(0, out_size, PROT_WRITE, MAP_SHARED, out_fd, offset);
109 if (out_buf==0) goto out;
112 madvise(const_cast(void *)(in_buf), in_size, MADV_SEQUENTIAL);
113 madvise(out_buf, out_size, MADV_SEQUENTIAL);
115 memcpy(out_buf, in_buf, in_size);
117 munmap(const_cast(void *)(in_buf), in_size); in_buf = 0;
118 munmap(out_buf, out_size); out_buf = 0;
125 if (in_buf!=0) munmap(const_cast(void *)(in_buf), in_size);
126 if (out_buf!=0) munmap(out_buf, out_size);
132 copyReg(char const *src, struct stat const *src_stat,
135 int in_fd = open(src, O_RDONLY|O_NOCTTY|O_NONBLOCK|O_NOFOLLOW|O_LARGEFILE);
136 int out_fd = in_fd==-1 ? -1 : open(dst, O_RDWR|O_CREAT|O_EXCL, 0200);
139 if (in_fd==-1 || out_fd==-1 ||
140 !verifySource(in_fd, src_stat)) goto err;
145 ssize_t l = read(in_fd, buf, sizeof buf);
148 if (!WwriteAll(out_fd, buf, l, 0)) goto err;
153 void (*old_handler)(int) = signal(SIGBUS, handlerSIGBUS);
155 res = copyMMap(in_fd, out_fd);
157 signal(SIGBUS, old_handler);
161 if (out_fd!=-1 && close(out_fd)==-1) res=false;
162 if (in_fd!=-1 && close(in_fd)==-1) res=false;
167 copyNode(char const UNUSED *src, struct stat const *src_stat,
170 return mknod(dst, src_stat->st_mode & (S_IFMT|S_IWUSR),
171 src_stat->st_rdev)!=-1;
175 copyDir(char const UNUSED *src, struct stat const UNUSED *src_stat,
178 return mkdir(dst, 0700)!=-1;
182 setModes(char const *path, struct stat const *st)
184 return (lchown(path, st->st_uid, st->st_gid)!=-1 &&
185 (S_ISLNK(st->st_mode) || chmod(path, st->st_mode)!=-1));
190 Unify_copy(char const *src, struct stat const *src_stat,
195 if (S_ISSOCK(src_stat->st_mode))
199 (((S_ISLNK (src_stat->st_mode) && copyLnk (src, dst)) ||
200 (S_ISREG (src_stat->st_mode) && copyReg (src, src_stat, dst)) ||
201 (S_ISDIR (src_stat->st_mode) && copyDir (src, src_stat, dst)) ||
202 ((S_ISBLK (src_stat->st_mode) ||
203 S_ISCHR (src_stat->st_mode) ||
204 S_ISFIFO(src_stat->st_mode)) && copyNode(src, src_stat, dst))
206 setModes(dst, src_stat) &&
207 Unify_setTime(dst, src_stat));