[PATCH] New smbclient commands "pread" and "pwrite"

Ralph Böhme slow at samba.org
Sun Jan 8 09:13:55 UTC 2017


Hi!

I found these handy when I had the need to trigger delayed write time entries in
locking.tdb. Not sure if this is generally useful.

Cheerio!
-slow
-------------- next part --------------
From 2cfe14266ee2753344416c8e586503a8cb76bac8 Mon Sep 17 00:00:00 2001
From: Ralph Boehme <slow at samba.org>
Date: Fri, 23 Dec 2016 09:22:28 +0100
Subject: [PATCH 1/2] s3/client: add pwrite

Signed-off-by: Ralph Boehme <slow at samba.org>
---
 docs-xml/manpages/smbclient.1.xml |  6 +++++
 source3/client/client.c           | 47 +++++++++++++++++++++++++++++++++++++++
 2 files changed, 53 insertions(+)

diff --git a/docs-xml/manpages/smbclient.1.xml b/docs-xml/manpages/smbclient.1.xml
index cc8c2b0..b9741d4 100644
--- a/docs-xml/manpages/smbclient.1.xml
+++ b/docs-xml/manpages/smbclient.1.xml
@@ -958,6 +958,12 @@
 		</varlistentry>
 
 		<varlistentry>
+		<term>pwrite <fnum> <offset> <string></term>
+		<listitem><para>Write string at offset into the filehandle specified by fnum.
+		</para></listitem>
+		</varlistentry>
+
+		<varlistentry>
 		<term>queue</term>
 		<listitem><para>Displays the print queue, showing the job id,
 		name, size and current status. </para></listitem>
diff --git a/source3/client/client.c b/source3/client/client.c
index 226eb27..8a7384b 100644
--- a/source3/client/client.c
+++ b/source3/client/client.c
@@ -2039,6 +2039,52 @@ static int cmd_put(void)
 	return do_put(rname, lname, false);
 }
 
+/****************************************************************************
+ Write into file handle at offset.
+****************************************************************************/
+
+static int cmd_pwrite(void)
+{
+	TALLOC_CTX *mem_ctx = talloc_tos();
+	char *fnum_str = NULL;
+	char *off_str = NULL;
+	char *data_str = NULL;
+	uint16_t fnum;
+	off_t off;
+	size_t nwritten;
+	NTSTATUS status;
+	bool ok;
+
+	ok = next_token_talloc(mem_ctx, &cmd_ptr, &fnum_str, NULL);
+	if (!ok) {
+		d_printf("pwrite <fnum> <offset> <string>\n");
+		return 1;
+	}
+
+	ok = next_token_talloc(mem_ctx, &cmd_ptr, &off_str, NULL);
+	if (!ok) {
+		d_printf("pwrite <fnum> <offset> <string>\n");
+		return 1;
+	}
+
+	ok = next_token_talloc(mem_ctx, &cmd_ptr, &data_str, NULL);
+	if (!ok) {
+		d_printf("pwrite <fnum> <offset> <string>\n");
+		return 1;
+	}
+
+	fnum = strtoul(fnum_str, NULL, 10);
+	off = strtoll(off_str, NULL, 10);
+
+	status = cli_writeall(cli, fnum, 0, (uint8_t *)data_str, off, strlen(data_str), &nwritten);
+	if (!NT_STATUS_IS_OK(status)) {
+		d_fprintf(stderr, "cli_writeall returned %s\n", nt_errstr(status));
+		return 1;
+	}
+
+	return 0;
+}
+
 /*************************************
  File list structure.
 *************************************/
