~ubuntu-branches/ubuntu/jaunty/xarchiver/jaunty

« back to all changes in this revision

Viewing changes to src/bzip2.c

  • Committer: Bazaar Package Importer
  • Author(s): Daniel Baumann
  • Date: 2008-11-07 14:54:00 UTC
  • mfrom: (1.1.7 upstream) (2.1.5 sid)
  • Revision ID: james.westby@ubuntu.com-20081107145400-z0j3jmgads8coae2
Tags: 0.5.1-1
MergingĀ upstreamĀ versionĀ 0.5.1.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*
2
 
 *  Copyright (C) 2006 Giuseppe Torelli <colossus73@gmail.com>
3
 
 *  Copyright (C) 2006 Benedikt Meurer - <benny@xfce.org>
 
2
 *  Copyright (C) 2008 Giuseppe Torelli - <colossus73@gmail.com>
4
3
 *
5
4
 *  This program is free software; you can redistribute it and/or modify
6
5
 *  it under the terms of the GNU General Public License as published by
22
21
#include "extract_dialog.h"
23
22
#include "string_utils.h"
24
23
 
25
 
extern gboolean TarOpen (GIOChannel *ioc, GIOCondition cond, gpointer data);
26
 
extern int output_fd;
27
 
extern gboolean cli;
28
 
 
 
24
extern delete_func      delete  [XARCHIVETYPE_COUNT];
 
25
extern add_func         add     [XARCHIVETYPE_COUNT];
 
26
extern extract_func     extract [XARCHIVETYPE_COUNT];
29
27
short int l;
30
28
 
31
 
void OpenBzip2 ( XArchive *archive )
 
