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/assertion.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"
39 #include "gl/dirname.h"
40 #include "gl/xalloc.h"
43 #define _(msgid) gettext (msgid)
45 #if defined _WIN32 || defined __WIN32__
46 #define WIN32_LEAN_AND_MEAN /* avoid including junk */
53 struct hmap_node name_node; /* Element in named_handles hmap. */
54 size_t ref_cnt; /* Number of references. */
55 char *id; /* Identifier token, NULL if none. */
56 char *name; /* User-friendly identifying name. */
57 enum fh_referent referent; /* What the file handle refers to. */
59 /* FH_REF_FILE only. */
60 char *file_name; /* File name as provided by user. */
61 char *file_name_encoding; /* The character encoding of file_name,
62 This is NOT the encoding of the file contents! */
63 enum fh_mode mode; /* File mode. */
64 enum fh_line_ends line_ends; /* Line ends for text files. */
66 /* FH_REF_FILE and FH_REF_INLINE only. */
67 size_t record_width; /* Length of fixed-format records. */
68 size_t tab_width; /* Tab width, 0=do not expand tabs. */
69 char *encoding; /* Charset for contents. */
71 /* FH_REF_DATASET only. */
72 struct dataset *ds; /* Dataset. */
75 /* All "struct file_handle"s with nonnull 'id' member. */
76 static struct hmap named_handles = HMAP_INITIALIZER (named_handles);
78 /* Default file handle for DATA LIST, REREAD, REPEATING DATA
80 static struct file_handle *default_handle;
82 /* The "file" that reads from BEGIN DATA...END DATA. */
83 static struct file_handle *inline_file;
85 static struct file_handle *create_handle (const char *id,
86 char *name, enum fh_referent,
87 const char *encoding);
88 static void free_handle (struct file_handle *);
89 static void unname_handle (struct file_handle *);
91 /* Hash table of all active locks. */
92 static struct hmap locks = HMAP_INITIALIZER (locks);
94 static struct file_identity *fh_get_identity (const struct file_handle *);
95 static void fh_free_identity (struct file_identity *);
96 static int fh_compare_file_identities (const struct file_identity *,
97 const struct file_identity *);
98 static unsigned int fh_hash_identity (const struct file_identity *);
100 /* File handle initialization routine. */
104 inline_file = create_handle ("INLINE", xstrdup ("INLINE"), FH_REF_INLINE,
106 inline_file->record_width = 80;
107 inline_file->tab_width = 8;
110 /* Removes all named file handles from the global list. */
114 struct file_handle *handle, *next;
116 HMAP_FOR_EACH_SAFE (handle, next,
117 struct file_handle, name_node, &named_handles)
118 unname_handle (handle);
120 free_handle (inline_file);
123 /* Free HANDLE and remove it from the global list. */
125 free_handle (struct file_handle *handle)
130 /* Remove handle from global list. */
131 if (handle->id != NULL)
132 hmap_delete (&named_handles, &handle->name_node);
137 free (handle->file_name);
138 free (handle->file_name_encoding);
139 free (handle->encoding);
143 /* Make HANDLE unnamed, so that it can no longer be referenced by
144 name. The caller must hold a reference to HANDLE, which is
145 not affected by this function. */
147 unname_handle (struct file_handle *handle)
149 assert (handle->id != NULL);
152 hmap_delete (&named_handles, &handle->name_node);
154 /* Drop the reference held by the named_handles table. */
158 /* Increments HANDLE's reference count and returns HANDLE. */
160 fh_ref (struct file_handle *handle)
162 if (handle == fh_inline_file ())
164 assert (handle->ref_cnt > 0);
169 /* Decrements HANDLE's reference count.
170 If the reference count drops to 0, HANDLE is destroyed. */
172 fh_unref (struct file_handle *handle)
176 if (handle == fh_inline_file ())
178 assert (handle->ref_cnt > 0);
179 if (--handle->ref_cnt == 0)
180 free_handle (handle);
184 /* Make HANDLE unnamed, so that it can no longer be referenced by
185 name. The caller must hold a reference to HANDLE, which is
186 not affected by this function.
188 This function ignores a null pointer as input. It has no
189 effect on the inline handle, which is always named INLINE.*/
191 fh_unname (struct file_handle *handle)
193 assert (handle->ref_cnt > 1);
194 if (handle != fh_inline_file () && handle->id != NULL)
195 unname_handle (handle);
198 /* Returns the handle with the given ID, or a null pointer if
201 fh_from_id (const char *id)
203 struct file_handle *handle;
205 HMAP_FOR_EACH_WITH_HASH (handle, struct file_handle, name_node,
206 utf8_hash_case_string (id, 0), &named_handles)
207 if (!utf8_strcasecmp (id, handle->id))
209 return fh_ref (handle);
215 /* Creates a new handle with identifier ID (which may be null)
216 and name HANDLE_NAME that refers to REFERENT. Links the new
217 handle into the global list. Returns the new handle.
219 The new handle is not fully initialized. The caller is
220 responsible for completing its initialization. */
221 static struct file_handle *
222 create_handle (const char *id, char *handle_name, enum fh_referent referent,
223 const char *encoding)
225 struct file_handle *handle = XZALLOC (struct file_handle);
228 handle->id = xstrdup_if_nonnull (id);
229 handle->name = handle_name;
230 handle->referent = referent;
231 handle->encoding = xstrdup (encoding);
235 hmap_insert (&named_handles, &handle->name_node,
236 utf8_hash_case_string (handle->id, 0));
242 /* Returns the unique handle of referent type FH_REF_INLINE,
243 which refers to the "inline file" that represents character
244 data in the command file between BEGIN DATA and END DATA. */
246 fh_inline_file (void)
251 /* Creates and returns a new file handle with the given ID, which may be null.
252 If it is non-null, it must be a UTF-8 encoded string that is unique among
253 existing file identifiers. The new handle is associated with file FILE_NAME
254 and the given PROPERTIES. */
256 fh_create_file (const char *id, const char *file_name, const char *file_name_encoding,
257 const struct fh_properties *properties)
260 struct file_handle *handle;
262 handle_name = id != NULL ? xstrdup (id) : xasprintf ("`%s'", file_name);
263 handle = create_handle (id, handle_name, FH_REF_FILE, properties->encoding);
264 handle->file_name = xstrdup (file_name);
265 handle->file_name_encoding = xstrdup_if_nonnull (file_name_encoding);
266 handle->mode = properties->mode;
267 handle->line_ends = properties->line_ends;
268 handle->record_width = properties->record_width;
269 handle->tab_width = properties->tab_width;
273 /* Creates a new file handle with the given ID, which must be
274 unique among existing file identifiers. The new handle is
275 associated with a dataset file (initially empty). */
277 fh_create_dataset (struct dataset *ds)
280 struct file_handle *handle;
282 name = dataset_name (ds);
284 name = _("active dataset");
286 handle = create_handle (NULL, xstrdup (name), FH_REF_DATASET, C_ENCODING);
291 /* Returns a set of default properties for a file handle. */
292 const struct fh_properties *
293 fh_default_properties (void)
295 #if defined _WIN32 || defined __WIN32__
296 #define DEFAULT_LINE_ENDS FH_END_CRLF
298 #define DEFAULT_LINE_ENDS FH_END_LF
301 static const struct fh_properties default_properties
302 = {FH_MODE_TEXT, DEFAULT_LINE_ENDS, 1024, 4, (char *) "Auto"};
303 return &default_properties;
306 /* Returns the identifier that may be used in syntax to name the
307 given HANDLE, which takes the form of a PSPP identifier. If
308 HANDLE has no identifier, returns a null pointer.
310 Return value is owned by the file handle.*/
312 fh_get_id (const struct file_handle *handle)
317 /* Returns a user-friendly string to identify the given HANDLE.
318 If HANDLE was created by referring to a file name, returns the
319 file name, enclosed in double quotes. Return value is owned
322 Useful for printing error messages about use of file handles. */
324 fh_get_name (const struct file_handle *handle)
329 /* Returns the type of object that HANDLE refers to. */
331 fh_get_referent (const struct file_handle *handle)
333 return handle->referent;
336 /* Returns the name of the file associated with HANDLE. */
338 fh_get_file_name (const struct file_handle *handle)
340 assert (handle->referent == FH_REF_FILE);
341 return handle->file_name;
345 /* Returns the character encoding of the name of the file associated with HANDLE. */
347 fh_get_file_name_encoding (const struct file_handle *handle)
349 assert (handle->referent == FH_REF_FILE);
350 return handle->file_name_encoding;
354 /* Returns the mode of HANDLE. */
356 fh_get_mode (const struct file_handle *handle)
358 assert (handle->referent == FH_REF_FILE);
362 /* Returns the line ends of HANDLE, which must be a handle associated with a
365 fh_get_line_ends (const struct file_handle *handle)
367 assert (handle->referent == FH_REF_FILE);
368 return handle->line_ends;
371 /* Returns the width of a logical record on HANDLE. */
373 fh_get_record_width (const struct file_handle *handle)
375 assert (handle->referent & (FH_REF_FILE | FH_REF_INLINE));
376 return handle->record_width;
379 /* Returns the number of characters per tab stop for HANDLE, or
380 zero if tabs are not to be expanded. Applicable only to
381 FH_MODE_TEXT files. */
383 fh_get_tab_width (const struct file_handle *handle)
385 assert (handle->referent & (FH_REF_FILE | FH_REF_INLINE));
386 return handle->tab_width;
389 /* Returns the encoding of characters read from HANDLE. */
391 fh_get_encoding (const struct file_handle *handle)
393 return handle->encoding;
396 /* Returns true if A and B refer to the same file or dataset, false
399 fh_equal (const struct file_handle *a, const struct file_handle *b)
401 if (a->referent != b->referent)
408 struct file_identity *a_id = fh_get_identity (a);
409 struct file_identity *b_id = fh_get_identity (b);
411 int cmp = fh_compare_file_identities (a_id, b_id);
413 fh_free_identity (a_id);
414 fh_free_identity (b_id);
423 return a->ds == b->ds;
430 /* Returns the dataset handle associated with HANDLE.
431 Applicable to only FH_REF_DATASET files. */
433 fh_get_dataset (const struct file_handle *handle)
435 assert (handle->referent == FH_REF_DATASET);
439 /* Returns the current default handle. */
441 fh_get_default_handle (void)
443 return default_handle ? default_handle : fh_inline_file ();
446 /* Sets NEW_DEFAULT_HANDLE as the default handle. */
448 fh_set_default_handle (struct file_handle *new_default_handle)
450 assert (new_default_handle == NULL
451 || (new_default_handle->referent & (FH_REF_INLINE | FH_REF_FILE)));
452 if (default_handle != NULL && default_handle != inline_file)
453 fh_unref (default_handle);
454 default_handle = new_default_handle;
455 if (default_handle != NULL)
456 default_handle = fh_ref (default_handle);
459 /* Information about a file handle's readers or writers. */
462 struct hmap_node node; /* hmap_node member. */
465 enum fh_referent referent; /* Type of underlying file. */
468 struct file_identity *file; /* FH_REF_FILE only. */
469 unsigned int unique_id; /* FH_REF_DATASET only. */
472 enum fh_access access; /* Type of file access. */
474 /* Number of openers. */
477 /* Applicable only when open_cnt > 0. */
478 bool exclusive; /* No other openers allowed? */
479 const char *type; /* Human-readable type of file. */
480 void *aux; /* Owner's auxiliary data. */
484 static void make_key (struct fh_lock *, const struct file_handle *,
486 static void free_key (struct fh_lock *);
487 static int compare_fh_locks (const struct fh_lock *a, const struct fh_lock *b);
488 static unsigned int hash_fh_lock (const struct fh_lock *lock);
490 /* Tries to lock handle H for the given kind of ACCESS and TYPE
491 of file. Returns a pointer to a struct fh_lock if successful,
492 otherwise a null pointer.
494 H's referent type must be one of the bits in MASK. The caller
495 must verify this ahead of time; we simply assert it here.
497 TYPE is the sort of file, e.g. "system file". Only one type
498 of access is allowed on a given file at a time for reading,
499 and similarly for writing. If successful, a reference to TYPE
500 is retained, so it should probably be a string literal.
502 TYPE should be marked with N_() in the caller: that is, the
503 caller should not translate it with gettext, but fh_lock will
506 ACCESS specifies whether the lock is for reading or writing.
507 EXCLUSIVE is true to require exclusive access, false to allow
508 sharing with other accessors. Exclusive read access precludes
509 other readers, but not writers; exclusive write access
510 precludes other writers, but not readers. A sharable read or
511 write lock precludes reader or writers, respectively, of a
514 A lock may be associated with auxiliary data. See
515 fh_lock_get_aux and fh_lock_set_aux for more details. */
517 fh_lock (struct file_handle *h, enum fh_referent mask UNUSED,
518 const char *type, enum fh_access access, bool exclusive)
520 struct fh_lock *key = NULL;
522 struct fh_lock *lock = NULL;
523 bool found_lock = false;
525 assert ((fh_get_referent (h) & mask) != 0);
526 assert (access == FH_ACC_READ || access == FH_ACC_WRITE);
528 key = xmalloc (sizeof *key);
530 make_key (key, h, access);
533 key->exclusive = exclusive;
537 hash = hash_fh_lock (key);
539 HMAP_FOR_EACH_WITH_HASH (lock, struct fh_lock, node, hash, &locks)
541 if (0 == compare_fh_locks (lock, key))
550 if (strcmp (lock->type, type))
552 if (access == FH_ACC_READ)
553 msg (SE, _("Can't read from %s as a %s because it is "
554 "already being read as a %s."),
555 fh_get_name (h), gettext (type), gettext (lock->type));
557 msg (SE, _("Can't write to %s as a %s because it is "
558 "already being written as a %s."),
559 fh_get_name (h), gettext (type), gettext (lock->type));
562 else if (exclusive || lock->exclusive)
564 msg (SE, _("Can't re-open %s as a %s."),
565 fh_get_name (h), gettext (type));
576 hmap_insert (&locks, &key->node, hash);
578 HMAP_FOR_EACH_WITH_HASH (lock, struct fh_lock, node, hash, &locks)
580 if (0 == compare_fh_locks (lock, key))
592 /* Releases LOCK that was acquired with fh_lock.
593 Returns true if LOCK is still locked, because other clients
596 Returns false if LOCK has now been destroyed. In this case
597 the caller must ensure that any auxiliary data associated with
598 LOCK is destroyed, to avoid a memory leak. The caller must
599 obtain a pointer to the auxiliary data, e.g. via
600 fh_lock_get_aux *before* calling fh_unlock (because it yields
601 undefined behavior to call fh_lock_get_aux on a destroyed
604 fh_unlock (struct fh_lock *lock)
608 assert (lock->open_cnt > 0);
609 if (--lock->open_cnt == 0)
611 hmap_delete (&locks, &lock->node);
620 /* Returns auxiliary data for LOCK.
622 Auxiliary data is shared by every client that holds LOCK (for
623 an exclusive lock, this is a single client). To avoid leaks,
624 auxiliary data must be released before LOCK is destroyed. */
626 fh_lock_get_aux (const struct fh_lock *lock)
631 /* Sets the auxiliary data for LOCK to AUX. */
633 fh_lock_set_aux (struct fh_lock *lock, void *aux)
638 /* Returns true if HANDLE is locked for the given type of ACCESS,
641 fh_is_locked (const struct file_handle *handle, enum fh_access access)
644 const struct fh_lock *k = NULL;
645 bool is_locked = false;
648 make_key (&key, handle, access);
650 hash = hash_fh_lock (&key);
653 HMAP_FOR_EACH_WITH_HASH (k, struct fh_lock, node, hash, &locks)
655 if (0 == compare_fh_locks (k, &key))
667 /* Initializes the key fields in LOCK for looking up or inserting
668 handle H for the given kind of ACCESS. */
670 make_key (struct fh_lock *lock, const struct file_handle *h,
671 enum fh_access access)
673 lock->referent = fh_get_referent (h);
674 lock->access = access;
675 if (lock->referent == FH_REF_FILE)
676 lock->u.file = fh_get_identity (h);
677 else if (lock->referent == FH_REF_DATASET)
678 lock->u.unique_id = dataset_seqno (fh_get_dataset (h));
681 /* Frees the key fields in LOCK. */
683 free_key (struct fh_lock *lock)
685 if (lock->referent == FH_REF_FILE)
686 fh_free_identity (lock->u.file);
689 /* Compares the key fields in struct fh_lock objects A and B and
690 returns a strcmp()-type result. */
692 compare_fh_locks (const struct fh_lock *a, const struct fh_lock *b)
694 if (a->referent != b->referent)
695 return a->referent < b->referent ? -1 : 1;
696 else if (a->access != b->access)
697 return a->access < b->access ? -1 : 1;
698 else if (a->referent == FH_REF_FILE)
699 return fh_compare_file_identities (a->u.file, b->u.file);
700 else if (a->referent == FH_REF_DATASET)
701 return (a->u.unique_id < b->u.unique_id ? -1
702 : a->u.unique_id > b->u.unique_id);
707 /* Returns a hash value for LOCK. */
709 hash_fh_lock (const struct fh_lock *lock)
712 if (lock->referent == FH_REF_FILE)
713 basis = fh_hash_identity (lock->u.file);
714 else if (lock->referent == FH_REF_DATASET)
715 basis = lock->u.unique_id;
718 return hash_int ((lock->referent << 3) | lock->access, basis);
726 /* A file's identity:
728 - For a file that exists, this is its device and inode.
730 - For a file that does not exist, but which has a directory
731 name that exists, this is the device and inode of the
732 directory, plus the file's base name.
734 - For a file that does not exist and has a nonexistent
735 directory, this is the file name.
737 Windows doesn't have inode numbers, so we just use the name
741 unsigned long long device; /* Device number. */
742 unsigned long long inode; /* Inode number. */
743 char *name; /* File name, where needed, otherwise NULL. */
746 /* Returns a pointer to a dynamically allocated structure whose
747 value can be used to tell whether two files are actually the
748 same file. The caller is responsible for freeing the structure with
749 fh_free_identity() when finished. */
750 static struct file_identity *
751 fh_get_identity (const struct file_handle *fh)
753 struct file_identity *identity = xmalloc (sizeof *identity);
755 const char *file_name = fh_get_file_name (fh);
757 #if !(defined _WIN32 || defined __WIN32__)
759 if (lstat (file_name, &s) == 0)
761 identity->device = s.st_dev;
762 identity->inode = s.st_ino;
763 identity->name = NULL;
767 char *dir = dir_name (file_name);
768 if (last_component (file_name) != NULL && stat (dir, &s) == 0)
770 identity->device = s.st_dev;
771 identity->inode = s.st_ino;
772 identity->name = base_name (file_name);
776 identity->device = 0;
778 identity->name = xstrdup (file_name);
784 HANDLE h = CreateFile (file_name, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_READONLY, NULL);
785 if (h != INVALID_HANDLE_VALUE)
787 BY_HANDLE_FILE_INFORMATION fi;
788 ok = GetFileInformationByHandle (h, &fi);
791 identity->device = fi.dwVolumeSerialNumber;
792 identity->inode = fi.nFileIndexHigh;
793 identity->inode <<= (sizeof fi.nFileIndexLow) * CHAR_BIT;
794 identity->inode |= fi.nFileIndexLow;
802 identity->device = 0;
806 size_t pathlen = 255;
811 cname = xrealloc (cname, bufsize);
812 pathlen = GetFullPathName (file_name, bufsize, cname, NULL);
814 while (pathlen > bufsize);
815 identity->name = xstrdup (cname);
817 str_lowercase (identity->name);
824 /* Frees IDENTITY obtained from fh_get_identity(). */
826 fh_free_identity (struct file_identity *identity)
828 if (identity != NULL)
830 free (identity->name);
835 /* Compares A and B, returning a strcmp()-type result. */
837 fh_compare_file_identities (const struct file_identity *a,
838 const struct file_identity *b)
840 if (a->device != b->device)
841 return a->device < b->device ? -1 : 1;
842 else if (a->inode != b->inode)
843 return a->inode < b->inode ? -1 : 1;
844 else if (a->name != NULL)
845 return b->name != NULL ? strcmp (a->name, b->name) : 1;
847 return b->name != NULL ? -1 : 0;
850 /* Returns a hash value for IDENTITY. */
852 fh_hash_identity (const struct file_identity *identity)
854 unsigned int hash = hash_int (identity->device, identity->inode);
855 if (identity->name != NULL)
856 hash = hash_string (identity->name, hash);