]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/findlib/attribs.c
reorder stream/data_stream tests so set_win32_attributes gets called
[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-2004 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 #if defined(HAVE_CYGWIN) || defined(HAVE_WIN32)
35
36 /* Forward referenced subroutines */
37 static int set_win32_attributes(JCR *jcr, ATTR *attr, BFILE *ofd);
38 void unix_name_to_win32(POOLMEM **win32_name, char *name);
39 void win_error(JCR *jcr, char *prefix, POOLMEM *ofile);
40 HANDLE bget_handle(BFILE *bfd);
41 #endif
42
43 /* For old systems that don't have lchown() use chown() */
44 #ifndef HAVE_LCHOWN
45 #define lchown chown
46 #endif
47
48 /*=============================================================*/
49 /*                                                             */
50 /*             ***  A l l  S y s t e m s ***                   */
51 /*                                                             */
52 /*=============================================================*/
53
54 /*
55  * Return the data stream that will be used 
56  */
57 int select_data_stream(FF_PKT *ff_pkt)
58 {
59    int stream;
60
61    /* Note, no sparse option for win32_data */
62    if (!is_portable_backup(&ff_pkt->bfd)) {
63       stream = STREAM_WIN32_DATA;
64       ff_pkt->flags &= ~FO_SPARSE;
65    } else if (ff_pkt->flags & FO_SPARSE) {
66       stream = STREAM_SPARSE_DATA;
67    } else {
68       stream = STREAM_FILE_DATA;
69    }
70 #ifdef HAVE_LIBZ
71    if (ff_pkt->flags & FO_GZIP) {
72       if (stream == STREAM_WIN32_DATA) {
73          stream = STREAM_WIN32_GZIP_DATA;
74       } else if (stream == STREAM_FILE_DATA) {
75          stream = STREAM_GZIP_DATA;
76       } else {
77          stream = STREAM_SPARSE_GZIP_DATA;
78       }
79    }
80 #endif
81    return stream;
82 }
83
84
85 /* 
86  * Encode a stat structure into a base64 character string   
87  *   All systems must create such a structure.
88  *   In addition, we tack on the LinkFI, which is non-zero in
89  *   the case of a hard linked file that has no data.  This
90  *   is a File Index pointing to the link that does have the
91  *   data (always the first one encountered in a save).
92  * You may piggyback attributes on this packet by encoding
93  *   them in the encode_attribsEx() subroutine, but this is
94  *   not recommended.
95  */
96 void encode_stat(char *buf, FF_PKT *ff_pkt, int data_stream)
97 {
98    char *p = buf;
99    struct stat *statp = &ff_pkt->statp;
100    /*
101     *  Encode a stat packet.  I should have done this more intelligently
102     *   with a length so that it could be easily expanded.
103     */
104    p += to_base64((int64_t)statp->st_dev, p);
105    *p++ = ' ';                        /* separate fields with a space */
106    p += to_base64((int64_t)statp->st_ino, p);
107    *p++ = ' ';
108    p += to_base64((int64_t)statp->st_mode, p);
109    *p++ = ' ';
110    p += to_base64((int64_t)statp->st_nlink, p);
111    *p++ = ' ';
112    p += to_base64((int64_t)statp->st_uid, p);
113    *p++ = ' ';
114    p += to_base64((int64_t)statp->st_gid, p);
115    *p++ = ' ';
116    p += to_base64((int64_t)statp->st_rdev, p);
117    *p++ = ' ';
118    p += to_base64((int64_t)statp->st_size, p);
119    *p++ = ' ';
120    p += to_base64((int64_t)statp->st_blksize, p);
121    *p++ = ' ';
122    p += to_base64((int64_t)statp->st_blocks, p);
123    *p++ = ' ';
124    p += to_base64((int64_t)statp->st_atime, p);
125    *p++ = ' ';
126    p += to_base64((int64_t)statp->st_mtime, p);
127    *p++ = ' ';
128    p += to_base64((int64_t)statp->st_ctime, p);
129    *p++ = ' ';
130    p += to_base64((int64_t)ff_pkt->LinkFI, p);
131    *p++ = ' ';
132
133 #ifdef HAVE_CHFLAGS
134    /* FreeBSD function */
135    p += to_base64((int64_t)statp->st_flags, p);  /* output st_flags */
136 #else
137    p += to_base64((int64_t)0, p);     /* output place holder */
138 #endif
139    *p++ = ' ';
140    p += to_base64((int64_t)data_stream, p);
141    *p = 0;
142    return;
143 }
144
145
146
147 /* Decode a stat packet from base64 characters */
148 int decode_stat(char *buf, struct stat *statp, int32_t *LinkFI) 
149 {
150    char *p = buf;
151    int64_t val;
152
153    p += from_base64(&val, p);
154    statp->st_dev = val;
155    p++;                               /* skip space */
156    p += from_base64(&val, p);
157    statp->st_ino = val;
158    p++;
159    p += from_base64(&val, p);
160    statp->st_mode = val;
161    p++;
162    p += from_base64(&val, p);
163    statp->st_nlink = val;
164    p++;
165    p += from_base64(&val, p);
166    statp->st_uid = val;
167    p++;
168    p += from_base64(&val, p);
169    statp->st_gid = val;
170    p++;
171    p += from_base64(&val, p);
172    statp->st_rdev = val;
173    p++;
174    p += from_base64(&val, p);
175    statp->st_size = val;
176    p++;
177    p += from_base64(&val, p);
178    statp->st_blksize = val;
179    p++;
180    p += from_base64(&val, p);
181    statp->st_blocks = val;
182    p++;
183    p += from_base64(&val, p);
184    statp->st_atime = val;
185    p++;
186    p += from_base64(&val, p);
187    statp->st_mtime = val;
188    p++;
189    p += from_base64(&val, p);
190    statp->st_ctime = val;
191
192    /* Optional FileIndex of hard linked file data */
193    if (*p == ' ' || (*p != 0 && *(p+1) == ' ')) {
194       p++;
195       p += from_base64(&val, p);
196       *LinkFI = (uint32_t)val;
197    } else {
198       *LinkFI = 0;
199       return 0;
200    }
201
202    /* FreeBSD user flags */
203    if (*p == ' ' || (*p != 0 && *(p+1) == ' ')) {
204       p++;
205       p += from_base64(&val, p);
206 #ifdef HAVE_CHFLAGS
207       statp->st_flags = (uint32_t)val;
208    } else {
209       statp->st_flags  = 0;
210 #endif
211    }
212     
213    /* Look for data stream id */
214    if (*p == ' ' || (*p != 0 && *(p+1) == ' ')) {
215       p++;
216       p += from_base64(&val, p);
217    } else {
218       val = 0;
219    }
220    return (int)val;
221 }
222
223 /* Decode a LinkFI field of encoded stat packet */
224 int32_t decode_LinkFI(char *buf, struct stat *statp)
225 {
226    char *p = buf;
227    int64_t val;
228
229    skip_nonspaces(&p);                /* st_dev */
230    p++;                               /* skip space */
231    skip_nonspaces(&p);                /* st_ino */
232    p++;
233    p += from_base64(&val, p);
234    statp->st_mode = val;              /* st_mode */
235    p++;
236    skip_nonspaces(&p);                /* st_nlink */
237    p++;
238    skip_nonspaces(&p);                /* st_uid */
239    p++;
240    skip_nonspaces(&p);                /* st_gid */
241    p++;
242    skip_nonspaces(&p);                /* st_rdev */
243    p++;
244    skip_nonspaces(&p);                /* st_size */
245    p++;
246    skip_nonspaces(&p);                /* st_blksize */
247    p++;
248    skip_nonspaces(&p);                /* st_blocks */
249    p++;
250    skip_nonspaces(&p);                /* st_atime */
251    p++;
252    skip_nonspaces(&p);                /* st_mtime */
253    p++;
254    skip_nonspaces(&p);                /* st_ctime */
255
256    /* Optional FileIndex of hard linked file data */
257    if (*p == ' ' || (*p != 0 && *(p+1) == ' ')) {
258       p++;
259       p += from_base64(&val, p);
260       return (int32_t)val;
261    }
262    return 0;
263 }
264
265 /*
266  * Set file modes, permissions and times
267  *
268  *  fname is the original filename  
269  *  ofile is the output filename (may be in a different directory)
270  *
271  * Returns:  1 on success
272  *           0 on failure
273  */
274 int set_attributes(JCR *jcr, ATTR *attr, BFILE *ofd)
275 {
276    struct utimbuf ut;    
277    mode_t old_mask;
278    int stat = 1;
279
280 #if defined(HAVE_CYGWIN) || defined(HAVE_WIN32)
281    if (attr->stream == STREAM_UNIX_ATTRIBUTES_EX &&
282        set_win32_attributes(jcr, attr, ofd)) {
283        if (is_bopen(ofd)) {
284            bclose(ofd); 
285        }
286        pm_strcpy(&attr->ofname, "*none*");
287        return 1;
288    }
289    if (attr->data_stream == STREAM_WIN32_DATA ||
290        attr->data_stream == STREAM_WIN32_GZIP_DATA) {
291       if (is_bopen(ofd)) {
292          bclose(ofd); 
293       }
294       pm_strcpy(&attr->ofname, "*none*");
295       return 1;
296    }
297
298
299    /*
300     * If Windows stuff failed, e.g. attempt to restore Unix file
301     *  to Windows, simply fall through and we will do it the     
302     *  universal way.
303     */
304 #endif
305
306    old_mask = umask(0);
307    if (is_bopen(ofd)) {
308       bclose(ofd);                    /* first close file */
309    }
310
311    ut.actime = attr->statp.st_atime;
312    ut.modtime = attr->statp.st_mtime;
313
314    /* ***FIXME**** optimize -- don't do if already correct */
315    /* 
316     * For link, change owner of link using lchown, but don't
317     *   try to do a chmod as that will update the file behind it.
318     */
319    if (attr->type == FT_LNK) {
320       /* Change owner of link, not of real file */
321       if (lchown(attr->ofname, attr->statp.st_uid, attr->statp.st_gid) < 0) {
322          Jmsg2(jcr, M_ERROR, 0, _("Unable to set file owner %s: ERR=%s\n"),
323             attr->ofname, strerror(errno));
324          stat = 0;
325       }
326    } else {
327       if (chown(attr->ofname, attr->statp.st_uid, attr->statp.st_gid) < 0) {
328          Jmsg2(jcr, M_ERROR, 0, _("Unable to set file owner %s: ERR=%s\n"),
329             attr->ofname, strerror(errno));
330          stat = 0;
331       }
332       if (chmod(attr->ofname, attr->statp.st_mode) < 0) {
333          Jmsg2(jcr, M_ERROR, 0, _("Unable to set file modes %s: ERR=%s\n"),
334             attr->ofname, strerror(errno));
335          stat = 0;
336       }
337
338       /* FreeBSD user flags */
339 #ifdef HAVE_CHFLAGS
340       if (chflags(attr->ofname, attr->statp.st_flags) < 0) {
341          Jmsg2(jcr, M_ERROR, 0, _("Unable to set file flags %s: ERR=%s\n"),
342             attr->ofname, strerror(errno));
343          stat = 0;
344       }
345 #endif
346       /*
347        * Reset file times.
348        */
349       if (utime(attr->ofname, &ut) < 0) {
350          Jmsg2(jcr, M_ERROR, 0, _("Unable to set file times %s: ERR=%s\n"),
351             attr->ofname, strerror(errno));
352          stat = 0;
353       }
354    }
355    pm_strcpy(&attr->ofname, "*none*");
356    umask(old_mask);
357    return stat;
358 }
359
360
361 /*=============================================================*/
362 /*                                                             */
363 /*                 * * *  U n i x * * * *                      */
364 /*                                                             */
365 /*=============================================================*/
366
367 #if !defined(HAVE_CYGWIN) && !defined(HAVE_WIN32)
368     
369 /*
370  * It is possible to piggyback additional data e.g. ACLs on
371  *   the encode_stat() data by returning the extended attributes
372  *   here.  They must be "self-contained" (i.e. you keep track
373  *   of your own length), and they must be in ASCII string
374  *   format. Using this feature is not recommended.
375  * The code below shows how to return nothing.  See the Win32
376  *   code below for returning something in the attributes.
377  */
378 int encode_attribsEx(JCR *jcr, char *attribsEx, FF_PKT *ff_pkt)
379 {
380    *attribsEx = 0;                    /* no extended attributes */
381    return STREAM_UNIX_ATTRIBUTES;
382 }
383
384 #endif
385
386
387
388 /*=============================================================*/
389 /*                                                             */
390 /*                 * * *  W i n 3 2 * * * *                    */
391 /*                                                             */
392 /*=============================================================*/
393
394 #if defined(HAVE_CYGWIN) || defined(HAVE_WIN32)
395
396 int encode_attribsEx(JCR *jcr, char *attribsEx, FF_PKT *ff_pkt)
397 {
398    char *p = attribsEx;
399    WIN32_FILE_ATTRIBUTE_DATA atts;
400    ULARGE_INTEGER li;
401
402    attribsEx[0] = 0;                  /* no extended attributes */
403
404    if (!p_GetFileAttributesEx) {                                 
405       return STREAM_UNIX_ATTRIBUTES;
406    }
407
408    unix_name_to_win32(&ff_pkt->sys_fname, ff_pkt->fname);
409    if (!p_GetFileAttributesEx(ff_pkt->sys_fname, GetFileExInfoStandard,
410                             (LPVOID)&atts)) {
411       win_error(jcr, "GetFileAttributesEx:", ff_pkt->sys_fname);
412       return STREAM_UNIX_ATTRIBUTES;
413    }
414
415    p += to_base64((uint64_t)atts.dwFileAttributes, p);
416    *p++ = ' ';                        /* separate fields with a space */
417    li.LowPart = atts.ftCreationTime.dwLowDateTime;
418    li.HighPart = atts.ftCreationTime.dwHighDateTime;
419    p += to_base64((uint64_t)li.QuadPart, p);
420    *p++ = ' ';
421    li.LowPart = atts.ftLastAccessTime.dwLowDateTime;
422    li.HighPart = atts.ftLastAccessTime.dwHighDateTime;
423    p += to_base64((uint64_t)li.QuadPart, p);
424    *p++ = ' ';
425    li.LowPart = atts.ftLastWriteTime.dwLowDateTime;
426    li.HighPart = atts.ftLastWriteTime.dwHighDateTime;
427    p += to_base64((uint64_t)li.QuadPart, p);
428    *p++ = ' ';
429    p += to_base64((uint64_t)atts.nFileSizeHigh, p);
430    *p++ = ' ';
431    p += to_base64((uint64_t)atts.nFileSizeLow, p);
432    *p = 0;
433    return STREAM_UNIX_ATTRIBUTES_EX;
434 }
435
436 /* Define attributes that are legal to set with SetFileAttributes() */
437 #define SET_ATTRS ( \
438          FILE_ATTRIBUTE_ARCHIVE| \
439          FILE_ATTRIBUTE_HIDDEN| \
440          FILE_ATTRIBUTE_NORMAL| \
441          FILE_ATTRIBUTE_NOT_CONTENT_INDEXED| \
442          FILE_ATTRIBUTE_OFFLINE| \
443          FILE_ATTRIBUTE_READONLY| \
444          FILE_ATTRIBUTE_SYSTEM| \
445          FILE_ATTRIBUTE_TEMPORARY)
446
447
448 /*
449  * Set Extended File Attributes for Win32
450  *
451  *  fname is the original filename  
452  *  ofile is the output filename (may be in a different directory)
453  *
454  * Returns:  1 on success
455  *           0 on failure
456  */
457 static int set_win32_attributes(JCR *jcr, ATTR *attr, BFILE *ofd)
458 {
459    char *p = attr->attrEx;
460    int64_t val;
461    WIN32_FILE_ATTRIBUTE_DATA atts;
462    ULARGE_INTEGER li;
463    POOLMEM *win32_ofile;
464
465    if (!p_GetFileAttributesEx) {                                 
466       return 0;
467    }
468
469    if (!p || !*p) {                   /* we should have attributes */
470       Dmsg2(100, "Attributes missing. of=%s ofd=%d\n", attr->ofname, ofd->fid);
471       if (is_bopen(ofd)) {
472          bclose(ofd);
473       }
474       return 0;
475    } else {
476       Dmsg2(100, "Attribs %s = %s\n", attr->ofname, attr->attrEx);
477    }
478
479    p += from_base64(&val, p);
480    atts.dwFileAttributes = val;
481    p++;                               /* skip space */
482    p += from_base64(&val, p);
483    li.QuadPart = val;
484    atts.ftCreationTime.dwLowDateTime = li.LowPart;
485    atts.ftCreationTime.dwHighDateTime = li.HighPart;
486    p++;                               /* skip space */
487    p += from_base64(&val, p);
488    li.QuadPart = val;
489    atts.ftLastAccessTime.dwLowDateTime = li.LowPart;
490    atts.ftLastAccessTime.dwHighDateTime = li.HighPart;
491    p++;                               /* skip space */
492    p += from_base64(&val, p);
493    li.QuadPart = val;
494    atts.ftLastWriteTime.dwLowDateTime = li.LowPart;
495    atts.ftLastWriteTime.dwHighDateTime = li.HighPart;
496    p++;   
497    p += from_base64(&val, p);
498    atts.nFileSizeHigh = val;
499    p++;
500    p += from_base64(&val, p);
501    atts.nFileSizeLow = val;
502
503    /* Convert to Windows path format */
504    win32_ofile = get_pool_memory(PM_FNAME);
505    unix_name_to_win32(&win32_ofile, attr->ofname);
506
507
508
509    /* At this point, we have reconstructed the WIN32_FILE_ATTRIBUTE_DATA pkt */
510
511    if (!is_bopen(ofd)) {
512       Dmsg1(100, "File not open: %s\n", attr->ofname);
513       bopen(ofd, attr->ofname, O_WRONLY|O_BINARY, 0);   /* attempt to open the file */
514    }
515
516    if (is_bopen(ofd)) {
517       Dmsg1(100, "SetFileTime %s\n", attr->ofname);
518       if (!SetFileTime(bget_handle(ofd),
519                          &atts.ftCreationTime,
520                          &atts.ftLastAccessTime,
521                          &atts.ftLastWriteTime)) {
522          win_error(jcr, "SetFileTime:", win32_ofile);
523       }
524       bclose(ofd);
525    }
526
527    Dmsg1(100, "SetFileAtts %s\n", attr->ofname);
528    if (!(atts.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) {
529       if (!SetFileAttributes(win32_ofile, atts.dwFileAttributes & SET_ATTRS)) {
530          win_error(jcr, "SetFileAttributes:", win32_ofile);
531       }
532    }
533    free_pool_memory(win32_ofile);
534    return 1;
535 }
536
537 void win_error(JCR *jcr, char *prefix, POOLMEM *win32_ofile)
538 {
539    DWORD lerror = GetLastError();
540    LPTSTR msg;
541    FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|
542                  FORMAT_MESSAGE_FROM_SYSTEM,
543                  NULL,
544                  lerror,
545                  0,
546                  (LPTSTR)&msg,
547                  0,
548                  NULL);
549    Dmsg3(100, "Error in %s on file %s: ERR=%s\n", prefix, win32_ofile, msg);
550    strip_trailing_junk(msg);
551    Jmsg(jcr, M_ERROR, 0, _("Error in %s file %s: ERR=%s\n"), prefix, win32_ofile, msg);
552    LocalFree(msg);
553 }
554
555 void win_error(JCR *jcr, char *prefix, DWORD lerror)
556 {
557    LPTSTR msg;
558    FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|
559                  FORMAT_MESSAGE_FROM_SYSTEM,
560                  NULL,
561                  lerror,
562                  0,
563                  (LPTSTR)&msg,
564                  0,
565                  NULL);
566    strip_trailing_junk(msg);
567    if (jcr) {
568       Jmsg2(jcr, M_ERROR, 0, _("Error in %s: ERR=%s\n"), prefix, msg);
569    } else {
570       MessageBox(NULL, msg, prefix, MB_OK);
571    }
572    LocalFree(msg);
573 }
574
575
576 /* Cygwin API definition */
577 extern "C" void cygwin_conv_to_win32_path(const char *path, char *win32_path);
578
579 void unix_name_to_win32(POOLMEM **win32_name, char *name)
580 {
581    /* One extra byte should suffice, but we double it */
582    *win32_name = check_pool_memory_size(*win32_name, 2*strlen(name)+1);
583    cygwin_conv_to_win32_path(name, *win32_name);
584 }
585
586 #endif  /* HAVE_CYGWIN */