29
void xa_open_bzip2_lzma (XArchive *archive)
32
30
{
33
 
        gchar *command;
34
 
    if ( g_str_has_suffix ( archive->escaped_path , ".tar.bz2") || g_str_has_suffix ( archive->escaped_path , ".tar.bz") || g_str_has_suffix ( archive->escaped_path , ".tbz") || g_str_has_suffix ( archive->escaped_path , ".tbz2" ) )
 
31
        XEntry *entry = NULL;
 
32
        gchar *filename = NULL;;
 
33
        gchar *_filename;
 
34
        gpointer item[2];
 
35
        gboolean result;
 
36
        gint len = 0;
 
37
 
 
38
        if (g_str_has_suffix(archive->escaped_path,".tar.bz2") || g_str_has_suffix (archive->escaped_path,".tar.bz")
 
39
        || g_str_has_suffix ( archive->escaped_path , ".tbz") || g_str_has_suffix (archive->escaped_path,".tbz2") )
35
40
        {
36
 
                gchar *tar;
37
 
 
38
 
                tar = g_find_program_in_path ("gtar");
39
 
                if (tar == NULL)
40
 
                        tar = g_strdup ("tar");
41
 
 
42
 
                command = g_strconcat (tar, " tfjv " , archive->escaped_path, NULL );
43
 
                archive->dummy_size = 0;
44
 
                archive->nr_of_files = 0;
45
 
                archive->nr_of_dirs = 0;
 
41
                archive->type = XARCHIVETYPE_TAR_BZ2;
46
42
                archive->format = "TAR.BZIP2";
47
 
                archive->parse_output = TarOpen;
48
 
                SpawnAsyncProcess ( archive , command , 0, 0);
49
 
 
50
 
                g_free (command);
51
 
                g_free (tar);
52
 
 
53
 
                if ( archive->child_pid == 0 )
 
43
                archive->delete =       delete[archive->type];
 
44
                archive->add =          add[archive->type];
 
45
                archive->extract =      extract[archive->type];
 
46
                xa_open_tar_compressed_file(archive);
 
47
        }
 
48
        else if (g_str_has_suffix(archive->escaped_path,".tar.lzma") || g_str_has_suffix (archive->escaped_path,".tlz"))
 
49
        {
 
50
                archive->type = XARCHIVETYPE_TAR_LZMA;
 
51
                archive->format = "TAR.LZMA";
 
52
                archive->delete =       delete[archive->type];
 
53
                archive->add =          add[archive->type];
 
54
                archive->extract =      extract[archive->type];
 
55
                xa_open_tar_compressed_file(archive);
 
56
        }
 
57
        else if (g_str_has_suffix(archive->escaped_path,".tar.lzop") || g_str_has_suffix (archive->escaped_path,".tzo"))
 
58
        {
 
59
                archive->type = XARCHIVETYPE_TAR_LZOP;
 
60
                archive->format = "TAR.LZOP";
 
61
                archive->delete =       delete[archive->type];
 
62
                archive->add =          add[archive->type];
 
63
                archive->extract =      extract[archive->type];
 
64
                xa_open_tar_compressed_file(archive);
 
65
        }
 
66
        else
 
67
        {
 
68
                struct stat my_stat;
 
69
                gchar *compressed = NULL;
 
70
                gchar *size = NULL,*command = NULL,*executable = NULL,*dot = NULL;
 
71
                unsigned short int i;
 
72
                GSList *list = NULL;
 
73
 
 
74
                if (archive->type == XARCHIVETYPE_BZIP2)
 
75
                {
 
76
                        archive->format = "BZIP2";
 
77
                        executable = "bzip2 ";
 
78
                        len = 4;
 
79
                }
 
80
                else if (archive->type == XARCHIVETYPE_LZMA)
 
81
                {
 
82
                        archive->format = "LZMA";
 
83
                        executable = "lzma ";
 
84
                        len = 5;
 
85
                }
 
86
                else if (archive->type == XARCHIVETYPE_LZOP)
 
87
                {
 
88
                        archive->format = "LZOP";
 
89
                        executable = "lzop ";
 
90
                        len = 5;
 
91
                } /* else fail? */
 
92
                archive->can_add = archive->has_test = archive->has_sfx = FALSE;
 
93
                archive->has_properties = archive->can_extract = TRUE;
 
94
                archive->nc = 3;
 
95
                archive->nr_of_files = 1;
 
96
 
 
97
                GType types[]= {GDK_TYPE_PIXBUF,G_TYPE_STRING,G_TYPE_UINT64,G_TYPE_UINT64,G_TYPE_POINTER};
 
98
                archive->column_types = g_malloc0(sizeof(types));
 
99
                for (i = 0; i < 5; i++)
 
100
                        archive->column_types[i] = types[i];
 
101
 
 
102
                char *names[]= {(_("Original")),(_("Compressed"))};
 
103
                xa_create_liststore (archive,names);
 
104
                result = xa_create_temp_directory (archive);
 
105
                if (!result)
54
106
                        return;
55
107
 
56
 
                char *names[]= {(_("Filename")),(_("Permissions")),(_("Symbolic Link")),(_("Owner/Group")),(_("Size")),(_("Date")),(_("Time"))};
57
 
                GType types[]= {G_TYPE_STRING,G_TYPE_STRING,G_TYPE_STRING,G_TYPE_STRING,G_TYPE_UINT64,G_TYPE_STRING,G_TYPE_STRING};
58
 
                xa_create_liststore ( 7, names , (GType *)types, archive);
59
 
                archive->type = XARCHIVETYPE_TAR_BZ2;
60
 
        }
61
 
        else
62
 
        {
63
 
                extract_window = xa_create_extract_dialog ( 0 , archive);
64
 
                command = xa_parse_extract_dialog_options ( archive , extract_window, NULL );
65
 
                gtk_widget_destroy ( extract_window->dialog1 );
66
 
                g_free (extract_window);
 
108
                /* Let's copy the bzip2 file in the tmp dir */
 
109
                command = g_strconcat("cp -f ",archive->escaped_path," ",archive->tmp,NULL);
 
110
                list = g_slist_append(list,command);
 
111
                /* Let's get its compressed file size */
 
112
                stat (archive->escaped_path,&my_stat);
 
113
                compressed = g_strdup_printf("%lld",(unsigned long long int)my_stat.st_size);
 
114
                item[1] = compressed;
 
115
 
 
116
                /* Let's extract it */
 
117
                _filename = g_path_get_basename(archive->escaped_path);
 
118
                if (_filename[0] == '.')
 
119
                        command = g_strconcat(executable,"-f -d ",archive->tmp,"/",archive->escaped_path,NULL);
 
120
                else
 
121
                        command = g_strconcat(executable,"-f -d ",archive->tmp,"/",_filename,NULL);
 
122
 
 
123
                list = g_slist_append(list,command);
 
124
                xa_run_command (archive,list);
 
125
 
 
126
                /* and let's get its uncompressed file size */
 
127
                dot = strrchr(_filename,'.');
 
128
                if (_filename || G_LIKELY(dot))
 
129
                {
 
130
                        filename = g_strndup(_filename,strlen(_filename) - len);
 
131
                        command = g_strconcat(archive->tmp,"/",filename,NULL);
 
132
                }
 
133
                else
 
134
                {
 
135
                        command = g_strconcat(archive->tmp,"/",archive->escaped_path,NULL);
 
136
                        filename = g_strdup(archive->escaped_path);
 
137
                }
 
138
                stat (command,&my_stat);
 
139
                g_free(command);
 
140
                size = g_strdup_printf("%lld",(unsigned long long int)my_stat.st_size);
 
141
                archive->dummy_size = my_stat.st_size;
 
142
                item[0] = size;
 
143
 
 
144
                entry = xa_set_archive_entries_for_each_row (archive,filename,item);
 
145
                g_free(compressed);
 
146
                g_free(size);
 
147
                g_free(filename);
 
148
 
 
149
                xa_update_window_with_archive_entries (archive,NULL);
 
150
                gtk_tree_view_set_model (GTK_TREE_VIEW(archive->treeview), archive->model);
 
151
                g_object_unref (archive->model);
67
152
        }
68
153
}
69
154
 
