]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/findlib/attribs.c
Major part of Win32 BackupRead/Write done
[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
34 #ifdef HAVE_CYGWIN
35
36 /* Forward referenced subroutines */
37 static
38 int set_win32_attributes(void *jcr, char *fname, char *ofile, char *lname, 
39                          int type, int stream, struct stat *statp,
40                          char *attribsEx, BFILE *ofd);
41 void unix_name_to_win32(POOLMEM **win32_name, char *name);
42 void win_error(void *jcr, char *prefix, POOLMEM *ofile);
43 HANDLE bget_handle(BFILE *bfd);
44 #endif
45
46 /* For old systems that don't have lchown() use chown() */
47 #ifndef HAVE_LCHOWN
48 #define lchown chown
49 #endif
50
51 /*=============================================================*/
52 /*                                                             */
53 /*             ***  A l l  S y s t e m s ***                   */
54 /*                                                             */
55 /*=============================================================*/
56
57
58 /* Encode a stat structure into a base64 character string */
59 void encode_stat(char *buf, struct stat *statp, uint32_t LinkFI)
60 {
61    char *p = buf;
62    /*
63     * NOTE: we should use rdev as major and minor device if
64     * it is a block or char device (S_ISCHR(statp->st_mode)
65     * or S_ISBLK(statp->st_mode)).  In all other cases,
66     * it is not used.   
67     *
68     */
69    p += to_base64((int64_t)statp->st_dev, p);
70    *p++ = ' ';                        /* separate fields with a space */
71    p += to_base64((int64_t)statp->st_ino, p);
72    *p++ = ' ';
73    p += to_base64((int64_t)statp->st_mode, p);
74    *p++ = ' ';
75    p += to_base64((int64_t)statp->st_nlink, p);
76    *p++ = ' ';
77    p += to_base64((int64_t)statp->st_uid, p);
78    *p++ = ' ';
79    p += to_base64((int64_t)statp->st_gid, p);
80    *p++ = ' ';
81    p += to_base64((int64_t)statp->st_rdev, p);
82    *p++ = ' ';
83    p += to_base64((int64_t)statp->st_size, p);
84    *p++ = ' ';
85    p += to_base64((int64_t)statp->st_blksize, p);
86    *p++ = ' ';
87    p += to_base64((int64_t)statp->st_blocks, p);
88    *p++ = ' ';
89    p += to_base64((int64_t)statp->st_atime, p);
90    *p++ = ' ';
91    p += to_base64((int64_t)statp->st_mtime, p);
92    *p++ = ' ';
93    p += to_base64((int64_t)statp->st_ctime, p);
94    *p++ = ' ';
95    p += to_base64((int64_t)LinkFI, p);
96
97 /* FreeBSD function */
98 #ifdef HAVE_CHFLAGS
99    *p++ = ' ';
100    p += to_base64((int64_t)statp->st_flags, p);
101 #endif
102    *p = 0;
103    return;
104 }
105
106
107
108 /* Decode a stat packet from base64 characters */
109 void
110 decode_stat(char *buf, struct stat *statp, uint32_t *LinkFI) 
111 {
112    char *p = buf;
113    int64_t val;
114
115    p += from_base64(&val, p);
116    statp->st_dev = val;
117    p++;                               /* skip space */
118    p += from_base64(&val, p);
119    statp->st_ino = val;
120    p++;
121    p += from_base64(&val, p);
122    statp->st_mode = val;
123    p++;
124    p += from_base64(&val, p);
125    statp->st_nlink = val;
126    p++;
127    p += from_base64(&val, p);
128    statp->st_uid = val;
129    p++;
130    p += from_base64(&val, p);
131    statp->st_gid = val;
132    p++;
133    p += from_base64(&val, p);
134    statp->st_rdev = val;
135    p++;
136    p += from_base64(&val, p);
137    statp->st_size = val;
138    p++;
139    p += from_base64(&val, p);
140    statp->st_blksize = val;
141    p++;
142    p += from_base64(&val, p);
143    statp->st_blocks = val;
144    p++;
145    p += from_base64(&val, p);
146    statp->st_atime = val;
147    p++;
148    p += from_base64(&val, p);
149    statp->st_mtime = val;
150    p++;
151    p += from_base64(&val, p);
152    statp->st_ctime = val;
153    /* Optional FileIndex of hard linked file data */
154    if (*p == ' ' || (*p != 0 && *(p+1) == ' ')) {
155       p++;
156       p += from_base64(&val, p);
157       *LinkFI = (uint32_t)val;
158   } else {
159       *LinkFI = 0;
160   }
161
162 /* FreeBSD user flags */
163 #ifdef HAVE_CHFLAGS
164    if (*p == ' ' || (*p != 0 && *(p+1) == ' ')) {
165       p++;
166       p += from_base64(&val, p);
167       statp->st_flags = (uint32_t)val;
168   } else {
169       statp->st_flags  = 0;
170   }
171 #endif
172 }
173
174 /*
175  * Set file modes, permissions and times
176  *
177  *  fname is the original filename  
178  *  ofile is the output filename (may be in a different directory)
179  *
180  * Returns:  1 on success
181  *           0 on failure
182  */
183 int set_attributes(void *jcr, char *fname, char *ofile, char *lname, 
184                    int type, int stream, struct stat *statp,
185                    char *attribsEx, BFILE *ofd)
186 {
187    struct utimbuf ut;    
188    mode_t old_mask;
189    int stat = 1;
190
191 #ifdef HAVE_CYGWIN
192    if (set_win32_attributes(jcr, fname, ofile, lname, type, stream,
193                             statp, attribsEx, ofd)) {
194       return 1;
195    }
196    /*
197     * If Windows stuff failed, e.g. attempt to restore Unix file
198     *  to Windows, simply fall through and we will do it the     
199     *  universal way.
200     */
201 #endif
202
203    old_mask = umask(0);
204    if (is_bopen(ofd)) {
205       bclose(ofd);                    /* first close file */
206    }
207
208    ut.actime = statp->st_atime;
209    ut.modtime = statp->st_mtime;
210
211    /* ***FIXME**** optimize -- don't do if already correct */
212    /* 
213     * For link, change owner of link using lchown, but don't
214     *   try to do a chmod as that will update the file behind it.
215     */
216    if (type == FT_LNK) {
217       /* Change owner of link, not of real file */
218       if (lchown(ofile, statp->st_uid, statp->st_gid) < 0) {
219          Jmsg2(jcr, M_WARNING, 0, _("Unable to set file owner %s: ERR=%s\n"),
220             ofile, strerror(errno));
221          stat = 0;
222       }
223    } else {
224       if (chown(ofile, statp->st_uid, statp->st_gid) < 0) {
225          Jmsg2(jcr, M_WARNING, 0, _("Unable to set file owner %s: ERR=%s\n"),
226             ofile, strerror(errno));
227          stat = 0;
228       }
229       if (chmod(ofile, statp->st_mode) < 0) {
230          Jmsg2(jcr, M_WARNING, 0, _("Unable to set file modes %s: ERR=%s\n"),
231             ofile, strerror(errno));
232          stat = 0;
233       }
234
235       /* FreeBSD user flags */
236 #ifdef HAVE_CHFLAGS
237       if (chflags(ofile, statp->st_flags) < 0) {
238          Jmsg2(jcr, M_WARNING, 0, _("Unable to set file flags %s: ERR=%s\n"),
239             ofile, strerror(errno));
240          stat = 0;
241       }
242 #endif
243       /*
244        * Reset file times.
245        */
246       if (utime(ofile, &ut) < 0) {
247          Jmsg2(jcr, M_ERROR, 0, _("Unable to set file times %s: ERR=%s\n"),
248             ofile, strerror(errno));
249          stat = 0;
250       }
251    }
252    umask(old_mask);
253    return stat;
254 }
255
256
257 /*=============================================================*/
258 /*                                                             */
259 /*                 * * *  U n i x * * * *                      */
260 /*                                                             */
261 /*=============================================================*/
262
263 #ifndef HAVE_CYGWIN
264     
265 /*
266  * If you have a Unix system with extended attributes (e.g.
267  *  ACLs for Solaris, do it here.
268  */
269 int encode_attribsEx(void *jcr, char *attribsEx, FF_PKT *ff_pkt)
270 {
271    *attribsEx = 0;                    /* no extended attributes */
272    return STREAM_UNIX_ATTRIBUTES;
273 }
274
275 #endif
276
277
278
279 /*=============================================================*/
280 /*                                                             */
281 /*                 * * *  W i n 3 2 * * * *                    */
282 /*                                                             */
283 /*=============================================================*/
284
285 #ifdef HAVE_CYGWIN
286
287 int encode_attribsEx(void *jcr, char *attribsEx, FF_PKT *ff_pkt)
288 {
289    char *p = attribsEx;
290    WIN32_FILE_ATTRIBUTE_DATA atts;
291    ULARGE_INTEGER li;
292
293    attribsEx[0] = 0;                  /* no extended attributes */
294
295    if (!p_GetFileAttributesEx) {
296       return STREAM_UNIX_ATTRIBUTES;
297    }
298
299    unix_name_to_win32(&ff_pkt->sys_fname, ff_pkt->fname);
300    if (!p_GetFileAttributesEx(ff_pkt->sys_fname, GetFileExInfoStandard,
301                             (LPVOID)&atts)) {
302       win_error(jcr, "GetFileAttributesEx:", ff_pkt->sys_fname);
303       return STREAM_WIN32_ATTRIBUTES;
304    }
305
306    p += to_base64((uint64_t)atts.dwFileAttributes, p);
307    *p++ = ' ';                        /* separate fields with a space */
308    li.LowPart = atts.ftCreationTime.dwLowDateTime;
309    li.HighPart = atts.ftCreationTime.dwHighDateTime;
310    p += to_base64((uint64_t)li.QuadPart, p);
311    *p++ = ' ';
312    li.LowPart = atts.ftLastAccessTime.dwLowDateTime;
313    li.HighPart = atts.ftLastAccessTime.dwHighDateTime;
314    p += to_base64((uint64_t)li.QuadPart, p);
315    *p++ = ' ';
316    li.LowPart = atts.ftLastWriteTime.dwLowDateTime;
317    li.HighPart = atts.ftLastWriteTime.dwHighDateTime;
318    p += to_base64((uint64_t)li.QuadPart, p);
319    *p++ = ' ';
320    p += to_base64((uint64_t)atts.nFileSizeHigh, p);
321    *p++ = ' ';
322    p += to_base64((uint64_t)atts.nFileSizeLow, p);
323    *p = 0;
324    return STREAM_WIN32_ATTRIBUTES;
325 }
326
327 /* Define attributes that are legal to set with SetFileAttributes() */
328 #define SET_ATTRS ( \
329          FILE_ATTRIBUTE_ARCHIVE| \
330          FILE_ATTRIBUTE_HIDDEN| \
331          FILE_ATTRIBUTE_NORMAL| \
332          FILE_ATTRIBUTE_NOT_CONTENT_INDEXED| \
333          FILE_ATTRIBUTE_OFFLINE| \
334          FILE_ATTRIBUTE_READONLY| \
335          FILE_ATTRIBUTE_SYSTEM| \
336          FILE_ATTRIBUTE_TEMPORARY)
337
338
339 /*
340  * Set Extended File Attributes for Win32
341  *
342  *  fname is the original filename  
343  *  ofile is the output filename (may be in a different directory)
344  *
345  * Returns:  1 on success
346  *           0 on failure
347  */
348 static
349 int set_win32_attributes(void *jcr, char *fname, char *ofile, char *lname, 
350                          int type, int stream, struct stat *statp,
351                          char *attribsEx, BFILE *ofd)
352 {
353    char *p = attribsEx;
354    int64_t val;
355    WIN32_FILE_ATTRIBUTE_DATA atts;
356    ULARGE_INTEGER li;
357    POOLMEM *win32_ofile;
358
359    if (!p || !*p) {                   /* we should have attributes */
360       Dmsg2(100, "Attributes missing. of=%s ofd=%d\n", ofile, ofd->fid);
361       if (is_bopen(ofd)) {
362          bclose(ofd);
363       }
364       return 0;
365    } else {
366       Dmsg2(100, "Attribs %s = %s\n", ofile, attribsEx);
367    }
368
369    p += from_base64(&val, p);
370    atts.dwFileAttributes = val;
371    p++;                               /* skip space */
372    p += from_base64(&val, p);
373    li.QuadPart = val;
374    atts.ftCreationTime.dwLowDateTime = li.LowPart;
375    atts.ftCreationTime.dwHighDateTime = li.HighPart;
376    p++;                               /* skip space */
377    p += from_base64(&val, p);
378    li.QuadPart = val;
379    atts.ftLastAccessTime.dwLowDateTime = li.LowPart;
380    atts.ftLastAccessTime.dwHighDateTime = li.HighPart;
381    p++;                               /* skip space */
382    p += from_base64(&val, p);
383    li.QuadPart = val;
384    atts.ftLastWriteTime.dwLowDateTime = li.LowPart;
385    atts.ftLastWriteTime.dwHighDateTime = li.HighPart;
386    p++;   
387    p += from_base64(&val, p);
388    atts.nFileSizeHigh = val;
389    p++;
390    p += from_base64(&val, p);
391    atts.nFileSizeLow = val;
392
393    /* Convert to Windows path format */
394    win32_ofile = get_pool_memory(PM_FNAME);
395    unix_name_to_win32(&win32_ofile, ofile);
396
397
398
399    /* At this point, we have reconstructed the WIN32_FILE_ATTRIBUTE_DATA pkt */
400
401
402    if (!is_bopen(ofd)) {
403       Dmsg1(100, "File not open: %s\n", ofile);
404       bopen(ofd, ofile, O_WRONLY|O_BINARY, 0);   /* attempt to open the file */
405    }
406
407    if (is_bopen(ofd)) {
408       Dmsg1(100, "SetFileTime %s\n", ofile);
409       if (!SetFileTime(bget_handle(ofd),
410                          &atts.ftCreationTime,
411                          &atts.ftLastAccessTime,
412                          &atts.ftLastWriteTime)) {
413          win_error(jcr, "SetFileTime:", win32_ofile);
414       }
415       bclose(ofd);
416    }
417
418    Dmsg1(100, "SetFileAtts %s\n", ofile);
419    if (!(atts.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) {
420       if (!SetFileAttributes(win32_ofile, atts.dwFileAttributes & SET_ATTRS)) {
421          win_error(jcr, "SetFileAttributes:", win32_ofile);
422       }
423    }
424    free_pool_memory(win32_ofile);
425    return 1;
426 }
427
428 void win_error(void *vjcr, char *prefix, POOLMEM *win32_ofile)
429 {
430    JCR *jcr = (JCR *)vjcr; 
431    DWORD lerror = GetLastError();
432    LPTSTR msg;
433    FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|
434                  FORMAT_MESSAGE_FROM_SYSTEM,
435                  NULL,
436                  lerror,
437                  0,
438                  (LPTSTR)&msg,
439                  0,
440                  NULL);
441    Dmsg3(100, "Error in %s on file %s: ERR=%s\n", prefix, win32_ofile, msg);
442    strip_trailing_junk(msg);
443    Jmsg(jcr, M_INFO, 0, _("Error in %s file %s: ERR=%s\n"), prefix, win32_ofile, msg);
444    LocalFree(msg);
445 }
446
447 void win_error(void *vjcr, char *prefix, DWORD lerror)
448 {
449    JCR *jcr = (JCR *)vjcr; 
450    LPTSTR msg;
451    FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|
452                  FORMAT_MESSAGE_FROM_SYSTEM,
453                  NULL,
454                  lerror,
455                  0,
456                  (LPTSTR)&msg,
457                  0,
458                  NULL);
459    strip_trailing_junk(msg);
460    if (jcr) {
461       Jmsg2(jcr, M_INFO, 0, _("Error in %s: ERR=%s\n"), prefix, msg);
462    } else {
463       MessageBox(NULL, msg, prefix, MB_OK);
464    }
465    LocalFree(msg);
466 }
467
468
469 /* Cygwin API definition */
470 extern "C" void cygwin_conv_to_win32_path(const char *path, char *win32_path);
471
472 void unix_name_to_win32(POOLMEM **win32_name, char *name)
473 {
474    /* One extra byte should suffice, but we double it */
475    *win32_name = check_pool_memory_size(*win32_name, 2*strlen(name)+1);
476    cygwin_conv_to_win32_path(name, *win32_name);
477 }
478
479 #endif  /* HAVE_CYGWIN */