]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/findlib/attribs.c
Mult Vols + fix restore hard links + file permissions
[bacula/bacula] / bacula / src / findlib / attribs.c
1 /*
2  *  Encode and decode standard Unix attributes and
3  *   Extended attributes for Win32 and
4  *   other non-Unix systems, or Unix systems with ACLs, ...
5  *
6  *    Kern Sibbald, October MMII
7  *
8  *   Version $Id$
9  *
10  */
11 /*
12    Copyright (C) 2000-2003 Kern Sibbald and John Walker
13
14    This program is free software; you can redistribute it and/or
15    modify it under the terms of the GNU General Public License as
16    published by the Free Software Foundation; either version 2 of
17    the License, or (at your option) any later version.
18
19    This program is distributed in the hope that it will be useful,
20    but WITHOUT ANY WARRANTY; without even the implied warranty of
21    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22    General Public License for more details.
23
24    You should have received a copy of the GNU General Public
25    License along with this program; if not, write to the Free
26    Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
27    MA 02111-1307, USA.
28
29  */
30
31 #include "bacula.h"
32 #include "find.h"
33 #include "jcr.h"
34
35 #ifdef HAVE_CYGWIN
36 #include <windows.h>
37
38 /* Forward referenced subroutines */
39 static
40 int set_win32_attributes(void *jcr, char *fname, char *ofile, char *lname, 
41                          int type, int stream, struct stat *statp,
42                          char *attribsEx, int *ofd);
43 void unix_name_to_win32(POOLMEM **win32_name, char *name);
44 extern "C" HANDLE get_osfhandle(int fd);
45 void win_error(void *jcr, char *prefix, POOLMEM *ofile);
46 #endif
47
48
49 /*=============================================================*/
50 /*                                                             */
51 /*             ***  A l l  S y s t e m s ***                   */
52 /*                                                             */
53 /*=============================================================*/
54
55
56 /* Encode a stat structure into a base64 character string */
57 void encode_stat(char *buf, struct stat *statp, uint32_t LinkFI)
58 {
59    char *p = buf;
60    /*
61     * NOTE: we should use rdev as major and minor device if
62     * it is a block or char device (S_ISCHR(statp->st_mode)
63     * or S_ISBLK(statp->st_mode)).  In all other cases,
64     * it is not used.   
65     *
66     */
67    p += to_base64((int64_t)statp->st_dev, p);
68    *p++ = ' ';                        /* separate fields with a space */
69    p += to_base64((int64_t)statp->st_ino, p);
70    *p++ = ' ';
71    p += to_base64((int64_t)statp->st_mode, p);
72    *p++ = ' ';
73    p += to_base64((int64_t)statp->st_nlink, p);
74    *p++ = ' ';
75    p += to_base64((int64_t)statp->st_uid, p);
76    *p++ = ' ';
77    p += to_base64((int64_t)statp->st_gid, p);
78    *p++ = ' ';
79    p += to_base64((int64_t)statp->st_rdev, p);
80    *p++ = ' ';
81    p += to_base64((int64_t)statp->st_size, p);
82    *p++ = ' ';
83    p += to_base64((int64_t)statp->st_blksize, p);
84    *p++ = ' ';
85    p += to_base64((int64_t)statp->st_blocks, p);
86    *p++ = ' ';
87    p += to_base64((int64_t)statp->st_atime, p);
88    *p++ = ' ';
89    p += to_base64((int64_t)statp->st_mtime, p);
90    *p++ = ' ';
91    p += to_base64((int64_t)statp->st_ctime, p);
92    *p++ = ' ';
93    p += to_base64((int64_t)LinkFI, p);
94    *p = 0;
95    return;
96 }
97
98
99
100 /* Decode a stat packet from base64 characters */
101 void
102 decode_stat(char *buf, struct stat *statp, uint32_t *LinkFI) 
103 {
104    char *p = buf;
105    int64_t val;
106
107    p += from_base64(&val, p);
108    statp->st_dev = val;
109    p++;                               /* skip space */
110    p += from_base64(&val, p);
111    statp->st_ino = val;
112    p++;
113    p += from_base64(&val, p);
114    statp->st_mode = val;
115    p++;
116    p += from_base64(&val, p);
117    statp->st_nlink = val;
118    p++;
119    p += from_base64(&val, p);
120    statp->st_uid = val;
121    p++;
122    p += from_base64(&val, p);
123    statp->st_gid = val;
124    p++;
125    p += from_base64(&val, p);
126    statp->st_rdev = val;
127    p++;
128    p += from_base64(&val, p);
129    statp->st_size = val;
130    p++;
131    p += from_base64(&val, p);
132    statp->st_blksize = val;
133    p++;
134    p += from_base64(&val, p);
135    statp->st_blocks = val;
136    p++;
137    p += from_base64(&val, p);
138    statp->st_atime = val;
139    p++;
140    p += from_base64(&val, p);
141    statp->st_mtime = val;
142    p++;
143    p += from_base64(&val, p);
144    statp->st_ctime = val;
145    /* Optional FileIndex of hard linked file data */
146    if (*p == ' ' || (*p != 0 && *(p+1) == ' ')) {
147       p++;
148       p += from_base64(&val, p);
149       *LinkFI = (uint32_t)val;
150   } else {
151       *LinkFI = 0;
152   }
153 }
154
155 /*
156  * Set file modes, permissions and times
157  *
158  *  fname is the original filename  
159  *  ofile is the output filename (may be in a different directory)
160  *
161  * Returns:  1 on success
162  *           0 on failure
163  */
164 int set_attributes(void *jcr, char *fname, char *ofile, char *lname, 
165                    int type, int stream, struct stat *statp,
166                    char *attribsEx, int *ofd)
167 {
168    struct utimbuf ut;    
169
170 #ifdef HAVE_CYGWIN
171    if (set_win32_attributes(jcr, fname, ofile, lname, type, stream,
172                             statp, attribsEx, ofd)) {
173       return 1;
174    }
175    /*
176     * If Windows stuff failed, e.g. attempt to restore Unix file
177     *  to Windows, simply fall through and we will do it the     
178     *  universal way.
179     */
180 #endif
181
182    if (*ofd != -1) {
183       close(*ofd);                    /* first close file */
184       *ofd = -1;
185    }
186
187    ut.actime = statp->st_atime;
188    ut.modtime = statp->st_mtime;
189
190    /* ***FIXME**** optimize -- don't do if already correct */
191    if (type == FT_LNK) {
192       if (lchown(ofile, statp->st_uid, statp->st_gid) < 0) {
193          Jmsg2(jcr, M_ERROR, 0, "Unable to set file owner %s: ERR=%s\n",
194             ofile, strerror(errno));
195          return 0;
196       }
197    } else {
198       if (chown(ofile, statp->st_uid, statp->st_gid) < 0) {
199          Jmsg2(jcr, M_ERROR, 0, "Unable to set file owner %s: ERR=%s\n",
200             ofile, strerror(errno));
201          return 0;
202       }
203    }
204    if (chmod(ofile, statp->st_mode) < 0) {
205       Jmsg2(jcr, M_ERROR, 0, "Unable to set file modes %s: ERR=%s\n",
206          ofile, strerror(errno));
207       return 0;
208    }
209
210    /*
211     * Update file times.
212     */
213    if (utime(ofile, &ut) < 0) {
214       Jmsg2(jcr, M_ERROR, 0, "Unable to set file times %s: ERR=%s\n",
215          ofile, strerror(errno));
216       return 0;
217    }
218    return 1;
219 }
220
221
222 /*=============================================================*/
223 /*                                                             */
224 /*                 * * *  U n i x * * * *                      */
225 /*                                                             */
226 /*=============================================================*/
227
228 #ifndef HAVE_CYGWIN
229     
230 /*
231  * If you have a Unix system with extended attributes (e.g.
232  *  ACLs for Solaris, do it here.
233  */
234 int encode_attribsEx(void *jcr, char *attribsEx, FF_PKT *ff_pkt)
235 {
236    *attribsEx = 0;                    /* no extended attributes */
237    return STREAM_UNIX_ATTRIBUTES;
238 }
239
240 #endif
241
242
243
244 /*=============================================================*/
245 /*                                                             */
246 /*                 * * *  W i n 3 2 * * * *                    */
247 /*                                                             */
248 /*=============================================================*/
249
250 #ifdef HAVE_CYGWIN
251
252 int encode_attribsEx(void *jcr, char *attribsEx, FF_PKT *ff_pkt)
253 {
254    char *p = attribsEx;
255    WIN32_FILE_ATTRIBUTE_DATA atts;
256    ULARGE_INTEGER li;
257
258    attribsEx[0] = 0;                  /* no extended attributes */
259
260    unix_name_to_win32(&ff_pkt->sys_fname, ff_pkt->fname);
261    if (!GetFileAttributesEx(ff_pkt->sys_fname, GetFileExInfoStandard,
262                             (LPVOID)&atts)) {
263       win_error(jcr, "GetFileAttributesEx:", ff_pkt->sys_fname);
264       return STREAM_WIN32_ATTRIBUTES;
265    }
266
267    p += to_base64((uint64_t)atts.dwFileAttributes, p);
268    *p++ = ' ';                        /* separate fields with a space */
269    li.LowPart = atts.ftCreationTime.dwLowDateTime;
270    li.HighPart = atts.ftCreationTime.dwHighDateTime;
271    p += to_base64((uint64_t)li.QuadPart, p);
272    *p++ = ' ';
273    li.LowPart = atts.ftLastAccessTime.dwLowDateTime;
274    li.HighPart = atts.ftLastAccessTime.dwHighDateTime;
275    p += to_base64((uint64_t)li.QuadPart, p);
276    *p++ = ' ';
277    li.LowPart = atts.ftLastWriteTime.dwLowDateTime;
278    li.HighPart = atts.ftLastWriteTime.dwHighDateTime;
279    p += to_base64((uint64_t)li.QuadPart, p);
280    *p++ = ' ';
281    p += to_base64((uint64_t)atts.nFileSizeHigh, p);
282    *p++ = ' ';
283    p += to_base64((uint64_t)atts.nFileSizeLow, p);
284    *p = 0;
285    return STREAM_WIN32_ATTRIBUTES;
286 }
287
288 /* Define attributes that are legal to set with SetFileAttributes() */
289 #define SET_ATTRS ( \
290          FILE_ATTRIBUTE_ARCHIVE| \
291          FILE_ATTRIBUTE_HIDDEN| \
292          FILE_ATTRIBUTE_NORMAL| \
293          FILE_ATTRIBUTE_NOT_CONTENT_INDEXED| \
294          FILE_ATTRIBUTE_OFFLINE| \
295          FILE_ATTRIBUTE_READONLY| \
296          FILE_ATTRIBUTE_SYSTEM| \
297          FILE_ATTRIBUTE_TEMPORARY)
298
299
300 /*
301  * Set Extended File Attributes for Win32
302  *
303  *  fname is the original filename  
304  *  ofile is the output filename (may be in a different directory)
305  *
306  * Returns:  1 on success
307  *           0 on failure
308  */
309 static
310 int set_win32_attributes(void *jcr, char *fname, char *ofile, char *lname, 
311                          int type, int stream, struct stat *statp,
312                          char *attribsEx, int *ofd)
313 {
314    char *p = attribsEx;
315    int64_t val;
316    WIN32_FILE_ATTRIBUTE_DATA atts;
317    ULARGE_INTEGER li;
318    int fid, stat;
319    POOLMEM *win32_ofile;
320
321    if (!p || !*p) {                   /* we should have attributes */
322       Dmsg2(100, "Attributes missing. of=%s ofd=%d\n", ofile, *ofd);
323       if (*ofd != -1) {
324          close(*ofd);
325          *ofd = -1;
326       }
327       return 0;
328    } else {
329       Dmsg2(100, "Attribs %s = %s\n", ofile, attribsEx);
330    }
331
332    p += from_base64(&val, p);
333    atts.dwFileAttributes = val;
334    p++;                               /* skip space */
335    p += from_base64(&val, p);
336    li.QuadPart = val;
337    atts.ftCreationTime.dwLowDateTime = li.LowPart;
338    atts.ftCreationTime.dwHighDateTime = li.HighPart;
339    p++;                               /* skip space */
340    p += from_base64(&val, p);
341    li.QuadPart = val;
342    atts.ftLastAccessTime.dwLowDateTime = li.LowPart;
343    atts.ftLastAccessTime.dwHighDateTime = li.HighPart;
344    p++;                               /* skip space */
345    p += from_base64(&val, p);
346    li.QuadPart = val;
347    atts.ftLastWriteTime.dwLowDateTime = li.LowPart;
348    atts.ftLastWriteTime.dwHighDateTime = li.HighPart;
349    p++;   
350    p += from_base64(&val, p);
351    atts.nFileSizeHigh = val;
352    p++;
353    p += from_base64(&val, p);
354    atts.nFileSizeLow = val;
355
356    /* At this point, we have reconstructed the WIN32_FILE_ATTRIBUTE_DATA pkt */
357
358    /* Convert to Windows path format */
359    win32_ofile = get_pool_memory(PM_FNAME);
360    unix_name_to_win32(&win32_ofile, ofile);
361
362    if (*ofd == -1) {
363       Dmsg1(100, "File not open: %s\n", ofile);
364       fid = open(ofile, O_RDWR|O_BINARY);   /* attempt to open the file */
365       if (fid >= 0) {
366          *ofd = fid;
367       }
368    }
369
370    if (*ofd != -1) {
371       Dmsg1(100, "SetFileTime %s\n", ofile);
372       stat = SetFileTime(get_osfhandle(*ofd),
373                          &atts.ftCreationTime,
374                          &atts.ftLastAccessTime,
375                          &atts.ftLastWriteTime);
376       if (stat != 1) {
377          win_error(jcr, "SetFileTime:", win32_ofile);
378       }
379       close(*ofd);
380       *ofd = -1;
381    }
382
383    Dmsg1(100, "SetFileAtts %s\n", ofile);
384    stat = SetFileAttributes(win32_ofile, atts.dwFileAttributes & SET_ATTRS);
385    if (stat != 1) {
386       win_error(jcr, "SetFileAttributes:", win32_ofile);
387    }
388    free_pool_memory(win32_ofile);
389    return 1;
390 }
391
392 void win_error(void *vjcr, char *prefix, POOLMEM *win32_ofile)
393 {
394    JCR *jcr = (JCR *)vjcr; 
395    DWORD lerror = GetLastError();
396    LPTSTR msg;
397    FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|
398                  FORMAT_MESSAGE_FROM_SYSTEM,
399                  NULL,
400                  lerror,
401                  0,
402                  (LPTSTR)&msg,
403                  0,
404                  NULL);
405    Dmsg3(100, "Error in %s on file %s: ERR=%s\n", prefix, win32_ofile, msg);
406    Jmsg3(jcr, M_INFO, 0, _("Error in %s file %s: ERR=%s\n"), prefix, win32_ofile, msg);
407    LocalFree(msg);
408 }
409
410 /* Cygwin API definition */
411 extern "C" void cygwin_conv_to_win32_path(const char *path, char *win32_path);
412
413 void unix_name_to_win32(POOLMEM **win32_name, char *name)
414 {
415    /* One extra byte should suffice, but we double it */
416    *win32_name = check_pool_memory_size(*win32_name, 2*strlen(name)+1);
417    cygwin_conv_to_win32_path(name, *win32_name);
418 }
419
420 #endif  /* HAVE_CYGWIN */