70
 
void gzip_bzip2_extract ( XArchive *archive , gboolean flag )
 
155
void xa_open_tar_compressed_file(XArchive *archive)
71
156
{
72
 
    gchar *text = NULL;
73
 
        gchar *filename_only = NULL;
74
157
        gchar *command = NULL;
 
158
        unsigned short int i;
 
159
 
 
160
        if (archive->type == XARCHIVETYPE_TAR_BZ2)
 
161
                command = g_strconcat(tar," tfjv ",archive->escaped_path,NULL);
 
162
        else if (archive->type == XARCHIVETYPE_TAR_LZMA)
 
163
                command = g_strconcat(tar," tv --use-compress-program=lzma -f ",archive->escaped_path,NULL);
 
164
        else if (archive->type == XARCHIVETYPE_TAR_LZOP)
 
165
                command = g_strconcat(tar," tv --use-compress-program=lzop -f ",archive->escaped_path,NULL);
 
166
        /* else fail? */
 
167
 
 
168
        archive->has_properties = archive->can_add = archive->can_extract = TRUE;
 
169
        archive->has_test = archive->has_sfx = FALSE;
 
170
        archive->dummy_size = 0;
 
171
        archive->nr_of_files = 0;
 
172
        archive->nc = 7;
 
173
        archive->parse_output = xa_get_tar_line_content;
 
174
        xa_spawn_async_process (archive,command);
 
175
        g_free (command);
 
176
 
 
177
        if (archive->child_pid == 0)
 
178
                return;
 
179
 
 
180
        GType types[]= {GDK_TYPE_PIXBUF,G_TYPE_STRING,G_TYPE_STRING,G_TYPE_STRING,G_TYPE_STRING,G_TYPE_UINT64,G_TYPE_STRING,G_TYPE_STRING,G_TYPE_POINTER};
 
181
        archive->column_types = g_malloc0(sizeof(types));
 
182
        for (i = 0; i < 9; i++)
 
183
                archive->column_types[i] = types[i];
 
184
 
 
185
        char *names[]= {(_("Points to")),(_("Permissions")),(_("Owner/Group")),(_("Size")),(_("Date")),(_("Time")),NULL};
 
186
        xa_create_liststore (archive,names);
 
187
}
 
188
 
 
189
gboolean lzma_bzip2_extract (XArchive *archive,GSList *dummy)
 
