summaryrefslogtreecommitdiff
path: root/libstore/copy.c
blob: ded612fe25611385f490bfbb853fa99ae5ea5a44 (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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
/* Copy store backend

   Copyright (C) 1995, 1996, 1997, 1999 Free Software Foundation, Inc.

   Written by Miles Bader <miles@gnu.ai.mit.edu>

   This task 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 <stdio.h>
#include <string.h>
#include <malloc.h>
#include <sys/mman.h>

#include "store.h"

static error_t
copy_read (struct store *store,
	   off_t addr, size_t index, size_t amount, void **buf, size_t *len)
{
  if (*len < amount)
    /* Have to allocate memory for the return value.  */
    {
      error_t err =
	vm_allocate (mach_task_self (), (vm_address_t *)buf, amount, 1);
      if (err)
	return err;
    }

  bcopy (store->hook + (addr * store->block_size), *buf, amount);
  *len = amount;
  return 0;
}

static error_t
copy_write (struct store *store,
	    off_t addr, size_t index, void *buf, size_t len, size_t *amount)
{
  bcopy (buf, store->hook + (addr * store->block_size), len);
  *amount = len;
  return 0;
}

error_t
copy_allocate_encoding (const struct store *store, struct store_enc *enc)
{
  /* ... */
  return EOPNOTSUPP;
}

error_t
copy_encode (const struct store *store, struct store_enc *enc)
{
  /* ... */
  return EOPNOTSUPP;
}

static error_t
copy_decode (struct store_enc *enc, const struct store_class *const *classes,
	     struct store **store)
{
  /* ... */
  return EOPNOTSUPP;
}

static error_t
copy_open (const char *name, int flags,
	   const struct store_class *const *classes,
	   struct store **store)
{
  return store_copy_open (name, flags, classes, store);
}

static error_t
copy_set_flags (struct store *store, int flags)
{
  if ((flags & ~(STORE_INACTIVE | STORE_ENFORCED)) != 0)
    /* Trying to set flags we don't support.  */
    return EINVAL;

  /* ... */

  store->flags |= flags;	/* When inactive, anything goes.  */

  return 0;
}

static error_t
copy_clear_flags (struct store *store, int flags)
{
  error_t err = 0;
  if ((flags & ~(STORE_INACTIVE | STORE_ENFORCED)) != 0)
    err = EINVAL;
  /* ... */
  if (! err)
    store->flags &= ~flags;
  return err;
}

/* Called just before deallocating STORE.  */
void
copy_cleanup (struct store *store)
{
  if (store->size > 0)
    munmap (store->hook, store->size);
}

/* Copy any format-dependent fields in FROM to TO; if there's some reason
   why the copy can't be made, an error should be returned.  This call is
   made after all format-indendependent fields have been cloned.  */
error_t
copy_clone (const struct store *from, struct store *to)
{
  error_t err =
    vm_allocate (mach_task_self (), (vm_address_t *)&to->hook, to->size, 1);
  if (! err)
    bcopy (from->hook, to->hook, from->size);
  return err;
}

struct store_class
store_copy_class =
{
  STORAGE_COPY, "copy", copy_read, copy_write,
  copy_allocate_encoding, copy_encode, copy_decode,
  copy_set_flags, copy_clear_flags, copy_cleanup, copy_clone, 0, copy_open
};

/* Return a new store in STORE which contains a snapshot of the contents of
   the store FROM; FROM is consumed.  */
error_t
store_copy_create (struct store *from, int flags, struct store **store)
{
  error_t err;
  struct store_run run;

  run.start = 0;
  run.length = from->size;

  flags |= STORE_ENFORCED;	/* Only uses local resources.  */

  err =
    _store_create (&store_copy_class,
		   MACH_PORT_NULL, flags, from->block_size, &run, 1, 0,
		   store);
  if (! err)
    {
      size_t buf_len = 0;

      /* Copy the input store.  */
      err = store_read (from, 0, from->size, &(*store)->hook, &buf_len);

      if (! err)
	/* Set the store name.  */
	{
	  if (from->name)
	    {
	      size_t len =
		strlen (from->class->name) + 1 + strlen (from->name) + 1;
	      (*store)->name = malloc (len);
	      if ((*store)->name)
		snprintf ((*store)->name, len,
			  "%s:%s", from->class->name, from->name);
	    }
	  else
	    (*store)->name = strdup (from->class->name);

	  if (! (*store)->name)
	    err = ENOMEM;
	}

      if (err)
	store_free (*store);
    }

  return err;
}

/* Return a new store in STORE which contains the memory buffer BUF, of
   length BUF_LEN, and uses the block size BLOCK_SIZE.  BUF must be
   vm_allocated, and will be consumed, and BUF_LEN must be a multiple of
   BLOCK_SIZE.  */
error_t
store_buffer_create (void *buf, size_t buf_len, int flags,
		     struct store **store)
{
  error_t err;
  struct store_run run;

  run.start = 0;
  run.length = buf_len;

  flags |= STORE_ENFORCED;	/* Only uses local resources.  */

  err =
    _store_create (&store_copy_class,
		   MACH_PORT_NULL, flags, 1, &run, 1, 0, store);
  if (! err)
    (*store)->hook = buf;

  return err;
}

/* Open the copy store NAME -- which consists of another store-class name, a
   ':', and a name for that store class to open -- and return the
   corresponding store in STORE.  CLASSES is used to select classes specified
   by the type name; if it is 0, STORE_STD_CLASSES is used.  */
error_t
store_copy_open (const char *name, int flags,
		 const struct store_class *const *classes,
		 struct store **store)
{
  struct store *from;
  error_t err =
    store_typed_open (name, flags | STORE_HARD_READONLY, classes, &from);

  if (! err)
    {
      err = store_copy_create (from, flags, store);
      if (err)
	store_free (from);
    }

  return err;
}