1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
4 Copyright (C) 2010 Sebastien Granjoux
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25 #include <libanjuta/interfaces/ianjuta-wizard.h>
27 /*---------------------------------------------------------------------------*/
29 #define TMP_DEF_FILENAME "NPWDEFXXXXXX"
30 #define TMP_TPL_FILENAME "NPWTPLXXXXXX"
32 #define FILE_BUFFER_SIZE 4096
35 *---------------------------------------------------------------------------*/
37 typedef struct _NPWTarPacket NPWTarPacket
;
50 *---------------------------------------------------------------------------*/
53 *---------------------------------------------------------------------------*/
56 npw_tar_packet_free (NPWTarPacket
*pack
)
58 g_object_unref (pack
->tarfile
);
59 if (pack
->destination
) g_object_unref (pack
->destination
);
64 *---------------------------------------------------------------------------*/
67 on_tar_listed (GPid pid
,
71 NPWTarPacket
*pack
= (NPWTarPacket
*)data
;
73 if (pack
->callback
!= NULL
)
81 line
= g_string_new (NULL
);
82 output
= g_io_channel_unix_new (pack
->stdout
);
87 status
= g_io_channel_read_line_string (output
, line
, &terminator
, NULL
);
88 if (status
== G_IO_STATUS_NORMAL
)
90 g_string_truncate (line
, terminator
);
92 list
= g_list_prepend (list
, g_strdup (line
->str
));
96 while (status
== G_IO_STATUS_AGAIN
);
97 g_io_channel_shutdown (output
, TRUE
, NULL
);
98 g_io_channel_unref (output
);
99 g_string_free (line
, TRUE
);
101 list
= g_list_reverse (list
);
102 ((NPWTarListFunc
)pack
->callback
) (pack
->tarfile
, list
, pack
->data
, NULL
);
104 g_list_foreach (list
, (GFunc
)g_free
, NULL
);
108 g_spawn_close_pid(pid
);
112 on_tar_completed (GPid pid
,
116 NPWTarPacket
*pack
= (NPWTarPacket
*)data
;
118 if (pack
->callback
!= NULL
)
120 GError
*error
= NULL
;
127 stderr
= g_io_channel_unix_new (pack
->stderr
);
128 g_io_channel_read_to_end (stderr
, &message
, &length
, &error
);
131 error
= g_error_new_literal (IANJUTA_WIZARD_ERROR
, 0, message
);
133 g_io_channel_shutdown (stderr
, TRUE
, NULL
);
134 g_io_channel_unref (stderr
);
137 ((NPWTarCompleteFunc
)pack
->callback
) (pack
->destination
, pack
->tarfile
, pack
->data
, error
);
138 g_clear_error (&error
);
141 g_spawn_close_pid(pid
);
145 *---------------------------------------------------------------------------*/
148 npw_tar_list (GFile
*tarfile
, NPWTarListFunc list
, gpointer data
, GError
**error
)
150 gchar
*argv
[] = {"tar", "--force-local", "--no-wildcards", "-tzf", NULL
, NULL
};
157 /* Use gtar if available */
158 prog
= g_find_program_in_path ("gtar");
159 filename
= g_file_get_path (tarfile
);
162 /* Execute tar command asynchronously */
163 pack
= g_new0(NPWTarPacket
, 1);
164 pack
->callback
= (gpointer
)list
;
166 pack
->tarfile
= g_object_ref (tarfile
);
167 ok
= g_spawn_async_with_pipes (NULL
, argv
, NULL
,
168 G_SPAWN_DO_NOT_REAP_CHILD
| G_SPAWN_SEARCH_PATH
| G_SPAWN_STDERR_TO_DEV_NULL
,
178 g_child_watch_add_full (G_PRIORITY_HIGH_IDLE
, pid
, on_tar_listed
, pack
, (GDestroyNotify
)npw_tar_packet_free
);
188 npw_tar_extract (GFile
*destination
, GFile
*tarfile
, NPWTarCompleteFunc complete
, gpointer data
, GError
**error
)
190 gchar
*argv
[] = {"tar", "--force-local", "--no-wildcards", "-C", NULL
, "-xzf", NULL
, NULL
};
198 /* Use gtar if available */
199 prog
= g_find_program_in_path ("gtar");
200 dirname
= g_file_get_path (destination
);
202 filename
= g_file_get_path (tarfile
);
205 /* Execute tar command asynchronously */
206 pack
= g_new0(NPWTarPacket
, 1);
207 pack
->callback
= (gpointer
)complete
;
209 pack
->tarfile
= g_object_ref (tarfile
);
210 pack
->destination
= g_object_ref (destination
);
211 ok
= g_spawn_async_with_pipes (NULL
, argv
, NULL
,
212 G_SPAWN_DO_NOT_REAP_CHILD
| G_SPAWN_SEARCH_PATH
,
222 g_child_watch_add_full (G_PRIORITY_HIGH_IDLE
, pid
, on_tar_completed
, pack
, (GDestroyNotify
)npw_tar_packet_free
);