summaryrefslogtreecommitdiff
path: root/utils/storeread.c
blob: 2586a8c250e6c80ad7b3532eac516d8608c56160 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/* Write portions of a store to stdout

   Copyright (C) 1996,97,99,2001,02,03,04 Free Software Foundation, Inc.
   Written by Miles Bader <miles@gnu.org>
   This file is part of the GNU Hurd.

   The GNU Hurd 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 2, or (at
   your option) any later version.

   The GNU Hurd 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, write to the Free Software Foundation, Inc.,
   59 Temple Place - Suite 330, Boston, MA 02111, USA. */

#include <argp.h>
#include <error.h>
#include <unistd.h>
#include <stdlib.h>
#include <hurd.h>
#include <sys/fcntl.h>
#include <sys/mman.h>

#include <hurd/store.h>
#include <version.h>

const char *argp_program_version = STANDARD_HURD_VERSION (storeread);

struct argp_option options[] = {
  {"file", 'f', 0, 0, "Use file IO instead of the raw device"},
  {"block-size", 'b', "BYTES", 0, "Set the file block size"},
  {0, 0}
};
const char arg_doc[] = "FILE [ADDR [LENGTH]]...";
const char doc[] = "Write portions of the contents of a store to stdout"
"\vADDR is in blocks, and defaults to 0;"
" LENGTH is in bytes, and defaults to the remainder of FILE.";

int
main (int argc, char **argv)
{
  struct store *store = 0;
  store_offset_t addr = -1;
  int dumped = 0, use_file_io = 0, block_size = 0;

  void dump (store_offset_t addr,  ssize_t len)
    {
      char buf[4096];
      void *data = buf;
      size_t data_len = sizeof (buf);

      /* XXX: store->size can be too big for len.  */
      error_t err =
	store_read (store, addr, len < 0 ? store->size : len,
		    &data, &data_len);
      if (err)
	error (5, err, store->name ? "%s" : "<store>", store->name);
      if (write (1, data, data_len) < 0)
	error (6, errno, "stdout");
      if (data != buf)
	munmap (data, data_len);
    }

  error_t parse_opt (int key, char *arg, struct argp_state *state)
    {
      switch (key)
	{
	case 'f': use_file_io = 1; break;
	case 'b': block_size = atoi (arg); break;

	case ARGP_KEY_ARG:
	  if (! store)
	    {
	      error_t err;
	      file_t source = file_name_lookup (arg, O_READ, 0);
	      if (errno)
		error (2, errno, "%s", arg);
	      if (use_file_io)
		if (block_size)
		  {
		    struct stat stat;
		    err = io_stat (source, &stat);
		    if (! err)
		      {
			struct store_run run = {0, stat.st_size / block_size};
			err = _store_file_create (source, 0, block_size, &run, 1,
						  &store);
		      }
		  }
		else
		  err = store_file_create (source, 0, &store);
	      else
		err = store_create (source, 0, 0, &store);
	      if (err)
		error (3, err, "%s", arg);
	    }
	  else if (addr < 0)
	    addr = atoll (arg);
	  else
	    {
	      dump (addr, atoi (arg));
	      dumped = 1;
	      addr = -1;
	    }
	  break;

	case ARGP_KEY_END:
	  if (!store)
	    argp_usage (state);

	  if (addr >= 0)
	    dump (addr, -1);
	  else if (! dumped)
	    dump (0, -1);
	  break;

	case ARGP_KEY_NO_ARGS:
	default:
	  return ARGP_ERR_UNKNOWN;
	}
      return 0;
    }
  struct argp argp = {options, parse_opt, arg_doc, doc};
  argp_parse (&argp, argc, argv, 0, 0, 0);
  exit (0);
}