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-2005 Kern Sibbald
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)
38 /* Forward referenced subroutines */
39 static bool set_win32_attributes(JCR *jcr, ATTR *attr, BFILE *ofd);
40 void unix_name_to_win32(POOLMEM **win32_name, char *name);
41 void win_error(JCR *jcr, char *prefix, POOLMEM *ofile);
42 HANDLE bget_handle(BFILE *bfd);
45 /* For old systems that don't have lchown() use chown() */
50 /*=============================================================*/
52 /* *** A l l S y s t e m s *** */
54 /*=============================================================*/
57 * Return the data stream that will be used
59 int select_data_stream(FF_PKT *ff_pkt)
63 /* Note, no sparse option for win32_data */
64 if (!is_portable_backup(&ff_pkt->bfd)) {
65 stream = STREAM_WIN32_DATA;
66 ff_pkt->flags &= ~FO_SPARSE;
67 } else if (ff_pkt->flags & FO_SPARSE) {
68 stream = STREAM_SPARSE_DATA;
70 stream = STREAM_FILE_DATA;
73 if (ff_pkt->flags & FO_GZIP) {
74 if (stream == STREAM_WIN32_DATA) {
75 stream = STREAM_WIN32_GZIP_DATA;
76 } else if (stream == STREAM_FILE_DATA) {
77 stream = STREAM_GZIP_DATA;
79 stream = STREAM_SPARSE_GZIP_DATA;
88 * Encode a stat structure into a base64 character string
89 * All systems must create such a structure.
90 * In addition, we tack on the LinkFI, which is non-zero in
91 * the case of a hard linked file that has no data. This
92 * is a File Index pointing to the link that does have the
93 * data (always the first one encountered in a save).
94 * You may piggyback attributes on this packet by encoding
95 * them in the encode_attribsEx() subroutine, but this is
98 void encode_stat(char *buf, FF_PKT *ff_pkt, int data_stream)
101 struct stat *statp = &ff_pkt->statp;
103 * Encode a stat packet. I should have done this more intelligently
104 * with a length so that it could be easily expanded.
106 p += to_base64((int64_t)statp->st_dev, p);
107 *p++ = ' '; /* separate fields with a space */
108 p += to_base64((int64_t)statp->st_ino, p);
110 p += to_base64((int64_t)statp->st_mode, p);
112 p += to_base64((int64_t)statp->st_nlink, p);
114 p += to_base64((int64_t)statp->st_uid, p);
116 p += to_base64((int64_t)statp->st_gid, p);
118 p += to_base64((int64_t)statp->st_rdev, p);
120 p += to_base64((int64_t)statp->st_size, p);
123 p += to_base64((int64_t)statp->st_blksize, p);
125 p += to_base64((int64_t)statp->st_blocks, p);
128 p += to_base64((int64_t)0, p); /* output place holder */
130 p += to_base64((int64_t)0, p); /* output place holder */
133 p += to_base64((int64_t)statp->st_atime, p);
135 p += to_base64((int64_t)statp->st_mtime, p);
137 p += to_base64((int64_t)statp->st_ctime, p);
139 p += to_base64((int64_t)ff_pkt->LinkFI, p);
143 /* FreeBSD function */
144 p += to_base64((int64_t)statp->st_flags, p); /* output st_flags */
146 p += to_base64((int64_t)0, p); /* output place holder */
149 p += to_base64((int64_t)data_stream, p);
155 /* Do casting according to unknown type to keep compiler happy */
156 #if !HAVE_GCC & HAVE_SUN_OS
157 #define plug(st, val) st = val /* brain damaged compiler */
159 template <class T> void plug(T &st, uint64_t val)
160 { st = static_cast<T>(val); }
164 /* Decode a stat packet from base64 characters */
165 int decode_stat(char *buf, struct stat *statp, int32_t *LinkFI)
170 p += from_base64(&val, p);
171 plug(statp->st_dev, val);
173 p += from_base64(&val, p);
174 plug(statp->st_ino, val);
176 p += from_base64(&val, p);
177 plug(statp->st_mode, val);
179 p += from_base64(&val, p);
180 plug(statp->st_nlink, val);
182 p += from_base64(&val, p);
183 plug(statp->st_uid, val);
185 p += from_base64(&val, p);
186 plug(statp->st_gid, val);
188 p += from_base64(&val, p);
189 plug(statp->st_rdev, val);
191 p += from_base64(&val, p);
192 plug(statp->st_size, val);
195 p += from_base64(&val, p);
196 plug(statp->st_blksize, val);
198 p += from_base64(&val, p);
199 plug(statp->st_blocks, val);
202 p += from_base64(&val, p);
203 // plug(statp->st_blksize, val);
205 p += from_base64(&val, p);
206 // plug(statp->st_blocks, val);
209 p += from_base64(&val, p);
210 plug(statp->st_atime, val);
212 p += from_base64(&val, p);
213 plug(statp->st_mtime, val);
215 p += from_base64(&val, p);
216 plug(statp->st_ctime, val);
218 /* Optional FileIndex of hard linked file data */
219 if (*p == ' ' || (*p != 0 && *(p+1) == ' ')) {
221 p += from_base64(&val, p);
222 *LinkFI = (uint32_t)val;
228 /* FreeBSD user flags */
229 if (*p == ' ' || (*p != 0 && *(p+1) == ' ')) {
231 p += from_base64(&val, p);
233 plug(statp->st_flags, val);
239 /* Look for data stream id */
240 if (*p == ' ' || (*p != 0 && *(p+1) == ' ')) {
242 p += from_base64(&val, p);
249 /* Decode a LinkFI field of encoded stat packet */
250 int32_t decode_LinkFI(char *buf, struct stat *statp)
255 skip_nonspaces(&p); /* st_dev */
256 p++; /* skip space */
257 skip_nonspaces(&p); /* st_ino */
259 p += from_base64(&val, p);
260 plug(statp->st_mode, val); /* st_mode */
262 skip_nonspaces(&p); /* st_nlink */
264 skip_nonspaces(&p); /* st_uid */
266 skip_nonspaces(&p); /* st_gid */
268 skip_nonspaces(&p); /* st_rdev */
270 skip_nonspaces(&p); /* st_size */
272 skip_nonspaces(&p); /* st_blksize */
274 skip_nonspaces(&p); /* st_blocks */
276 skip_nonspaces(&p); /* st_atime */
278 skip_nonspaces(&p); /* st_mtime */
280 skip_nonspaces(&p); /* st_ctime */
282 /* Optional FileIndex of hard linked file data */
283 if (*p == ' ' || (*p != 0 && *(p+1) == ' ')) {
285 p += from_base64(&val, p);
292 * Set file modes, permissions and times
294 * fname is the original filename
295 * ofile is the output filename (may be in a different directory)
297 * Returns: true on success
300 bool set_attributes(JCR *jcr, ATTR *attr, BFILE *ofd)
307 #if defined(HAVE_CYGWIN) || defined(HAVE_WIN32)
308 if (attr->stream == STREAM_UNIX_ATTRIBUTES_EX &&
309 set_win32_attributes(jcr, attr, ofd)) {
313 pm_strcpy(attr->ofname, "*none*");
316 if (attr->data_stream == STREAM_WIN32_DATA ||
317 attr->data_stream == STREAM_WIN32_GZIP_DATA) {
321 pm_strcpy(attr->ofname, "*none*");
327 * If Windows stuff failed, e.g. attempt to restore Unix file
328 * to Windows, simply fall through and we will do it the
335 char ec1[50], ec2[50];
336 fsize = blseek(ofd, 0, SEEK_CUR);
337 bclose(ofd); /* first close file */
338 if (fsize > 0 && fsize != (off_t)attr->statp.st_size) {
339 Jmsg3(jcr, M_ERROR, 0, _("File size of restored file %s not correct. Original %s, restored %s.\n"),
340 attr->ofname, edit_uint64(attr->statp.st_size, ec1),
341 edit_uint64(fsize, ec2));
345 ut.actime = attr->statp.st_atime;
346 ut.modtime = attr->statp.st_mtime;
348 /* ***FIXME**** optimize -- don't do if already correct */
350 * For link, change owner of link using lchown, but don't
351 * try to do a chmod as that will update the file behind it.
353 if (attr->type == FT_LNK) {
354 /* Change owner of link, not of real file */
355 if (lchown(attr->ofname, attr->statp.st_uid, attr->statp.st_gid) < 0) {
357 Jmsg2(jcr, M_ERROR, 0, _("Unable to set file owner %s: ERR=%s\n"),
358 attr->ofname, be.strerror());
362 if (chown(attr->ofname, attr->statp.st_uid, attr->statp.st_gid) < 0) {
364 Jmsg2(jcr, M_ERROR, 0, _("Unable to set file owner %s: ERR=%s\n"),
365 attr->ofname, be.strerror());
368 if (chmod(attr->ofname, attr->statp.st_mode) < 0) {
370 Jmsg2(jcr, M_ERROR, 0, _("Unable to set file modes %s: ERR=%s\n"),
371 attr->ofname, be.strerror());
378 if (utime(attr->ofname, &ut) < 0) {
380 Jmsg2(jcr, M_ERROR, 0, _("Unable to set file times %s: ERR=%s\n"),
381 attr->ofname, be.strerror());
388 * Note, this should really be done before the utime() above,
389 * but if the immutable bit is set, it will make the utimes()
392 if (chflags(attr->ofname, attr->statp.st_flags) < 0) {
394 Jmsg2(jcr, M_ERROR, 0, _("Unable to set file flags %s: ERR=%s\n"),
395 attr->ofname, be.strerror());
400 pm_strcpy(attr->ofname, "*none*");
406 /*=============================================================*/
408 /* * * * U n i x * * * * */
410 /*=============================================================*/
412 #if !defined(HAVE_CYGWIN) && !defined(HAVE_WIN32)
415 * It is possible to piggyback additional data e.g. ACLs on
416 * the encode_stat() data by returning the extended attributes
417 * here. They must be "self-contained" (i.e. you keep track
418 * of your own length), and they must be in ASCII string
419 * format. Using this feature is not recommended.
420 * The code below shows how to return nothing. See the Win32
421 * code below for returning something in the attributes.
423 int encode_attribsEx(JCR *jcr, char *attribsEx, FF_PKT *ff_pkt)
425 #ifdef HAVE_DARWIN_OS
427 * We save the Mac resource fork length so that on a
428 * restore, we can be sure we put back the whole resource.
432 if (ff_pkt->flags & FO_HFSPLUS) {
433 p += to_base64((uint64_t)(ff_pkt->hfsinfo.rsrclength), p);
437 *attribsEx = 0; /* no extended attributes */
439 return STREAM_UNIX_ATTRIBUTES;
446 /*=============================================================*/
448 /* * * * W i n 3 2 * * * * */
450 /*=============================================================*/
452 #if defined(HAVE_CYGWIN) || defined(HAVE_WIN32)
454 int encode_attribsEx(JCR *jcr, char *attribsEx, FF_PKT *ff_pkt)
457 WIN32_FILE_ATTRIBUTE_DATA atts;
460 attribsEx[0] = 0; /* no extended attributes */
462 #if USE_WIN32_UNICODE
463 if (!p_GetFileAttributesExW)
464 return STREAM_UNIX_ATTRIBUTES;
466 unix_name_to_win32(&ff_pkt->sys_fname, ff_pkt->fname);
468 WCHAR szBuf[MAX_PATH_UNICODE];
469 UTF8_2_wchar(szBuf, ff_pkt->sys_fname, MAX_PATH_UNICODE);
471 if (!p_GetFileAttributesExW(szBuf, GetFileExInfoStandard,
473 win_error(jcr, "GetFileAttributesExW:", ff_pkt->sys_fname);
474 return STREAM_UNIX_ATTRIBUTES;
477 if (!p_GetFileAttributesExA)
478 return STREAM_UNIX_ATTRIBUTES;
480 unix_name_to_win32(&ff_pkt->sys_fname, ff_pkt->fname);
482 if (!p_GetFileAttributesExA(ff_pkt->sys_fname, GetFileExInfoStandard,
484 win_error(jcr, "GetFileAttributesExA:", ff_pkt->sys_fname);
485 return STREAM_UNIX_ATTRIBUTES;
489 p += to_base64((uint64_t)atts.dwFileAttributes, p);
490 *p++ = ' '; /* separate fields with a space */
491 li.LowPart = atts.ftCreationTime.dwLowDateTime;
492 li.HighPart = atts.ftCreationTime.dwHighDateTime;
493 p += to_base64((uint64_t)li.QuadPart, p);
495 li.LowPart = atts.ftLastAccessTime.dwLowDateTime;
496 li.HighPart = atts.ftLastAccessTime.dwHighDateTime;
497 p += to_base64((uint64_t)li.QuadPart, p);
499 li.LowPart = atts.ftLastWriteTime.dwLowDateTime;
500 li.HighPart = atts.ftLastWriteTime.dwHighDateTime;
501 p += to_base64((uint64_t)li.QuadPart, p);
503 p += to_base64((uint64_t)atts.nFileSizeHigh, p);
505 p += to_base64((uint64_t)atts.nFileSizeLow, p);
507 return STREAM_UNIX_ATTRIBUTES_EX;
510 /* Define attributes that are legal to set with SetFileAttributes() */
511 #define SET_ATTRS ( \
512 FILE_ATTRIBUTE_ARCHIVE| \
513 FILE_ATTRIBUTE_HIDDEN| \
514 FILE_ATTRIBUTE_NORMAL| \
515 FILE_ATTRIBUTE_NOT_CONTENT_INDEXED| \
516 FILE_ATTRIBUTE_OFFLINE| \
517 FILE_ATTRIBUTE_READONLY| \
518 FILE_ATTRIBUTE_SYSTEM| \
519 FILE_ATTRIBUTE_TEMPORARY)
523 * Set Extended File Attributes for Win32
525 * fname is the original filename
526 * ofile is the output filename (may be in a different directory)
528 * Returns: true on success
531 static bool set_win32_attributes(JCR *jcr, ATTR *attr, BFILE *ofd)
533 char *p = attr->attrEx;
535 WIN32_FILE_ATTRIBUTE_DATA atts;
537 POOLMEM *win32_ofile;
539 #if USE_WIN32_UNICODE
540 if (!p_GetFileAttributesExW)
543 if (!p_GetFileAttributesExA)
548 if (!p || !*p) { /* we should have attributes */
549 Dmsg2(100, "Attributes missing. of=%s ofd=%d\n", attr->ofname, ofd->fid);
555 Dmsg2(100, "Attribs %s = %s\n", attr->ofname, attr->attrEx);
558 p += from_base64(&val, p);
559 plug(atts.dwFileAttributes, val);
560 p++; /* skip space */
561 p += from_base64(&val, p);
563 atts.ftCreationTime.dwLowDateTime = li.LowPart;
564 atts.ftCreationTime.dwHighDateTime = li.HighPart;
565 p++; /* skip space */
566 p += from_base64(&val, p);
568 atts.ftLastAccessTime.dwLowDateTime = li.LowPart;
569 atts.ftLastAccessTime.dwHighDateTime = li.HighPart;
570 p++; /* skip space */
571 p += from_base64(&val, p);
573 atts.ftLastWriteTime.dwLowDateTime = li.LowPart;
574 atts.ftLastWriteTime.dwHighDateTime = li.HighPart;
576 p += from_base64(&val, p);
577 plug(atts.nFileSizeHigh, val);
579 p += from_base64(&val, p);
580 plug(atts.nFileSizeLow, val);
582 /* Convert to Windows path format */
583 win32_ofile = get_pool_memory(PM_FNAME);
584 unix_name_to_win32(&win32_ofile, attr->ofname);
588 /* At this point, we have reconstructed the WIN32_FILE_ATTRIBUTE_DATA pkt */
590 if (!is_bopen(ofd)) {
591 Dmsg1(100, "File not open: %s\n", attr->ofname);
592 bopen(ofd, attr->ofname, O_WRONLY|O_BINARY, 0); /* attempt to open the file */
596 Dmsg1(100, "SetFileTime %s\n", attr->ofname);
597 if (!SetFileTime(bget_handle(ofd),
598 &atts.ftCreationTime,
599 &atts.ftLastAccessTime,
600 &atts.ftLastWriteTime)) {
601 win_error(jcr, "SetFileTime:", win32_ofile);
606 Dmsg1(100, "SetFileAtts %s\n", attr->ofname);
607 if (!(atts.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
609 #if USE_WIN32_UNICODE
610 WCHAR szBuf[MAX_PATH_UNICODE];
611 UTF8_2_wchar(szBuf, win32_ofile, MAX_PATH_UNICODE);
613 if (!SetFileAttributesW(szBuf, atts.dwFileAttributes & SET_ATTRS)) {
614 win_error(jcr, "SetFileAttributesW:", win32_ofile);
616 if (!SetFileAttributes(win32_ofile, atts.dwFileAttributes & SET_ATTRS)) {
617 win_error(jcr, "SetFileAttributesA:", win32_ofile);
621 free_pool_memory(win32_ofile);
625 void win_error(JCR *jcr, char *prefix, POOLMEM *win32_ofile)
627 DWORD lerror = GetLastError();
629 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|
630 FORMAT_MESSAGE_FROM_SYSTEM,
637 Dmsg3(100, "Error in %s on file %s: ERR=%s\n", prefix, win32_ofile, msg);
638 strip_trailing_junk(msg);
639 Jmsg(jcr, M_ERROR, 0, _("Error in %s file %s: ERR=%s\n"), prefix, win32_ofile, msg);
643 void win_error(JCR *jcr, char *prefix, DWORD lerror)
646 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|
647 FORMAT_MESSAGE_FROM_SYSTEM,
654 strip_trailing_junk(msg);
656 Jmsg2(jcr, M_ERROR, 0, _("Error in %s: ERR=%s\n"), prefix, msg);
658 MessageBox(NULL, msg, prefix, MB_OK);
664 /* Cygwin API definition */
665 extern "C" void cygwin_conv_to_win32_path(const char *path, char *win32_path);
667 void unix_name_to_win32(POOLMEM **win32_name, char *name)
669 /* One extra byte should suffice, but we double it */
670 *win32_name = check_pool_memory_size(*win32_name, 2*strlen(name)+1);
671 cygwin_conv_to_win32_path(name, *win32_name);
674 #endif /* HAVE_CYGWIN */