1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 1997-9, 2000, 2006, 2009, 2010, 2011, 2012, 2013 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. */
54 enum fh_line_ends line_ends; /* Line ends for text files. */
56 /* FH_REF_FILE and FH_REF_INLINE only. */
57 size_t record_width; /* Length of fixed-format records. */
58 size_t tab_width; /* Tab width, 0=do not expand tabs. */
59 char *encoding; /* Charset for contents. */
61 /* FH_REF_DATASET only. */
62 struct dataset *ds; /* Dataset. */
65 /* All "struct file_handle"s with nonnull 'id' member. */
66 static struct hmap named_handles = HMAP_INITIALIZER (named_handles);
68 /* Default file handle for DATA LIST, REREAD, REPEATING DATA
70 static struct file_handle *default_handle;
72 /* The "file" that reads from BEGIN DATA...END DATA. */
73 static struct file_handle *inline_file;
75 static struct file_handle *create_handle (const char *id,
76 char *name, enum fh_referent,
77 const char *encoding);
78 static void free_handle (struct file_handle *);
79 static void unname_handle (struct file_handle *);
81 /* Hash table of all active locks. */
82 static struct hmap locks = HMAP_INITIALIZER (locks);
84 /* File handle initialization routine. */
88 inline_file = create_handle ("INLINE", xstrdup ("INLINE"), FH_REF_INLINE,
90 inline_file->record_width = 80;
91 inline_file->tab_width = 8;
94 /* Removes all named file handles from the global list. */
98 struct file_handle *handle, *next;
100 HMAP_FOR_EACH_SAFE (handle, next,
101 struct file_handle, name_node, &named_handles)
102 unname_handle (handle);
105 /* Free HANDLE and remove it from the global list. */
107 free_handle (struct file_handle *handle)
109 /* Remove handle from global list. */
110 if (handle->id != NULL)
111 hmap_delete (&named_handles, &handle->name_node);
116 free (handle->file_name);
117 free (handle->encoding);
121 /* Make HANDLE unnamed, so that it can no longer be referenced by
122 name. The caller must hold a reference to HANDLE, which is
123 not affected by this function. */
125 unname_handle (struct file_handle *handle)
127 assert (handle->id != NULL);
130 hmap_delete (&named_handles, &handle->name_node);
132 /* Drop the reference held by the named_handles table. */
136 /* Increments HANDLE's reference count and returns HANDLE. */
138 fh_ref (struct file_handle *handle)
140 assert (handle->ref_cnt > 0);
145 /* Decrements HANDLE's reference count.
146 If the reference count drops to 0, HANDLE is destroyed. */
148 fh_unref (struct file_handle *handle)
152 assert (handle->ref_cnt > 0);
153 if (--handle->ref_cnt == 0)
154 free_handle (handle);
158 /* Make HANDLE unnamed, so that it can no longer be referenced by
159 name. The caller must hold a reference to HANDLE, which is
160 not affected by this function.
162 This function ignores a null pointer as input. It has no
163 effect on the inline handle, which is always named INLINE.*/
165 fh_unname (struct file_handle *handle)
167 assert (handle->ref_cnt > 1);
168 if (handle != fh_inline_file () && handle->id != NULL)
169 unname_handle (handle);
172 /* Returns the handle with the given ID, or a null pointer if
175 fh_from_id (const char *id)
177 struct file_handle *handle;
179 HMAP_FOR_EACH_WITH_HASH (handle, struct file_handle, name_node,
180 utf8_hash_case_string (id, 0), &named_handles)
181 if (!utf8_strcasecmp (id, handle->id))
183 return fh_ref (handle);
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 hmap_insert (&named_handles, &handle->name_node,
210 utf8_hash_case_string (handle->id, 0));
216 /* Returns the unique handle of referent type FH_REF_INLINE,
217 which refers to the "inline file" that represents character
218 data in the command file between BEGIN DATA and END DATA. */
220 fh_inline_file (void)
225 /* Creates and returns a new file handle with the given ID, which may be null.
226 If it is non-null, it must be a UTF-8 encoded string that is unique among
227 existing file identifiers. The new handle is associated with file FILE_NAME
228 and the given PROPERTIES. */
230 fh_create_file (const char *id, const char *file_name,
231 const struct fh_properties *properties)
234 struct file_handle *handle;
236 handle_name = id != NULL ? xstrdup (id) : xasprintf ("`%s'", file_name);
237 handle = create_handle (id, handle_name, FH_REF_FILE, properties->encoding);
238 handle->file_name = xstrdup (file_name);
239 handle->mode = properties->mode;
240 handle->line_ends = properties->line_ends;
241 handle->record_width = properties->record_width;
242 handle->tab_width = properties->tab_width;
246 /* Creates a new file handle with the given ID, which must be
247 unique among existing file identifiers. The new handle is
248 associated with a dataset file (initially empty). */
250 fh_create_dataset (struct dataset *ds)
253 struct file_handle *handle;
255 name = dataset_name (ds);
257 name = _("active dataset");
259 handle = create_handle (NULL, xstrdup (name), FH_REF_DATASET, C_ENCODING);
264 /* Returns a set of default properties for a file handle. */
265 const struct fh_properties *
266 fh_default_properties (void)
268 static const struct fh_properties default_properties
269 = {FH_MODE_TEXT, FH_END_LF, 1024, 4, (char *) "Auto"};
270 return &default_properties;
273 /* Returns the identifier that may be used in syntax to name the
274 given HANDLE, which takes the form of a PSPP identifier. If
275 HANDLE has no identifier, returns a null pointer.
277 Return value is owned by the file handle.*/
279 fh_get_id (const struct file_handle *handle)
284 /* Returns a user-friendly string to identify the given HANDLE.
285 If HANDLE was created by referring to a file name, returns the
286 file name, enclosed in double quotes. Return value is owned
289 Useful for printing error messages about use of file handles. */
291 fh_get_name (const struct file_handle *handle)
296 /* Returns the type of object that HANDLE refers to. */
298 fh_get_referent (const struct file_handle *handle)
300 return handle->referent;
303 /* Returns the name of the file associated with HANDLE. */
305 fh_get_file_name (const struct file_handle *handle)
307 assert (handle->referent == FH_REF_FILE);
308 return handle->file_name;
311 /* Returns the mode of HANDLE. */
313 fh_get_mode (const struct file_handle *handle)
315 assert (handle->referent == FH_REF_FILE);
319 /* Returns the line ends of HANDLE, which must be a handle associated with a
322 fh_get_line_ends (const struct file_handle *handle)
324 assert (handle->referent == FH_REF_FILE);
325 return handle->line_ends;
328 /* Returns the width of a logical record on HANDLE. */
330 fh_get_record_width (const struct file_handle *handle)
332 assert (handle->referent & (FH_REF_FILE | FH_REF_INLINE));
333 return handle->record_width;
336 /* Returns the number of characters per tab stop for HANDLE, or
337 zero if tabs are not to be expanded. Applicable only to
338 FH_MODE_TEXT files. */
340 fh_get_tab_width (const struct file_handle *handle)
342 assert (handle->referent & (FH_REF_FILE | FH_REF_INLINE));
343 return handle->tab_width;
346 /* Returns the encoding of characters read from HANDLE. */
348 fh_get_encoding (const struct file_handle *handle)
350 return handle->encoding;
353 /* Returns the dataset handle associated with HANDLE.
354 Applicable to only FH_REF_DATASET files. */
356 fh_get_dataset (const struct file_handle *handle)
358 assert (handle->referent == FH_REF_DATASET);
362 /* Returns the current default handle. */
364 fh_get_default_handle (void)
366 return default_handle ? default_handle : fh_inline_file ();
369 /* Sets NEW_DEFAULT_HANDLE as the default handle. */
371 fh_set_default_handle (struct file_handle *new_default_handle)
373 assert (new_default_handle == NULL
374 || (new_default_handle->referent & (FH_REF_INLINE | FH_REF_FILE)));
375 if (default_handle != NULL && default_handle != inline_file)
376 fh_unref (default_handle);
377 default_handle = new_default_handle;
378 if (default_handle != NULL)
379 fh_ref (default_handle);
382 /* Information about a file handle's readers or writers. */
385 struct hmap_node node; /* hmap_node member. */
388 enum fh_referent referent; /* Type of underlying file. */
391 struct file_identity *file; /* FH_REF_FILE only. */
392 unsigned int unique_id; /* FH_REF_DATASET only. */
395 enum fh_access access; /* Type of file access. */
397 /* Number of openers. */
400 /* Applicable only when open_cnt > 0. */
401 bool exclusive; /* No other openers allowed? */
402 const char *type; /* Human-readable type of file. */
403 void *aux; /* Owner's auxiliary data. */
407 static void make_key (struct fh_lock *, const struct file_handle *,
409 static void free_key (struct fh_lock *);
410 static int compare_fh_locks (const struct fh_lock *a, const struct fh_lock *b);
411 static unsigned int hash_fh_lock (const struct fh_lock *lock);
413 /* Tries to lock handle H for the given kind of ACCESS and TYPE
414 of file. Returns a pointer to a struct fh_lock if successful,
415 otherwise a null pointer.
417 H's referent type must be one of the bits in MASK. The caller
418 must verify this ahead of time; we simply assert it here.
420 TYPE is the sort of file, e.g. "system file". Only one type
421 of access is allowed on a given file at a time for reading,
422 and similarly for writing. If successful, a reference to TYPE
423 is retained, so it should probably be a string literal.
425 TYPE should be marked with N_() in the caller: that is, the
426 caller should not translate it with gettext, but fh_lock will
429 ACCESS specifies whether the lock is for reading or writing.
430 EXCLUSIVE is true to require exclusive access, false to allow
431 sharing with other accessors. Exclusive read access precludes
432 other readers, but not writers; exclusive write access
433 precludes other writers, but not readers. A sharable read or
434 write lock precludes reader or writers, respectively, of a
437 A lock may be associated with auxiliary data. See
438 fh_lock_get_aux and fh_lock_set_aux for more details. */
440 fh_lock (struct file_handle *h, enum fh_referent mask UNUSED,
441 const char *type, enum fh_access access, bool exclusive)
443 struct fh_lock *key = NULL;
445 struct fh_lock *lock = NULL;
446 bool found_lock = false;
448 assert ((fh_get_referent (h) & mask) != 0);
449 assert (access == FH_ACC_READ || access == FH_ACC_WRITE);
451 key = xmalloc (sizeof *key);
453 make_key (key, h, access);
456 key->exclusive = exclusive;
460 hash = hash_fh_lock (key);
462 HMAP_FOR_EACH_WITH_HASH (lock, struct fh_lock, node, hash, &locks)
464 if ( 0 == compare_fh_locks (lock, key))
473 if (strcmp (lock->type, type))
475 if (access == FH_ACC_READ)
476 msg (SE, _("Can't read from %s as a %s because it is "
477 "already being read as a %s."),
478 fh_get_name (h), gettext (type), gettext (lock->type));
480 msg (SE, _("Can't write to %s as a %s because it is "
481 "already being written as a %s."),
482 fh_get_name (h), gettext (type), gettext (lock->type));
485 else if (exclusive || lock->exclusive)
487 msg (SE, _("Can't re-open %s as a %s."),
488 fh_get_name (h), gettext (type));
499 hmap_insert (&locks, &key->node, hash);
501 HMAP_FOR_EACH_WITH_HASH (lock, struct fh_lock, node, hash, &locks)
503 if ( 0 == compare_fh_locks (lock, key))
515 /* Releases LOCK that was acquired with fh_lock.
516 Returns true if LOCK is still locked, because other clients
519 Returns false if LOCK has now been destroyed. In this case
520 the caller must ensure that any auxiliary data associated with
521 LOCK is destroyed, to avoid a memory leak. The caller must
522 obtain a pointer to the auxiliary data, e.g. via
523 fh_lock_get_aux *before* calling fh_unlock (because it yields
524 undefined behavior to call fh_lock_get_aux on a destroyed
527 fh_unlock (struct fh_lock *lock)
531 assert (lock->open_cnt > 0);
532 if (--lock->open_cnt == 0)
534 hmap_delete (&locks, &lock->node);
543 /* Returns auxiliary data for LOCK.
545 Auxiliary data is shared by every client that holds LOCK (for
546 an exclusive lock, this is a single client). To avoid leaks,
547 auxiliary data must be released before LOCK is destroyed. */
549 fh_lock_get_aux (const struct fh_lock *lock)
554 /* Sets the auxiliary data for LOCK to AUX. */
556 fh_lock_set_aux (struct fh_lock *lock, void *aux)
561 /* Returns true if HANDLE is locked for the given type of ACCESS,
564 fh_is_locked (const struct file_handle *handle, enum fh_access access)
567 const struct fh_lock *k = NULL;
568 bool is_locked = false;
571 make_key (&key, handle, access);
573 hash = hash_fh_lock (&key);
576 HMAP_FOR_EACH_WITH_HASH (k, struct fh_lock, node, hash, &locks)
578 if ( 0 == compare_fh_locks (k, &key))
590 /* Initializes the key fields in LOCK for looking up or inserting
591 handle H for the given kind of ACCESS. */
593 make_key (struct fh_lock *lock, const struct file_handle *h,
594 enum fh_access access)
596 lock->referent = fh_get_referent (h);
597 lock->access = access;
598 if (lock->referent == FH_REF_FILE)
599 lock->u.file = fn_get_identity (fh_get_file_name (h));
600 else if (lock->referent == FH_REF_DATASET)
601 lock->u.unique_id = dataset_seqno (fh_get_dataset (h));
604 /* Frees the key fields in LOCK. */
606 free_key (struct fh_lock *lock)
608 if (lock->referent == FH_REF_FILE)
609 fn_free_identity (lock->u.file);
612 /* Compares the key fields in struct fh_lock objects A and B and
613 returns a strcmp()-type result. */
615 compare_fh_locks (const struct fh_lock *a, const struct fh_lock *b)
617 if (a->referent != b->referent)
618 return a->referent < b->referent ? -1 : 1;
619 else if (a->access != b->access)
620 return a->access < b->access ? -1 : 1;
621 else if (a->referent == FH_REF_FILE)
622 return fn_compare_file_identities (a->u.file, b->u.file);
623 else if (a->referent == FH_REF_DATASET)
624 return (a->u.unique_id < b->u.unique_id ? -1
625 : a->u.unique_id > b->u.unique_id);
630 /* Returns a hash value for LOCK. */
632 hash_fh_lock (const struct fh_lock *lock)
635 if (lock->referent == FH_REF_FILE)
636 basis = fn_hash_identity (lock->u.file);
637 else if (lock->referent == FH_REF_DATASET)
638 basis = lock->u.unique_id;
641 return hash_int ((lock->referent << 3) | lock->access, basis);