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,
37 /* Forward referenced subroutines */
39 int set_win32_attributes(void *jcr, char *fname, char *ofile, char *lname,
40 int type, int stream, struct stat *statp,
41 char *attribsEx, BFILE *ofd);
42 void unix_name_to_win32(POOLMEM **win32_name, char *name);
43 void win_error(void *jcr, char *prefix, POOLMEM *ofile);
44 HANDLE bget_handle(BFILE *bfd);
47 /* For old systems that don't have lchown() use chown() */
52 /*=============================================================*/
54 /* *** A l l S y s t e m s *** */
56 /*=============================================================*/
59 /* Encode a stat structure into a base64 character string */
60 void encode_stat(char *buf, struct stat *statp, uint32_t LinkFI)
64 * NOTE: we should use rdev as major and minor device if
65 * it is a block or char device (S_ISCHR(statp->st_mode)
66 * or S_ISBLK(statp->st_mode)). In all other cases,
70 p += to_base64((int64_t)statp->st_dev, p);
71 *p++ = ' '; /* separate fields with a space */
72 p += to_base64((int64_t)statp->st_ino, p);
74 p += to_base64((int64_t)statp->st_mode, p);
76 p += to_base64((int64_t)statp->st_nlink, p);
78 p += to_base64((int64_t)statp->st_uid, p);
80 p += to_base64((int64_t)statp->st_gid, p);
82 p += to_base64((int64_t)statp->st_rdev, p);
84 p += to_base64((int64_t)statp->st_size, p);
86 p += to_base64((int64_t)statp->st_blksize, p);
88 p += to_base64((int64_t)statp->st_blocks, p);
90 p += to_base64((int64_t)statp->st_atime, p);
92 p += to_base64((int64_t)statp->st_mtime, p);
94 p += to_base64((int64_t)statp->st_ctime, p);
96 p += to_base64((int64_t)LinkFI, p);
98 /* FreeBSD function */
101 p += to_base64((int64_t)statp->st_flags, p);
109 /* Decode a stat packet from base64 characters */
111 decode_stat(char *buf, struct stat *statp, uint32_t *LinkFI)
116 p += from_base64(&val, p);
118 p++; /* skip space */
119 p += from_base64(&val, p);
122 p += from_base64(&val, p);
123 statp->st_mode = val;
125 p += from_base64(&val, p);
126 statp->st_nlink = val;
128 p += from_base64(&val, p);
131 p += from_base64(&val, p);
134 p += from_base64(&val, p);
135 statp->st_rdev = val;
137 p += from_base64(&val, p);
138 statp->st_size = val;
140 p += from_base64(&val, p);
141 statp->st_blksize = val;
143 p += from_base64(&val, p);
144 statp->st_blocks = val;
146 p += from_base64(&val, p);
147 statp->st_atime = val;
149 p += from_base64(&val, p);
150 statp->st_mtime = val;
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) == ' ')) {
157 p += from_base64(&val, p);
158 *LinkFI = (uint32_t)val;
163 /* FreeBSD user flags */
165 if (*p == ' ' || (*p != 0 && *(p+1) == ' ')) {
167 p += from_base64(&val, p);
168 statp->st_flags = (uint32_t)val;
176 * Set file modes, permissions and times
178 * fname is the original filename
179 * ofile is the output filename (may be in a different directory)
181 * Returns: 1 on success
184 int set_attributes(void *jcr, char *fname, char *ofile, char *lname,
185 int type, int stream, struct stat *statp,
186 char *attribsEx, BFILE *ofd)
193 if (set_win32_attributes(jcr, fname, ofile, lname, type, stream,
194 statp, attribsEx, ofd)) {
198 * If Windows stuff failed, e.g. attempt to restore Unix file
199 * to Windows, simply fall through and we will do it the
206 bclose(ofd); /* first close file */
209 ut.actime = statp->st_atime;
210 ut.modtime = statp->st_mtime;
212 /* ***FIXME**** optimize -- don't do if already correct */
214 * For link, change owner of link using lchown, but don't
215 * try to do a chmod as that will update the file behind it.
217 if (type == FT_LNK) {
218 /* Change owner of link, not of real file */
219 if (lchown(ofile, statp->st_uid, statp->st_gid) < 0) {
220 Jmsg2(jcr, M_WARNING, 0, _("Unable to set file owner %s: ERR=%s\n"),
221 ofile, strerror(errno));
225 if (chown(ofile, statp->st_uid, statp->st_gid) < 0) {
226 Jmsg2(jcr, M_WARNING, 0, _("Unable to set file owner %s: ERR=%s\n"),
227 ofile, strerror(errno));
230 if (chmod(ofile, statp->st_mode) < 0) {
231 Jmsg2(jcr, M_WARNING, 0, _("Unable to set file modes %s: ERR=%s\n"),
232 ofile, strerror(errno));
236 /* FreeBSD user flags */
238 if (chflags(ofile, statp->st_flags) < 0) {
239 Jmsg2(jcr, M_WARNING, 0, _("Unable to set file flags %s: ERR=%s\n"),
240 ofile, strerror(errno));
247 if (utime(ofile, &ut) < 0) {
248 Jmsg2(jcr, M_ERROR, 0, _("Unable to set file times %s: ERR=%s\n"),
249 ofile, strerror(errno));
258 /*=============================================================*/
260 /* * * * U n i x * * * * */
262 /*=============================================================*/
267 * If you have a Unix system with extended attributes (e.g.
268 * ACLs for Solaris, do it here.
270 int encode_attribsEx(void *jcr, char *attribsEx, FF_PKT *ff_pkt)
272 *attribsEx = 0; /* no extended attributes */
273 return STREAM_UNIX_ATTRIBUTES;
280 /*=============================================================*/
282 /* * * * W i n 3 2 * * * * */
284 /*=============================================================*/
288 int encode_attribsEx(void *jcr, char *attribsEx, FF_PKT *ff_pkt)
291 WIN32_FILE_ATTRIBUTE_DATA atts;
294 attribsEx[0] = 0; /* no extended attributes */
296 if (!p_GetFileAttributesEx) {
297 return STREAM_UNIX_ATTRIBUTES;
300 unix_name_to_win32(&ff_pkt->sys_fname, ff_pkt->fname);
301 if (!p_GetFileAttributesEx(ff_pkt->sys_fname, GetFileExInfoStandard,
303 win_error(jcr, "GetFileAttributesEx:", ff_pkt->sys_fname);
304 return STREAM_WIN32_ATTRIBUTES;
307 p += to_base64((uint64_t)atts.dwFileAttributes, p);
308 *p++ = ' '; /* separate fields with a space */
309 li.LowPart = atts.ftCreationTime.dwLowDateTime;
310 li.HighPart = atts.ftCreationTime.dwHighDateTime;
311 p += to_base64((uint64_t)li.QuadPart, p);
313 li.LowPart = atts.ftLastAccessTime.dwLowDateTime;
314 li.HighPart = atts.ftLastAccessTime.dwHighDateTime;
315 p += to_base64((uint64_t)li.QuadPart, p);
317 li.LowPart = atts.ftLastWriteTime.dwLowDateTime;
318 li.HighPart = atts.ftLastWriteTime.dwHighDateTime;
319 p += to_base64((uint64_t)li.QuadPart, p);
321 p += to_base64((uint64_t)atts.nFileSizeHigh, p);
323 p += to_base64((uint64_t)atts.nFileSizeLow, p);
325 return STREAM_WIN32_ATTRIBUTES;
328 /* Define attributes that are legal to set with SetFileAttributes() */
329 #define SET_ATTRS ( \
330 FILE_ATTRIBUTE_ARCHIVE| \
331 FILE_ATTRIBUTE_HIDDEN| \
332 FILE_ATTRIBUTE_NORMAL| \
333 FILE_ATTRIBUTE_NOT_CONTENT_INDEXED| \
334 FILE_ATTRIBUTE_OFFLINE| \
335 FILE_ATTRIBUTE_READONLY| \
336 FILE_ATTRIBUTE_SYSTEM| \
337 FILE_ATTRIBUTE_TEMPORARY)
341 * Set Extended File Attributes for Win32
343 * fname is the original filename
344 * ofile is the output filename (may be in a different directory)
346 * Returns: 1 on success
350 int set_win32_attributes(void *jcr, char *fname, char *ofile, char *lname,
351 int type, int stream, struct stat *statp,
352 char *attribsEx, BFILE *ofd)
356 WIN32_FILE_ATTRIBUTE_DATA atts;
359 POOLMEM *win32_ofile;
361 if (!p || !*p) { /* we should have attributes */
362 Dmsg2(100, "Attributes missing. of=%s ofd=%d\n", ofile, ofd->fid);
368 Dmsg2(100, "Attribs %s = %s\n", ofile, attribsEx);
371 p += from_base64(&val, p);
372 atts.dwFileAttributes = val;
373 p++; /* skip space */
374 p += from_base64(&val, p);
376 atts.ftCreationTime.dwLowDateTime = li.LowPart;
377 atts.ftCreationTime.dwHighDateTime = li.HighPart;
378 p++; /* skip space */
379 p += from_base64(&val, p);
381 atts.ftLastAccessTime.dwLowDateTime = li.LowPart;
382 atts.ftLastAccessTime.dwHighDateTime = li.HighPart;
383 p++; /* skip space */
384 p += from_base64(&val, p);
386 atts.ftLastWriteTime.dwLowDateTime = li.LowPart;
387 atts.ftLastWriteTime.dwHighDateTime = li.HighPart;
389 p += from_base64(&val, p);
390 atts.nFileSizeHigh = val;
392 p += from_base64(&val, p);
393 atts.nFileSizeLow = val;
395 /* At this point, we have reconstructed the WIN32_FILE_ATTRIBUTE_DATA pkt */
397 /* Convert to Windows path format */
398 win32_ofile = get_pool_memory(PM_FNAME);
399 unix_name_to_win32(&win32_ofile, ofile);
401 if (!is_bopen(ofd)) {
402 Dmsg1(100, "File not open: %s\n", ofile);
403 bopen(ofd, ofile, O_WRONLY|O_BINARY, 0); /* attempt to open the file */
407 Dmsg1(100, "SetFileTime %s\n", ofile);
408 stat = SetFileTime(bget_handle(ofd),
409 &atts.ftCreationTime,
410 &atts.ftLastAccessTime,
411 &atts.ftLastWriteTime);
413 win_error(jcr, "SetFileTime:", win32_ofile);
418 Dmsg1(100, "SetFileAtts %s\n", ofile);
419 stat = SetFileAttributes(win32_ofile, atts.dwFileAttributes & SET_ATTRS);
421 win_error(jcr, "SetFileAttributes:", win32_ofile);
423 free_pool_memory(win32_ofile);
427 void win_error(void *vjcr, char *prefix, POOLMEM *win32_ofile)
429 JCR *jcr = (JCR *)vjcr;
430 DWORD lerror = GetLastError();
432 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|
433 FORMAT_MESSAGE_FROM_SYSTEM,
440 Dmsg3(100, "Error in %s on file %s: ERR=%s\n", prefix, win32_ofile, msg);
441 strip_trailing_junk(msg);
442 Jmsg3(jcr, M_INFO, 0, _("Error in %s file %s: ERR=%s\n"), prefix, win32_ofile, msg);
446 void win_error(void *vjcr, char *prefix, DWORD lerror)
448 JCR *jcr = (JCR *)vjcr;
450 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|
451 FORMAT_MESSAGE_FROM_SYSTEM,
458 strip_trailing_junk(msg);
460 Jmsg2(jcr, M_INFO, 0, _("Error in %s: ERR=%s\n"), prefix, msg);
462 MessageBox(NULL, msg, prefix, MB_OK);
468 /* Cygwin API definition */
469 extern "C" void cygwin_conv_to_win32_path(const char *path, char *win32_path);
471 void unix_name_to_win32(POOLMEM **win32_name, char *name)
473 /* One extra byte should suffice, but we double it */
474 *win32_name = check_pool_memory_size(*win32_name, 2*strlen(name)+1);
475 cygwin_conv_to_win32_path(name, *win32_name);
478 #endif /* HAVE_CYGWIN */