cleanups
[util-vserver.git] / util-vserver / src / vunify-doit.hc
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 #include "wrappers-io.h"
19
20 #include <sys/types.h>
21 #include <utime.h>
22
23 static bool
24 doitUnify(char const *src, struct stat const *src_stat,
25           char const *dst, struct stat const UNUSED *dst_stat)
26 {
27   size_t        l = strlen(dst);
28   char          tmpfile[l + sizeof(";XXXXXX")];
29   int           fd;
30   bool          res = false;
31
32   // at first, set the ILI flags on 'src'
33   if (vc_set_iattr_compat(src, src_stat->st_dev, src_stat->st_ino,
34                           0, VC_IATTR_IUNLINK, VC_IATTR_IUNLINK,
35                           &src_stat->st_mode)==-1)
36     return false;
37
38   // now, create a temporary filename
39   memcpy(tmpfile,   dst, l);
40   memcpy(tmpfile+l, ";XXXXXX", 8);
41   fd = mkstemp(tmpfile);
42   close(fd);
43
44   if (fd==-1) {
45     perror("mkstemp()");
46     return false;
47   }
48
49   // and rename the old file to this name
50   if (rename(dst, tmpfile)==-1) {
51     perror("rename()");
52     goto err;
53   }
54
55   // now, link the src-file to dst
56   if (link(src, dst)==-1) {
57     perror("link()");
58
59     unlink(dst);
60     if (rename(tmpfile, dst)==-1) {
61       perror("FATAL error in rename()");
62       _exit(1);
63     }
64     goto err;
65   }
66
67   res = true;
68
69   err:
70   unlink(tmpfile);
71
72   return res;
73 }
74
75 static bool
76 doitDeUnify(char const UNUSED *src, struct stat const UNUSED *src_stat,
77             char const *dst,        struct stat const UNUSED *dst_stat)
78 {
79   size_t                l = strlen(dst);
80   char                  tmpfile[l + sizeof(";XXXXXX")];
81   int                   fd_src, fd_tmp;
82   struct stat           st;
83   struct utimbuf        utm;
84
85   fd_src = open(dst, O_RDONLY);
86   if (fd_src==-1) {
87     perror("open()");
88     return false;
89   }
90
91   if (fstat(fd_src, &st)==-1) {
92     perror("fstat()");
93     close(fd_src);
94     return false;
95   }
96   
97   memcpy(tmpfile,   dst, l);
98   memcpy(tmpfile+l, ";XXXXXX", 8);
99   fd_tmp = mkstemp(tmpfile);
100
101   if (fd_tmp==-1) {
102     perror("mkstemp()");
103     tmpfile[0] = '\0';
104     goto err;
105   }
106
107   if (fchown(fd_tmp, st.st_uid, st.st_gid)==-1 ||
108       fchmod(fd_tmp, st.st_mode)==-1) {
109     perror("fchown()/fchmod()");
110     goto err;
111   }
112
113   // todo: acl?
114
115   for (;;) {
116     char        buf[0x4000];
117     ssize_t     len = read(fd_src, buf, sizeof buf);
118     if (len==-1) {
119       perror("read()");
120       goto err;
121     }
122     if (len==0) break;
123
124     if (!WwriteAll(fd_tmp, buf, len)) goto err;
125   }
126
127   if (close(fd_src)==-1) {
128     perror("close()");
129     goto err;
130   }
131   if (close(fd_tmp)==-1) {
132     perror("close()");
133     goto err;
134   }
135   
136   utm.actime  = st.st_atime;
137   utm.modtime = st.st_mtime;
138
139   // ALERT: race !!!
140   if (utime(tmpfile, &utm)==-1) {
141     perror("utime()");
142     goto err1;
143   }
144
145   if (unlink(dst)==-1) {
146     perror("unlink()");
147     goto err1;
148   }
149   
150   // ALERT: race !!!
151   if (rename(tmpfile, dst)==-1) {
152     perror("FATAL error in rename()");
153     _exit(1);
154   }
155
156   return true;
157   
158   err:
159   close(fd_src);
160   close(fd_tmp);
161   err1:
162   if (tmpfile[0]) unlink(tmpfile);
163
164   return false;
165 }