1 // $Id: unify-deunify.c,v 1.2 2004/02/18 04:48:24 ensc Exp $ --*- c -*--
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.
33 #define ENSC_WRAPPERS_IO 1
37 Unify_deUnify(char const *dst)
39 size_t l = strlen(dst);
40 char tmpfile[l + sizeof(";XXXXXX")];
45 fd_src = open(dst, O_RDONLY);
51 if (fstat(fd_src, &st)==-1) {
57 memcpy(tmpfile, dst, l);
58 memcpy(tmpfile+l, ";XXXXXX", 8);
59 fd_tmp = mkstemp(tmpfile);
67 if (fchown(fd_tmp, st.st_uid, st.st_gid)==-1 ||
68 fchmod(fd_tmp, st.st_mode)==-1) {
69 perror("fchown()/fchmod()");
77 ssize_t len = read(fd_src, buf, sizeof buf);
84 if (!WwriteAll(fd_tmp, buf, len, 0)) goto err;
87 if (close(fd_src)==-1) {
91 if (close(fd_tmp)==-1) {
96 utm.actime = st.st_atime;
97 utm.modtime = st.st_mtime;
100 if (utime(tmpfile, &utm)==-1) {
105 if (unlink(dst)==-1) {
111 if (rename(tmpfile, dst)==-1) {
112 perror("FATAL error in rename()");
122 if (tmpfile[0]) unlink(tmpfile);