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) 2000-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 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);
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);
120 p += to_base64((int64_t)statp->st_blksize, p);
122 p += to_base64((int64_t)statp->st_blocks, p);
124 p += to_base64((int64_t)statp->st_atime, p);
126 p += to_base64((int64_t)statp->st_mtime, p);
128 p += to_base64((int64_t)statp->st_ctime, p);
130 p += to_base64((int64_t)ff_pkt->LinkFI, p);
134 /* FreeBSD function */
135 p += to_base64((int64_t)statp->st_flags, p); /* output st_flags */
137 p += to_base64((int64_t)0, p); /* output place holder */
140 p += to_base64((int64_t)data_stream, p);
147 /* Decode a stat packet from base64 characters */
148 int decode_stat(char *buf, struct stat *statp, int32_t *LinkFI)
153 p += from_base64(&val, p);
155 p++; /* skip space */
156 p += from_base64(&val, p);
159 p += from_base64(&val, p);
160 statp->st_mode = val;
162 p += from_base64(&val, p);
163 statp->st_nlink = val;
165 p += from_base64(&val, p);
168 p += from_base64(&val, p);
171 p += from_base64(&val, p);
172 statp->st_rdev = val;
174 p += from_base64(&val, p);
175 statp->st_size = val;
177 p += from_base64(&val, p);
178 statp->st_blksize = val;
180 p += from_base64(&val, p);
181 statp->st_blocks = val;
183 p += from_base64(&val, p);
184 statp->st_atime = val;
186 p += from_base64(&val, p);
187 statp->st_mtime = val;
189 p += from_base64(&val, p);
190 statp->st_ctime = val;
192 /* Optional FileIndex of hard linked file data */
193 if (*p == ' ' || (*p != 0 && *(p+1) == ' ')) {
195 p += from_base64(&val, p);
196 *LinkFI = (uint32_t)val;
202 /* FreeBSD user flags */
203 if (*p == ' ' || (*p != 0 && *(p+1) == ' ')) {
205 p += from_base64(&val, p);
207 statp->st_flags = (uint32_t)val;
213 /* Look for data stream id */
214 if (*p == ' ' || (*p != 0 && *(p+1) == ' ')) {
216 p += from_base64(&val, p);
223 /* Decode a LinkFI field of encoded stat packet */
224 int32_t decode_LinkFI(char *buf, struct stat *statp)
229 skip_nonspaces(&p); /* st_dev */
230 p++; /* skip space */
231 skip_nonspaces(&p); /* st_ino */
233 p += from_base64(&val, p);
234 statp->st_mode = val; /* st_mode */
236 skip_nonspaces(&p); /* st_nlink */
238 skip_nonspaces(&p); /* st_uid */
240 skip_nonspaces(&p); /* st_gid */
242 skip_nonspaces(&p); /* st_rdev */
244 skip_nonspaces(&p); /* st_size */
246 skip_nonspaces(&p); /* st_blksize */
248 skip_nonspaces(&p); /* st_blocks */
250 skip_nonspaces(&p); /* st_atime */
252 skip_nonspaces(&p); /* st_mtime */
254 skip_nonspaces(&p); /* st_ctime */
256 /* Optional FileIndex of hard linked file data */
257 if (*p == ' ' || (*p != 0 && *(p+1) == ' ')) {
259 p += from_base64(&val, p);
266 * Set file modes, permissions and times
268 * fname is the original filename
269 * ofile is the output filename (may be in a different directory)
271 * Returns: 1 on success
274 int set_attributes(JCR *jcr, ATTR *attr, BFILE *ofd)
280 #if defined(HAVE_CYGWIN) || defined(HAVE_WIN32)
281 if (attr->data_stream == STREAM_WIN32_DATA ||
282 attr->data_stream == STREAM_WIN32_GZIP_DATA) {
286 pm_strcpy(&attr->ofname, "*none*");
290 if (attr->stream == STREAM_UNIX_ATTRIBUTES_EX &&
291 set_win32_attributes(jcr, attr, ofd)) {
295 pm_strcpy(&attr->ofname, "*none*");
299 * If Windows stuff failed, e.g. attempt to restore Unix file
300 * to Windows, simply fall through and we will do it the
307 bclose(ofd); /* first close file */
310 ut.actime = attr->statp.st_atime;
311 ut.modtime = attr->statp.st_mtime;
313 /* ***FIXME**** optimize -- don't do if already correct */
315 * For link, change owner of link using lchown, but don't
316 * try to do a chmod as that will update the file behind it.
318 if (attr->type == FT_LNK) {
319 /* Change owner of link, not of real file */
320 if (lchown(attr->ofname, attr->statp.st_uid, attr->statp.st_gid) < 0) {
321 Jmsg2(jcr, M_ERROR, 0, _("Unable to set file owner %s: ERR=%s\n"),
322 attr->ofname, strerror(errno));
326 if (chown(attr->ofname, attr->statp.st_uid, attr->statp.st_gid) < 0) {
327 Jmsg2(jcr, M_ERROR, 0, _("Unable to set file owner %s: ERR=%s\n"),
328 attr->ofname, strerror(errno));
331 if (chmod(attr->ofname, attr->statp.st_mode) < 0) {
332 Jmsg2(jcr, M_ERROR, 0, _("Unable to set file modes %s: ERR=%s\n"),
333 attr->ofname, strerror(errno));
337 /* FreeBSD user flags */
339 if (chflags(attr->ofname, attr->statp.st_flags) < 0) {
340 Jmsg2(jcr, M_ERROR, 0, _("Unable to set file flags %s: ERR=%s\n"),
341 attr->ofname, strerror(errno));
348 if (utime(attr->ofname, &ut) < 0) {
349 Jmsg2(jcr, M_ERROR, 0, _("Unable to set file times %s: ERR=%s\n"),
350 attr->ofname, strerror(errno));
354 pm_strcpy(&attr->ofname, "*none*");
360 /*=============================================================*/
362 /* * * * U n i x * * * * */
364 /*=============================================================*/
366 #if !defined(HAVE_CYGWIN) && !defined(HAVE_WIN32)
369 * It is possible to piggyback additional data e.g. ACLs on
370 * the encode_stat() data by returning the extended attributes
371 * here. They must be "self-contained" (i.e. you keep track
372 * of your own length), and they must be in ASCII string
373 * format. Using this feature is not recommended.
374 * The code below shows how to return nothing. See the Win32
375 * code below for returning something in the attributes.
377 int encode_attribsEx(JCR *jcr, char *attribsEx, FF_PKT *ff_pkt)
379 *attribsEx = 0; /* no extended attributes */
380 return STREAM_UNIX_ATTRIBUTES;
387 /*=============================================================*/
389 /* * * * W i n 3 2 * * * * */
391 /*=============================================================*/
393 #if defined(HAVE_CYGWIN) || defined(HAVE_WIN32)
395 int encode_attribsEx(JCR *jcr, char *attribsEx, FF_PKT *ff_pkt)
398 WIN32_FILE_ATTRIBUTE_DATA atts;
401 attribsEx[0] = 0; /* no extended attributes */
403 if (!p_GetFileAttributesEx) {
404 return STREAM_UNIX_ATTRIBUTES;
407 unix_name_to_win32(&ff_pkt->sys_fname, ff_pkt->fname);
408 if (!p_GetFileAttributesEx(ff_pkt->sys_fname, GetFileExInfoStandard,
410 win_error(jcr, "GetFileAttributesEx:", ff_pkt->sys_fname);
411 return STREAM_UNIX_ATTRIBUTES;
414 p += to_base64((uint64_t)atts.dwFileAttributes, p);
415 *p++ = ' '; /* separate fields with a space */
416 li.LowPart = atts.ftCreationTime.dwLowDateTime;
417 li.HighPart = atts.ftCreationTime.dwHighDateTime;
418 p += to_base64((uint64_t)li.QuadPart, p);
420 li.LowPart = atts.ftLastAccessTime.dwLowDateTime;
421 li.HighPart = atts.ftLastAccessTime.dwHighDateTime;
422 p += to_base64((uint64_t)li.QuadPart, p);
424 li.LowPart = atts.ftLastWriteTime.dwLowDateTime;
425 li.HighPart = atts.ftLastWriteTime.dwHighDateTime;
426 p += to_base64((uint64_t)li.QuadPart, p);
428 p += to_base64((uint64_t)atts.nFileSizeHigh, p);
430 p += to_base64((uint64_t)atts.nFileSizeLow, p);
432 return STREAM_UNIX_ATTRIBUTES_EX;
435 /* Define attributes that are legal to set with SetFileAttributes() */
436 #define SET_ATTRS ( \
437 FILE_ATTRIBUTE_ARCHIVE| \
438 FILE_ATTRIBUTE_HIDDEN| \
439 FILE_ATTRIBUTE_NORMAL| \
440 FILE_ATTRIBUTE_NOT_CONTENT_INDEXED| \
441 FILE_ATTRIBUTE_OFFLINE| \
442 FILE_ATTRIBUTE_READONLY| \
443 FILE_ATTRIBUTE_SYSTEM| \
444 FILE_ATTRIBUTE_TEMPORARY)
448 * Set Extended File Attributes for Win32
450 * fname is the original filename
451 * ofile is the output filename (may be in a different directory)
453 * Returns: 1 on success
456 static int set_win32_attributes(JCR *jcr, ATTR *attr, BFILE *ofd)
458 char *p = attr->attrEx;
460 WIN32_FILE_ATTRIBUTE_DATA atts;
462 POOLMEM *win32_ofile;
464 if (!p_GetFileAttributesEx) {
468 if (!p || !*p) { /* we should have attributes */
469 Dmsg2(100, "Attributes missing. of=%s ofd=%d\n", attr->ofname, ofd->fid);
475 Dmsg2(100, "Attribs %s = %s\n", attr->ofname, attr->attrEx);
478 p += from_base64(&val, p);
479 atts.dwFileAttributes = val;
480 p++; /* skip space */
481 p += from_base64(&val, p);
483 atts.ftCreationTime.dwLowDateTime = li.LowPart;
484 atts.ftCreationTime.dwHighDateTime = li.HighPart;
485 p++; /* skip space */
486 p += from_base64(&val, p);
488 atts.ftLastAccessTime.dwLowDateTime = li.LowPart;
489 atts.ftLastAccessTime.dwHighDateTime = li.HighPart;
490 p++; /* skip space */
491 p += from_base64(&val, p);
493 atts.ftLastWriteTime.dwLowDateTime = li.LowPart;
494 atts.ftLastWriteTime.dwHighDateTime = li.HighPart;
496 p += from_base64(&val, p);
497 atts.nFileSizeHigh = val;
499 p += from_base64(&val, p);
500 atts.nFileSizeLow = val;
502 /* Convert to Windows path format */
503 win32_ofile = get_pool_memory(PM_FNAME);
504 unix_name_to_win32(&win32_ofile, attr->ofname);
508 /* At this point, we have reconstructed the WIN32_FILE_ATTRIBUTE_DATA pkt */
510 if (!is_bopen(ofd)) {
511 Dmsg1(100, "File not open: %s\n", attr->ofname);
512 bopen(ofd, attr->ofname, O_WRONLY|O_BINARY, 0); /* attempt to open the file */
516 Dmsg1(100, "SetFileTime %s\n", attr->ofname);
517 if (!SetFileTime(bget_handle(ofd),
518 &atts.ftCreationTime,
519 &atts.ftLastAccessTime,
520 &atts.ftLastWriteTime)) {
521 win_error(jcr, "SetFileTime:", win32_ofile);
526 Dmsg1(100, "SetFileAtts %s\n", attr->ofname);
527 if (!(atts.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) {
528 if (!SetFileAttributes(win32_ofile, atts.dwFileAttributes & SET_ATTRS)) {
529 win_error(jcr, "SetFileAttributes:", win32_ofile);
532 free_pool_memory(win32_ofile);
536 void win_error(JCR *jcr, char *prefix, POOLMEM *win32_ofile)
538 DWORD lerror = GetLastError();
540 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|
541 FORMAT_MESSAGE_FROM_SYSTEM,
548 Dmsg3(100, "Error in %s on file %s: ERR=%s\n", prefix, win32_ofile, msg);
549 strip_trailing_junk(msg);
550 Jmsg(jcr, M_ERROR, 0, _("Error in %s file %s: ERR=%s\n"), prefix, win32_ofile, msg);
554 void win_error(JCR *jcr, char *prefix, DWORD lerror)
557 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|
558 FORMAT_MESSAGE_FROM_SYSTEM,
565 strip_trailing_junk(msg);
567 Jmsg2(jcr, M_ERROR, 0, _("Error in %s: ERR=%s\n"), prefix, msg);
569 MessageBox(NULL, msg, prefix, MB_OK);
575 /* Cygwin API definition */
576 extern "C" void cygwin_conv_to_win32_path(const char *path, char *win32_path);
578 void unix_name_to_win32(POOLMEM **win32_name, char *name)
580 /* One extra byte should suffice, but we double it */
581 *win32_name = check_pool_memory_size(*win32_name, 2*strlen(name)+1);
582 cygwin_conv_to_win32_path(name, *win32_name);
585 #endif /* HAVE_CYGWIN */