2 * Encode and decode standard Unix attributes and
3 * Extended attributes for Win32 and
4 * other non-Unix systems, or Unix systems with ACLs, ...
6 * Kern Sibbald, October MMII
12 Copyright (C) 2002-2004 Kern Sibbald and John Walker
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.
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.
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,
34 #if defined(HAVE_CYGWIN) || defined(HAVE_WIN32)
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);
43 /* For old systems that don't have lchown() use chown() */
48 /*=============================================================*/
50 /* *** A l l S y s t e m s *** */
52 /*=============================================================*/
55 * Return the data stream that will be used
57 int select_data_stream(FF_PKT *ff_pkt)
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;
68 stream = STREAM_FILE_DATA;
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;
77 stream = STREAM_SPARSE_GZIP_DATA;
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
96 void encode_stat(char *buf, FF_PKT *ff_pkt, int data_stream)
99 struct stat *statp = &ff_pkt->statp;
101 * Encode a stat packet. I should have done this more intelligently
102 * with a length so that it could be easily expanded.
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);
108 p += to_base64((int64_t)statp->st_mode, p);
110 p += to_base64((int64_t)statp->st_nlink, p);
112 p += to_base64((int64_t)statp->st_uid, p);
114 p += to_base64((int64_t)statp->st_gid, p);
116 p += to_base64((int64_t)statp->st_rdev, p);
118 p += to_base64((int64_t)statp->st_size, p);
121 p += to_base64((int64_t)statp->st_blksize, p);
123 p += to_base64((int64_t)statp->st_blocks, p);
126 p += to_base64((int64_t)0, p); /* output place holder */
128 p += to_base64((int64_t)0, p); /* output place holder */
131 p += to_base64((int64_t)statp->st_atime, p);
133 p += to_base64((int64_t)statp->st_mtime, p);
135 p += to_base64((int64_t)statp->st_ctime, p);
137 p += to_base64((int64_t)ff_pkt->LinkFI, p);
141 /* FreeBSD function */
142 p += to_base64((int64_t)statp->st_flags, p); /* output st_flags */
144 p += to_base64((int64_t)0, p); /* output place holder */
147 p += to_base64((int64_t)data_stream, p);
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 */
157 template <class T> void plug(T &st, uint64_t val)
158 { st = static_cast<T>(val); }
162 /* Decode a stat packet from base64 characters */
163 int decode_stat(char *buf, struct stat *statp, int32_t *LinkFI)
168 p += from_base64(&val, p);
169 plug(statp->st_dev, val);
171 p += from_base64(&val, p);
172 plug(statp->st_ino, val);
174 p += from_base64(&val, p);
175 plug(statp->st_mode, val);
177 p += from_base64(&val, p);
178 plug(statp->st_nlink, val);
180 p += from_base64(&val, p);
181 plug(statp->st_uid, val);
183 p += from_base64(&val, p);
184 plug(statp->st_gid, val);
186 p += from_base64(&val, p);
187 plug(statp->st_rdev, val);
189 p += from_base64(&val, p);
190 plug(statp->st_size, val);
193 p += from_base64(&val, p);
194 plug(statp->st_blksize, val);
196 p += from_base64(&val, p);
197 plug(statp->st_blocks, val);
200 p += from_base64(&val, p);
201 // plug(statp->st_blksize, val);
203 p += from_base64(&val, p);
204 // plug(statp->st_blocks, val);
207 p += from_base64(&val, p);
208 plug(statp->st_atime, val);
210 p += from_base64(&val, p);
211 plug(statp->st_mtime, val);
213 p += from_base64(&val, p);
214 plug(statp->st_ctime, val);
216 /* Optional FileIndex of hard linked file data */
217 if (*p == ' ' || (*p != 0 && *(p+1) == ' ')) {
219 p += from_base64(&val, p);
220 *LinkFI = (uint32_t)val;
226 /* FreeBSD user flags */
227 if (*p == ' ' || (*p != 0 && *(p+1) == ' ')) {
229 p += from_base64(&val, p);
231 plug(statp->st_flags, val);
237 /* Look for data stream id */
238 if (*p == ' ' || (*p != 0 && *(p+1) == ' ')) {
240 p += from_base64(&val, p);
247 /* Decode a LinkFI field of encoded stat packet */
248 int32_t decode_LinkFI(char *buf, struct stat *statp)
253 skip_nonspaces(&p); /* st_dev */
254 p++; /* skip space */
255 skip_nonspaces(&p); /* st_ino */
257 p += from_base64(&val, p);
258 plug(statp->st_mode, val); /* st_mode */
260 skip_nonspaces(&p); /* st_nlink */
262 skip_nonspaces(&p); /* st_uid */
264 skip_nonspaces(&p); /* st_gid */
266 skip_nonspaces(&p); /* st_rdev */
268 skip_nonspaces(&p); /* st_size */
270 skip_nonspaces(&p); /* st_blksize */
272 skip_nonspaces(&p); /* st_blocks */
274 skip_nonspaces(&p); /* st_atime */
276 skip_nonspaces(&p); /* st_mtime */
278 skip_nonspaces(&p); /* st_ctime */
280 /* Optional FileIndex of hard linked file data */
281 if (*p == ' ' || (*p != 0 && *(p+1) == ' ')) {
283 p += from_base64(&val, p);
290 * Set file modes, permissions and times
292 * fname is the original filename
293 * ofile is the output filename (may be in a different directory)
295 * Returns: true on success
298 bool set_attributes(JCR *jcr, ATTR *attr, BFILE *ofd)
305 #if defined(HAVE_CYGWIN) || defined(HAVE_WIN32)
306 if (attr->stream == STREAM_UNIX_ATTRIBUTES_EX &&
307 set_win32_attributes(jcr, attr, ofd)) {
311 pm_strcpy(attr->ofname, "*none*");
314 if (attr->data_stream == STREAM_WIN32_DATA ||
315 attr->data_stream == STREAM_WIN32_GZIP_DATA) {
319 pm_strcpy(attr->ofname, "*none*");
325 * If Windows stuff failed, e.g. attempt to restore Unix file
326 * to Windows, simply fall through and we will do it the
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));
343 ut.actime = attr->statp.st_atime;
344 ut.modtime = attr->statp.st_mtime;
346 /* ***FIXME**** optimize -- don't do if already correct */
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.
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) {
355 Jmsg2(jcr, M_ERROR, 0, _("Unable to set file owner %s: ERR=%s\n"),
356 attr->ofname, be.strerror());
360 if (chown(attr->ofname, attr->statp.st_uid, attr->statp.st_gid) < 0) {
362 Jmsg2(jcr, M_ERROR, 0, _("Unable to set file owner %s: ERR=%s\n"),
363 attr->ofname, be.strerror());
366 if (chmod(attr->ofname, attr->statp.st_mode) < 0) {
368 Jmsg2(jcr, M_ERROR, 0, _("Unable to set file modes %s: ERR=%s\n"),
369 attr->ofname, be.strerror());
376 if (utime(attr->ofname, &ut) < 0) {
378 Jmsg2(jcr, M_ERROR, 0, _("Unable to set file times %s: ERR=%s\n"),
379 attr->ofname, be.strerror());
386 * Note, this should really be done before the utime() above,
387 * but if the immutable bit is set, it will make the utimes()
390 if (chflags(attr->ofname, attr->statp.st_flags) < 0) {
392 Jmsg2(jcr, M_ERROR, 0, _("Unable to set file flags %s: ERR=%s\n"),
393 attr->ofname, be.strerror());
398 pm_strcpy(attr->ofname, "*none*");
404 /*=============================================================*/
406 /* * * * U n i x * * * * */
408 /*=============================================================*/
410 #if !defined(HAVE_CYGWIN) && !defined(HAVE_WIN32)
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.
421 int encode_attribsEx(JCR *jcr, char *attribsEx, FF_PKT *ff_pkt)
423 *attribsEx = 0; /* no extended attributes */
424 return STREAM_UNIX_ATTRIBUTES;
431 /*=============================================================*/
433 /* * * * W i n 3 2 * * * * */
435 /*=============================================================*/
437 #if defined(HAVE_CYGWIN) || defined(HAVE_WIN32)
439 int encode_attribsEx(JCR *jcr, char *attribsEx, FF_PKT *ff_pkt)
442 WIN32_FILE_ATTRIBUTE_DATA atts;
445 attribsEx[0] = 0; /* no extended attributes */
447 if (!p_GetFileAttributesEx) {
448 return STREAM_UNIX_ATTRIBUTES;
451 unix_name_to_win32(&ff_pkt->sys_fname, ff_pkt->fname);
452 if (!p_GetFileAttributesEx(ff_pkt->sys_fname, GetFileExInfoStandard,
454 win_error(jcr, "GetFileAttributesEx:", ff_pkt->sys_fname);
455 return STREAM_UNIX_ATTRIBUTES;
458 p += to_base64((uint64_t)atts.dwFileAttributes, p);
459 *p++ = ' '; /* separate fields with a space */
460 li.LowPart = atts.ftCreationTime.dwLowDateTime;
461 li.HighPart = atts.ftCreationTime.dwHighDateTime;
462 p += to_base64((uint64_t)li.QuadPart, p);
464 li.LowPart = atts.ftLastAccessTime.dwLowDateTime;
465 li.HighPart = atts.ftLastAccessTime.dwHighDateTime;
466 p += to_base64((uint64_t)li.QuadPart, p);
468 li.LowPart = atts.ftLastWriteTime.dwLowDateTime;
469 li.HighPart = atts.ftLastWriteTime.dwHighDateTime;
470 p += to_base64((uint64_t)li.QuadPart, p);
472 p += to_base64((uint64_t)atts.nFileSizeHigh, p);
474 p += to_base64((uint64_t)atts.nFileSizeLow, p);
476 return STREAM_UNIX_ATTRIBUTES_EX;
479 /* Define attributes that are legal to set with SetFileAttributes() */
480 #define SET_ATTRS ( \
481 FILE_ATTRIBUTE_ARCHIVE| \
482 FILE_ATTRIBUTE_HIDDEN| \
483 FILE_ATTRIBUTE_NORMAL| \
484 FILE_ATTRIBUTE_NOT_CONTENT_INDEXED| \
485 FILE_ATTRIBUTE_OFFLINE| \
486 FILE_ATTRIBUTE_READONLY| \
487 FILE_ATTRIBUTE_SYSTEM| \
488 FILE_ATTRIBUTE_TEMPORARY)
492 * Set Extended File Attributes for Win32
494 * fname is the original filename
495 * ofile is the output filename (may be in a different directory)
497 * Returns: true on success
500 static bool set_win32_attributes(JCR *jcr, ATTR *attr, BFILE *ofd)
502 char *p = attr->attrEx;
504 WIN32_FILE_ATTRIBUTE_DATA atts;
506 POOLMEM *win32_ofile;
508 if (!p_GetFileAttributesEx) {
512 if (!p || !*p) { /* we should have attributes */
513 Dmsg2(100, "Attributes missing. of=%s ofd=%d\n", attr->ofname, ofd->fid);
519 Dmsg2(100, "Attribs %s = %s\n", attr->ofname, attr->attrEx);
522 p += from_base64(&val, p);
523 plug(atts.dwFileAttributes, val);
524 p++; /* skip space */
525 p += from_base64(&val, p);
527 atts.ftCreationTime.dwLowDateTime = li.LowPart;
528 atts.ftCreationTime.dwHighDateTime = li.HighPart;
529 p++; /* skip space */
530 p += from_base64(&val, p);
532 atts.ftLastAccessTime.dwLowDateTime = li.LowPart;
533 atts.ftLastAccessTime.dwHighDateTime = li.HighPart;
534 p++; /* skip space */
535 p += from_base64(&val, p);
537 atts.ftLastWriteTime.dwLowDateTime = li.LowPart;
538 atts.ftLastWriteTime.dwHighDateTime = li.HighPart;
540 p += from_base64(&val, p);
541 plug(atts.nFileSizeHigh, val);
543 p += from_base64(&val, p);
544 plug(atts.nFileSizeLow, val);
546 /* Convert to Windows path format */
547 win32_ofile = get_pool_memory(PM_FNAME);
548 unix_name_to_win32(&win32_ofile, attr->ofname);
552 /* At this point, we have reconstructed the WIN32_FILE_ATTRIBUTE_DATA pkt */
554 if (!is_bopen(ofd)) {
555 Dmsg1(100, "File not open: %s\n", attr->ofname);
556 bopen(ofd, attr->ofname, O_WRONLY|O_BINARY, 0); /* attempt to open the file */
560 Dmsg1(100, "SetFileTime %s\n", attr->ofname);
561 if (!SetFileTime(bget_handle(ofd),
562 &atts.ftCreationTime,
563 &atts.ftLastAccessTime,
564 &atts.ftLastWriteTime)) {
565 win_error(jcr, "SetFileTime:", win32_ofile);
570 Dmsg1(100, "SetFileAtts %s\n", attr->ofname);
571 if (!(atts.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) {
572 if (!SetFileAttributes(win32_ofile, atts.dwFileAttributes & SET_ATTRS)) {
573 win_error(jcr, "SetFileAttributes:", win32_ofile);
576 free_pool_memory(win32_ofile);
580 void win_error(JCR *jcr, char *prefix, POOLMEM *win32_ofile)
582 DWORD lerror = GetLastError();
584 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|
585 FORMAT_MESSAGE_FROM_SYSTEM,
592 Dmsg3(100, "Error in %s on file %s: ERR=%s\n", prefix, win32_ofile, msg);
593 strip_trailing_junk(msg);
594 Jmsg(jcr, M_ERROR, 0, _("Error in %s file %s: ERR=%s\n"), prefix, win32_ofile, msg);
598 void win_error(JCR *jcr, char *prefix, DWORD lerror)
601 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|
602 FORMAT_MESSAGE_FROM_SYSTEM,
609 strip_trailing_junk(msg);
611 Jmsg2(jcr, M_ERROR, 0, _("Error in %s: ERR=%s\n"), prefix, msg);
613 MessageBox(NULL, msg, prefix, MB_OK);
619 /* Cygwin API definition */
620 extern "C" void cygwin_conv_to_win32_path(const char *path, char *win32_path);
622 void unix_name_to_win32(POOLMEM **win32_name, char *name)
624 /* One extra byte should suffice, but we double it */
625 *win32_name = check_pool_memory_size(*win32_name, 2*strlen(name)+1);
626 cygwin_conv_to_win32_path(name, *win32_name);
629 #endif /* HAVE_CYGWIN */