File: temp-file.c

package info (click to toggle)
pspp 0.7.9%2Bgit20120620-1.1
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 71,980 kB
  • sloc: ansic: 384,310; sh: 22,024; cpp: 1,445; yacc: 1,251; perl: 903; lisp: 868; makefile: 358; xml: 182; java: 5
file content (132 lines) | stat: -rw-r--r-- 2,935 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/* PSPP - a program for statistical analysis.
   Copyright (C) 2010, 2011 Free Software Foundation, Inc.

   This program is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>. */

/* Functions for temporary files that honor $TMPDIR. */

#include <config.h>

#include "libpspp/temp-file.h"
#include "libpspp/hmapx.h"
#include "libpspp/hash-functions.h"

#include <stdlib.h>

#include "gl/clean-temp.h"
#include "gl/xvasprintf.h"

/* Creates and returns a new temporary file that will be removed automatically
   when the process exits.  The file is opened in mode "wb+".  To close the
   file before the process exits, use close_temp_file() to ensure that it gets
   deleted early.

   Returns NULL if creating the temporary file fails.

   This is similar to tmpfile(), except:

     - It honors the $TMPDIR environment variable.

*/

static void cleanup (void);

static struct temp_dir *temp_dir;
struct hmapx map;

static void
setup (void)
{
  hmapx_init (&map);
  temp_dir = create_temp_dir ("pspp", NULL, true);
}

static void
initialise (void)
{
  if (temp_dir == NULL)
    {
      setup ();
      if (temp_dir == NULL)
        return ;
      atexit (cleanup);
    }
}


const char *
temp_dir_name (void)
{
  initialise ();

  if (temp_dir)
    return temp_dir->dir_name;

  return NULL;
}

static void
cleanup (void)
{
  struct hmapx_node *node;
  char *fn;

  cleanup_temp_dir (temp_dir);

  HMAPX_FOR_EACH (fn, node, &map)
    {
      free (fn);
    }

  hmapx_destroy (&map);
}

FILE *
create_temp_file (void)
{
  static int idx = 0;
  char *file_name;
  FILE *stream;

  initialise ();
  if (temp_dir == NULL)
    return NULL;

  file_name = xasprintf ("%s/%d", temp_dir->dir_name, idx++);
  register_temp_file (temp_dir, file_name);
  stream = fopen_temp (file_name, "wb+");
  if (stream == NULL)
    unregister_temp_file (temp_dir, file_name);
  else
    setvbuf (stream, NULL, _IOFBF, 65536);

  hmapx_insert (&map, file_name, hash_pointer (stream, 0));

  return stream;
}

/* Closes and removes a temporary file created by create_temp_file(). */
void
close_temp_file (FILE *file)
{
  if (file != NULL)
    {
      struct hmapx_node *node = hmapx_first_with_hash (&map, hash_pointer (file, 0));
      char *fn = node->data;
      fclose_temp (file);
      cleanup_temp_file (temp_dir, fn); 
      hmapx_delete (&map, node);
      free (fn);
    }
}