2 * Bacula low level File I/O routines. This routine simulates
3 * open(), read(), write(), and close(), but using native routines.
4 * I.e. on Windows, we use Windows APIs.
6 * Kern Sibbald, April MMIII
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,
34 /* ===============================================================
36 * U N I X AND W I N D O W S
38 * ===============================================================
41 int is_win32_stream(int stream)
44 case STREAM_WIN32_DATA:
45 case STREAM_WIN32_GZIP_DATA:
51 const char *stream_to_ascii(int stream)
56 case STREAM_GZIP_DATA:
58 case STREAM_SPARSE_GZIP_DATA:
59 return "GZIP sparse data";
60 case STREAM_WIN32_DATA:
62 case STREAM_WIN32_GZIP_DATA:
63 return "Win32 GZIP data";
64 case STREAM_UNIX_ATTRIBUTES:
65 return "File attributes";
66 case STREAM_FILE_DATA:
68 case STREAM_MD5_SIGNATURE:
69 return "MD5 signature";
70 case STREAM_UNIX_ATTRIBUTES_EX:
71 return "Extended attributes";
72 case STREAM_SPARSE_DATA:
74 case STREAM_PROGRAM_NAMES:
75 return "Program names";
76 case STREAM_PROGRAM_DATA:
77 return "Program data";
78 case STREAM_SHA1_SIGNATURE:
79 return "SHA1 signature";
81 sprintf(buf, "%d", stream);
82 return (const char *)buf;
88 /* ===============================================================
92 * ===============================================================
95 #if defined(HAVE_CYGWIN) || defined(HAVE_WIN32)
97 void unix_name_to_win32(POOLMEM **win32_name, char *name);
98 extern "C" HANDLE get_osfhandle(int fd);
102 void binit(BFILE *bfd)
104 memset(bfd, 0, sizeof(BFILE));
106 bfd->mode = BF_CLOSED;
107 bfd->use_backup_api = have_win32_api();
109 bfd->lpContext = NULL;
115 * Enables using the Backup API (win32_data).
116 * Returns 1 if function worked
117 * Returns 0 if failed (i.e. do not have Backup API on this machine)
119 int set_win32_backup(BFILE *bfd)
121 /* We enable if possible here */
122 bfd->use_backup_api = have_win32_api();
123 return bfd->use_backup_api;
127 int set_portable_backup(BFILE *bfd)
129 bfd->use_backup_api = 0;
134 * Return 1 if we are NOT using Win32 BackupWrite()
137 int is_portable_backup(BFILE *bfd)
139 return !bfd->use_backup_api;
144 return p_BackupRead && p_BackupWrite;
150 * Return 1 if we support the stream
151 * 0 if we do not support the stream
153 int is_stream_supported(int stream)
155 /* No Win32 backup on this machine */
158 case STREAM_GZIP_DATA:
159 case STREAM_SPARSE_GZIP_DATA:
162 case STREAM_WIN32_DATA:
163 case STREAM_WIN32_GZIP_DATA:
164 return have_win32_api();
168 case STREAM_GZIP_DATA:
169 case STREAM_SPARSE_GZIP_DATA:
171 case STREAM_UNIX_ATTRIBUTES:
172 case STREAM_FILE_DATA:
173 case STREAM_MD5_SIGNATURE:
174 case STREAM_UNIX_ATTRIBUTES_EX:
175 case STREAM_SPARSE_DATA:
176 case STREAM_PROGRAM_NAMES:
177 case STREAM_PROGRAM_DATA:
178 case STREAM_SHA1_SIGNATURE:
179 case 0: /* compatibility with old tapes */
185 HANDLE bget_handle(BFILE *bfd)
190 int bopen(BFILE *bfd, const char *fname, int flags, mode_t mode)
192 POOLMEM *win32_fname;
193 DWORD dwaccess, dwflags;
195 /* Convert to Windows path format */
196 win32_fname = get_pool_memory(PM_FNAME);
197 unix_name_to_win32(&win32_fname, (char *)fname);
199 if (flags & O_CREAT) { /* Create */
200 if (bfd->use_backup_api) {
201 dwaccess = GENERIC_WRITE|FILE_ALL_ACCESS|WRITE_OWNER|WRITE_DAC|ACCESS_SYSTEM_SECURITY;
202 dwflags = FILE_FLAG_BACKUP_SEMANTICS;
204 dwaccess = GENERIC_WRITE;
207 bfd->fh = CreateFile(win32_fname,
208 dwaccess, /* Requested access */
210 NULL, /* SecurityAttributes */
211 CREATE_ALWAYS, /* CreationDisposition */
212 dwflags, /* Flags and attributes */
213 NULL); /* TemplateFile */
214 bfd->mode = BF_WRITE;
216 } else if (flags & O_WRONLY) { /* Open existing for write */
217 if (bfd->use_backup_api) {
219 dwaccess = GENERIC_WRITE|/*FILE_ALL_ACCESS|*/WRITE_OWNER|WRITE_DAC/*|ACCESS_SYSTEM_SECURITY*/;
221 dwaccess = GENERIC_WRITE|FILE_ALL_ACCESS|WRITE_OWNER|WRITE_DAC|ACCESS_SYSTEM_SECURITY;
223 dwflags = FILE_FLAG_BACKUP_SEMANTICS;
225 dwaccess = GENERIC_WRITE;
228 bfd->fh = CreateFile(win32_fname,
229 dwaccess, /* Requested access */
231 NULL, /* SecurityAttributes */
232 OPEN_EXISTING, /* CreationDisposition */
233 dwflags, /* Flags and attributes */
234 NULL); /* TemplateFile */
235 bfd->mode = BF_WRITE;
238 if (bfd->use_backup_api) {
239 dwaccess = GENERIC_READ|READ_CONTROL|ACCESS_SYSTEM_SECURITY;
240 dwflags = FILE_FLAG_BACKUP_SEMANTICS;
242 dwaccess = GENERIC_READ;
245 bfd->fh = CreateFile(win32_fname,
246 dwaccess, /* Requested access */
247 FILE_SHARE_READ, /* Shared mode */
248 NULL, /* SecurityAttributes */
249 OPEN_EXISTING, /* CreationDisposition */
250 dwflags, /* Flags and attributes */
251 NULL); /* TemplateFile */
255 if (bfd->fh == INVALID_HANDLE_VALUE) {
256 bfd->lerror = GetLastError();
257 bfd->berrno = b_errno_win32;
258 errno = b_errno_win32;
259 bfd->mode = BF_CLOSED;
262 bfd->lpContext = NULL;
263 free_pool_memory(win32_fname);
264 return bfd->mode == BF_CLOSED ? -1 : 1;
268 * Returns 0 on success
271 int bclose(BFILE *bfd)
276 free_pool_memory(bfd->errmsg);
279 if (bfd->mode == BF_CLOSED) {
282 if (bfd->use_backup_api && bfd->mode == BF_READ) {
284 if (!bfd->lpContext && !p_BackupRead(bfd->fh,
286 (DWORD)0, /* bytes to read */
287 &bfd->rw_bytes, /* bytes read */
289 1, /* ProcessSecurity */
290 &bfd->lpContext)) { /* Read context */
291 errno = b_errno_win32;
294 } else if (bfd->use_backup_api && bfd->mode == BF_WRITE) {
296 if (!bfd->lpContext && !p_BackupWrite(bfd->fh,
298 (DWORD)0, /* bytes to read */
299 &bfd->rw_bytes, /* bytes written */
301 1, /* ProcessSecurity */
302 &bfd->lpContext)) { /* Write context */
303 errno = b_errno_win32;
307 if (!CloseHandle(bfd->fh)) {
309 errno = b_errno_win32;
311 bfd->mode = BF_CLOSED;
312 bfd->lpContext = NULL;
317 * Generate error message
320 char *xberror(BFILE *bfd)
324 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|
325 FORMAT_MESSAGE_FROM_SYSTEM,
332 strip_trailing_junk(msg);
334 bfd->errmsg = get_pool_memory(PM_FNAME);
336 pm_strcpy(&bfd->errmsg, msg);
341 /* Returns: bytes read on success
345 ssize_t bread(BFILE *bfd, void *buf, size_t count)
349 if (bfd->use_backup_api) {
350 if (!p_BackupRead(bfd->fh,
355 1, /* Process Security */
356 &bfd->lpContext)) { /* Context */
357 bfd->lerror = GetLastError();
358 bfd->berrno = b_errno_win32;
359 errno = b_errno_win32;
363 if (!ReadFile(bfd->fh,
368 bfd->lerror = GetLastError();
369 bfd->berrno = b_errno_win32;
370 errno = b_errno_win32;
375 return (ssize_t)bfd->rw_bytes;
378 ssize_t bwrite(BFILE *bfd, void *buf, size_t count)
382 if (bfd->use_backup_api) {
383 if (!p_BackupWrite(bfd->fh,
388 1, /* Process Security */
389 &bfd->lpContext)) { /* Context */
390 bfd->lerror = GetLastError();
391 bfd->berrno = b_errno_win32;
392 errno = b_errno_win32;
396 if (!WriteFile(bfd->fh,
401 bfd->lerror = GetLastError();
402 bfd->berrno = b_errno_win32;
403 errno = b_errno_win32;
407 return (ssize_t)bfd->rw_bytes;
410 int is_bopen(BFILE *bfd)
412 return bfd->mode != BF_CLOSED;
415 off_t blseek(BFILE *bfd, off_t offset, int whence)
417 /* ****FIXME**** this must be implemented if we want to read Win32 Archives */
421 #else /* Unix systems */
423 /* ===============================================================
427 * ===============================================================
429 void binit(BFILE *bfd)
431 memset(bfd, 0, sizeof(BFILE));
437 return 0; /* no can do */
441 * Enables using the Backup API (win32_data).
442 * Returns 1 if function worked
443 * Returns 0 if failed (i.e. do not have Backup API on this machine)
445 int set_win32_backup(BFILE *bfd)
447 return 0; /* no can do */
451 int set_portable_backup(BFILE *bfd)
453 return 1; /* no problem */
457 * Return 1 if we are writing in portable format
460 int is_portable_backup(BFILE *bfd)
462 return 1; /* portable by definition */
465 int is_stream_supported(int stream)
467 /* No Win32 backup on this machine */
470 case STREAM_GZIP_DATA:
471 case STREAM_SPARSE_GZIP_DATA:
473 case STREAM_WIN32_DATA:
474 case STREAM_WIN32_GZIP_DATA:
479 case STREAM_GZIP_DATA:
480 case STREAM_SPARSE_GZIP_DATA:
482 case STREAM_UNIX_ATTRIBUTES:
483 case STREAM_FILE_DATA:
484 case STREAM_MD5_SIGNATURE:
485 case STREAM_UNIX_ATTRIBUTES_EX:
486 case STREAM_SPARSE_DATA:
487 case STREAM_PROGRAM_NAMES:
488 case STREAM_PROGRAM_DATA:
489 case STREAM_SHA1_SIGNATURE:
490 case 0: /* compatibility with old tapes */
497 int bopen(BFILE *bfd, const char *fname, int flags, mode_t mode)
499 bfd->fid = open(fname, flags, mode);
501 Dmsg1(400, "Open file %d\n", bfd->fid);
506 int bclose(BFILE *bfd)
509 Dmsg1(400, "Close file %d\n", bfd->fid);
510 if (bfd->fid == -1) {
513 stat = close(bfd->fid);
520 ssize_t bread(BFILE *bfd, void *buf, size_t count)
523 stat = read(bfd->fid, buf, count);
528 ssize_t bwrite(BFILE *bfd, void *buf, size_t count)
531 stat = write(bfd->fid, buf, count);
536 int is_bopen(BFILE *bfd)
538 return bfd->fid >= 0;
541 off_t blseek(BFILE *bfd, off_t offset, int whence)
544 pos = lseek(bfd->fid, offset, whence);
550 char *xberror(BFILE *bfd)
552 return strerror(bfd->berrno);