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 3 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, see <http://www.gnu.org/licenses/>.
22 #include "system/filesys.h"
23 #include "libcli/libcli.h"
24 #include "lib/cmdline/popt_common.h"
28 /* ------------------------------------------------------------------------- */
29 /* UNIX file descriptor IO. */
30 /* ------------------------------------------------------------------------- */
38 #define IO_HANDLE_TO_FD(h) (((struct fd_handle *)(h))->fd)
40 static bool fd_seek_func(void * handle
, uint64_t offset
)
44 ret
= lseek(IO_HANDLE_TO_FD(handle
), offset
, SEEK_SET
);
46 fprintf(stderr
, "%s: seek failed: %s\n",
47 PROGNAME
, strerror(errno
));
54 static bool fd_read_func(void * handle
,
61 ret
= read(IO_HANDLE_TO_FD(handle
), buf
, wanted
);
63 fprintf(stderr
, "%s: %llu byte read failed: %s\n",
64 PROGNAME
, (unsigned long long)wanted
,
69 *actual
= (uint64_t)ret
;
73 static bool fd_write_func(void * handle
,
80 ret
= write(IO_HANDLE_TO_FD(handle
), buf
, wanted
);
82 fprintf(stderr
, "%s: %llu byte write failed: %s\n",
83 PROGNAME
, (unsigned long long)wanted
,
88 *actual
= (uint64_t)ret
;
92 static struct dd_iohandle
* open_fd_handle(const char * path
,
96 struct fd_handle
* fdh
;
99 DEBUG(4, ("opening fd stream for %s\n", path
));
100 if ((fdh
= talloc_zero(NULL
, struct fd_handle
)) == NULL
) {
104 fdh
->h
.io_read
= fd_read_func
;
105 fdh
->h
.io_write
= fd_write_func
;
106 fdh
->h
.io_seek
= fd_seek_func
;
108 if (options
& DD_DIRECT_IO
) {
109 #ifdef HAVE_OPEN_O_DIRECT
112 DEBUG(1, ("no support for direct IO on this platform\n"));
116 if (options
& DD_SYNC_IO
)
119 oflags
|= (options
& DD_WRITE
) ? (O_WRONLY
| O_CREAT
) : (O_RDONLY
);
121 fdh
->fd
= open(path
, oflags
, 0644);
123 fprintf(stderr
, "%s: %s: %s\n",
124 PROGNAME
, path
, strerror(errno
));
129 if (options
& DD_OPLOCK
) {
130 DEBUG(2, ("FIXME: take local oplock on %s\n", path
));
133 SMB_ASSERT((void *)fdh
== (void *)&fdh
->h
);
137 /* ------------------------------------------------------------------------- */
138 /* CIFS client IO. */
139 /* ------------------------------------------------------------------------- */
143 struct dd_iohandle h
;
144 struct smbcli_state
* cli
;
149 #define IO_HANDLE_TO_SMB(h) ((struct cifs_handle *)(h))
151 static bool smb_seek_func(void * handle
, uint64_t offset
)
153 IO_HANDLE_TO_SMB(handle
)->offset
= offset
;
157 static bool smb_read_func(void * handle
, uint8_t * buf
, uint64_t wanted
,
162 struct cifs_handle
* smbh
;
165 smbh
= IO_HANDLE_TO_SMB(handle
);
167 r
.generic
.level
= RAW_READ_READX
;
168 r
.readx
.in
.file
.fnum
= smbh
->fnum
;
169 r
.readx
.in
.offset
= smbh
->offset
;
170 r
.readx
.in
.mincnt
= wanted
;
171 r
.readx
.in
.maxcnt
= wanted
;
172 r
.readx
.out
.data
= buf
;
174 /* FIXME: Should I really set readx.in.remaining? That just seems
177 ret
= smb_raw_read(smbh
->cli
->tree
, &r
);
178 if (!NT_STATUS_IS_OK(ret
)) {
179 fprintf(stderr
, "%s: %llu byte read failed: %s\n",
180 PROGNAME
, (unsigned long long)wanted
,
185 /* Trap integer wrap. */
186 SMB_ASSERT((smbh
->offset
+ r
.readx
.out
.nread
) >= smbh
->offset
);
188 *actual
= r
.readx
.out
.nread
;
189 smbh
->offset
+= r
.readx
.out
.nread
;
193 static bool smb_write_func(void * handle
, uint8_t * buf
, uint64_t wanted
,
198 struct cifs_handle
* smbh
;
201 smbh
= IO_HANDLE_TO_SMB(handle
);
203 w
.generic
.level
= RAW_WRITE_WRITEX
;
204 w
.writex
.in
.file
.fnum
= smbh
->fnum
;
205 w
.writex
.in
.offset
= smbh
->offset
;
206 w
.writex
.in
.count
= wanted
;
207 w
.writex
.in
.data
= buf
;
209 ret
= smb_raw_write(smbh
->cli
->tree
, &w
);
210 if (!NT_STATUS_IS_OK(ret
)) {
211 fprintf(stderr
, "%s: %llu byte write failed: %s\n",
212 PROGNAME
, (unsigned long long)wanted
,
217 *actual
= w
.writex
.out
.nwritten
;
218 smbh
->offset
+= w
.writex
.out
.nwritten
;
222 static struct smbcli_state
* init_smb_session(struct resolve_context
*resolve_ctx
,
223 struct tevent_context
*ev
,
227 const char *socket_options
,
228 struct smbcli_options
*options
,
229 struct smbcli_session_options
*session_options
,
230 struct smb_iconv_convenience
*iconv_convenience
,
231 struct gensec_settings
*gensec_settings
)
234 struct smbcli_state
* cli
= NULL
;
236 /* When we support SMB URLs, we can get different user credentials for
237 * each connection, but for now, we just use the same one for both.
239 ret
= smbcli_full_connection(NULL
, &cli
, host
, ports
, share
,
242 cmdline_credentials
, resolve_ctx
,
248 if (!NT_STATUS_IS_OK(ret
)) {
249 fprintf(stderr
, "%s: connecting to //%s/%s: %s\n",
250 PROGNAME
, host
, share
, nt_errstr(ret
));
257 static int open_smb_file(struct smbcli_state
* cli
,
266 o
.ntcreatex
.level
= RAW_OPEN_NTCREATEX
;
267 o
.ntcreatex
.in
.fname
= path
;
269 /* TODO: It's not clear whether to use these flags or to use the
270 * similarly named NTCREATEX flags in the create_options field.
272 if (options
& DD_DIRECT_IO
)
273 o
.ntcreatex
.in
.flags
|= FILE_FLAG_NO_BUFFERING
;
275 if (options
& DD_SYNC_IO
)
276 o
.ntcreatex
.in
.flags
|= FILE_FLAG_WRITE_THROUGH
;
278 o
.ntcreatex
.in
.access_mask
|=
279 (options
& DD_WRITE
) ? SEC_FILE_WRITE_DATA
280 : SEC_FILE_READ_DATA
;
282 /* Try to create the file only if we will be writing to it. */
283 o
.ntcreatex
.in
.open_disposition
=
284 (options
& DD_WRITE
) ? NTCREATEX_DISP_OPEN_IF
285 : NTCREATEX_DISP_OPEN
;
287 o
.ntcreatex
.in
.share_access
=
288 NTCREATEX_SHARE_ACCESS_READ
| NTCREATEX_SHARE_ACCESS_WRITE
;
290 if (options
& DD_OPLOCK
) {
291 o
.ntcreatex
.in
.flags
|= NTCREATEX_FLAGS_REQUEST_OPLOCK
;
294 ret
= smb_raw_open(cli
->tree
, NULL
, &o
);
295 if (!NT_STATUS_IS_OK(ret
)) {
296 fprintf(stderr
, "%s: opening %s: %s\n",
297 PROGNAME
, path
, nt_errstr(ret
));
301 return(o
.ntcreatex
.out
.file
.fnum
);
304 static struct dd_iohandle
* open_cifs_handle(struct resolve_context
*resolve_ctx
,
305 struct tevent_context
*ev
,
312 const char *socket_options
,
313 struct smbcli_options
*smb_options
,
314 struct smbcli_session_options
*smb_session_options
,
315 struct smb_iconv_convenience
*iconv_convenience
,
316 struct gensec_settings
*gensec_settings
)
318 struct cifs_handle
* smbh
;
320 if (path
== NULL
|| *path
== '\0') {
321 fprintf(stderr
, "%s: missing path name within share //%s/%s\n",
322 PROGNAME
, host
, share
);
325 DEBUG(4, ("opening SMB stream to //%s/%s for %s\n",
328 if ((smbh
= talloc_zero(NULL
, struct cifs_handle
)) == NULL
) {
332 smbh
->h
.io_read
= smb_read_func
;
333 smbh
->h
.io_write
= smb_write_func
;
334 smbh
->h
.io_seek
= smb_seek_func
;
336 if ((smbh
->cli
= init_smb_session(resolve_ctx
, ev
, host
, ports
, share
,
338 smb_options
, smb_session_options
,
340 gensec_settings
)) == NULL
) {
344 DEBUG(4, ("connected to //%s/%s with xmit size of %u bytes\n",
345 host
, share
, smbh
->cli
->transport
->negotiate
.max_xmit
));
347 smbh
->fnum
= open_smb_file(smbh
->cli
, path
, options
);
351 /* ------------------------------------------------------------------------- */
352 /* Abstract IO interface. */
353 /* ------------------------------------------------------------------------- */
355 struct dd_iohandle
* dd_open_path(struct resolve_context
*resolve_ctx
,
356 struct tevent_context
*ev
,
361 const char *socket_options
,
362 struct smbcli_options
*smb_options
,
363 struct smbcli_session_options
*smb_session_options
,
364 struct smb_iconv_convenience
*iconv_convenience
,
365 struct gensec_settings
*gensec_settings
)
367 if (file_exist(path
)) {
368 return(open_fd_handle(path
, io_size
, options
));
373 if (smbcli_parse_unc(path
, NULL
, &host
, &share
)) {
375 remain
= strstr(path
, share
) + strlen(share
);
377 /* Skip over leading directory separators. */
378 while (*remain
== '/' || *remain
== '\\') { remain
++; }
380 return(open_cifs_handle(resolve_ctx
, ev
, host
, ports
,
383 socket_options
, smb_options
,
389 return(open_fd_handle(path
, io_size
, options
));
393 /* Fill the buffer till it has at least need_size bytes. Use read operations of
394 * block_size bytes. Return the number of bytes read and fill buf_size with
395 * the new buffer size.
397 * NOTE: The IO buffer is guaranteed to be big enough to fit
398 * need_size + block_size bytes into it.
400 bool dd_fill_block(struct dd_iohandle
* h
,
408 SMB_ASSERT(block_size
> 0);
409 SMB_ASSERT(need_size
> 0);
411 while (*buf_size
< need_size
) {
413 if (!h
->io_read(h
, buf
+ (*buf_size
), block_size
, &read_size
)) {
417 if (read_size
== 0) {
418 h
->io_flags
|= DD_END_OF_FILE
;
422 DEBUG(6, ("added %llu bytes to IO buffer (need %llu bytes)\n",
423 (unsigned long long)read_size
,
424 (unsigned long long)need_size
));
426 *buf_size
+= read_size
;
427 dd_stats
.in
.bytes
+= read_size
;
429 if (read_size
== block_size
) {
430 dd_stats
.in
.fblocks
++;
432 DEBUG(3, ("partial read of %llu bytes (expected %llu)\n",
433 (unsigned long long)read_size
,
434 (unsigned long long)block_size
));
435 dd_stats
.in
.pblocks
++;
442 /* Flush a buffer that contains buf_size bytes. Use writes of block_size to do it,
443 * and shift any remaining bytes back to the head of the buffer when there are
444 * no more block_size sized IOs left.
446 bool dd_flush_block(struct dd_iohandle
* h
,
452 uint64_t total_size
= 0;
454 SMB_ASSERT(block_size
> 0);
456 /* We have explicitly been asked to write a partial block. */
457 if ((*buf_size
) < block_size
) {
459 if (!h
->io_write(h
, buf
, *buf_size
, &write_size
)) {
463 if (write_size
== 0) {
464 fprintf(stderr
, "%s: unexpectedly wrote 0 bytes\n",
469 total_size
+= write_size
;
470 dd_stats
.out
.bytes
+= write_size
;
471 dd_stats
.out
.pblocks
++;
474 /* Write as many full blocks as there are in the buffer. */
475 while (((*buf_size
) - total_size
) >= block_size
) {
477 if (!h
->io_write(h
, buf
+ total_size
, block_size
, &write_size
)) {
481 if (write_size
== 0) {
482 fprintf(stderr
, "%s: unexpectedly wrote 0 bytes\n",
487 if (write_size
== block_size
) {
488 dd_stats
.out
.fblocks
++;
490 dd_stats
.out
.pblocks
++;
493 total_size
+= write_size
;
494 dd_stats
.out
.bytes
+= write_size
;
496 DEBUG(6, ("flushed %llu bytes from IO buffer of %llu bytes (%llu remain)\n",
497 (unsigned long long)block_size
,
498 (unsigned long long)block_size
,
499 (unsigned long long)(block_size
- total_size
)));
502 SMB_ASSERT(total_size
> 0);
504 /* We have flushed as much of the IO buffer as we can while
505 * still doing block_size'd operations. Shift any remaining data
506 * to the front of the IO buffer.
508 if ((*buf_size
) > total_size
) {
509 uint64_t remain
= (*buf_size
) - total_size
;
511 DEBUG(3, ("shifting %llu remainder bytes to IO buffer head\n",
512 (unsigned long long)remain
));
514 memmove(buf
, buf
+ total_size
, remain
);
515 (*buf_size
) = remain
;
516 } else if ((*buf_size
) == total_size
) {
519 /* Else buffer contains buf_size bytes that we will append
520 * to next time round.
522 DEBUG(3, ("%llu unflushed bytes left in IO buffer\n",
523 (unsigned long long)(*buf_size
)));
529 /* vim: set sw=8 sts=8 ts=8 tw=79 : */