190
{
 
191
        GSList *list = NULL;
 
192
        gchar  *command = NULL,*executable = NULL,*filename = NULL, *dot = NULL, *filename_noext = NULL;
75
193
        gboolean result = FALSE;
76
 
        gboolean ext;
77
 
 
78
 
        if ( ! cli )
79
 
                archive->extraction_path = g_strdup (gtk_entry_get_text ( GTK_ENTRY (extract_window->destination_path_entry) ));
80
 
 
81
 
        if ( strlen ( archive->extraction_path ) > 0 )
82
 
        {
83
 
                if (! cli)
84
 
                {
85
 
                        if (flag)
86
 
                                text = g_strdup_printf(_("Extracting gzip file to %s"), archive->extraction_path);
87
 
                        else
88
 
                                text = g_strdup_printf(_("Extracting bzip2 file to %s"), archive->extraction_path);
89
 
                        Update_StatusBar ( text );
90
 
                        g_free (text);
91
 
                }
92
 
 
93
 
                filename_only = g_strrstr (archive->escaped_path , "/");
94
 
                if (file_extension_is (filename_only,".gz") || file_extension_is (filename_only,".bz2") )
95
 
                        ext = TRUE;
96
 
                else
97
 
                        ext = FALSE;
98
 
 
99
 
                if (ext)
100
 
                        command = g_strconcat ("cp -f ", archive->escaped_path, " /tmp", NULL);
101
 
                else
102
 
                        command = g_strconcat ("cp -f ", archive->escaped_path, " /tmp" , filename_only, flag ? ".gz" : ".bz2", NULL);
103
 
 
104
 
                result = xa_run_command (command , 0);
105
 
                g_free (command);
106
 
                if (result == 0)
107
 
                        return ;
108
 
                if ( ext  )
109
 
                        command = g_strconcat (flag ? "gzip -f -d -n " : "bzip2 -f -d ", "/tmp",filename_only, NULL);
110
 
                else
111
 
                        command = g_strconcat (flag ? "gzip -f -d -n " : "bzip2 -f -d ","/tmp",filename_only, flag ? ".gz" : ".bz2", NULL);
112
 
 
113
 
                result = xa_run_command (command , 0);
114
 
                g_free (command);
115
 
                if (result == 0)
116
 
                        return;
117
 
 
118
 
                if (ext)
119
 
                {
120
 
                        if (flag)
121
 
                                filename_only[strlen(filename_only) - 3] = '\0';
122
 
                        else
123
 
                                filename_only[strlen(filename_only) - 4] = '\0';
124
 
                                command = g_strconcat ("mv -f /tmp",filename_only, " ", archive->extraction_path,NULL);
125
 
                }
126
 
                else
127
 
                {
128
 
                        if ( g_file_test (archive->extraction_path, G_FILE_TEST_IS_DIR) )
129
 
                                command = g_strconcat ("mv -f /tmp",filename_only, " ", archive->extraction_path,filename_only,NULL);
130
 
                        else
131
 
                                command = g_strconcat ("mv -f /tmp",filename_only, " ", archive->extraction_path,NULL);
132
 
                }
133
 
 
134
 
                result = xa_run_command (command , 0);
135
 
                g_free (command);
136
 
                if (result == 0)
137
 
                        return;
138
 
        }
139
 
        if (result == 0)
140
 
        {
141
 
                xa_set_button_state (1,1,0,0,0,0);
142
 
                archive->status = XA_ARCHIVESTATUS_IDLE;
143
 
                gtk_widget_set_sensitive (Stop_button, FALSE);
144
 
                gtk_widget_hide ( viewport2 );
145
 
                Update_StatusBar ( _("Operation canceled."));
146
 
        }
147
 
        else
148
 
                xa_watch_child (archive->child_pid, 0, archive);
149
 
}
150
 
 
151
 
void xa_add_delete_tar_bzip2_gzip ( GString *list , XArchive *archive , gboolean dummy , gboolean add )
152
 
