Allow output files to overwrite input files (bug #21280). Thanks to
[pspp-builds.git] / src / data / scratch-writer.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 2006 Free Software Foundation, Inc.
3
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.
8
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.
13
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/>. */
16
17 #include <config.h>
18
19 #include "scratch-writer.h"
20
21 #include <stdlib.h>
22
23 #include <data/case.h>
24 #include <data/case-map.h>
25 #include <data/casereader.h>
26 #include <data/casewriter-provider.h>
27 #include <data/casewriter.h>
28 #include <data/dictionary.h>
29 #include <data/file-handle-def.h>
30 #include <data/scratch-handle.h>
31 #include <data/variable.h>
32 #include <libpspp/compiler.h>
33 #include <libpspp/taint.h>
34
35 #include "xalloc.h"
36
37 /* A scratch file writer. */
38 struct scratch_writer
39   {
40     struct file_handle *fh;             /* Underlying file handle. */
41     struct fh_lock *lock;               /* Exclusive access to file handle. */
42     struct dictionary *dict;            /* Dictionary for subwriter. */
43     struct case_map *compactor;         /* Compacts into dictionary. */
44     struct casewriter *subwriter;       /* Data output. */
45   };
46
47 static struct casewriter_class scratch_writer_casewriter_class;
48
49 /* Opens FH, which must have referent type FH_REF_SCRATCH, and
50    returns a scratch_writer for it, or a null pointer on
51    failure.  Cases stored in the scratch_writer will be expected
52    to be drawn from DICTIONARY. */
53 struct casewriter *
54 scratch_writer_open (struct file_handle *fh,
55                      const struct dictionary *dictionary)
56 {
57   struct scratch_writer *writer;
58   struct casewriter *casewriter;
59   struct fh_lock *lock;
60   size_t dict_value_cnt;
61
62   /* Get exclusive write access to handle. */
63   lock = fh_lock (fh, FH_REF_SCRATCH, "scratch file", FH_ACC_WRITE, true);
64   if (lock == NULL)
65     return NULL;
66
67   /* Create writer. */
68   writer = xmalloc (sizeof *writer);
69   writer->lock = lock;
70   writer->fh = fh_ref (fh);
71
72   writer->dict = dict_clone (dictionary);
73   dict_delete_scratch_vars (writer->dict);
74   if (dict_count_values (writer->dict, 0)
75       < dict_get_next_value_idx (writer->dict))
76     {
77       writer->compactor = case_map_to_compact_dict (writer->dict, 0);
78       dict_compact_values (writer->dict);
79     }
80   else
81     writer->compactor = NULL;
82   dict_value_cnt = dict_get_next_value_idx (writer->dict);
83   writer->subwriter = autopaging_writer_create (dict_value_cnt);
84
85   casewriter = casewriter_create (dict_value_cnt,
86                                   &scratch_writer_casewriter_class, writer);
87   taint_propagate (casewriter_get_taint (writer->subwriter),
88                    casewriter_get_taint (casewriter));
89   return casewriter;
90 }
91
92 /* Writes case C to WRITER. */
93 static void
94 scratch_writer_casewriter_write (struct casewriter *w UNUSED, void *writer_,
95                                  struct ccase *c)
96 {
97   struct scratch_writer *writer = writer_;
98   struct ccase tmp;
99   if (writer->compactor)
100     {
101       case_map_execute (writer->compactor, c, &tmp);
102       case_destroy (c);
103     }
104   else
105     case_move (&tmp, c);
106   casewriter_write (writer->subwriter, &tmp);
107 }
108
109 /* Closes WRITER. */
110 static void
111 scratch_writer_casewriter_destroy (struct casewriter *w UNUSED, void *writer_)
112 {
113   static unsigned int next_unique_id = 0x12345678;
114
115   struct scratch_writer *writer = writer_;
116   struct casereader *reader = casewriter_make_reader (writer->subwriter);
117   if (!casereader_error (reader))
118     {
119       /* Destroy previous contents of handle. */
120       struct scratch_handle *sh = fh_get_scratch_handle (writer->fh);
121       if (sh != NULL)
122         scratch_handle_destroy (sh);
123
124       /* Create new contents. */
125       sh = xmalloc (sizeof *sh);
126       sh->unique_id = ++next_unique_id;
127       sh->dictionary = writer->dict;
128       sh->casereader = reader;
129       fh_set_scratch_handle (writer->fh, sh);
130     }
131   else
132     {
133       casereader_destroy (reader);
134       dict_destroy (writer->dict);
135     }
136
137   fh_unlock (writer->lock);
138   fh_unref (writer->fh);
139   free (writer);
140 }
141
142 static struct casewriter_class scratch_writer_casewriter_class =
143   {
144     scratch_writer_casewriter_write,
145     scratch_writer_casewriter_destroy,
146     NULL,
147   };