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