UTILS: add nfs-cat utility

Signed-off-by: Ronnie Sahlberg <ronniesahlberg@gmail.com>
libnfs-4.0.0-vitalif
Ronnie Sahlberg 2015-05-24 06:47:14 -07:00
parent 63d40b1737
commit 4b1de15bb5
7 changed files with 306 additions and 5 deletions

View File

@ -1,9 +1,10 @@
XSLTPROC = /usr/bin/xsltproc
EXTRA_DIST = nfs-ls.1 nfs-ls.1.xml
EXTRA_DIST = nfs-cat.1 nfs-cat.1.xml nfs-ls.1 nfs-ls.1.xml
# Manpages
man1_MANS = nfs-ls.1
man1_MANS = nfs-cat.1 nfs-ls.1
doc:
-test -z "$(XSLTPROC)" || $(XSLTPROC) -o nfs-cat.1 http://docbook.sourceforge.net/release/xsl/current/manpages/docbook.xsl nfs-cat.1.xml
-test -z "$(XSLTPROC)" || $(XSLTPROC) -o nfs-ls.1 http://docbook.sourceforge.net/release/xsl/current/manpages/docbook.xsl nfs-ls.1.xml

54
doc/nfs-cat.1 Normal file
View File

@ -0,0 +1,54 @@
'\" t
.\" Title: nfs-cat
.\" Author: [FIXME: author] [see http://docbook.sf.net/el/author]
.\" Generator: DocBook XSL Stylesheets v1.78.1 <http://docbook.sf.net/>
.\" Date: 05/24/2015
.\" Manual: nfs-cat: read a file from nfs
.\" Source: nfs-cat
.\" Language: English
.\"
.TH "NFS\-CAT" "1" "05/24/2015" "nfs\-cat" "nfs\-cat: read a file from nfs"
.\" -----------------------------------------------------------------
.\" * Define some portability stuff
.\" -----------------------------------------------------------------
.\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
.\" http://bugs.debian.org/507673
.\" http://lists.gnu.org/archive/html/groff/2009-02/msg00013.html
.\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
.ie \n(.g .ds Aq \(aq
.el .ds Aq '
.\" -----------------------------------------------------------------
.\" * set default formatting
.\" -----------------------------------------------------------------
.\" disable hyphenation
.nh
.\" disable justification (adjust text to left margin only)
.ad l
.\" -----------------------------------------------------------------
.\" * MAIN CONTENT STARTS HERE *
.\" -----------------------------------------------------------------
.SH "NAME"
nfs-cat \- Utility to read a file off NFS
.SH "SYNOPSIS"
.HP \w'\fBnfs\-cat\ <NFS\-URL>\fR\ 'u
\fBnfs\-cat <NFS\-URL>\fR
.SH "DESCRIPTION"
.PP
nfs\-cat is a utility to read a file off an NFS server\&.
.PP
Example: Print the content of a file:
.sp
.if n \{\
.RS 4
.\}
.nf
$ nfs\-cat nfs://127\&.0\&.0\&.1/data/tmp/foo\&.c
.fi
.if n \{\
.RE
.\}
.sp
.SH "SEE ALSO"
.PP
\m[blue]\fB\%http://github.com/sahlberg/libnfs\fR\m[]

42
doc/nfs-cat.1.xml Normal file
View File

@ -0,0 +1,42 @@
<?xml version="1.0" encoding="iso-8859-1"?>
<refentry id="nfs-ls.1">
<refmeta>
<refentrytitle>nfs-cat</refentrytitle>
<manvolnum>1</manvolnum>
<refmiscinfo class="source">nfs-cat</refmiscinfo>
<refmiscinfo class="manual">nfs-cat: read a file from nfs</refmiscinfo>
</refmeta>
<refnamediv>
<refname>nfs-cat</refname>
<refpurpose>Utility to read a file off NFS</refpurpose>
</refnamediv>
<refsynopsisdiv>
<cmdsynopsis>
<command>nfs-cat &lt;NFS-URL&gt;</command>
</cmdsynopsis>
</refsynopsisdiv>
<refsect1><title>DESCRIPTION</title>
<para>
nfs-cat is a utility to read a file off an NFS server.
</para>
<para>
Example: Print the content of a file:
<screen format="linespecific">
$ nfs-cat nfs://127.0.0.1/data/tmp/foo.c
</screen>
</para>
</refsect1>
<refsect1><title>SEE ALSO</title>
<para>
<ulink url="http://github.com/sahlberg/libnfs"/>
</para>
</refsect1>
</refentry>

View File

@ -2,12 +2,12 @@
.\" Title: nfs-ls
.\" Author: [FIXME: author] [see http://docbook.sf.net/el/author]
.\" Generator: DocBook XSL Stylesheets v1.78.1 <http://docbook.sf.net/>
.\" Date: 05/14/2014
.\" Date: 05/24/2015
.\" Manual: nfs-ls: list servers, exports and directories
.\" Source: nfs-ls
.\" Language: English
.\"
.TH "NFS\-LS" "1" "05/14/2014" "nfs\-ls" "nfs\-ls: list servers, exports"
.TH "NFS\-LS" "1" "05/24/2015" "nfs\-ls" "nfs\-ls: list servers, exports"
.\" -----------------------------------------------------------------
.\" * Define some portability stuff
.\" -----------------------------------------------------------------

View File

@ -105,6 +105,8 @@ Utility programs for LibNFS
%files utils
%defattr(-,root,root)
%{_bindir}/nfs-ls
%{_bindir}/nfs-cat
%{_mandir}/man1/nfs-cat.1.gz
%{_mandir}/man1/nfs-ls.1.gz
%changelog

View File

@ -1,4 +1,4 @@
bin_PROGRAMS = nfs-ls
bin_PROGRAMS = nfs-cat nfs-ls
AM_CPPFLAGS = \
-I$(abs_top_srcdir)/include \

202
utils/nfs-cat.c Normal file
View File

@ -0,0 +1,202 @@
/*
Copyright (C) by Ronnie Sahlberg <ronniesahlberg@gmail.com> 2015
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, see <http://www.gnu.org/licenses/>.
*/
#define _FILE_OFFSET_BITS 64
#define _GNU_SOURCE
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#ifdef AROS
#include "aros_compat.h"
#endif
#ifdef WIN32
#include "win32_compat.h"
#pragma comment(lib, "ws2_32.lib")
WSADATA wsaData;
#else
#include <sys/stat.h>
#include <string.h>
#endif
#ifdef HAVE_POLL_H
#include <poll.h>
#endif
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <sys/types.h>
#include <fcntl.h>
#include "libnfs.h"
#include "libnfs-raw.h"
#include "libnfs-raw-mount.h"
struct file_context {
int fd;
struct nfs_context *nfs;
struct nfsfh *nfsfh;
struct nfs_url *url;
};
void usage(void)
{
fprintf(stderr, "Usage: nfs-cat <file>\n");
fprintf(stderr, "<file> cat an nfs file.\n");
exit(0);
}
static void
free_file_context(struct file_context *file_context)
{
if (file_context->fd != -1) {
close(file_context->fd);
}
if (file_context->nfsfh != NULL) {
nfs_close(file_context->nfs, file_context->nfsfh);
}
if (file_context->nfs != NULL) {
nfs_destroy_context(file_context->nfs);
}
nfs_destroy_url(file_context->url);
free(file_context);
}
static struct file_context *
open_file(const char *url, int flags)
{
struct file_context *file_context;
file_context = malloc(sizeof(struct file_context));
if (file_context == NULL) {
fprintf(stderr, "Failed to malloc file_context\n");
return NULL;
}
file_context->fd = -1;
file_context->nfs = NULL;
file_context->nfsfh = NULL;
file_context->url = NULL;
file_context->nfs = nfs_init_context();
if (file_context->nfs == NULL) {
fprintf(stderr, "failed to init context\n");
free_file_context(file_context);
return NULL;
}
rpc_set_tcp_syncnt(nfs_get_rpc_context(file_context->nfs), 2);
file_context->url = nfs_parse_url_full(file_context->nfs, url);
if (file_context->url == NULL) {
fprintf(stderr, "%s\n", nfs_get_error(file_context->nfs));
free_file_context(file_context);
return NULL;
}
if (nfs_mount(file_context->nfs, file_context->url->server,
file_context->url->path) != 0) {
fprintf(stderr, "Failed to mount nfs share : %s\n",
nfs_get_error(file_context->nfs));
free_file_context(file_context);
return NULL;
}
if (flags == O_RDONLY) {
if (nfs_open(file_context->nfs, file_context->url->file, flags,
&file_context->nfsfh) != 0) {
fprintf(stderr, "Failed to open file %s: %s\n",
file_context->url->file,
nfs_get_error(file_context->nfs));
free_file_context(file_context);
return NULL;
}
} else {
if (nfs_creat(file_context->nfs, file_context->url->file, 0660,
&file_context->nfsfh) != 0) {
fprintf(stderr, "Failed to creat file %s: %s\n",
file_context->url->file,
nfs_get_error(file_context->nfs));
free_file_context(file_context);
return NULL;
}
}
return file_context;
}
#define BUFSIZE 1024*1024
static char buf[BUFSIZE];
int main(int argc, char *argv[])
{
int ret;
struct file_context *nf;
struct nfs_stat_64 st;
uint64_t off;
int64_t count;
#ifdef WIN32
if (WSAStartup(MAKEWORD(2,2), &wsaData) != 0) {
printf("Failed to start Winsock2\n");
return 10;
}
#endif
#ifdef AROS
aros_init_socket();
#endif
if (argc < 2) {
usage();
}
nf = open_file(argv[1], O_RDONLY);
if (nf == NULL) {
fprintf(stderr, "Failed to open %s\n", argv[1]);
exit(10);
}
if (nfs_fstat64(nf->nfs, nf->nfsfh, &st) < 0) {
fprintf(stderr, "Failed to stat %s\n", argv[1]);
exit(10);
}
off = 0;
while (off < st.nfs_size) {
count = st.nfs_size - off;
if (count > BUFSIZE) {
count = BUFSIZE;
}
count = nfs_pread(nf->nfs, nf->nfsfh, off, count, buf);
if (count < 0) {
fprintf(stderr, "Failed to read from file\n");
free_file_context(nf);
return 10;
}
fwrite(buf, count, 1, stdout);
off += count;
}
free_file_context(nf);
return 0;
}