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