1
1
/* libguestfs - the guestfsd daemon
2
- * Copyright (C) 2009-2010 Red Hat Inc.
2
+ * Copyright (C) 2009-2011 Red Hat Inc.
3
3
*
4
4
* This program is free software; you can redistribute it and/or modify
5
5
* it under the terms of the GNU General Public License as published by
@@ -36,18 +36,14 @@ optgroup_xz_available (void)
36
36
return prog_exists ("xz" );
37
37
}
38
38
39
- /* Redirect errors from the tar command to the error file, then
40
- * provide functions for reading it in. We overwrite the file each
41
- * time, and since it's small and stored on the appliance we don't
42
- * bother to delete it.
43
- */
44
- static const char * error_file = "/tmp/error" ;
45
-
39
+ /* Read the error file. Returns a string that the caller must free. */
46
40
static char *
47
- read_error_file (void )
41
+ read_error_file (char * error_file )
48
42
{
49
43
size_t len ;
50
- char * str = read_file (error_file , & len );
44
+ char * str ;
45
+
46
+ str = read_file (error_file , & len );
51
47
if (str == NULL ) {
52
48
str = strdup ("(no error)" );
53
49
if (str == NULL ) {
@@ -78,6 +74,16 @@ do_tXz_in (const char *dir, const char *filter)
78
74
int err , r ;
79
75
FILE * fp ;
80
76
char * cmd ;
77
+ char error_file [] = "/tmp/tarXXXXXX" ;
78
+ int fd ;
79
+
80
+ fd = mkstemp (error_file );
81
+ if (fd == -1 ) {
82
+ reply_with_perror ("mkstemp" );
83
+ return -1 ;
84
+ }
85
+
86
+ close (fd );
81
87
82
88
/* "tar -C /sysroot%s -xf -" but we have to quote the dir. */
83
89
if (asprintf_nowarn (& cmd , "tar -C %R -%sxf - 2> %s" ,
@@ -86,6 +92,7 @@ do_tXz_in (const char *dir, const char *filter)
86
92
r = cancel_receive ();
87
93
errno = err ;
88
94
reply_with_perror ("asprintf" );
95
+ unlink (error_file );
89
96
return -1 ;
90
97
}
91
98
@@ -98,6 +105,7 @@ do_tXz_in (const char *dir, const char *filter)
98
105
r = cancel_receive ();
99
106
errno = err ;
100
107
reply_with_perror ("%s" , cmd );
108
+ unlink (error_file );
101
109
free (cmd );
102
110
return -1 ;
103
111
}
@@ -106,14 +114,15 @@ do_tXz_in (const char *dir, const char *filter)
106
114
/* The semantics of fwrite are too undefined, so write to the
107
115
* file descriptor directly instead.
108
116
*/
109
- int fd = fileno (fp );
117
+ fd = fileno (fp );
110
118
111
119
r = receive_file (write_cb , & fd );
112
120
if (r == -1 ) { /* write error */
113
121
cancel_receive ();
114
- char * errstr = read_error_file ();
122
+ char * errstr = read_error_file (error_file );
115
123
reply_with_error ("write error on directory: %s: %s" , dir , errstr );
116
124
free (errstr );
125
+ unlink (error_file );
117
126
pclose (fp );
118
127
return -1 ;
119
128
}
@@ -123,17 +132,21 @@ do_tXz_in (const char *dir, const char *filter)
123
132
*/
124
133
reply_with_error ("file upload cancelled" );
125
134
pclose (fp );
135
+ unlink (error_file );
126
136
return -1 ;
127
137
}
128
138
129
139
if (pclose (fp ) != 0 ) {
130
- char * errstr = read_error_file ();
140
+ char * errstr = read_error_file (error_file );
131
141
reply_with_error ("tar subcommand failed on directory: %s: %s" ,
132
142
dir , errstr );
133
143
free (errstr );
144
+ unlink (error_file );
134
145
return -1 ;
135
146
}
136
147
148
+ unlink (error_file );
149
+
137
150
return 0 ;
138
151
}
139
152
0 commit comments