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/variable.h"
28 #include "libpspp/cast.h"
29 #include "libpspp/compiler.h"
30 #include "libpspp/hash-functions.h"
31 #include "libpspp/hmap.h"
32 #include "libpspp/i18n.h"
33 #include "libpspp/message.h"
34 #include "libpspp/str.h"
38 #include "gl/dirname.h"
39 #include "gl/xalloc.h"
42 #define _(msgid) gettext (msgid)
44 #if defined _WIN32 || defined __WIN32__
45 #define WIN32_LEAN_AND_MEAN /* avoid including junk */
52 struct hmap_node name_node; /* Element in named_handles hmap. */
53 size_t ref_cnt; /* Number of references. */
54 char *id; /* Identifier token, NULL if none. */
55 char *name; /* User-friendly identifying name. */
56 enum fh_referent referent; /* What the file handle refers to. */
58 /* FH_REF_FILE only. */
59 char *file_name; /* File name as provided by user. */
60 char *file_name_encoding; /* The character encoding of file_name,
61 This is NOT the encoding of the file contents! */
62 enum fh_mode mode; /* File mode. */
63 enum fh_line_ends line_ends; /* Line ends for text files. */
65 /* FH_REF_FILE and FH_REF_INLINE only. */
66 size_t record_width; /* Length of fixed-format records. */
67 size_t tab_width; /* Tab width, 0=do not expand tabs. */
68 char *encoding; /* Charset for contents. */
70 /* FH_REF_DATASET only. */
71 struct dataset *ds; /* Dataset. */
74 /* All "struct file_handle"s with nonnull 'id' member. */
75 static struct hmap named_handles = HMAP_INITIALIZER (named_handles);
77 /* Default file handle for DATA LIST, REREAD, REPEATING DATA
79 static struct file_handle *default_handle;
81 /* The "file" that reads from BEGIN DATA...END DATA. */
82 static struct file_handle *inline_file;
84 static struct file_handle *create_handle (const char *id,
85 char *name, enum fh_referent,
86 const char *encoding);
87 static void free_handle (struct file_handle *);
88 static void unname_handle (struct file_handle *);
90 /* Hash table of all active locks. */
91 static struct hmap locks = HMAP_INITIALIZER (locks);
93 static struct file_identity *fh_get_identity (const struct file_handle *);
94 static void fh_free_identity (struct file_identity *);
95 static int fh_compare_file_identities (const struct file_identity *,
96 const struct file_identity *);
97 static unsigned int fh_hash_identity (const struct file_identity *);
99 /* File handle initialization routine. */
103 inline_file = create_handle ("INLINE", xstrdup ("INLINE"), FH_REF_INLINE,
105 inline_file->record_width = 80;
106 inline_file->tab_width = 8;
109 /* Removes all named file handles from the global list. */
113 struct file_handle *handle, *next;
115 HMAP_FOR_EACH_SAFE (handle, next,
116 struct file_handle, name_node, &named_handles)
117 unname_handle (handle);
119 free_handle (inline_file);
122 /* Free HANDLE and remove it from the global list. */
124 free_handle (struct file_handle *handle)
129 /* Remove handle from global list. */
130 if (handle->id != NULL)
131 hmap_delete (&named_handles, &handle->name_node);
136 free (handle->file_name);
137 free (handle->file_name_encoding);
138 free (handle->encoding);
142 /* Make HANDLE unnamed, so that it can no longer be referenced by
143 name. The caller must hold a reference to HANDLE, which is
144 not affected by this function. */
146 unname_handle (struct file_handle *handle)
148 assert (handle->id != NULL);
151 hmap_delete (&named_handles, &handle->name_node);
153 /* Drop the reference held by the named_handles table. */
157 /* Increments HANDLE's reference count and returns HANDLE. */
159 fh_ref (struct file_handle *handle)
161 if (handle == fh_inline_file ())
163 assert (handle->ref_cnt > 0);
168 /* Decrements HANDLE's reference count.
169 If the reference count drops to 0, HANDLE is destroyed. */
171 fh_unref (struct file_handle *handle)
175 if (handle == fh_inline_file ())
177 assert (handle->ref_cnt > 0);
178 if (--handle->ref_cnt == 0)
179 free_handle (handle);
183 /* Make HANDLE unnamed, so that it can no longer be referenced by
184 name. The caller must hold a reference to HANDLE, which is
185 not affected by this function.
187 This function ignores a null pointer as input. It has no
188 effect on the inline handle, which is always named INLINE.*/
190 fh_unname (struct file_handle *handle)
192 assert (handle->ref_cnt > 1);
193 if (handle != fh_inline_file () && handle->id != NULL)
194 unname_handle (handle);
197 /* Returns the handle with the given ID, or a null pointer if
200 fh_from_id (const char *id)
202 struct file_handle *handle;
204 HMAP_FOR_EACH_WITH_HASH (handle, struct file_handle, name_node,
205 utf8_hash_case_string (id, 0), &named_handles)
206 if (!utf8_strcasecmp (id, handle->id))
208 return fh_ref (handle);
214 /* Creates a new handle with identifier ID (which may be null)
215 and name HANDLE_NAME that refers to REFERENT. Links the new
216 handle into the global list. Returns the new handle.
218 The new handle is not fully initialized. The caller is
219 responsible for completing its initialization. */
220 static struct file_handle *
221 create_handle (const char *id, char *handle_name, enum fh_referent referent,
222 const char *encoding)
224 struct file_handle *handle = XZALLOC (struct file_handle);
227 handle->id = xstrdup_if_nonnull (id);
228 handle->name = handle_name;
229 handle->referent = referent;
230 handle->encoding = xstrdup (encoding);
234 hmap_insert (&named_handles, &handle->name_node,
235 utf8_hash_case_string (handle->id, 0));
241 /* Returns the unique handle of referent type FH_REF_INLINE,
242 which refers to the "inline file" that represents character
243 data in the command file between BEGIN DATA and END DATA. */
245 fh_inline_file (void)
250 /* Creates and returns a new file handle with the given ID, which may be null.
251 If it is non-null, it must be a UTF-8 encoded string that is unique among
252 existing file identifiers. The new handle is associated with file FILE_NAME
253 and the given PROPERTIES. */
255 fh_create_file (const char *id, const char *file_name, const char *file_name_encoding,
256 const struct fh_properties *properties)
259 struct file_handle *handle;
261 handle_name = id != NULL ? xstrdup (id) : xasprintf ("`%s'", file_name);
262 handle = create_handle (id, handle_name, FH_REF_FILE, properties->encoding);
263 handle->file_name = xstrdup (file_name);
264 handle->file_name_encoding = xstrdup_if_nonnull (file_name_encoding);
265 handle->mode = properties->mode;
266 handle->line_ends = properties->line_ends;
267 handle->record_width = properties->record_width;
268 handle->tab_width = properties->tab_width;
272 /* Creates a new file handle with the given ID, which must be
273 unique among existing file identifiers. The new handle is
274 associated with a dataset file (initially empty). */
276 fh_create_dataset (struct dataset *ds)
279 struct file_handle *handle;
281 name = dataset_name (ds);
283 name = _("active dataset");
285 handle = create_handle (NULL, xstrdup (name), FH_REF_DATASET, C_ENCODING);
290 /* Returns a set of default properties for a file handle. */
291 const struct fh_properties *
292 fh_default_properties (void)
294 #if defined _WIN32 || defined __WIN32__
295 #define DEFAULT_LINE_ENDS FH_END_CRLF
297 #define DEFAULT_LINE_ENDS FH_END_LF
300 static const struct fh_properties default_properties
301 = {FH_MODE_TEXT, DEFAULT_LINE_ENDS, 1024, 4, (char *) "Auto"};
302 return &default_properties;
305 /* Returns the identifier that may be used in syntax to name the
306 given HANDLE, which takes the form of a PSPP identifier. If
307 HANDLE has no identifier, returns a null pointer.
309 Return value is owned by the file handle.*/
311 fh_get_id (const struct file_handle *handle)
316 /* Returns a user-friendly string to identify the given HANDLE.
317 If HANDLE was created by referring to a file name, returns the
318 file name, enclosed in double quotes. Return value is owned
321 Useful for printing error messages about use of file handles. */
323 fh_get_name (const struct file_handle *handle)
328 /* Returns the type of object that HANDLE refers to. */
330 fh_get_referent (const struct file_handle *handle)
332 return handle->referent;
335 /* Returns the name of the file associated with HANDLE. */
337 fh_get_file_name (const struct file_handle *handle)
339 assert (handle->referent == FH_REF_FILE);
340 return handle->file_name;
344 /* Returns the character encoding of the name of the file associated with HANDLE. */
346 fh_get_file_name_encoding (const struct file_handle *handle)
348 assert (handle->referent == FH_REF_FILE);
349 return handle->file_name_encoding;
353 /* Returns the mode of HANDLE. */
355 fh_get_mode (const struct file_handle *handle)
357 assert (handle->referent == FH_REF_FILE);
361 /* Returns the line ends of HANDLE, which must be a handle associated with a
364 fh_get_line_ends (const struct file_handle *handle)
366 assert (handle->referent == FH_REF_FILE);
367 return handle->line_ends;
370 /* Returns the width of a logical record on HANDLE. */
372 fh_get_record_width (const struct file_handle *handle)
374 assert (handle->referent & (FH_REF_FILE | FH_REF_INLINE));
375 return handle->record_width;
378 /* Returns the number of characters per tab stop for HANDLE, or
379 zero if tabs are not to be expanded. Applicable only to
380 FH_MODE_TEXT files. */
382 fh_get_tab_width (const struct file_handle *handle)
384 assert (handle->referent & (FH_REF_FILE | FH_REF_INLINE));
385 return handle->tab_width;
388 /* Returns the encoding of characters read from HANDLE. */
390 fh_get_encoding (const struct file_handle *handle)
392 return handle->encoding;
395 /* Returns the dataset handle associated with HANDLE.
396 Applicable to only FH_REF_DATASET files. */
398 fh_get_dataset (const struct file_handle *handle)
400 assert (handle->referent == FH_REF_DATASET);
404 /* Returns the current default handle. */
406 fh_get_default_handle (void)
408 return default_handle ? default_handle : fh_inline_file ();
411 /* Sets NEW_DEFAULT_HANDLE as the default handle. */
413 fh_set_default_handle (struct file_handle *new_default_handle)
415 assert (new_default_handle == NULL
416 || (new_default_handle->referent & (FH_REF_INLINE | FH_REF_FILE)));
417 if (default_handle != NULL && default_handle != inline_file)
418 fh_unref (default_handle);
419 default_handle = new_default_handle;
420 if (default_handle != NULL)
421 default_handle = fh_ref (default_handle);
424 /* Information about a file handle's readers or writers. */
427 struct hmap_node node; /* hmap_node member. */
430 enum fh_referent referent; /* Type of underlying file. */
433 struct file_identity *file; /* FH_REF_FILE only. */
434 unsigned int unique_id; /* FH_REF_DATASET only. */
437 enum fh_access access; /* Type of file access. */
439 /* Number of openers. */
442 /* Applicable only when open_cnt > 0. */
443 bool exclusive; /* No other openers allowed? */
444 const char *type; /* Human-readable type of file. */
445 void *aux; /* Owner's auxiliary data. */
449 static void make_key (struct fh_lock *, const struct file_handle *,
451 static void free_key (struct fh_lock *);
452 static int compare_fh_locks (const struct fh_lock *a, const struct fh_lock *b);
453 static unsigned int hash_fh_lock (const struct fh_lock *lock);
455 /* Tries to lock handle H for the given kind of ACCESS and TYPE
456 of file. Returns a pointer to a struct fh_lock if successful,
457 otherwise a null pointer.
459 H's referent type must be one of the bits in MASK. The caller
460 must verify this ahead of time; we simply assert it here.
462 TYPE is the sort of file, e.g. "system file". Only one type
463 of access is allowed on a given file at a time for reading,
464 and similarly for writing. If successful, a reference to TYPE
465 is retained, so it should probably be a string literal.
467 TYPE should be marked with N_() in the caller: that is, the
468 caller should not translate it with gettext, but fh_lock will
471 ACCESS specifies whether the lock is for reading or writing.
472 EXCLUSIVE is true to require exclusive access, false to allow
473 sharing with other accessors. Exclusive read access precludes
474 other readers, but not writers; exclusive write access
475 precludes other writers, but not readers. A sharable read or
476 write lock precludes reader or writers, respectively, of a
479 A lock may be associated with auxiliary data. See
480 fh_lock_get_aux and fh_lock_set_aux for more details. */
482 fh_lock (struct file_handle *h, enum fh_referent mask UNUSED,
483 const char *type, enum fh_access access, bool exclusive)
485 struct fh_lock *key = NULL;
487 struct fh_lock *lock = NULL;
488 bool found_lock = false;
490 assert ((fh_get_referent (h) & mask) != 0);
491 assert (access == FH_ACC_READ || access == FH_ACC_WRITE);
493 key = xmalloc (sizeof *key);
495 make_key (key, h, access);
498 key->exclusive = exclusive;
502 hash = hash_fh_lock (key);
504 HMAP_FOR_EACH_WITH_HASH (lock, struct fh_lock, node, hash, &locks)
506 if (0 == compare_fh_locks (lock, key))
515 if (strcmp (lock->type, type))
517 if (access == FH_ACC_READ)
518 msg (SE, _("Can't read from %s as a %s because it is "
519 "already being read as a %s."),
520 fh_get_name (h), gettext (type), gettext (lock->type));
522 msg (SE, _("Can't write to %s as a %s because it is "
523 "already being written as a %s."),
524 fh_get_name (h), gettext (type), gettext (lock->type));
527 else if (exclusive || lock->exclusive)
529 msg (SE, _("Can't re-open %s as a %s."),
530 fh_get_name (h), gettext (type));
541 hmap_insert (&locks, &key->node, hash);
543 HMAP_FOR_EACH_WITH_HASH (lock, struct fh_lock, node, hash, &locks)
545 if (0 == compare_fh_locks (lock, key))
557 /* Releases LOCK that was acquired with fh_lock.
558 Returns true if LOCK is still locked, because other clients
561 Returns false if LOCK has now been destroyed. In this case
562 the caller must ensure that any auxiliary data associated with
563 LOCK is destroyed, to avoid a memory leak. The caller must
564 obtain a pointer to the auxiliary data, e.g. via
565 fh_lock_get_aux *before* calling fh_unlock (because it yields
566 undefined behavior to call fh_lock_get_aux on a destroyed
569 fh_unlock (struct fh_lock *lock)
573 assert (lock->open_cnt > 0);
574 if (--lock->open_cnt == 0)
576 hmap_delete (&locks, &lock->node);
585 /* Returns auxiliary data for LOCK.
587 Auxiliary data is shared by every client that holds LOCK (for
588 an exclusive lock, this is a single client). To avoid leaks,
589 auxiliary data must be released before LOCK is destroyed. */
591 fh_lock_get_aux (const struct fh_lock *lock)
596 /* Sets the auxiliary data for LOCK to AUX. */
598 fh_lock_set_aux (struct fh_lock *lock, void *aux)
603 /* Returns true if HANDLE is locked for the given type of ACCESS,
606 fh_is_locked (const struct file_handle *handle, enum fh_access access)
609 const struct fh_lock *k = NULL;
610 bool is_locked = false;
613 make_key (&key, handle, access);
615 hash = hash_fh_lock (&key);
618 HMAP_FOR_EACH_WITH_HASH (k, struct fh_lock, node, hash, &locks)
620 if (0 == compare_fh_locks (k, &key))
632 /* Initializes the key fields in LOCK for looking up or inserting
633 handle H for the given kind of ACCESS. */
635 make_key (struct fh_lock *lock, const struct file_handle *h,
636 enum fh_access access)
638 lock->referent = fh_get_referent (h);
639 lock->access = access;
640 if (lock->referent == FH_REF_FILE)
641 lock->u.file = fh_get_identity (h);
642 else if (lock->referent == FH_REF_DATASET)
643 lock->u.unique_id = dataset_seqno (fh_get_dataset (h));
646 /* Frees the key fields in LOCK. */
648 free_key (struct fh_lock *lock)
650 if (lock->referent == FH_REF_FILE)
651 fh_free_identity (lock->u.file);
654 /* Compares the key fields in struct fh_lock objects A and B and
655 returns a strcmp()-type result. */
657 compare_fh_locks (const struct fh_lock *a, const struct fh_lock *b)
659 if (a->referent != b->referent)
660 return a->referent < b->referent ? -1 : 1;
661 else if (a->access != b->access)
662 return a->access < b->access ? -1 : 1;
663 else if (a->referent == FH_REF_FILE)
664 return fh_compare_file_identities (a->u.file, b->u.file);
665 else if (a->referent == FH_REF_DATASET)
666 return (a->u.unique_id < b->u.unique_id ? -1
667 : a->u.unique_id > b->u.unique_id);
672 /* Returns a hash value for LOCK. */
674 hash_fh_lock (const struct fh_lock *lock)
677 if (lock->referent == FH_REF_FILE)
678 basis = fh_hash_identity (lock->u.file);
679 else if (lock->referent == FH_REF_DATASET)
680 basis = lock->u.unique_id;
683 return hash_int ((lock->referent << 3) | lock->access, basis);
691 /* A file's identity:
693 - For a file that exists, this is its device and inode.
695 - For a file that does not exist, but which has a directory
696 name that exists, this is the device and inode of the
697 directory, plus the file's base name.
699 - For a file that does not exist and has a nonexistent
700 directory, this is the file name.
702 Windows doesn't have inode numbers, so we just use the name
706 unsigned long long device; /* Device number. */
707 unsigned long long inode; /* Inode number. */
708 char *name; /* File name, where needed, otherwise NULL. */
711 /* Returns a pointer to a dynamically allocated structure whose
712 value can be used to tell whether two files are actually the
713 same file. The caller is responsible for freeing the structure with
714 fh_free_identity() when finished. */
715 static struct file_identity *
716 fh_get_identity (const struct file_handle *fh)
718 struct file_identity *identity = xmalloc (sizeof *identity);
720 const char *file_name = fh_get_file_name (fh);
722 #if !(defined _WIN32 || defined __WIN32__)
724 if (lstat (file_name, &s) == 0)
726 identity->device = s.st_dev;
727 identity->inode = s.st_ino;
728 identity->name = NULL;
732 char *dir = dir_name (file_name);
733 if (last_component (file_name) != NULL && stat (dir, &s) == 0)
735 identity->device = s.st_dev;
736 identity->inode = s.st_ino;
737 identity->name = base_name (file_name);
741 identity->device = 0;
743 identity->name = xstrdup (file_name);
749 HANDLE h = CreateFile (file_name, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_READONLY, NULL);
750 if (h != INVALID_HANDLE_VALUE)
752 BY_HANDLE_FILE_INFORMATION fi;
753 ok = GetFileInformationByHandle (h, &fi);
756 identity->device = fi.dwVolumeSerialNumber;
757 identity->inode = fi.nFileIndexHigh;
758 identity->inode <<= (sizeof fi.nFileIndexLow) * CHAR_BIT;
759 identity->inode |= fi.nFileIndexLow;
767 identity->device = 0;
771 size_t pathlen = 255;
776 cname = xrealloc (cname, bufsize);
777 pathlen = GetFullPathName (file_name, bufsize, cname, NULL);
779 while (pathlen > bufsize);
780 identity->name = xstrdup (cname);
782 str_lowercase (identity->name);
789 /* Frees IDENTITY obtained from fh_get_identity(). */
791 fh_free_identity (struct file_identity *identity)
793 if (identity != NULL)
795 free (identity->name);
800 /* Compares A and B, returning a strcmp()-type result. */
802 fh_compare_file_identities (const struct file_identity *a,
803 const struct file_identity *b)
805 if (a->device != b->device)
806 return a->device < b->device ? -1 : 1;
807 else if (a->inode != b->inode)
808 return a->inode < b->inode ? -1 : 1;
809 else if (a->name != NULL)
810 return b->name != NULL ? strcmp (a->name, b->name) : 1;
812 return b->name != NULL ? -1 : 0;
815 /* Returns a hash value for IDENTITY. */
817 fh_hash_identity (const struct file_identity *identity)
819 unsigned int hash = hash_int (identity->device, identity->inode);
820 if (identity->name != NULL)
821 hash = hash_string (identity->name, hash);