}
}
- subreq = cli_smb_req_create(state, ev, cli, SMBreadX, 0, wct,
+ subreq = cli_smb_req_create(state, ev, cli, SMBreadX, 0, 0, wct,
state->vwv, 0, NULL);
if (subreq == NULL) {
TALLOC_FREE(req);
state->next_offset += chunk->total_size;
state->remaining -= chunk->total_size;
- DLIST_ADD_END(state->chunks, chunk, NULL);
+ DLIST_ADD_END(state->chunks, chunk);
state->num_chunks++;
state->num_waiting++;
if (chunk->tmp_size == 0) {
/*
- * we git a short read, we're done
+ * we got a short read, we're done
*/
tevent_req_done(req);
return;
if (chunk->tmp_size < chunk->total_size) {
/*
- * we git a short read, we're done
+ * we got a short read, we're done
*/
tevent_req_done(req);
return;
return status;
}
+struct cli_read_state {
+ struct cli_state *cli;
+ char *buf;
+ size_t buflen;
+ size_t received;
+};
+
+static void cli_read_done(struct tevent_req *subreq);
+
+struct tevent_req *cli_read_send(
+ TALLOC_CTX *mem_ctx,
+ struct tevent_context *ev,
+ struct cli_state *cli,
+ uint16_t fnum,
+ char *buf,
+ off_t offset,
+ size_t size)
+{
+ struct tevent_req *req, *subreq;
+ struct cli_read_state *state;
+
+ req = tevent_req_create(mem_ctx, &state, struct cli_read_state);
+ if (req == NULL) {
+ return NULL;
+ }
+ state->cli = cli;
+ state->buf = buf;
+ state->buflen = size;
+
+ if (smbXcli_conn_protocol(state->cli->conn) >= PROTOCOL_SMB2_02) {
+ uint32_t max_size;
+ bool ok;
+
+ ok = smb2cli_conn_req_possible(state->cli->conn, &max_size);
+ if (!ok) {
+ tevent_req_nterror(
+ req,
+ NT_STATUS_INSUFFICIENT_RESOURCES);
+ return tevent_req_post(req, ev);
+ }
+
+ /*
+ * downgrade depending on the available credits
+ */
+ size = MIN(max_size, size);
+
+ subreq = cli_smb2_read_send(
+ state, ev, cli, fnum, offset, size);
+ if (tevent_req_nomem(subreq, req)) {
+ return tevent_req_post(req, ev);
+ }
+ } else {
+ bool ok;
+ ok = smb1cli_conn_req_possible(state->cli->conn);
+ if (!ok) {
+ tevent_req_nterror(
+ req,
+ NT_STATUS_INSUFFICIENT_RESOURCES);
+ return tevent_req_post(req, ev);
+ }
+
+ subreq = cli_read_andx_send(
+ state, ev, cli, fnum, offset, size);
+ if (tevent_req_nomem(subreq, req)) {
+ return tevent_req_post(req, ev);
+ }
+ }
+
+ tevent_req_set_callback(subreq, cli_read_done, req);
+
+ return req;
+}
+
+static void cli_read_done(struct tevent_req *subreq)
+{
+ struct tevent_req *req = tevent_req_callback_data(
+ subreq, struct tevent_req);
+ struct cli_read_state *state = tevent_req_data(
+ req, struct cli_read_state);
+ NTSTATUS status;
+ ssize_t received;
+ uint8_t *buf;
+
+ if (smbXcli_conn_protocol(state->cli->conn) >= PROTOCOL_SMB2_02) {
+ status = cli_smb2_read_recv(subreq, &received, &buf);
+ } else {
+ status = cli_read_andx_recv(subreq, &received, &buf);
+ }
+
+ if (NT_STATUS_EQUAL(status, NT_STATUS_END_OF_FILE)) {
+ received = 0;
+ status = NT_STATUS_OK;
+ }
+ if (tevent_req_nterror(req, status)) {
+ return;
+ }
+ if ((received < 0) || (received > state->buflen)) {
+ state->received = 0;
+ tevent_req_nterror(req, NT_STATUS_UNEXPECTED_IO_ERROR);
+ return;
+ }
+
+ memcpy(state->buf, buf, received);
+ state->received = received;
+ tevent_req_done(req);
+}
+
+NTSTATUS cli_read_recv(struct tevent_req *req, size_t *received)
+{
+ struct cli_read_state *state = tevent_req_data(
+ req, struct cli_read_state);
+ NTSTATUS status;
+
+ if (tevent_req_is_nterror(req, &status)) {
+ return status;
+ }
+ if (received != NULL) {
+ *received = state->received;
+ }
+ return NT_STATUS_OK;
+}
+
static NTSTATUS cli_read_sink(char *buf, size_t n, void *priv)
{
char **pbuf = (char **)priv;
state->iov[1].iov_base = discard_const_p(void, buf);
state->iov[1].iov_len = state->size;
- subreq = cli_smb_req_create(state, ev, cli, SMBwriteX, 0, wct, vwv,
+ subreq = cli_smb_req_create(state, ev, cli, SMBwriteX, 0, 0, wct, vwv,
2, state->iov);
if (tevent_req_nomem(subreq, req)) {
return tevent_req_post(req, ev);
return NT_STATUS_OK;
}
-struct cli_writeall_state {
+struct cli_write_state {
+ struct cli_state *cli;
+ size_t written;
+};
+
+static void cli_write_done(struct tevent_req *subreq);
+
+struct tevent_req *cli_write_send(TALLOC_CTX *mem_ctx,
+ struct tevent_context *ev,
+ struct cli_state *cli, uint16_t fnum,
+ uint16_t mode, const uint8_t *buf,
+ off_t offset, size_t size)
+{
+ struct tevent_req *req = NULL;
+ struct cli_write_state *state = NULL;
+ struct tevent_req *subreq = NULL;
+
+ req = tevent_req_create(mem_ctx, &state, struct cli_write_state);
+ if (req == NULL) {
+ return NULL;
+ }
+ state->cli = cli;
+
+ if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
+ uint32_t max_size;
+ bool ok;
+
+ ok = smb2cli_conn_req_possible(state->cli->conn, &max_size);
+ if (!ok) {
+ tevent_req_nterror(
+ req,
+ NT_STATUS_INSUFFICIENT_RESOURCES);
+ return tevent_req_post(req, ev);
+ }
+
+ /*
+ * downgrade depending on the available credits
+ */
+ size = MIN(max_size, size);
+
+ subreq = cli_smb2_write_send(state,
+ ev,
+ cli,
+ fnum,
+ mode,
+ buf,
+ offset,
+ size);
+ } else {
+ bool ok;
+
+ ok = smb1cli_conn_req_possible(state->cli->conn);
+ if (!ok) {
+ tevent_req_nterror(
+ req,
+ NT_STATUS_INSUFFICIENT_RESOURCES);
+ return tevent_req_post(req, ev);
+ }
+
+ subreq = cli_write_andx_send(state,
+ ev,
+ cli,
+ fnum,
+ mode,
+ buf,
+ offset,
+ size);
+ }
+ if (tevent_req_nomem(subreq, req)) {
+ return tevent_req_post(req, ev);
+ }
+ tevent_req_set_callback(subreq, cli_write_done, req);
+
+ return req;
+}
+
+static void cli_write_done(struct tevent_req *subreq)
+{
+ struct tevent_req *req =
+ tevent_req_callback_data(subreq,
+ struct tevent_req);
+ struct cli_write_state *state =
+ tevent_req_data(req,
+ struct cli_write_state);
+ NTSTATUS status;
+
+ if (smbXcli_conn_protocol(state->cli->conn) >= PROTOCOL_SMB2_02) {
+ status = cli_smb2_write_recv(subreq, &state->written);
+ } else {
+ status = cli_write_andx_recv(subreq, &state->written);
+ }
+ TALLOC_FREE(subreq);
+ if (tevent_req_nterror(req, status)) {
+ return;
+ }
+ tevent_req_done(req);
+}
+
+NTSTATUS cli_write_recv(struct tevent_req *req, size_t *pwritten)
+{
+ struct cli_write_state *state =
+ tevent_req_data(req,
+ struct cli_write_state);
+ NTSTATUS status;
+
+ if (tevent_req_is_nterror(req, &status)) {
+ tevent_req_received(req);
+ return status;
+ }
+ if (pwritten != NULL) {
+ *pwritten = state->written;
+ }
+ tevent_req_received(req);
+ return NT_STATUS_OK;
+}
+
+struct cli_smb1_writeall_state {
struct tevent_context *ev;
struct cli_state *cli;
uint16_t fnum;
size_t written;
};
-static void cli_writeall_written(struct tevent_req *req);
+static void cli_smb1_writeall_written(struct tevent_req *req);
-static struct tevent_req *cli_writeall_send(TALLOC_CTX *mem_ctx,
- struct tevent_context *ev,
- struct cli_state *cli,
- uint16_t fnum,
- uint16_t mode,
- const uint8_t *buf,
- off_t offset, size_t size)
+static struct tevent_req *cli_smb1_writeall_send(TALLOC_CTX *mem_ctx,
+ struct tevent_context *ev,
+ struct cli_state *cli,
+ uint16_t fnum,
+ uint16_t mode,
+ const uint8_t *buf,
+ off_t offset, size_t size)
{
struct tevent_req *req, *subreq;
- struct cli_writeall_state *state;
+ struct cli_smb1_writeall_state *state;
- req = tevent_req_create(mem_ctx, &state, struct cli_writeall_state);
+ req = tevent_req_create(mem_ctx, &state,
+ struct cli_smb1_writeall_state);
if (req == NULL) {
return NULL;
}
if (tevent_req_nomem(subreq, req)) {
return tevent_req_post(req, ev);
}
- tevent_req_set_callback(subreq, cli_writeall_written, req);
+ tevent_req_set_callback(subreq, cli_smb1_writeall_written, req);
return req;
}
-static void cli_writeall_written(struct tevent_req *subreq)
+static void cli_smb1_writeall_written(struct tevent_req *subreq)
{
struct tevent_req *req = tevent_req_callback_data(
subreq, struct tevent_req);
- struct cli_writeall_state *state = tevent_req_data(
- req, struct cli_writeall_state);
+ struct cli_smb1_writeall_state *state = tevent_req_data(
+ req, struct cli_smb1_writeall_state);
NTSTATUS status;
size_t written, to_write;
if (tevent_req_nomem(subreq, req)) {
return;
}
- tevent_req_set_callback(subreq, cli_writeall_written, req);
+ tevent_req_set_callback(subreq, cli_smb1_writeall_written, req);
}
-static NTSTATUS cli_writeall_recv(struct tevent_req *req,
- size_t *pwritten)
+static NTSTATUS cli_smb1_writeall_recv(struct tevent_req *req,
+ size_t *pwritten)
+{
+ struct cli_smb1_writeall_state *state = tevent_req_data(
+ req, struct cli_smb1_writeall_state);
+ NTSTATUS status;
+
+ if (tevent_req_is_nterror(req, &status)) {
+ return status;
+ }
+ if (pwritten != NULL) {
+ *pwritten = state->written;
+ }
+ return NT_STATUS_OK;
+}
+
+struct cli_writeall_state {
+ struct cli_state *cli;
+ size_t written;
+};
+
+static void cli_writeall_done(struct tevent_req *subreq);
+
+struct tevent_req *cli_writeall_send(
+ TALLOC_CTX *mem_ctx,
+ struct tevent_context *ev,
+ struct cli_state *cli,
+ uint16_t fnum,
+ uint16_t mode,
+ const uint8_t *buf,
+ off_t offset,
+ size_t size)
+{
+ struct tevent_req *req, *subreq;
+ struct cli_writeall_state *state;
+
+ req = tevent_req_create(mem_ctx, &state, struct cli_writeall_state);
+ if (req == NULL) {
+ return NULL;
+ }
+ state->cli = cli;
+
+ if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
+ subreq = cli_smb2_writeall_send(
+ state,
+ ev,
+ cli,
+ fnum,
+ mode,
+ buf,
+ offset,
+ size);
+ } else {
+ subreq = cli_smb1_writeall_send(
+ state,
+ ev,
+ cli,
+ fnum,
+ mode,
+ buf,
+ offset,
+ size);
+ }
+
+ if (tevent_req_nomem(subreq, req)) {
+ return tevent_req_post(req, ev);
+ }
+ tevent_req_set_callback(subreq, cli_writeall_done, req);
+
+ return req;
+}
+
+static void cli_writeall_done(struct tevent_req *subreq)
+{
+ struct tevent_req *req = tevent_req_callback_data(
+ subreq, struct tevent_req);
+ struct cli_writeall_state *state = tevent_req_data(
+ req, struct cli_writeall_state);
+ NTSTATUS status;
+
+ if (smbXcli_conn_protocol(state->cli->conn) >= PROTOCOL_SMB2_02) {
+ status = cli_smb2_writeall_recv(subreq, &state->written);
+ } else {
+ status = cli_smb1_writeall_recv(subreq, &state->written);
+ }
+ TALLOC_FREE(subreq);
+ if (tevent_req_nterror(req, status)) {
+ return;
+ }
+ tevent_req_done(req);
+}
+
+NTSTATUS cli_writeall_recv(struct tevent_req *req, size_t *pwritten)
{
struct cli_writeall_state *state = tevent_req_data(
req, struct cli_writeall_state);
return NT_STATUS_OK;
}
+
NTSTATUS cli_writeall(struct cli_state *cli, uint16_t fnum, uint16_t mode,
const uint8_t *buf, off_t offset, size_t size,
size_t *pwritten)
if (ev == NULL) {
goto fail;
}
- if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
- req = cli_smb2_writeall_send(frame, ev, cli, fnum, mode,
- buf, offset, size);
- } else {
- req = cli_writeall_send(frame, ev, cli, fnum, mode,
- buf, offset, size);
- }
+ req = cli_writeall_send(frame, ev, cli, fnum, mode, buf, offset, size);
if (req == NULL) {
goto fail;
}
if (!tevent_req_poll_ntstatus(req, ev, &status)) {
goto fail;
}
- if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
- status = cli_smb2_writeall_recv(req, pwritten);
- } else {
- status = cli_writeall_recv(req, pwritten);
- }
+ status = cli_writeall_recv(req, pwritten);
fail:
TALLOC_FREE(frame);
return status;
}
state->next_offset += chunk->total_size;
- DLIST_ADD_END(state->chunks, chunk, NULL);
+ DLIST_ADD_END(state->chunks, chunk);
state->num_chunks++;
state->num_waiting++;
uint8_t *buf = talloc_size(frame, SPLICE_BLOCK_SIZE);
size_t nread;
off_t remaining = initial_size;
+ *written = 0;
while (remaining) {
+ size_t to_read = MIN(remaining, SPLICE_BLOCK_SIZE);
+
status = cli_read(srccli, src_fnum,
- (char *)buf, src_offset, SPLICE_BLOCK_SIZE,
+ (char *)buf, src_offset, to_read,
&nread);
if (!NT_STATUS_IS_OK(status)) {
return status;
}
src_offset += nread;
dst_offset += nread;
+ *written += nread;
if (remaining < nread) {
return NT_STATUS_INTERNAL_ERROR;
}