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