1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 1997-9, 2000, 2006, 2009, 2010, 2011, 2012 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19 #include "data/file-handle-def.h"
26 #include "data/dataset.h"
27 #include "data/file-name.h"
28 #include "data/variable.h"
29 #include "libpspp/cast.h"
30 #include "libpspp/compiler.h"
31 #include "libpspp/hash-functions.h"
32 #include "libpspp/hmap.h"
33 #include "libpspp/i18n.h"
34 #include "libpspp/message.h"
35 #include "libpspp/str.h"
37 #include "gl/xalloc.h"
40 #define _(msgid) gettext (msgid)
45 struct hmap_node name_node; /* Element in named_handles hmap. */
46 size_t ref_cnt; /* Number of references. */
47 char *id; /* Identifier token, NULL if none. */
48 char *name; /* User-friendly identifying name. */
49 enum fh_referent referent; /* What the file handle refers to. */
51 /* FH_REF_FILE only. */
52 char *file_name; /* File name as provided by user. */
53 enum fh_mode mode; /* File mode. */
55 /* FH_REF_FILE and FH_REF_INLINE only. */
56 size_t record_width; /* Length of fixed-format records. */
57 size_t tab_width; /* Tab width, 0=do not expand tabs. */
58 char *encoding; /* Charset for contents. */
60 /* FH_REF_DATASET only. */
61 struct dataset *ds; /* Dataset. */
64 /* All "struct file_handle"s with nonnull 'id' member. */
65 static struct hmap named_handles = HMAP_INITIALIZER (named_handles);
67 /* Default file handle for DATA LIST, REREAD, REPEATING DATA
69 static struct file_handle *default_handle;
71 /* The "file" that reads from BEGIN DATA...END DATA. */
72 static struct file_handle *inline_file;
74 static struct file_handle *create_handle (const char *id,
75 char *name, enum fh_referent,
76 const char *encoding);
77 static void free_handle (struct file_handle *);
78 static void unname_handle (struct file_handle *);
80 /* Hash table of all active locks. */
81 static struct hmap locks = HMAP_INITIALIZER (locks);
83 /* File handle initialization routine. */
87 inline_file = create_handle ("INLINE", xstrdup ("INLINE"), FH_REF_INLINE,
89 inline_file->record_width = 80;
90 inline_file->tab_width = 8;
93 /* Removes all named file handles from the global list. */
97 struct file_handle *handle, *next;
99 HMAP_FOR_EACH_SAFE (handle, next,
100 struct file_handle, name_node, &named_handles)
101 unname_handle (handle);
104 /* Free HANDLE and remove it from the global list. */
106 free_handle (struct file_handle *handle)
108 /* Remove handle from global list. */
109 if (handle->id != NULL)
110 hmap_delete (&named_handles, &handle->name_node);
115 free (handle->file_name);
116 free (handle->encoding);
120 /* Make HANDLE unnamed, so that it can no longer be referenced by
121 name. The caller must hold a reference to HANDLE, which is
122 not affected by this function. */
124 unname_handle (struct file_handle *handle)
126 assert (handle->id != NULL);
129 hmap_delete (&named_handles, &handle->name_node);
131 /* Drop the reference held by the named_handles table. */
135 /* Increments HANDLE's reference count and returns HANDLE. */
137 fh_ref (struct file_handle *handle)
139 assert (handle->ref_cnt > 0);
144 /* Decrements HANDLE's reference count.
145 If the reference count drops to 0, HANDLE is destroyed. */
147 fh_unref (struct file_handle *handle)
151 assert (handle->ref_cnt > 0);
152 if (--handle->ref_cnt == 0)
153 free_handle (handle);
157 /* Make HANDLE unnamed, so that it can no longer be referenced by
158 name. The caller must hold a reference to HANDLE, which is
159 not affected by this function.
161 This function ignores a null pointer as input. It has no
162 effect on the inline handle, which is always named INLINE.*/
164 fh_unname (struct file_handle *handle)
166 assert (handle->ref_cnt > 1);
167 if (handle != fh_inline_file () && handle->id != NULL)
168 unname_handle (handle);
171 /* Returns the handle with the given ID, or a null pointer if
174 fh_from_id (const char *id)
176 struct file_handle *handle;
178 HMAP_FOR_EACH_WITH_HASH (handle, struct file_handle, name_node,
179 hash_case_string (id, 0), &named_handles)
180 if (!strcasecmp (id, handle->id))
189 /* Creates a new handle with identifier ID (which may be null)
190 and name HANDLE_NAME that refers to REFERENT. Links the new
191 handle into the global list. Returns the new handle.
193 The new handle is not fully initialized. The caller is
194 responsible for completing its initialization. */
195 static struct file_handle *
196 create_handle (const char *id, char *handle_name, enum fh_referent referent,
197 const char *encoding)
199 struct file_handle *handle = xzalloc (sizeof *handle);
202 handle->id = id != NULL ? xstrdup (id) : NULL;
203 handle->name = handle_name;
204 handle->referent = referent;
205 handle->encoding = xstrdup (encoding);
209 assert (fh_from_id (id) == NULL);
210 hmap_insert (&named_handles, &handle->name_node,
211 hash_case_string (handle->id, 0));
218 /* Returns the unique handle of referent type FH_REF_INLINE,
219 which refers to the "inline file" that represents character
220 data in the command file between BEGIN DATA and END DATA. */
222 fh_inline_file (void)
224 fh_ref (inline_file);
228 /* Creates and returns a new file handle with the given ID, which may be null.
229 If it is non-null, it must be a UTF-8 encoded string that is unique among
230 existing file identifiers. The new handle is associated with file FILE_NAME
231 and the given PROPERTIES. */
233 fh_create_file (const char *id, const char *file_name,
234 const struct fh_properties *properties)
237 struct file_handle *handle;
239 handle_name = id != NULL ? xstrdup (id) : xasprintf ("`%s'", file_name);
240 handle = create_handle (id, handle_name, FH_REF_FILE, properties->encoding);
241 handle->file_name = xstrdup (file_name);
242 handle->mode = properties->mode;
243 handle->record_width = properties->record_width;
244 handle->tab_width = properties->tab_width;
248 /* Creates a new file handle with the given ID, which must be
249 unique among existing file identifiers. The new handle is
250 associated with a dataset file (initially empty). */
252 fh_create_dataset (struct dataset *ds)
255 struct file_handle *handle;
257 name = dataset_name (ds);
259 name = _("active dataset");
261 handle = create_handle (NULL, xstrdup (name), FH_REF_DATASET, C_ENCODING);
266 /* Returns a set of default properties for a file handle. */
267 const struct fh_properties *
268 fh_default_properties (void)
270 static const struct fh_properties default_properties
271 = {FH_MODE_TEXT, 1024, 4, (char *) "Auto"};
272 return &default_properties;
275 /* Returns the identifier that may be used in syntax to name the
276 given HANDLE, which takes the form of a PSPP identifier. If
277 HANDLE has no identifier, returns a null pointer.
279 Return value is owned by the file handle.*/
281 fh_get_id (const struct file_handle *handle)
286 /* Returns a user-friendly string to identify the given HANDLE.
287 If HANDLE was created by referring to a file name, returns the
288 file name, enclosed in double quotes. Return value is owned
291 Useful for printing error messages about use of file handles. */
293 fh_get_name (const struct file_handle *handle)
298 /* Returns the type of object that HANDLE refers to. */
300 fh_get_referent (const struct file_handle *handle)
302 return handle->referent;
305 /* Returns the name of the file associated with HANDLE. */
307 fh_get_file_name (const struct file_handle *handle)
309 assert (handle->referent == FH_REF_FILE);
310 return handle->file_name;
313 /* Returns the mode of HANDLE. */
315 fh_get_mode (const struct file_handle *handle)
317 assert (handle->referent == FH_REF_FILE);
321 /* Returns the width of a logical record on HANDLE. */
323 fh_get_record_width (const struct file_handle *handle)
325 assert (handle->referent & (FH_REF_FILE | FH_REF_INLINE));
326 return handle->record_width;
329 /* Returns the number of characters per tab stop for HANDLE, or
330 zero if tabs are not to be expanded. Applicable only to
331 FH_MODE_TEXT files. */
333 fh_get_tab_width (const struct file_handle *handle)
335 assert (handle->referent & (FH_REF_FILE | FH_REF_INLINE));
336 return handle->tab_width;
339 /* Returns the encoding of characters read from HANDLE. */
341 fh_get_encoding (const struct file_handle *handle)
343 return handle->encoding;
346 /* Returns the dataset handle associated with HANDLE.
347 Applicable to only FH_REF_DATASET files. */
349 fh_get_dataset (const struct file_handle *handle)
351 assert (handle->referent == FH_REF_DATASET);
355 /* Returns the current default handle. */
357 fh_get_default_handle (void)
359 return default_handle ? fh_ref (default_handle) : fh_inline_file ();
362 /* Sets NEW_DEFAULT_HANDLE as the default handle. */
364 fh_set_default_handle (struct file_handle *new_default_handle)
366 assert (new_default_handle == NULL
367 || (new_default_handle->referent & (FH_REF_INLINE | FH_REF_FILE)));
368 if (default_handle != NULL)
369 fh_unref (default_handle);
370 default_handle = new_default_handle;
371 if (default_handle != NULL)
372 fh_ref (default_handle);
375 /* Information about a file handle's readers or writers. */
378 struct hmap_node node; /* hmap_node member. */
381 enum fh_referent referent; /* Type of underlying file. */
384 struct file_identity *file; /* FH_REF_FILE only. */
385 unsigned int unique_id; /* FH_REF_DATASET only. */
388 enum fh_access access; /* Type of file access. */
390 /* Number of openers. */
393 /* Applicable only when open_cnt > 0. */
394 bool exclusive; /* No other openers allowed? */
395 const char *type; /* Human-readable type of file. */
396 void *aux; /* Owner's auxiliary data. */
400 static void make_key (struct fh_lock *, const struct file_handle *,
402 static void free_key (struct fh_lock *);
403 static int compare_fh_locks (const struct fh_lock *a, const struct fh_lock *b);
404 static unsigned int hash_fh_lock (const struct fh_lock *lock);
406 /* Tries to lock handle H for the given kind of ACCESS and TYPE
407 of file. Returns a pointer to a struct fh_lock if successful,
408 otherwise a null pointer.
410 H's referent type must be one of the bits in MASK. The caller
411 must verify this ahead of time; we simply assert it here.
413 TYPE is the sort of file, e.g. "system file". Only one type
414 of access is allowed on a given file at a time for reading,
415 and similarly for writing. If successful, a reference to TYPE
416 is retained, so it should probably be a string literal.
418 TYPE should be marked with N_() in the caller: that is, the
419 caller should not translate it with gettext, but fh_lock will
422 ACCESS specifies whether the lock is for reading or writing.
423 EXCLUSIVE is true to require exclusive access, false to allow
424 sharing with other accessors. Exclusive read access precludes
425 other readers, but not writers; exclusive write access
426 precludes other writers, but not readers. A sharable read or
427 write lock precludes reader or writers, respectively, of a
430 A lock may be associated with auxiliary data. See
431 fh_lock_get_aux and fh_lock_set_aux for more details. */
433 fh_lock (struct file_handle *h, enum fh_referent mask UNUSED,
434 const char *type, enum fh_access access, bool exclusive)
436 struct fh_lock *key = NULL;
438 struct fh_lock *lock = NULL;
439 bool found_lock = false;
441 assert ((fh_get_referent (h) & mask) != 0);
442 assert (access == FH_ACC_READ || access == FH_ACC_WRITE);
444 key = xmalloc (sizeof *key);
446 make_key (key, h, access);
449 key->exclusive = exclusive;
453 hash = hash_fh_lock (key);
455 HMAP_FOR_EACH_WITH_HASH (lock, struct fh_lock, node, hash, &locks)
457 if ( 0 == compare_fh_locks (lock, key))
466 if (strcmp (lock->type, type))
468 if (access == FH_ACC_READ)
469 msg (SE, _("Can't read from %s as a %s because it is "
470 "already being read as a %s."),
471 fh_get_name (h), gettext (type), gettext (lock->type));
473 msg (SE, _("Can't write to %s as a %s because it is "
474 "already being written as a %s."),
475 fh_get_name (h), gettext (type), gettext (lock->type));
478 else if (exclusive || lock->exclusive)
480 msg (SE, _("Can't re-open %s as a %s."),
481 fh_get_name (h), gettext (type));
492 hmap_insert (&locks, &key->node, hash);
494 HMAP_FOR_EACH_WITH_HASH (lock, struct fh_lock, node, hash, &locks)
496 if ( 0 == compare_fh_locks (lock, key))
508 /* Releases LOCK that was acquired with fh_lock.
509 Returns true if LOCK is still locked, because other clients
512 Returns false if LOCK has now been destroyed. In this case
513 the caller must ensure that any auxiliary data associated with
514 LOCK is destroyed, to avoid a memory leak. The caller must
515 obtain a pointer to the auxiliary data, e.g. via
516 fh_lock_get_aux *before* calling fh_unlock (because it yields
517 undefined behavior to call fh_lock_get_aux on a destroyed
520 fh_unlock (struct fh_lock *lock)
524 assert (lock->open_cnt > 0);
525 if (--lock->open_cnt == 0)
527 hmap_delete (&locks, &lock->node);
536 /* Returns auxiliary data for LOCK.
538 Auxiliary data is shared by every client that holds LOCK (for
539 an exclusive lock, this is a single client). To avoid leaks,
540 auxiliary data must be released before LOCK is destroyed. */
542 fh_lock_get_aux (const struct fh_lock *lock)
547 /* Sets the auxiliary data for LOCK to AUX. */
549 fh_lock_set_aux (struct fh_lock *lock, void *aux)
554 /* Returns true if HANDLE is locked for the given type of ACCESS,
557 fh_is_locked (const struct file_handle *handle, enum fh_access access)
560 const struct fh_lock *k = NULL;
561 bool is_locked = false;
564 make_key (&key, handle, access);
566 hash = hash_fh_lock (&key);
569 HMAP_FOR_EACH_WITH_HASH (k, struct fh_lock, node, hash, &locks)
571 if ( 0 == compare_fh_locks (k, &key))
583 /* Initializes the key fields in LOCK for looking up or inserting
584 handle H for the given kind of ACCESS. */
586 make_key (struct fh_lock *lock, const struct file_handle *h,
587 enum fh_access access)
589 lock->referent = fh_get_referent (h);
590 lock->access = access;
591 if (lock->referent == FH_REF_FILE)
592 lock->u.file = fn_get_identity (fh_get_file_name (h));
593 else if (lock->referent == FH_REF_DATASET)
594 lock->u.unique_id = dataset_seqno (fh_get_dataset (h));
597 /* Frees the key fields in LOCK. */
599 free_key (struct fh_lock *lock)
601 if (lock->referent == FH_REF_FILE)
602 fn_free_identity (lock->u.file);
605 /* Compares the key fields in struct fh_lock objects A and B and
606 returns a strcmp()-type result. */
608 compare_fh_locks (const struct fh_lock *a, const struct fh_lock *b)
610 if (a->referent != b->referent)
611 return a->referent < b->referent ? -1 : 1;
612 else if (a->access != b->access)
613 return a->access < b->access ? -1 : 1;
614 else if (a->referent == FH_REF_FILE)
615 return fn_compare_file_identities (a->u.file, b->u.file);
616 else if (a->referent == FH_REF_DATASET)
617 return (a->u.unique_id < b->u.unique_id ? -1
618 : a->u.unique_id > b->u.unique_id);
623 /* Returns a hash value for LOCK. */
625 hash_fh_lock (const struct fh_lock *lock)
628 if (lock->referent == FH_REF_FILE)
629 basis = fn_hash_identity (lock->u.file);
630 else if (lock->referent == FH_REF_DATASET)
631 basis = lock->u.unique_id;
634 return hash_int ((lock->referent << 3) | lock->access, basis);