3 IO routines, generic and specific.
5 Copyright (C) James Peach 2005-2006
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 #include "system/filesys.h"
24 #include "libcli/raw/libcliraw.h"
25 #include "libcli/libcli.h"
26 #include "lib/cmdline/popt_common.h"
30 /* ------------------------------------------------------------------------- */
31 /* UNIX file descriptor IO. */
32 /* ------------------------------------------------------------------------- */
40 #define IO_HANDLE_TO_FD(h) (((struct fd_handle *)(h))->fd)
42 static BOOL fd_seek_func(void * handle, uint64_t offset)
46 ret = lseek(IO_HANDLE_TO_FD(handle), offset, SEEK_SET);
48 fprintf(stderr, "%s: seek failed: %s\n",
49 PROGNAME, strerror(errno));
56 static BOOL fd_read_func(void * handle, uint8_t * buf, uint64_t wanted, uint64_t * actual)
60 ret = read(IO_HANDLE_TO_FD(handle), buf, wanted);
62 fprintf(stderr, "%s: %llu byte read failed: %s\n",
63 PROGNAME, (unsigned long long)wanted, strerror(errno));
67 *actual = (uint64_t)ret;
72 fd_write_func(void * handle, uint8_t * buf, uint64_t wanted, uint64_t * actual)
76 ret = write(IO_HANDLE_TO_FD(handle), buf, wanted);
78 fprintf(stderr, "%s: %llu byte write failed: %s\n",
79 PROGNAME, (unsigned long long)wanted, strerror(errno));
83 *actual = (uint64_t)ret;
87 static struct dd_iohandle *
88 open_fd_handle(const char * path, uint64_t iosz, int options)
90 struct fd_handle * fdh;
93 DEBUG(4, ("opening fd stream for %s\n", path));
94 if ((fdh = talloc_zero(NULL, struct fd_handle)) == NULL) {
98 fdh->h.io_read = fd_read_func;
99 fdh->h.io_write = fd_write_func;
100 fdh->h.io_seek = fd_seek_func;
102 if (options & DD_DIRECT_IO)
105 if (options & DD_SYNC_IO)
108 oflags |= (options & DD_WRITE) ? (O_WRONLY | O_CREAT) : (O_RDONLY);
110 fdh->fd = open(path, oflags, 0644);
112 fprintf(stderr, "%s: %s: %s\n",
113 PROGNAME, path, strerror(errno));
118 if (options & DD_OPLOCK) {
119 DEBUG(2, ("FIXME: take local oplock on %s\n", path));
122 SMB_ASSERT((void *)fdh == (void *)&fdh->h);
126 /* ------------------------------------------------------------------------- */
127 /* CIFS client IO. */
128 /* ------------------------------------------------------------------------- */
132 struct dd_iohandle h;
133 struct smbcli_state * cli;
138 #define IO_HANDLE_TO_SMB(h) ((struct smb_handle *)(h))
140 BOOL smb_seek_func(void * handle, uint64_t offset)
142 IO_HANDLE_TO_SMB(handle)->offset = offset;
146 BOOL smb_read_func(void * handle, uint8_t * buf,
147 uint64_t wanted, uint64_t * actual)
151 struct smb_handle * smbh;
154 smbh = IO_HANDLE_TO_SMB(handle);
156 r.generic.level = RAW_READ_READX;
157 r.readx.in.fnum = smbh->fnum;
158 r.readx.in.offset = smbh->offset;
159 r.readx.in.mincnt = wanted;
160 r.readx.in.maxcnt = wanted;
161 r.readx.out.data = buf;
163 /* FIXME: Should I really set readx.in.remaining? That just seems
166 ret = smb_raw_read(smbh->cli->tree, &r);
167 if (!NT_STATUS_IS_OK(ret)) {
168 fprintf(stderr, "%s: %llu byte read failed: %s\n",
169 PROGNAME, (unsigned long long)wanted, nt_errstr(ret));
173 /* Trap integer wrap. */
174 SMB_ASSERT((smbh->offset + r.readx.out.nread) >= smbh->offset);
176 *actual = r.readx.out.nread;
177 smbh->offset += r.readx.out.nread;
181 BOOL smb_write_func(void * handle, uint8_t * buf,
182 uint64_t wanted, uint64_t * actual)
186 struct smb_handle * smbh;
189 smbh = IO_HANDLE_TO_SMB(handle);
191 w.generic.level = RAW_WRITE_WRITEX;
192 w.writex.in.fnum = smbh->fnum;
193 w.writex.in.offset = smbh->offset;
194 w.writex.in.count = wanted;
195 w.writex.in.data = buf;
197 ret = smb_raw_write(smbh->cli->tree, &w);
198 if (!NT_STATUS_IS_OK(ret)) {
199 fprintf(stderr, "%s: %llu byte write failed: %s\n",
200 PROGNAME, (unsigned long long)wanted, nt_errstr(ret));
204 *actual = w.writex.out.nwritten;
205 smbh->offset += w.writex.out.nwritten;
209 static struct smbcli_state * init_smb_session(const char * host, const char * share)
212 struct smbcli_state * cli = NULL;
214 /* When we support SMB URLs, we can get different user credentials for
215 * each connection, but for now, we just use the same one for both.
217 ret = smbcli_full_connection(NULL, &cli, host, share,
218 NULL /* devtype */, cmdline_credentials, NULL /* events */);
220 if (!NT_STATUS_IS_OK(ret)) {
221 fprintf(stderr, "%s: connecting to //%s/%s: %s\n",
222 PROGNAME, host, share, nt_errstr(ret));
229 static int open_smb_file(struct smbcli_state * cli, const char * path, int options)
236 o.ntcreatex.level = RAW_OPEN_NTCREATEX;
237 o.ntcreatex.in.fname = path;
239 /* TODO: It's not clear whether to use these flags or to use the
240 * similarly named NTCREATEX flags in the create_options field.
242 if (options & DD_DIRECT_IO)
243 o.ntcreatex.in.flags |= FILE_FLAG_NO_BUFFERING;
245 if (options & DD_SYNC_IO)
246 o.ntcreatex.in.flags |= FILE_FLAG_WRITE_THROUGH;
248 o.ntcreatex.in.access_mask |=
249 (options & DD_WRITE) ? SEC_FILE_WRITE_DATA
250 : SEC_FILE_READ_DATA;
252 /* Try to create the file only if we will be writing to it. */
253 o.ntcreatex.in.open_disposition =
254 (options & DD_WRITE) ? NTCREATEX_DISP_OPEN_IF
255 : NTCREATEX_DISP_OPEN;
257 o.ntcreatex.in.share_access =
258 NTCREATEX_SHARE_ACCESS_READ | NTCREATEX_SHARE_ACCESS_WRITE;
260 if (options & DD_OPLOCK) {
261 o.ntcreatex.in.flags |= NTCREATEX_FLAGS_REQUEST_OPLOCK;
264 ret = smb_raw_open(cli->tree, NULL, &o);
265 if (!NT_STATUS_IS_OK(ret)) {
266 fprintf(stderr, "%s: opening %s: %s\n",
267 PROGNAME, path, nt_errstr(ret));
271 return(o.ntcreatex.out.fnum);
274 static struct dd_iohandle * open_smb_handle(const char * host, const char * share,
275 const char * path, uint64_t iosz, int options)
277 struct smb_handle * smbh;
279 DEBUG(4, ("opening SMB stream to //%s/%s for %s\n",
282 if ((smbh = talloc_zero(NULL, struct smb_handle)) == NULL) {
286 smbh->h.io_read = smb_read_func;
287 smbh->h.io_write = smb_write_func;
288 smbh->h.io_seek = smb_seek_func;
290 if ((smbh->cli = init_smb_session(host, share)) == NULL) {
294 DEBUG(4, ("connected to //%s/%s with xmit size of %u bytes\n",
295 host, share, smbh->cli->transport->negotiate.max_xmit));
297 smbh->fnum = open_smb_file(smbh->cli, path, options);
301 /* ------------------------------------------------------------------------- */
302 /* Abstract IO interface. */
303 /* ------------------------------------------------------------------------- */
305 struct dd_iohandle * dd_open_path(const char * path, uint64_t iosz, int options)
307 if (file_exist(path)) {
308 return(open_fd_handle(path, iosz, options));
313 if (smbcli_parse_unc(path, NULL, &host, &share)) {
315 remain = strstr(path, share) + strlen(share);
317 /* Skip over leading directory separators. */
318 while (*remain == '/' || *remain == '\\') { remain++; }
320 return(open_smb_handle(host, share, remain,
324 return(open_fd_handle(path, iosz, options));
328 /* Fill the buffer till it has at least needsz bytes. Use read operations of
329 * blocksz bytes. Return the number of bytes read and fill bufsz with the new
332 * NOTE: The IO buffer is guaranteed to be big enough to fit needsz + blocksz
335 BOOL dd_fill_block(struct dd_iohandle * h, uint8_t * buf,
336 uint64_t * bufsz, uint64_t needsz, uint64_t blocksz)
340 SMB_ASSERT(blocksz > 0);
341 SMB_ASSERT(needsz > 0);
343 while (*bufsz < needsz) {
345 if (!h->io_read(h, buf + (*bufsz), blocksz, &readsz)) {
350 h->io_flags |= DD_END_OF_FILE;
354 DEBUG(6, ("added %llu bytes to IO buffer (need %llu bytes)\n",
355 (unsigned long long)readsz, (unsigned long long)needsz));
358 dd_stats.in.bytes += readsz;
360 if (readsz == blocksz) {
361 dd_stats.in.fblocks++;
363 DEBUG(3, ("partial read of %llu bytes (expected %llu)\n",
364 (unsigned long long)readsz, (unsigned long long)blocksz));
365 dd_stats.in.pblocks++;
372 /* Flush a buffer that contains bufsz bytes. Use writes of blocksz to do it,
373 * and shift any remaining bytes back to the head of the buffer when there are
374 * no more blocksz sized IOs left.
376 BOOL dd_flush_block(struct dd_iohandle * h, uint8_t * buf,
377 uint64_t * bufsz, uint64_t blocksz)
380 uint64_t totalsz = 0;
382 SMB_ASSERT(blocksz > 0);
384 /* We have explicitly been asked to write a partial block. */
385 if ((*bufsz) < blocksz) {
387 if (!h->io_write(h, buf, *bufsz, &writesz)) {
392 fprintf(stderr, "%s: unexpectedly wrote 0 bytes\n",
398 dd_stats.out.bytes += writesz;
399 dd_stats.out.pblocks++;
402 /* Write as many full blocks as there are in the buffer. */
403 while (((*bufsz) - totalsz) >= blocksz) {
405 if (!h->io_write(h, buf + totalsz, blocksz, &writesz)) {
410 fprintf(stderr, "%s: unexpectedly wrote 0 bytes\n",
415 if (writesz == blocksz) {
416 dd_stats.out.fblocks++;
418 dd_stats.out.pblocks++;
422 dd_stats.out.bytes += writesz;
424 DEBUG(6, ("flushed %llu bytes from IO buffer of %llu bytes (%llu remain)\n",
425 (unsigned long long)blocksz, (unsigned long long)blocksz,
426 (unsigned long long)(blocksz - totalsz)));
429 SMB_ASSERT(totalsz > 0);
431 /* We have flushed as much of the IO buffer as we can while
432 * still doing blocksz-sized operations. Shift any remaining data
433 * to the front of the IO buffer.
435 if ((*bufsz) > totalsz) {
436 uint64_t remain = (*bufsz) - totalsz;
438 DEBUG(3, ("shifting %llu remainder bytes to IO buffer head\n",
439 (unsigned long long)remain));
441 memmove(buf, buf + totalsz, remain);
443 } else if ((*bufsz) == totalsz) {
446 /* Else buffer contains bufsz bytes that we will append
447 * to next time round.
449 DEBUG(3, ("%llu unflushed bytes left in IO buffer\n",
450 (unsigned long long)(*bufsz)));
456 /* vim: set sw=8 sts=8 ts=8 tw=79 : */