1 /* Managing temporary directories and their content within libgccjit.so
2 Copyright (C) 2014 Free Software Foundation, Inc.
3 Contributed by David Malcolm <dmalcolm@redhat.com>.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
12 GCC is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
23 #include "coretypes.h"
25 #include "jit-tempdir.h"
28 /* Construct a tempdir path template suitable for use by mkdtemp
29 e.g. "/tmp/libgccjit-XXXXXX", but respecting the rules in
30 libiberty's choose_tempdir rather than hardcoding "/tmp/".
32 The memory is allocated using malloc and must be freed.
33 Aborts the process if allocation fails. */
36 make_tempdir_path_template ()
38 const char *tmpdir_buf
;
40 const char *file_template_buf
;
41 size_t file_template_len
;
44 /* The result of choose_tmpdir is a cached buffer within libiberty, so
45 we must *not* free it. */
46 tmpdir_buf
= choose_tmpdir ();
48 /* choose_tmpdir aborts on malloc failure. */
49 gcc_assert (tmpdir_buf
);
51 tmpdir_len
= strlen (tmpdir_buf
);
52 /* tmpdir_buf should now have a dir separator as the final byte. */
53 gcc_assert (tmpdir_len
> 0);
54 gcc_assert (tmpdir_buf
[tmpdir_len
- 1] == DIR_SEPARATOR
);
56 file_template_buf
= "libgccjit-XXXXXX";
57 file_template_len
= strlen (file_template_buf
);
59 result
= XNEWVEC (char, tmpdir_len
+ file_template_len
+ 1);
60 strcpy (result
, tmpdir_buf
);
61 strcpy (result
+ tmpdir_len
, file_template_buf
);
66 /* The constructor for the jit::tempdir object.
67 The real work is done by the jit::tempdir::create method. */
69 gcc::jit::tempdir::tempdir (int keep_intermediates
)
70 : m_keep_intermediates (keep_intermediates
),
71 m_path_template (NULL
),
72 m_path_tempdir (NULL
),
79 /* Do the real work of creating the on-disk tempdir.
80 We do this here, rather than in the jit::tempdir constructor
81 so that we can handle failure without needing exceptions. */
84 gcc::jit::tempdir::create ()
86 m_path_template
= make_tempdir_path_template ();
90 /* Create tempdir using mkdtemp. This is created with 0700 perms and
91 is unique. Hence no other (non-root) users should have access to
92 the paths within it. */
93 m_path_tempdir
= mkdtemp (m_path_template
);
96 m_path_c_file
= concat (m_path_tempdir
, "/fake.c", NULL
);
97 m_path_s_file
= concat (m_path_tempdir
, "/fake.s", NULL
);
98 m_path_so_file
= concat (m_path_tempdir
, "/fake.so", NULL
);
104 /* The destructor for the jit::tempdir object, which
105 cleans up the filesystem directory and its contents
106 (unless keep_intermediates was set). */
108 gcc::jit::tempdir::~tempdir ()
110 if (m_keep_intermediates
)
111 fprintf (stderr
, "intermediate files written to %s\n", m_path_tempdir
);
114 /* Clean up .s/.so and tempdir. */
116 unlink (m_path_s_file
);
118 unlink (m_path_so_file
);
120 rmdir (m_path_tempdir
);
123 free (m_path_template
);
124 /* m_path_tempdir aliases m_path_template, or is NULL, so don't
125 attempt to free it . */
126 free (m_path_c_file
);
127 free (m_path_s_file
);
128 free (m_path_so_file
);