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,
36 void unix_name_to_win32(POOLMEM **win32_name, char *name);
37 extern "C" HANDLE get_osfhandle(int fd);
40 void binit(BFILE *bfd)
43 bfd->mode = BF_CLOSED;
45 bfd->use_backup_api = p_BackupRead && p_BackupWrite;
47 bfd->lpContext = NULL;
52 * Enables/disables using the Backup API (win32_data).
53 * Returns 1 if function worked
54 * Returns 0 if failed (i.e. do not have Backup API on this machine)
56 int set_win32_data(BFILE *bfd, int enable)
59 bfd->use_backup_api = 0;
62 /* We enable if possible here */
63 bfd->use_backup_api = p_BackupRead && p_BackupWrite;
64 return bfd->use_backup_api;
67 int is_win32_data(BFILE *bfd)
69 return bfd->use_backup_api;
72 HANDLE bget_handle(BFILE *bfd)
74 if (!bfd->use_win_api) {
75 return get_osfhandle(bfd->fid);
80 int bopen(BFILE *bfd, const char *fname, int flags, mode_t mode)
84 if (!bfd->use_win_api) {
85 bfd->fid = open(fname, flags, mode);
87 bfd->mode = BF_READ; /* not important if BF_READ or BF_WRITE */
92 /* Convert to Windows path format */
93 win32_fname = get_pool_memory(PM_FNAME);
94 unix_name_to_win32(&win32_fname, (char *)fname);
96 if (flags & O_WRONLY) { /* creating */
97 bfd->fh = CreateFile(win32_fname,
98 FILE_ALL_ACCESS|WRITE_DAC|ACCESS_SYSTEM_SECURITY, /* access */
100 NULL, /* SecurityAttributes */
101 CREATE_ALWAYS, /* CreationDisposition */
102 FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS, /* Flags and attributes */
103 NULL); /* TemplateFile */
105 if (bfd->fh == INVALID_HANDLE_VALUE) {
106 bfd->lerror = GetLastError();
107 bfd->mode = BF_CLOSED;
109 bfd->mode = BF_WRITE;
112 bfd->fh = CreateFile(win32_fname,
113 GENERIC_READ|ACCESS_SYSTEM_SECURITY, /* access */
114 FILE_SHARE_READ, /* shared mode */
115 NULL, /* SecurityAttributes */
116 OPEN_EXISTING, /* CreationDisposition */
117 FILE_FLAG_BACKUP_SEMANTICS, /* Flags and attributes */
118 NULL); /* TemplateFile */
120 if (bfd->fh == INVALID_HANDLE_VALUE) {
121 bfd->lerror = GetLastError();
122 bfd->mode = BF_CLOSED;
128 bfd->lpContext = NULL;
129 return bfd->mode == BF_CLOSED ? -1 : 1;
133 * Returns 0 on success
136 int bclose(BFILE *bfd)
139 if (!bfd->use_win_api) {
140 int stat = close(bfd->fid);
142 bfd->mode = BF_CLOSED;
146 free_pool_memory(bfd->errmsg);
149 if (bfd->mode == BF_CLOSED) {
152 if (bfd->use_backup_api && bfd->mode == BF_READ) {
154 if (!bfd->lpContext && !p_BackupRead(bfd->fh,
156 (DWORD)0, /* bytes to read */
157 &bfd->rw_bytes, /* bytes read */
159 1, /* ProcessSecurity */
160 &bfd->lpContext)) { /* Read context */
163 } else if (bfd->use_backup_api && bfd->mode == BF_WRITE) {
165 if (!bfd->lpContext && !p_BackupWrite(bfd->fh,
167 (DWORD)0, /* bytes to read */
168 &bfd->rw_bytes, /* bytes written */
170 1, /* ProcessSecurity */
171 &bfd->lpContext)) { /* Write context */
175 if (!CloseHandle(bfd->fh)) {
178 bfd->mode = BF_CLOSED;
179 bfd->lpContext = NULL;
184 * Generate error message
186 char *berror(BFILE *bfd)
190 if (!bfd->use_win_api) {
191 return strerror(errno);
193 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|
194 FORMAT_MESSAGE_FROM_SYSTEM,
201 strip_trailing_junk(msg);
203 bfd->errmsg = get_pool_memory(PM_FNAME);
205 pm_strcpy(&bfd->errmsg, msg);
210 /* Returns: bytes read on success
214 ssize_t bread(BFILE *bfd, void *buf, size_t count)
216 if (!bfd->use_win_api) {
217 return read(bfd->fid, buf, count);
221 if (bfd->use_backup_api) {
222 if (!p_BackupRead(bfd->fh,
227 1, /* Process Security */
228 &bfd->lpContext)) { /* Context */
229 bfd->lerror = GetLastError();
233 if (!ReadFile(bfd->fh,
238 bfd->lerror = GetLastError();
243 return (ssize_t)bfd->rw_bytes;
246 ssize_t bwrite(BFILE *bfd, void *buf, size_t count)
248 if (!bfd->use_win_api) {
249 return write(bfd->fid, buf, count);
253 if (bfd->use_backup_api) {
254 if (!p_BackupWrite(bfd->fh,
259 1, /* Process Security */
260 &bfd->lpContext)) { /* Context */
261 bfd->lerror = GetLastError();
265 if (!WriteFile(bfd->fh,
270 bfd->lerror = GetLastError();
274 return (ssize_t)bfd->rw_bytes;
277 int is_bopen(BFILE *bfd)
279 return bfd->mode != BF_CLOSED;
282 off_t blseek(BFILE *bfd, off_t offset, int whence)
284 if (!bfd->use_win_api) {
285 return lseek(bfd->fid, offset, whence);
290 #else /* Unix systems */
292 /* ===============================================================
296 * ===============================================================
298 void binit(BFILE *bfd)
303 int is_win32_data(BFILE *bfd)
309 int bopen(BFILE *bfd, const char *fname, int flags, mode_t mode)
311 bfd->fid = open(fname, flags, mode);
316 int bclose(BFILE *bfd)
318 int stat = close(bfd->fid);
324 ssize_t bread(BFILE *bfd, void *buf, size_t count)
327 stat = read(bfd->fid, buf, count);
332 ssize_t bwrite(BFILE *bfd, void *buf, size_t count)
335 stat = write(bfd->fid, buf, count);
340 int is_bopen(BFILE *bfd)
342 return bfd->fid >= 0;
345 off_t blseek(BFILE *bfd, off_t offset, int whence)
348 pos = lseek(bfd->fid, offset, whence);
353 char *berror(BFILE *bfd)
355 return strerror(bfd->berrno);