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-2003 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,
36 /* Forward referenced subroutines */
38 int set_win32_attributes(JCR *jcr, ATTR *attr, BFILE *ofd);
39 void unix_name_to_win32(POOLMEM **win32_name, char *name);
40 void win_error(JCR *jcr, char *prefix, POOLMEM *ofile);
41 HANDLE bget_handle(BFILE *bfd);
44 /* For old systems that don't have lchown() use chown() */
49 /*=============================================================*/
51 /* *** A l l S y s t e m s *** */
53 /*=============================================================*/
56 * Return the data stream that will be used
58 int select_data_stream(FF_PKT *ff_pkt)
62 /* Note, no sparse option for win32_data */
63 if (is_win32_backup()) {
64 stream = STREAM_WIN32_DATA;
65 ff_pkt->flags &= ~FO_SPARSE;
66 } else if (ff_pkt->flags & FO_SPARSE) {
67 stream = STREAM_SPARSE_DATA;
69 stream = STREAM_FILE_DATA;
72 if (ff_pkt->flags & FO_GZIP) {
73 if (stream == STREAM_WIN32_DATA) {
74 stream = STREAM_WIN32_GZIP_DATA;
75 } else if (stream == STREAM_FILE_DATA) {
76 stream = STREAM_GZIP_DATA;
78 stream = STREAM_SPARSE_GZIP_DATA;
87 * Encode a stat structure into a base64 character string
88 * All systems must create such a structure.
89 * In addition, we tack on the LinkFI, which is non-zero in
90 * the case of a hard linked file that has no data. This
91 * is a File Index pointing to the link that does have the
92 * data (always the first one encountered in a save).
93 * You may piggyback attributes on this packet by encoding
94 * them in the encode_attribsEx() subroutine, but this is
97 void encode_stat(char *buf, FF_PKT *ff_pkt, int data_stream)
100 struct stat *statp = &ff_pkt->statp;
102 * Encode a stat packet. I should have done this more intelligently
103 * with a length so that it could be easily expanded.
105 p += to_base64((int64_t)statp->st_dev, p);
106 *p++ = ' '; /* separate fields with a space */
107 p += to_base64((int64_t)statp->st_ino, p);
109 p += to_base64((int64_t)statp->st_mode, p);
111 p += to_base64((int64_t)statp->st_nlink, p);
113 p += to_base64((int64_t)statp->st_uid, p);
115 p += to_base64((int64_t)statp->st_gid, p);
117 p += to_base64((int64_t)statp->st_rdev, p);
119 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);
125 p += to_base64((int64_t)statp->st_atime, p);
127 p += to_base64((int64_t)statp->st_mtime, p);
129 p += to_base64((int64_t)statp->st_ctime, p);
131 p += to_base64((int64_t)ff_pkt->LinkFI, p);
135 /* FreeBSD function */
136 p += to_base64((int64_t)statp->st_flags, p); /* output st_flags */
138 p += to_base64((int64_t)0, p); /* output place holder */
141 p += to_base64((int64_t)data_stream, p);
148 /* Decode a stat packet from base64 characters */
149 int decode_stat(char *buf, struct stat *statp, uint32_t *LinkFI)
154 p += from_base64(&val, p);
156 p++; /* skip space */
157 p += from_base64(&val, p);
160 p += from_base64(&val, p);
161 statp->st_mode = val;
163 p += from_base64(&val, p);
164 statp->st_nlink = val;
166 p += from_base64(&val, p);
169 p += from_base64(&val, p);
172 p += from_base64(&val, p);
173 statp->st_rdev = val;
175 p += from_base64(&val, p);
176 statp->st_size = val;
178 p += from_base64(&val, p);
179 statp->st_blksize = val;
181 p += from_base64(&val, p);
182 statp->st_blocks = val;
184 p += from_base64(&val, p);
185 statp->st_atime = val;
187 p += from_base64(&val, p);
188 statp->st_mtime = val;
190 p += from_base64(&val, p);
191 statp->st_ctime = val;
193 /* Optional FileIndex of hard linked file data */
194 if (*p == ' ' || (*p != 0 && *(p+1) == ' ')) {
196 p += from_base64(&val, p);
197 *LinkFI = (uint32_t)val;
203 /* FreeBSD user flags */
204 if (*p == ' ' || (*p != 0 && *(p+1) == ' ')) {
206 p += from_base64(&val, p);
208 statp->st_flags = (uint32_t)val;
214 /* Look for data stream id */
215 if (*p == ' ' || (*p != 0 && *(p+1) == ' ')) {
217 p += from_base64(&val, p);
225 * Set file modes, permissions and times
227 * fname is the original filename
228 * ofile is the output filename (may be in a different directory)
230 * Returns: 1 on success
233 int set_attributes(JCR *jcr, ATTR *attr, BFILE *ofd)
240 if (attr->stream == STREAM_UNIX_ATTRIBUTES_EX &&
241 set_win32_attributes(jcr, attr, ofd)) {
245 * If Windows stuff failed, e.g. attempt to restore Unix file
246 * to Windows, simply fall through and we will do it the
253 bclose(ofd); /* first close file */
256 ut.actime = attr->statp.st_atime;
257 ut.modtime = attr->statp.st_mtime;
259 /* ***FIXME**** optimize -- don't do if already correct */
261 * For link, change owner of link using lchown, but don't
262 * try to do a chmod as that will update the file behind it.
264 if (attr->type == FT_LNK) {
265 /* Change owner of link, not of real file */
266 if (lchown(attr->ofname, attr->statp.st_uid, attr->statp.st_gid) < 0) {
267 Jmsg2(jcr, M_WARNING, 0, _("Unable to set file owner %s: ERR=%s\n"),
268 attr->ofname, strerror(errno));
272 if (chown(attr->ofname, attr->statp.st_uid, attr->statp.st_gid) < 0) {
273 Jmsg2(jcr, M_WARNING, 0, _("Unable to set file owner %s: ERR=%s\n"),
274 attr->ofname, strerror(errno));
277 if (chmod(attr->ofname, attr->statp.st_mode) < 0) {
278 Jmsg2(jcr, M_WARNING, 0, _("Unable to set file modes %s: ERR=%s\n"),
279 attr->ofname, strerror(errno));
283 /* FreeBSD user flags */
285 if (chflags(attr->ofname, attr->statp.st_flags) < 0) {
286 Jmsg2(jcr, M_WARNING, 0, _("Unable to set file flags %s: ERR=%s\n"),
287 attr->ofname, strerror(errno));
294 if (utime(attr->ofname, &ut) < 0) {
295 Jmsg2(jcr, M_ERROR, 0, _("Unable to set file times %s: ERR=%s\n"),
296 attr->ofname, strerror(errno));
305 /*=============================================================*/
307 /* * * * U n i x * * * * */
309 /*=============================================================*/
314 * It is possible to piggyback additional data e.g. ACLs on
315 * the encode_stat() data by returning the extended attributes
316 * here. They must be "self-contained" (i.e. you keep track
317 * of your own length), and they must be in ASCII string
318 * format. Using this feature is not recommended.
319 * The code below shows how to return nothing. See the Win32
320 * code below for returning something in the attributes.
322 int encode_attribsEx(JCR *jcr, char *attribsEx, FF_PKT *ff_pkt)
324 *attribsEx = 0; /* no extended attributes */
325 return STREAM_UNIX_ATTRIBUTES;
332 /*=============================================================*/
334 /* * * * W i n 3 2 * * * * */
336 /*=============================================================*/
340 int encode_attribsEx(JCR *jcr, char *attribsEx, FF_PKT *ff_pkt)
343 WIN32_FILE_ATTRIBUTE_DATA atts;
346 attribsEx[0] = 0; /* no extended attributes */
348 if (!p_GetFileAttributesEx) {
349 return STREAM_UNIX_ATTRIBUTES;
352 unix_name_to_win32(&ff_pkt->sys_fname, ff_pkt->fname);
353 if (!p_GetFileAttributesEx(ff_pkt->sys_fname, GetFileExInfoStandard,
355 win_error(jcr, "GetFileAttributesEx:", ff_pkt->sys_fname);
356 return STREAM_UNIX_ATTRIBUTES;
359 p += to_base64((uint64_t)atts.dwFileAttributes, p);
360 *p++ = ' '; /* separate fields with a space */
361 li.LowPart = atts.ftCreationTime.dwLowDateTime;
362 li.HighPart = atts.ftCreationTime.dwHighDateTime;
363 p += to_base64((uint64_t)li.QuadPart, p);
365 li.LowPart = atts.ftLastAccessTime.dwLowDateTime;
366 li.HighPart = atts.ftLastAccessTime.dwHighDateTime;
367 p += to_base64((uint64_t)li.QuadPart, p);
369 li.LowPart = atts.ftLastWriteTime.dwLowDateTime;
370 li.HighPart = atts.ftLastWriteTime.dwHighDateTime;
371 p += to_base64((uint64_t)li.QuadPart, p);
373 p += to_base64((uint64_t)atts.nFileSizeHigh, p);
375 p += to_base64((uint64_t)atts.nFileSizeLow, p);
377 return STREAM_UNIX_ATTRIBUTES_EX;
380 /* Define attributes that are legal to set with SetFileAttributes() */
381 #define SET_ATTRS ( \
382 FILE_ATTRIBUTE_ARCHIVE| \
383 FILE_ATTRIBUTE_HIDDEN| \
384 FILE_ATTRIBUTE_NORMAL| \
385 FILE_ATTRIBUTE_NOT_CONTENT_INDEXED| \
386 FILE_ATTRIBUTE_OFFLINE| \
387 FILE_ATTRIBUTE_READONLY| \
388 FILE_ATTRIBUTE_SYSTEM| \
389 FILE_ATTRIBUTE_TEMPORARY)
393 * Set Extended File Attributes for Win32
395 * fname is the original filename
396 * ofile is the output filename (may be in a different directory)
398 * Returns: 1 on success
402 int set_win32_attributes(JCR *jcr, ATTR *attr, BFILE *ofd)
404 char *p = attr->attrEx;
406 WIN32_FILE_ATTRIBUTE_DATA atts;
408 POOLMEM *win32_ofile;
410 if (!p || !*p) { /* we should have attributes */
411 Dmsg2(100, "Attributes missing. of=%s ofd=%d\n", attr->ofname, ofd->fid);
417 Dmsg2(100, "Attribs %s = %s\n", attr->ofname, attr->attrEx);
420 p += from_base64(&val, p);
421 atts.dwFileAttributes = val;
422 p++; /* skip space */
423 p += from_base64(&val, p);
425 atts.ftCreationTime.dwLowDateTime = li.LowPart;
426 atts.ftCreationTime.dwHighDateTime = li.HighPart;
427 p++; /* skip space */
428 p += from_base64(&val, p);
430 atts.ftLastAccessTime.dwLowDateTime = li.LowPart;
431 atts.ftLastAccessTime.dwHighDateTime = li.HighPart;
432 p++; /* skip space */
433 p += from_base64(&val, p);
435 atts.ftLastWriteTime.dwLowDateTime = li.LowPart;
436 atts.ftLastWriteTime.dwHighDateTime = li.HighPart;
438 p += from_base64(&val, p);
439 atts.nFileSizeHigh = val;
441 p += from_base64(&val, p);
442 atts.nFileSizeLow = val;
444 /* Convert to Windows path format */
445 win32_ofile = get_pool_memory(PM_FNAME);
446 unix_name_to_win32(&win32_ofile, attr->ofname);
450 /* At this point, we have reconstructed the WIN32_FILE_ATTRIBUTE_DATA pkt */
453 if (!is_bopen(ofd)) {
454 Dmsg1(100, "File not open: %s\n", attr->ofname);
455 bopen(ofd, attr->ofname, O_WRONLY|O_BINARY, 0); /* attempt to open the file */
459 Dmsg1(100, "SetFileTime %s\n", attr->ofname);
460 if (!SetFileTime(bget_handle(ofd),
461 &atts.ftCreationTime,
462 &atts.ftLastAccessTime,
463 &atts.ftLastWriteTime)) {
464 win_error(jcr, "SetFileTime:", win32_ofile);
469 Dmsg1(100, "SetFileAtts %s\n", attr->ofname);
470 if (!(atts.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) {
471 if (!SetFileAttributes(win32_ofile, atts.dwFileAttributes & SET_ATTRS)) {
472 win_error(jcr, "SetFileAttributes:", win32_ofile);
475 free_pool_memory(win32_ofile);
479 void win_error(JCR *jcr, char *prefix, POOLMEM *win32_ofile)
481 DWORD lerror = GetLastError();
483 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|
484 FORMAT_MESSAGE_FROM_SYSTEM,
491 Dmsg3(100, "Error in %s on file %s: ERR=%s\n", prefix, win32_ofile, msg);
492 strip_trailing_junk(msg);
493 Jmsg(jcr, M_INFO, 0, _("Error in %s file %s: ERR=%s\n"), prefix, win32_ofile, msg);
497 void win_error(JCR *jcr, char *prefix, DWORD lerror)
500 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|
501 FORMAT_MESSAGE_FROM_SYSTEM,
508 strip_trailing_junk(msg);
510 Jmsg2(jcr, M_INFO, 0, _("Error in %s: ERR=%s\n"), prefix, msg);
512 MessageBox(NULL, msg, prefix, MB_OK);
518 /* Cygwin API definition */
519 extern "C" void cygwin_conv_to_win32_path(const char *path, char *win32_path);
521 void unix_name_to_win32(POOLMEM **win32_name, char *name)
523 /* One extra byte should suffice, but we double it */
524 *win32_name = check_pool_memory_size(*win32_name, 2*strlen(name)+1);
525 cygwin_conv_to_win32_path(name, *win32_name);
528 #endif /* HAVE_CYGWIN */