]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/findlib/attribs.c
Move some variables to eliminate Solaris 2.6 compiler warnings.
[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) 2002-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 bool 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 #ifndef HAVE_MINGW
121    p += to_base64((int64_t)statp->st_blksize, p);
122    *p++ = ' ';
123    p += to_base64((int64_t)statp->st_blocks, p);
124    *p++ = ' ';
125 #else
126    p += to_base64((int64_t)0, p); /* output place holder */
127    *p++ = ' ';
128    p += to_base64((int64_t)0, p); /* output place holder */
129    *p++ = ' ';
130 #endif
131    p += to_base64((int64_t)statp->st_atime, p);
132    *p++ = ' ';
133    p += to_base64((int64_t)statp->st_mtime, p);
134    *p++ = ' ';
135    p += to_base64((int64_t)statp->st_ctime, p);
136    *p++ = ' ';
137    p += to_base64((int64_t)ff_pkt->LinkFI, p);
138    *p++ = ' ';
139
140 #ifdef HAVE_CHFLAGS
141    /* FreeBSD function */
142    p += to_base64((int64_t)statp->st_flags, p);  /* output st_flags */
143 #else
144    p += to_base64((int64_t)0, p);     /* output place holder */
145 #endif
146    *p++ = ' ';
147    p += to_base64((int64_t)data_stream, p);
148    *p = 0;
149    return;
150 }
151
152
153 /* Do casting according to unknown type to keep compiler happy */
154 #if !HAVE_GCC & HAVE_SUN_OS
155 #define plug(st, val) st = val        /* brain damaged compiler */
156 #else
157 template <class T> void plug(T &st, uint64_t val)
158     { st = static_cast<T>(val); }
159 #endif
160
161
162 /* Decode a stat packet from base64 characters */
163 int decode_stat(char *buf, struct stat *statp, int32_t *LinkFI)
164 {
165    char *p = buf;
166    int64_t val;
167
168    p += from_base64(&val, p);
169    plug(statp->st_dev, val);
170    p++;
171    p += from_base64(&val, p);
172    plug(statp->st_ino, val);
173    p++;
174    p += from_base64(&val, p);
175    plug(statp->st_mode, val);
176    p++;
177    p += from_base64(&val, p);
178    plug(statp->st_nlink, val);
179    p++;
180    p += from_base64(&val, p);
181    plug(statp->st_uid, val);
182    p++;
183    p += from_base64(&val, p);
184    plug(statp->st_gid, val);
185    p++;
186    p += from_base64(&val, p);
187    plug(statp->st_rdev, val);
188    p++;
189    p += from_base64(&val, p);
190    plug(statp->st_size, val);
191    p++;
192 #ifndef HAVE_MINGW
193    p += from_base64(&val, p);
194    plug(statp->st_blksize, val);
195    p++;
196    p += from_base64(&val, p);
197    plug(statp->st_blocks, val);
198    p++;
199 #else
200    p += from_base64(&val, p);
201 //   plug(statp->st_blksize, val);
202    p++;
203    p += from_base64(&val, p);
204 //   plug(statp->st_blocks, val);
205    p++;
206 #endif
207    p += from_base64(&val, p);
208    plug(statp->st_atime, val);
209    p++;
210    p += from_base64(&val, p);
211    plug(statp->st_mtime, val);
212    p++;
213    p += from_base64(&val, p);
214    plug(statp->st_ctime, val);
215
216    /* Optional FileIndex of hard linked file data */
217    if (*p == ' ' || (*p != 0 && *(p+1) == ' ')) {
218       p++;
219       p += from_base64(&val, p);
220       *LinkFI = (uint32_t)val;
221    } else {
222       *LinkFI = 0;
223       return 0;
224    }
225
226    /* FreeBSD user flags */
227    if (*p == ' ' || (*p != 0 && *(p+1) == ' ')) {
228       p++;
229       p += from_base64(&val, p);
230 #ifdef HAVE_CHFLAGS
231       plug(statp->st_flags, val);
232    } else {
233       statp->st_flags  = 0;
234 #endif
235    }
236
237    /* Look for data stream id */
238    if (*p == ' ' || (*p != 0 && *(p+1) == ' ')) {
239       p++;
240       p += from_base64(&val, p);
241    } else {
242       val = 0;
243    }
244    return (int)val;
245 }
246
247 /* Decode a LinkFI field of encoded stat packet */
248 int32_t decode_LinkFI(char *buf, struct stat *statp)
249 {
250    char *p = buf;
251    int64_t val;
252
253    skip_nonspaces(&p);                /* st_dev */
254    p++;                               /* skip space */
255    skip_nonspaces(&p);                /* st_ino */
256    p++;
257    p += from_base64(&val, p);
258    plug(statp->st_mode, val);         /* st_mode */
259    p++;
260    skip_nonspaces(&p);                /* st_nlink */
261    p++;
262    skip_nonspaces(&p);                /* st_uid */
263    p++;
264    skip_nonspaces(&p);                /* st_gid */
265    p++;
266    skip_nonspaces(&p);                /* st_rdev */
267    p++;
268    skip_nonspaces(&p);                /* st_size */
269    p++;
270    skip_nonspaces(&p);                /* st_blksize */
271    p++;
272    skip_nonspaces(&p);                /* st_blocks */
273    p++;
274    skip_nonspaces(&p);                /* st_atime */
275    p++;
276    skip_nonspaces(&p);                /* st_mtime */
277    p++;
278    skip_nonspaces(&p);                /* st_ctime */
279
280    /* Optional FileIndex of hard linked file data */
281    if (*p == ' ' || (*p != 0 && *(p+1) == ' ')) {
282       p++;
283       p += from_base64(&val, p);
284       return (int32_t)val;
285    }
286    return 0;
287 }
288
289 /*
290  * Set file modes, permissions and times
291  *
292  *  fname is the original filename
293  *  ofile is the output filename (may be in a different directory)
294  *
295  * Returns:  true  on success
296  *           false on failure
297  */
298 bool set_attributes(JCR *jcr, ATTR *attr, BFILE *ofd)
299 {
300    struct utimbuf ut;
301    mode_t old_mask;
302    bool ok = true;
303    off_t fsize;
304
305 #if defined(HAVE_CYGWIN) || defined(HAVE_WIN32)
306    if (attr->stream == STREAM_UNIX_ATTRIBUTES_EX &&
307        set_win32_attributes(jcr, attr, ofd)) {
308        if (is_bopen(ofd)) {
309            bclose(ofd);
310        }
311        pm_strcpy(attr->ofname, "*none*");
312        return true;
313    }
314    if (attr->data_stream == STREAM_WIN32_DATA ||
315        attr->data_stream == STREAM_WIN32_GZIP_DATA) {
316       if (is_bopen(ofd)) {
317          bclose(ofd);
318       }
319       pm_strcpy(attr->ofname, "*none*");
320       return true;
321    }
322
323
324    /*
325     * If Windows stuff failed, e.g. attempt to restore Unix file
326     *  to Windows, simply fall through and we will do it the
327     *  universal way.
328     */
329 #endif
330
331    old_mask = umask(0);
332    if (is_bopen(ofd)) {
333       char ec1[50], ec2[50];
334       fsize = blseek(ofd, 0, SEEK_CUR);
335       bclose(ofd);                    /* first close file */
336       if (fsize > 0 && fsize != attr->statp.st_size) {
337          Jmsg3(jcr, M_ERROR, 0, _("File size of restored file %s not correct. Original %s, restored %s.\n"),
338             attr->ofname, edit_uint64(attr->statp.st_size, ec1),
339             edit_uint64(fsize, ec2));
340       }
341    }
342
343    ut.actime = attr->statp.st_atime;
344    ut.modtime = attr->statp.st_mtime;
345
346    /* ***FIXME**** optimize -- don't do if already correct */
347    /*
348     * For link, change owner of link using lchown, but don't
349     *   try to do a chmod as that will update the file behind it.
350     */
351    if (attr->type == FT_LNK) {
352       /* Change owner of link, not of real file */
353       if (lchown(attr->ofname, attr->statp.st_uid, attr->statp.st_gid) < 0) {
354          berrno be;
355          Jmsg2(jcr, M_ERROR, 0, _("Unable to set file owner %s: ERR=%s\n"),
356             attr->ofname, be.strerror());
357          ok = false;
358       }
359    } else {
360       if (chown(attr->ofname, attr->statp.st_uid, attr->statp.st_gid) < 0) {
361          berrno be;
362          Jmsg2(jcr, M_ERROR, 0, _("Unable to set file owner %s: ERR=%s\n"),
363             attr->ofname, be.strerror());
364          ok = false;
365       }
366       if (chmod(attr->ofname, attr->statp.st_mode) < 0) {
367          berrno be;
368          Jmsg2(jcr, M_ERROR, 0, _("Unable to set file modes %s: ERR=%s\n"),
369             attr->ofname, be.strerror());
370          ok = false;
371       }
372
373       /*
374        * Reset file times.
375        */
376       if (utime(attr->ofname, &ut) < 0) {
377          berrno be;
378          Jmsg2(jcr, M_ERROR, 0, _("Unable to set file times %s: ERR=%s\n"),
379             attr->ofname, be.strerror());
380          ok = false;
381       }
382 #ifdef HAVE_CHFLAGS
383       /*
384        * FreeBSD user flags
385        *
386        * Note, this should really be done before the utime() above,
387        *  but if the immutable bit is set, it will make the utimes()
388        *  fail.
389        */
390       if (chflags(attr->ofname, attr->statp.st_flags) < 0) {
391          berrno be;
392          Jmsg2(jcr, M_ERROR, 0, _("Unable to set file flags %s: ERR=%s\n"),
393             attr->ofname, be.strerror());
394          ok = false;
395       }
396 #endif
397    }
398    pm_strcpy(attr->ofname, "*none*");
399    umask(old_mask);
400    return ok;
401 }
402
403
404 /*=============================================================*/
405 /*                                                             */
406 /*                 * * *  U n i x * * * *                      */
407 /*                                                             */
408 /*=============================================================*/
409
410 #if !defined(HAVE_CYGWIN) && !defined(HAVE_WIN32)
411
412 /*
413  * It is possible to piggyback additional data e.g. ACLs on
414  *   the encode_stat() data by returning the extended attributes
415  *   here.  They must be "self-contained" (i.e. you keep track
416  *   of your own length), and they must be in ASCII string
417  *   format. Using this feature is not recommended.
418  * The code below shows how to return nothing.  See the Win32
419  *   code below for returning something in the attributes.
420  */
421 int encode_attribsEx(JCR *jcr, char *attribsEx, FF_PKT *ff_pkt)
422 {
423 #ifdef HAVE_DARWIN_OS
424    /*
425     * We save the Mac resource fork length so that on a
426     * restore, we can be sure we put back the whole resource.
427     */
428    char *p;
429    p = attribsEx;
430    if (ff_pkt->flags & FO_HFSPLUS) {
431       p += to_base64((uint64_t)(ff_pkt->hfsinfo.rsrclength), p);
432    }
433    *p = 0;
434 #else
435    *attribsEx = 0;                    /* no extended attributes */
436 #endif
437    return STREAM_UNIX_ATTRIBUTES;
438 }
439
440 #endif
441
442
443
444 /*=============================================================*/
445 /*                                                             */
446 /*                 * * *  W i n 3 2 * * * *                    */
447 /*                                                             */
448 /*=============================================================*/
449
450 #if defined(HAVE_CYGWIN) || defined(HAVE_WIN32)
451
452 int encode_attribsEx(JCR *jcr, char *attribsEx, FF_PKT *ff_pkt)
453 {
454    char *p = attribsEx;
455    WIN32_FILE_ATTRIBUTE_DATA atts;
456    ULARGE_INTEGER li;
457
458    attribsEx[0] = 0;                  /* no extended attributes */
459
460    if (!p_GetFileAttributesEx) {
461       return STREAM_UNIX_ATTRIBUTES;
462    }
463
464    unix_name_to_win32(&ff_pkt->sys_fname, ff_pkt->fname);
465    if (!p_GetFileAttributesEx(ff_pkt->sys_fname, GetFileExInfoStandard,
466                             (LPVOID)&atts)) {
467       win_error(jcr, "GetFileAttributesEx:", ff_pkt->sys_fname);
468       return STREAM_UNIX_ATTRIBUTES;
469    }
470
471    p += to_base64((uint64_t)atts.dwFileAttributes, p);
472    *p++ = ' ';                        /* separate fields with a space */
473    li.LowPart = atts.ftCreationTime.dwLowDateTime;
474    li.HighPart = atts.ftCreationTime.dwHighDateTime;
475    p += to_base64((uint64_t)li.QuadPart, p);
476    *p++ = ' ';
477    li.LowPart = atts.ftLastAccessTime.dwLowDateTime;
478    li.HighPart = atts.ftLastAccessTime.dwHighDateTime;
479    p += to_base64((uint64_t)li.QuadPart, p);
480    *p++ = ' ';
481    li.LowPart = atts.ftLastWriteTime.dwLowDateTime;
482    li.HighPart = atts.ftLastWriteTime.dwHighDateTime;
483    p += to_base64((uint64_t)li.QuadPart, p);
484    *p++ = ' ';
485    p += to_base64((uint64_t)atts.nFileSizeHigh, p);
486    *p++ = ' ';
487    p += to_base64((uint64_t)atts.nFileSizeLow, p);
488    *p = 0;
489    return STREAM_UNIX_ATTRIBUTES_EX;
490 }
491
492 /* Define attributes that are legal to set with SetFileAttributes() */
493 #define SET_ATTRS ( \
494          FILE_ATTRIBUTE_ARCHIVE| \
495          FILE_ATTRIBUTE_HIDDEN| \
496          FILE_ATTRIBUTE_NORMAL| \
497          FILE_ATTRIBUTE_NOT_CONTENT_INDEXED| \
498          FILE_ATTRIBUTE_OFFLINE| \
499          FILE_ATTRIBUTE_READONLY| \
500          FILE_ATTRIBUTE_SYSTEM| \
501          FILE_ATTRIBUTE_TEMPORARY)
502
503
504 /*
505  * Set Extended File Attributes for Win32
506  *
507  *  fname is the original filename
508  *  ofile is the output filename (may be in a different directory)
509  *
510  * Returns:  true  on success
511  *           false on failure
512  */
513 static bool set_win32_attributes(JCR *jcr, ATTR *attr, BFILE *ofd)
514 {
515    char *p = attr->attrEx;
516    int64_t val;
517    WIN32_FILE_ATTRIBUTE_DATA atts;
518    ULARGE_INTEGER li;
519    POOLMEM *win32_ofile;
520
521    if (!p_GetFileAttributesEx) {
522       return false;
523    }
524
525    if (!p || !*p) {                   /* we should have attributes */
526       Dmsg2(100, "Attributes missing. of=%s ofd=%d\n", attr->ofname, ofd->fid);
527       if (is_bopen(ofd)) {
528          bclose(ofd);
529       }
530       return false;
531    } else {
532       Dmsg2(100, "Attribs %s = %s\n", attr->ofname, attr->attrEx);
533    }
534
535    p += from_base64(&val, p);
536    plug(atts.dwFileAttributes, val);
537    p++;                               /* skip space */
538    p += from_base64(&val, p);
539    li.QuadPart = val;
540    atts.ftCreationTime.dwLowDateTime = li.LowPart;
541    atts.ftCreationTime.dwHighDateTime = li.HighPart;
542    p++;                               /* skip space */
543    p += from_base64(&val, p);
544    li.QuadPart = val;
545    atts.ftLastAccessTime.dwLowDateTime = li.LowPart;
546    atts.ftLastAccessTime.dwHighDateTime = li.HighPart;
547    p++;                               /* skip space */
548    p += from_base64(&val, p);
549    li.QuadPart = val;
550    atts.ftLastWriteTime.dwLowDateTime = li.LowPart;
551    atts.ftLastWriteTime.dwHighDateTime = li.HighPart;
552    p++;
553    p += from_base64(&val, p);
554    plug(atts.nFileSizeHigh, val);
555    p++;
556    p += from_base64(&val, p);
557    plug(atts.nFileSizeLow, val);
558
559    /* Convert to Windows path format */
560    win32_ofile = get_pool_memory(PM_FNAME);
561    unix_name_to_win32(&win32_ofile, attr->ofname);
562
563
564
565    /* At this point, we have reconstructed the WIN32_FILE_ATTRIBUTE_DATA pkt */
566
567    if (!is_bopen(ofd)) {
568       Dmsg1(100, "File not open: %s\n", attr->ofname);
569       bopen(ofd, attr->ofname, O_WRONLY|O_BINARY, 0);   /* attempt to open the file */
570    }
571
572    if (is_bopen(ofd)) {
573       Dmsg1(100, "SetFileTime %s\n", attr->ofname);
574       if (!SetFileTime(bget_handle(ofd),
575                          &atts.ftCreationTime,
576                          &atts.ftLastAccessTime,
577                          &atts.ftLastWriteTime)) {
578          win_error(jcr, "SetFileTime:", win32_ofile);
579       }
580       bclose(ofd);
581    }
582
583    Dmsg1(100, "SetFileAtts %s\n", attr->ofname);
584    if (!(atts.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) {
585       if (!SetFileAttributes(win32_ofile, atts.dwFileAttributes & SET_ATTRS)) {
586          win_error(jcr, "SetFileAttributes:", win32_ofile);
587       }
588    }
589    free_pool_memory(win32_ofile);
590    return true;
591 }
592
593 void win_error(JCR *jcr, char *prefix, POOLMEM *win32_ofile)
594 {
595    DWORD lerror = GetLastError();
596    LPTSTR msg;
597    FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|
598                  FORMAT_MESSAGE_FROM_SYSTEM,
599                  NULL,
600                  lerror,
601                  0,
602                  (LPTSTR)&msg,
603                  0,
604                  NULL);
605    Dmsg3(100, "Error in %s on file %s: ERR=%s\n", prefix, win32_ofile, msg);
606    strip_trailing_junk(msg);
607    Jmsg(jcr, M_ERROR, 0, _("Error in %s file %s: ERR=%s\n"), prefix, win32_ofile, msg);
608    LocalFree(msg);
609 }
610
611 void win_error(JCR *jcr, char *prefix, DWORD lerror)
612 {
613    LPTSTR msg;
614    FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|
615                  FORMAT_MESSAGE_FROM_SYSTEM,
616                  NULL,
617                  lerror,
618                  0,
619                  (LPTSTR)&msg,
620                  0,
621                  NULL);
622    strip_trailing_junk(msg);
623    if (jcr) {
624       Jmsg2(jcr, M_ERROR, 0, _("Error in %s: ERR=%s\n"), prefix, msg);
625    } else {
626       MessageBox(NULL, msg, prefix, MB_OK);
627    }
628    LocalFree(msg);
629 }
630
631
632 /* Cygwin API definition */
633 extern "C" void cygwin_conv_to_win32_path(const char *path, char *win32_path);
634
635 void unix_name_to_win32(POOLMEM **win32_name, char *name)
636 {
637    /* One extra byte should suffice, but we double it */
638    *win32_name = check_pool_memory_size(*win32_name, 2*strlen(name)+1);
639    cygwin_conv_to_win32_path(name, *win32_name);
640 }
641
642 #endif  /* HAVE_CYGWIN */