@@ -5025,6 +5071,7 @@ static struct {
   {"prompt",cmd_prompt,"toggle prompting for filenames for mget and mput",{COMPL_NONE,COMPL_NONE}},
   {"put",cmd_put,"<local name> [remote name] put a file",{COMPL_LOCAL,COMPL_REMOTE}},
   {"pwd",cmd_pwd,"show current remote directory (same as 'cd' with no args)",{COMPL_NONE,COMPL_NONE}},
+  {"pwrite",cmd_pwrite,"<fnum> <off> <string> write string at offset into fnum",{COMPL_NONE,COMPL_NONE}},
   {"q",cmd_quit,"logoff the server",{COMPL_NONE,COMPL_NONE}},
   {"queue",cmd_queue,"show the print queue",{COMPL_NONE,COMPL_NONE}},
   {"quit",cmd_quit,"logoff the server",{COMPL_NONE,COMPL_NONE}},
-- 
2.7.4


From d19aa4372df2e7492b4f857d18a07e265409ab53 Mon Sep 17 00:00:00 2001
From: Ralph Boehme <slow at samba.org>
Date: Fri, 23 Dec 2016 09:46:20 +0100
Subject: [PATCH 2/2] s3/client: add pread

Signed-off-by: Ralph Boehme <slow at samba.org>
---
 docs-xml/manpages/smbclient.1.xml |  6 ++++
 source3/client/client.c           | 64 +++++++++++++++++++++++++++++++++++++++
 2 files changed, 70 insertions(+)

diff --git a/docs-xml/manpages/smbclient.1.xml b/docs-xml/manpages/smbclient.1.xml
index b9741d4..e874dac 100644
--- a/docs-xml/manpages/smbclient.1.xml
+++ b/docs-xml/manpages/smbclient.1.xml
@@ -932,6 +932,12 @@
 		</varlistentry>
 
 		<varlistentry>
+		<term>pread <fnum> <count> <offset></term>
+		<listitem><para>Read <count> bytes at <offset> from
+		filehandle specified by <fnum>.</para></listitem>
+		</varlistentry>
+
+		<varlistentry>
 		<term>print <file name></term>
 		<listitem><para>Print the specified file from the local machine
 		through a printable service on the server. </para></listitem>
diff --git a/source3/client/client.c b/source3/client/client.c
index 8a7384b..8bc5e01 100644
--- a/source3/client/client.c
+++ b/source3/client/client.c
@@ -2040,6 +2040,69 @@ static int cmd_put(void)
 }
 
 /****************************************************************************
+ Read from file handle at offset.
+****************************************************************************/
+
+static int cmd_pread(void)
+{
+	TALLOC_CTX *mem_ctx = talloc_tos();
+	char *fnum_str = NULL;
+	char *count_str = NULL;
+	char *off_str = NULL;
+	char *buf = NULL;
+	uint16_t fnum;
+	size_t count;
+	off_t off;
+	size_t nread, i;
+	bool need_nl = false;
+	NTSTATUS status;
+	bool ok;
+
+	ok = next_token_talloc(mem_ctx, &cmd_ptr, &fnum_str, NULL);
+	if (!ok) {
+		d_printf("pread <fnum> <count> <offset>\n");
+		return 1;
+	}
+
+	ok = next_token_talloc(mem_ctx, &cmd_ptr, &count_str, NULL);
+	if (!ok) {
+		d_printf("pread <fnum> <count> <offset>\n");
+		return 1;
+	}
+
+	ok = next_token_talloc(mem_ctx, &cmd_ptr, &off_str, NULL);
+	if (!ok) {
+		d_printf("pread <fnum> <count> <offset>\n");
+		return 1;
+	}
+
+	fnum = strtoul(fnum_str, NULL, 10);
+	count = strtoull(count_str, NULL, 10);
+	off = strtoll(off_str, NULL, 10);
+
+	buf = talloc_array(mem_ctx, char, count);
+
+	status = cli_read(cli, fnum, buf, off, count, &nread);
+	if (!NT_STATUS_IS_OK(status)) {
+		d_fprintf(stderr, "cli_read returned %s\n", nt_errstr(status));
+		return 1;
+	}
+
+	for (i = 0; i < nread; i++) {
+		if (isascii(buf[i])) {
+			need_nl = true;
+			d_printf("%c", buf[i]);
+		}
+	}
+
+	if (need_nl) {
+		d_printf("\n");
+	}
+
+	return 0;
+}
+
+/****************************************************************************
  Write into file handle at offset.
 ****************************************************************************/
 
@@ -5067,6 +5130,7 @@ static struct {
   {"posix_unlink",cmd_posix_unlink,"<name> removes a file using POSIX interface",{COMPL_REMOTE,COMPL_NONE}},
   {"posix_whoami",cmd_posix_whoami,"retun logged on user information "
 			"using POSIX interface",{COMPL_REMOTE,COMPL_NONE}},
+  {"pread",cmd_pread,"<fnum> <count> <off> read count bytes at off from fnum",{COMPL_NONE,COMPL_NONE}},
   {"print",cmd_print,"<file name> print a file",{COMPL_NONE,COMPL_NONE}},
   {"prompt",cmd_prompt,"toggle prompting for filenames for mget and mput",{COMPL_NONE,COMPL_NONE}},
   {"put",cmd_put,"<local name> [remote name] put a file",{COMPL_LOCAL,COMPL_REMOTE}},
-- 
2.7.4



More information about the samba-technical mailing list