1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 
3 /*
4  *  Engrampa
5  *
6  *  Copyright (C) 2004 The Free Software Foundation, Inc.
7  *
8  *  This program is free software; you can redistribute it and/or modify
9  *  it under the terms of the GNU General Public License as published by
10  *  the Free Software Foundation; either version 2 of the License, or
11  *  (at your option) any later version.
12  *
13  *  This program is distributed in the hope that it will be useful,
14  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *  GNU General Public License for more details.
17  *
18  *  You should have received a copy of the GNU General Public License
19  *  along with this program; if not, write to the Free Software
20  *  Foundation, Inc., 59 Temple Street #330, Boston, MA 02110-1301, USA.
21  */
22 
23 #include <config.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <time.h>
27 
28 #include <glib.h>
29 
30 #include "file-data.h"
31 #include "file-utils.h"
32 #include "glib-utils.h"
33 #include "fr-command.h"
34 #include "fr-command-iso.h"
35 
36 static void fr_command_iso_class_init  (FrCommandIsoClass *class);
37 static void fr_command_iso_init        (FrCommand         *afile);
38 static void fr_command_iso_finalize    (GObject           *object);
39 
40 /* Parent Class */
41 
42 static FrCommandClass *parent_class = NULL;
43 
44 
45 static time_t
mktime_from_string(char * month,char * mday,char * year)46 mktime_from_string (char *month,
47 		    char *mday,
48 		    char *year)
49 {
50 	static char  *months[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun",
51 				   "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
52 	struct tm     tm = {0, };
53 
54 	tm.tm_isdst = -1;
55 
56 	if (month != NULL) {
57 		int i;
58 		for (i = 0; i < 12; i++)
59 			if (strcmp (months[i], month) == 0) {
60 				tm.tm_mon = i;
61 				break;
62 			}
63 	}
64 	tm.tm_mday = atoi (mday);
65 	tm.tm_year = atoi (year) - 1900;
66 
67 	return mktime (&tm);
68 }
69 
70 
71 static void
list__process_line(char * line,gpointer data)72 list__process_line (char     *line,
73 		    gpointer  data)
74 {
75 	FileData      *fdata;
76 	FrCommand     *comm = FR_COMMAND (data);
77 	FrCommandIso  *comm_iso = FR_COMMAND_ISO (comm);
78 	char         **fields;
79 	const char    *name_field;
80 
81 	g_return_if_fail (line != NULL);
82 
83 	if (line[0] == 'd') /* Ignore directories. */
84 		return;
85 
86 	if (line[0] == 'D') {
87 		g_free (comm_iso->cur_path);
88 		comm_iso->cur_path = g_strdup (get_last_field (line, 4));
89 
90 	} else if (line[0] == '-') { /* Is file */
91 		const char *last_field, *first_bracket;
92 
93 		fdata = file_data_new ();
94 
95 		fields = split_line (line, 8);
96 		fdata->size = g_ascii_strtoull (fields[4], NULL, 10);
97 		fdata->modified = mktime_from_string (fields[5], fields[6], fields[7]);
98 		g_strfreev (fields);
99 
100 		/* Full path */
101 
102 		last_field = get_last_field (line, 9);
103 		first_bracket = strchr (last_field, ']');
104 		if (first_bracket == NULL) {
105 			file_data_free (fdata);
106 			return;
107 		}
108 
109 		name_field = eat_spaces (first_bracket + 1);
110 		if ((name_field == NULL)
111 		    || (strcmp (name_field, ".") == 0)
112 		    || (strcmp (name_field, "..") == 0)) {
113 			file_data_free (fdata);
114 			return;
115 		}
116 
117 		if (comm_iso->cur_path[0] != '/')
118 			fdata->full_path = g_strstrip (g_strconcat ("/", comm_iso->cur_path, name_field, NULL));
119 		else
120 			fdata->full_path = g_strstrip (g_strconcat (comm_iso->cur_path, name_field, NULL));
121 		fdata->original_path = fdata->full_path;
122 		fdata->name = g_strdup (file_name_from_path (fdata->full_path));
123 		fdata->path = remove_level_from_path (fdata->full_path);
124 
125 		fr_command_add_file (comm, fdata);
126 	}
127 }
128 
129 
130 static void
list__begin(gpointer data)131 list__begin (gpointer data)
132 {
133 	FrCommandIso *comm = data;
134 
135 	g_free (comm->cur_path);
136 	comm->cur_path = NULL;
137 }
138 
139 
140 static void
fr_command_iso_list(FrCommand * comm)141 fr_command_iso_list (FrCommand *comm)
142 {
143 	fr_process_set_out_line_func (comm->process, list__process_line, comm);
144 
145 	fr_process_begin_command (comm->process, "sh");
146 	fr_process_set_begin_func (comm->process, list__begin, comm);
147 	fr_process_add_arg (comm->process, SHDIR "isoinfo.sh");
148 	fr_process_add_arg (comm->process, "-i");
149 	fr_process_add_arg (comm->process, comm->filename);
150 	fr_process_add_arg (comm->process, "-l");
151 	fr_process_end_command (comm->process);
152 
153 	fr_process_start (comm->process);
154 }
155 
156 
157 static void
fr_command_iso_extract(FrCommand * comm,const char * from_file,GList * file_list,const char * dest_dir,gboolean overwrite,gboolean skip_older,gboolean junk_paths)158 fr_command_iso_extract (FrCommand  *comm,
159 			const char *from_file,
160 			GList      *file_list,
161 			const char *dest_dir,
162 			gboolean    overwrite,
163 			gboolean    skip_older,
164 			gboolean    junk_paths)
165 {
166 	GList *scan;
167 
168 	for (scan = file_list; scan; scan = scan->next) {
169 		char       *path = scan->data;
170 		const char *filename;
171 		char       *file_dir;
172 		char       *temp_dest_dir = NULL;
173 
174 		filename = file_name_from_path (path);
175 		file_dir = remove_level_from_path (path);
176 		if ((file_dir != NULL) && (strcmp (file_dir, "/") != 0))
177 			temp_dest_dir = g_build_filename (dest_dir, file_dir, NULL);
178 		 else
179 			temp_dest_dir = g_strdup (dest_dir);
180 		g_free (file_dir);
181 
182 		if (temp_dest_dir == NULL)
183 			continue;
184 
185 		make_directory_tree_from_path (temp_dest_dir, 0700, NULL);
186 
187 		fr_process_begin_command (comm->process, "sh");
188 		fr_process_set_working_dir (comm->process, temp_dest_dir);
189 		fr_process_add_arg (comm->process, SHDIR "isoinfo.sh");
190 		fr_process_add_arg (comm->process, "-i");
191 		fr_process_add_arg (comm->process, comm->filename);
192 		fr_process_add_arg (comm->process, "-x");
193 		fr_process_add_arg (comm->process, path);
194 		fr_process_add_arg (comm->process, filename);
195 		fr_process_end_command (comm->process);
196 
197 		g_free (temp_dest_dir);
198 	}
199 }
200 
201 
202 const char *iso_mime_type[] = { "application/x-cd-image", NULL };
203 
204 
205 static const char **
fr_command_iso_get_mime_types(FrCommand * comm)206 fr_command_iso_get_mime_types (FrCommand *comm)
207 {
208 	return iso_mime_type;
209 }
210 
211 
212 static FrCommandCap
fr_command_iso_get_capabilities(FrCommand * comm,const char * mime_type,gboolean check_command)213 fr_command_iso_get_capabilities (FrCommand  *comm,
214 			         const char *mime_type,
215 				 gboolean    check_command)
216 {
217 	FrCommandCap capabilities;
218 
219 	capabilities = FR_COMMAND_CAN_ARCHIVE_MANY_FILES;
220 	if (is_program_available ("isoinfo", check_command))
221 		capabilities |= FR_COMMAND_CAN_READ;
222 
223 	return capabilities;
224 }
225 
226 
227 static const char *
fr_command_iso_get_packages(FrCommand * comm,const char * mime_type)228 fr_command_iso_get_packages (FrCommand  *comm,
229 			     const char *mime_type)
230 {
231 	return PACKAGES ("genisoimage");
232 }
233 
234 
235 static void
fr_command_iso_class_init(FrCommandIsoClass * class)236 fr_command_iso_class_init (FrCommandIsoClass *class)
237 {
238 	GObjectClass   *gobject_class = G_OBJECT_CLASS (class);
239 	FrCommandClass *afc;
240 
241 	parent_class = g_type_class_peek_parent (class);
242 	afc = (FrCommandClass*) class;
243 
244 	gobject_class->finalize = fr_command_iso_finalize;
245 
246 	afc->list             = fr_command_iso_list;
247 	afc->extract          = fr_command_iso_extract;
248 	afc->get_mime_types   = fr_command_iso_get_mime_types;
249 	afc->get_capabilities = fr_command_iso_get_capabilities;
250 	afc->get_packages     = fr_command_iso_get_packages;
251 }
252 
253 
254 static void
fr_command_iso_init(FrCommand * comm)255 fr_command_iso_init (FrCommand *comm)
256 {
257 	FrCommandIso *comm_iso = FR_COMMAND_ISO (comm);
258 
259 	comm_iso->cur_path = NULL;
260 	comm_iso->joliet = TRUE;
261 
262 	comm->propAddCanUpdate             = FALSE;
263 	comm->propAddCanReplace            = FALSE;
264 	comm->propExtractCanAvoidOverwrite = FALSE;
265 	comm->propExtractCanSkipOlder      = FALSE;
266 	comm->propExtractCanJunkPaths      = FALSE;
267 	comm->propPassword                 = FALSE;
268 	comm->propTest                     = FALSE;
269 	comm->propCanExtractAll            = FALSE;
270 }
271 
272 
273 static void
fr_command_iso_finalize(GObject * object)274 fr_command_iso_finalize (GObject *object)
275 {
276 	FrCommandIso *comm_iso;
277 
278 	g_return_if_fail (object != NULL);
279 	g_return_if_fail (FR_IS_COMMAND_ISO (object));
280 
281 	comm_iso = FR_COMMAND_ISO (object);
282 
283 	g_free (comm_iso->cur_path);
284 	comm_iso->cur_path = NULL;
285 
286 	/* Chain up */
287 	if (G_OBJECT_CLASS (parent_class)->finalize)
288 		G_OBJECT_CLASS (parent_class)->finalize (object);
289 }
290 
291 
292 GType
fr_command_iso_get_type()293 fr_command_iso_get_type ()
294 {
295 	static GType type = 0;
296 
297 	if (! type) {
298 		GTypeInfo type_info = {
299 			sizeof (FrCommandIsoClass),
300 			NULL,
301 			NULL,
302 			(GClassInitFunc) fr_command_iso_class_init,
303 			NULL,
304 			NULL,
305 			sizeof (FrCommandIso),
306 			0,
307 			(GInstanceInitFunc) fr_command_iso_init
308 		};
309 
310 		type = g_type_register_static (FR_TYPE_COMMAND,
311 					       "FRCommandIso",
312 					       &type_info,
313 					       0);
314 	}
315 
316 	return type;
317 }
318