{
153
 
        gchar *command, *msg, *tar,*temp_name,*file_ext;
154
 
        gboolean result;
155
 
 
156
 
        if ( ! cli )
157
 
        {
158
 
                gtk_widget_show (viewport2);
159
 
                msg = g_strdup_printf(_("Decompressing tar file with %s, please wait...") , dummy ? "gzip" : "bzip2");
160
 
                Update_StatusBar ( msg );
161
 
                g_free (msg);
162
 
        }
163
 
 
164
 
        /* Let's copy the archive to /tmp first */
165
 
        temp_name = g_strconcat ( " /tmp", g_strrstr (archive->escaped_path , "/"), NULL);
166
 
        command = g_strconcat ("cp -ar " ,archive->escaped_path,temp_name,NULL);
167
 
        if ( ! cli)
168
 
                result = xa_run_command (command , 0);
169
 
        else
170
 
                result = SpawnSyncCommand ( command );
171
 
        g_free (command);
172
 
        if (result == 0)
173
 
        {
174
 
                g_free (temp_name);
175
 
                return;
176
 
        }
177
 
        command = g_strconcat (dummy ? "gzip -f " : "bzip2 ", "-f -d ",temp_name,NULL);
178
 
        if ( ! cli )
179
 
                result = xa_run_command (command , 0);
180
 
        else
181
 
                result = SpawnSyncCommand ( command );
182
 
        g_free (command);
183
 
        if (result == 0)
184
 
        {
185
 
                g_free (temp_name);
186
 
                return;
187
 
        }
188
 
 
189
 
        tar = g_find_program_in_path ("gtar");
190
 
        if (tar == NULL)
191
 
                tar = g_strdup ("tar");
192
 
        l = strlen (temp_name);
193
 
 
194
 
        if (file_extension_is (archive->escaped_path,".tar.bz2") )
195
 
                temp_name[l - 4] = 0;
196
 
        else if (file_extension_is (archive->escaped_path,".tbz2") )
197
 
        {
198
 
                temp_name[l - 3] = 'a';
199
 
                temp_name[l - 2] = 'r';
200
 
                temp_name[l - 1] = 0;
201
 
        }
202
 
        else if (file_extension_is (archive->escaped_path,".tar.gz") )
203
 
                temp_name[l - 3] = 0;
204
 
 
205
 
        else if (file_extension_is (archive->escaped_path,".tgz") || file_extension_is (archive->escaped_path, ".tbz") )
206
 
        {
207
 
                temp_name[l - 2] = 'a';
208
 
                temp_name[l - 1] = 'r';
209
 
        }
210
 
 
211
 
        if ( add )
212
 
                command = g_strconcat (tar, " ",
213
 
                                                        archive->add_recurse ? "" : "--no-recursion ",
214
 
                                                        archive->remove_files ? "--remove-files " : "",
215
 
                                                        archive->update ? "-uvvf " : "-rvvf ",
216
 
                                                        temp_name,
217
 
                                                        list->str , NULL );
218
 
        else
219
 
                command = g_strconcat (tar, " --delete -f " , temp_name , list->str , NULL );
220
 
        if ( ! cli)
221
 
                result = xa_run_command (command , 0);
222
 
        else
223
 
                result = SpawnSyncCommand ( command );
224
 
        g_free (command);
225
 
        g_free (tar);
226
 
        if (result == 0)
227
 
        {
228
 
                g_free (temp_name);
229
 
                return;
230
 
        }
231
 
 
232
 
        if ( ! cli )
233
 
        {
234
 
                msg = g_strdup_printf(_("Recompressing tar file with %s, please wait...") , dummy ? "gzip" : "bzip2");
235
 
                Update_StatusBar ( msg );
236
 
                g_free (msg);
237
 
        }
238
 
 
239
 
        command = g_strconcat ( dummy ? "gzip -f " : "bzip2 ", "-f " , temp_name , NULL );
240
 
        if ( ! cli )
241
 
                result = xa_run_command (command , 0);
242
 
        else
243
 
                result = SpawnSyncCommand ( command );
244
 
        g_free (command);
245
 
 
246
 
        if (result == 0)
247
 
        {
248
 
                g_free (temp_name);
249
 
                return;
250
 
        }
251
 
        if (dummy)
252
 
                file_ext = ".gz";
253
 
        else
254
 
                file_ext = ".bz2";
255
 
        /* Let's move the modified archive from /tmp to the original archive location */
256
 
        command = g_strconcat ( "mv " , temp_name , file_ext, " " ,archive->escaped_path, NULL );
257
 
        if ( ! cli )
258
 
                result = xa_run_command (command , 1);
259
 
        else
260
 
                result = SpawnSyncCommand ( command );
261
 
        g_free (command);
262
 
        g_free (temp_name);
263
 
 
264
 
        if (result == 0)
265
 
                return;
266
 
}
267
 
 
 
194
        gint len = 0;
 
195
 
 
196
        if (archive->type == XARCHIVETYPE_BZIP2)
 
197
        {
 
198
                executable = "bzip2 ";
 
199
                len = 4;
 
200
        }
 
201
        else if (archive->type == XARCHIVETYPE_LZMA)
 
202
        {
 
203
                executable = "lzma ";
 
204
                len = 5;
 
205
        }
 
206
        else if (archive->type == XARCHIVETYPE_LZOP)
 
207
        {
 
208
                executable = "lzop ";
 
209
                len = 5;
 
210
        }
 
211
        /* else fail? */
 
212
        filename = xa_remove_path_from_archive_name(archive->escaped_path);
 
213
        dot = strrchr(filename,'.');
 
214
        if (G_LIKELY(dot))
 
215
        {
 
216
                filename_noext = g_strndup(filename,(dot - filename));
 
217
                g_free(filename);
 
218
        }
 
219
        else
 
220
                filename_noext = filename;
 
221
 
 
222
        command = g_strconcat("sh -c \"",executable, " ",archive->escaped_path," -dc > ",archive->extraction_path,"/",filename_noext,"\"",NULL);
 
223
        g_free(filename_noext);
 
224
        list = g_slist_append(list,command);
 
225
        result = xa_run_command (archive,list);
 
226
        return result;
 
227
}