1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 
3 /*
4  *  Engrampa
5  *
6  *  Copyright (C) 2006 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-cpio.h"
35 
36 static void fr_command_cpio_class_init  (FrCommandCpioClass *class);
37 static void fr_command_cpio_init        (FrCommand         *afile);
38 static void fr_command_cpio_finalize    (GObject           *object);
39 
40 /* Parent Class */
41 
42 static FrCommandClass *parent_class = NULL;
43 
44 
45 /* -- list -- */
46 
47 static time_t
mktime_from_string(char * month,char * mday,char * year)48 mktime_from_string (char *month,
49 		    char *mday,
50 		    char *year)
51 {
52 	static char  *months[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun",
53 				   "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
54 	struct tm     tm = {0, };
55 
56 	tm.tm_isdst = -1;
57 
58 	if (month != NULL) {
59 		int i;
60 		for (i = 0; i < 12; i++)
61 			if (strcmp (months[i], month) == 0) {
62 				tm.tm_mon = i;
63 				break;
64 			}
65 	}
66 	tm.tm_mday = atoi (mday);
67 	if (strchr (year, ':') != NULL) {
68 		char **fields = g_strsplit (year, ":", 2);
69         	if (n_fields (fields) == 2) {
70 	        	time_t      now;
71         		struct tm  *now_tm;
72 
73 	  		tm.tm_hour = atoi (fields[0]);
74 	  		tm.tm_min = atoi (fields[1]);
75 
76 	  		now = time(NULL);
77 	  		now_tm = localtime (&now);
78 	  		tm.tm_year = now_tm->tm_year;
79         	}
80 	} else
81 		tm.tm_year = atoi (year) - 1900;
82 
83 	return mktime (&tm);
84 }
85 
86 
87 static void
list__process_line(char * line,gpointer data)88 list__process_line (char     *line,
89 		    gpointer  data)
90 {
91 	FileData    *fdata;
92 	FrCommand   *comm = FR_COMMAND (data);
93 	char       **fields;
94 	const char  *name_field;
95 	char        *name;
96 	int          ofs = 0;
97 
98 	g_return_if_fail (line != NULL);
99 
100 	fdata = file_data_new ();
101 
102 #ifdef __sun
103 	fields = split_line (line, 9);
104 	fdata->size = g_ascii_strtoull (fields[4], NULL, 10);
105 	fdata->modified = mktime_from_string (fields[5], fields[6], fields[8]);
106 	g_strfreev (fields);
107 
108 	name_field = get_last_field (line, 10);
109 #else /* !__sun */
110 	/* Handle char and block device files */
111 	if ((line[0] == 'c') || (line[0] == 'b')) {
112 		fields = split_line (line, 9);
113 		ofs = 1;
114 		fdata->size = 0;
115 		/* FIXME: We should also specify the content type */
116 	}
117 	else {
118 		fields = split_line (line, 8);
119 		fdata->size = g_ascii_strtoull (fields[4], NULL, 10);
120 	}
121 	fdata->modified = mktime_from_string (fields[5+ofs], fields[6+ofs], fields[7+ofs]);
122 	g_strfreev (fields);
123 
124 	name_field = get_last_field (line, 9+ofs);
125 #endif /* !__sun */
126 
127 	fields = g_strsplit (name_field, " -> ", 2);
128 
129 	if (fields[1] == NULL) {
130 		g_strfreev (fields);
131 		fields = g_strsplit (name_field, " link to ", 2);
132 	}
133 
134 	fdata->dir = line[0] == 'd';
135 
136 	name = g_strcompress (fields[0]);
137 	if (*(fields[0]) == '/') {
138 		fdata->full_path = g_strdup (name);
139 		fdata->original_path = fdata->full_path;
140 	}
141 	else {
142 		fdata->full_path = g_strconcat ("/", name, NULL);
143 		fdata->original_path = fdata->full_path + 1;
144 	}
145 
146 	if (fdata->dir && (name[strlen (name) - 1] != '/')) {
147 		char *old_full_path = fdata->full_path;
148 		fdata->full_path = g_strconcat (old_full_path, "/", NULL);
149 		g_free (old_full_path);
150 		fdata->original_path = g_strdup (name);
151 		fdata->free_original_path = TRUE;
152 	}
153 	g_free (name);
154 
155 	if (fields[1] != NULL)
156 		fdata->link = g_strcompress (fields[1]);
157 	g_strfreev (fields);
158 
159 	if (fdata->dir)
160 		fdata->name = dir_name_from_path (fdata->full_path);
161 	else
162 		fdata->name = g_strdup (file_name_from_path (fdata->full_path));
163 	fdata->path = remove_level_from_path (fdata->full_path);
164 
165 	if (*fdata->name == 0)
166 		file_data_free (fdata);
167 	else
168 		fr_command_add_file (comm, fdata);
169 }
170 
171 
172 static void
fr_command_cpio_list(FrCommand * comm)173 fr_command_cpio_list (FrCommand  *comm)
174 {
175 	fr_process_set_out_line_func (comm->process, list__process_line, comm);
176 
177 	fr_process_begin_command (comm->process, "sh");
178 	fr_process_add_arg (comm->process, "-c");
179 	fr_process_add_arg_concat (comm->process, "cpio -itv < ", comm->e_filename, NULL);
180 	fr_process_end_command (comm->process);
181 	fr_process_start (comm->process);
182 }
183 
184 
185 static void
fr_command_cpio_extract(FrCommand * comm,const char * from_file,GList * file_list,const char * dest_dir,gboolean overwrite,gboolean skip_older,gboolean junk_paths)186 fr_command_cpio_extract (FrCommand *comm,
187 			const char *from_file,
188 			GList      *file_list,
189 			const char *dest_dir,
190 			gboolean    overwrite,
191 			gboolean    skip_older,
192 			gboolean    junk_paths)
193 {
194 	GList   *scan;
195 	GString *cmd;
196 
197 	fr_process_begin_command (comm->process, "sh");
198 	if (dest_dir != NULL)
199                 fr_process_set_working_dir (comm->process, dest_dir);
200 	fr_process_add_arg (comm->process, "-c");
201 
202 	cmd = g_string_new ("cpio -idu --no-absolute-filenames ");
203 	for (scan = file_list; scan; scan = scan->next) {
204 		char *filepath = scan->data;
205 		char *filename;
206 
207 		if (filepath[0] == '/')
208 			filename = g_shell_quote (filepath + 1);
209 		else
210 			filename = g_shell_quote (filepath);
211 		g_string_append (cmd, filename);
212 		g_string_append (cmd, " ");
213 
214 		g_free (filename);
215 	}
216         g_string_append (cmd, " < ");
217 	g_string_append (cmd, comm->e_filename);
218 	fr_process_add_arg (comm->process, cmd->str);
219 	g_string_free (cmd, TRUE);
220 
221 	fr_process_end_command (comm->process);
222 }
223 
224 
225 const char *cpio_mime_type[] = { "application/x-cpio", NULL };
226 
227 
228 static const char **
fr_command_cpio_get_mime_types(FrCommand * comm)229 fr_command_cpio_get_mime_types (FrCommand *comm)
230 {
231 	return cpio_mime_type;
232 }
233 
234 
235 static FrCommandCap
fr_command_cpio_get_capabilities(FrCommand * comm,const char * mime_type,gboolean check_command)236 fr_command_cpio_get_capabilities (FrCommand  *comm,
237 			          const char *mime_type,
238 				  gboolean    check_command)
239 {
240 	FrCommandCap capabilities;
241 
242 	capabilities = FR_COMMAND_CAN_ARCHIVE_MANY_FILES;
243 	if (is_program_available ("cpio", check_command))
244 		capabilities |= FR_COMMAND_CAN_READ;
245 
246 	return capabilities;
247 }
248 
249 
250 static const char *
fr_command_cpio_get_packages(FrCommand * comm,const char * mime_type)251 fr_command_cpio_get_packages (FrCommand  *comm,
252 			      const char *mime_type)
253 {
254 	return PACKAGES ("cpio");
255 }
256 
257 
258 static void
fr_command_cpio_class_init(FrCommandCpioClass * class)259 fr_command_cpio_class_init (FrCommandCpioClass *class)
260 {
261         GObjectClass   *gobject_class = G_OBJECT_CLASS (class);
262         FrCommandClass *afc;
263 
264         parent_class = g_type_class_peek_parent (class);
265 	afc = (FrCommandClass*) class;
266 
267 	gobject_class->finalize = fr_command_cpio_finalize;
268 
269         afc->list             = fr_command_cpio_list;
270 	afc->extract          = fr_command_cpio_extract;
271 	afc->get_mime_types   = fr_command_cpio_get_mime_types;
272 	afc->get_capabilities = fr_command_cpio_get_capabilities;
273 	afc->get_packages     = fr_command_cpio_get_packages;
274 }
275 
276 
277 static void
fr_command_cpio_init(FrCommand * comm)278 fr_command_cpio_init (FrCommand *comm)
279 {
280 	comm->propAddCanUpdate             = FALSE;
281 	comm->propAddCanReplace            = FALSE;
282 	comm->propAddCanStoreFolders       = FALSE;
283 	comm->propExtractCanAvoidOverwrite = FALSE;
284 	comm->propExtractCanSkipOlder      = FALSE;
285 	comm->propExtractCanJunkPaths      = FALSE;
286 	comm->propPassword                 = FALSE;
287 	comm->propTest                     = FALSE;
288 }
289 
290 
291 static void
fr_command_cpio_finalize(GObject * object)292 fr_command_cpio_finalize (GObject *object)
293 {
294         g_return_if_fail (object != NULL);
295         g_return_if_fail (FR_IS_COMMAND_CPIO (object));
296 
297 	/* Chain up */
298         if (G_OBJECT_CLASS (parent_class)->finalize)
299 		G_OBJECT_CLASS (parent_class)->finalize (object);
300 }
301 
302 
303 GType
fr_command_cpio_get_type()304 fr_command_cpio_get_type ()
305 {
306         static GType type = 0;
307 
308         if (! type) {
309                 GTypeInfo type_info = {
310 			sizeof (FrCommandCpioClass),
311 			NULL,
312 			NULL,
313 			(GClassInitFunc) fr_command_cpio_class_init,
314 			NULL,
315 			NULL,
316 			sizeof (FrCommandCpio),
317 			0,
318 			(GInstanceInitFunc) fr_command_cpio_init
319 		};
320 
321 		type = g_type_register_static (FR_TYPE_COMMAND,
322 					       "FRCommandCpio",
323 					       &type_info,
324 					       0);
325         }
326 
327         return type;
328 }
329