+Unicode/UTF8 locale conversion support for 'G_BROKEN_FILENAMES' etc.
[captive.git] / src / client / cmdline / cmd_get.c
1 /* $Id$
2  * client cmdline interface command "get" for libcaptive
3  * Copyright (C) 2003 Jan Kratochvil <project-captive@jankratochvil.net>
4  * 
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; exactly version 2 of June 1991 is required
8  * 
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  * 
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17  */
18
19
20 #include "config.h"
21
22 #include <glib/gmessages.h>
23 #include <glib/gerror.h>
24 #include <popt.h>
25 #include <captive/client-file.h>
26 #include <fcntl.h>
27 #include <errno.h>
28 #include <unistd.h>
29 #include <captive/macros.h>
30 #include <stdlib.h>
31
32 #include "cmd_get.h"    /* self */
33 #include "cmd_cd.h"     /* for cmdline_path_from_cwd() */
34 #include "main.h"
35 #include "utf8.h"
36
37
38 /* Config: */
39 #define DEFAULT_TRANSFER_BUFFER_SIZE (0x10000)
40
41
42 GQuark cmdline_cmd_get_error_quark(void)
43 {
44 GQuark r=0;
45
46         if (!r)
47                 r=g_quark_from_static_string("cmdline-cmd-get");
48
49         return r;
50 }
51
52
53 static gchar *optarg_transfer_buffer;
54
55 const struct poptOption cmd_get_table[]={
56                 CMDLINE_POPT("buffer-size",'b',POPT_ARG_STRING,&optarg_transfer_buffer,
57                                 N_("Transfer buffer size"),N_("size")),
58                 CMDLINE_POPT_AUTOHELP
59                 POPT_TABLEEND
60                 };
61
62
63 void cmd_get(const char **cmd_argv,GError **errp)
64 {
65 CaptiveFileObject *captive_file_object;
66 const gchar *sourcefile,*targetfile;
67 guint perm=0644;
68 int fdtgt;
69 guint8 *transfer_buffer;
70 long transfer_buffer_size=DEFAULT_TRANSFER_BUFFER_SIZE;
71
72         g_return_if_fail(!errp || !*errp);
73
74         if (optarg_transfer_buffer) {
75 const gchar *string=captive_strdup_alloca(optarg_transfer_buffer);
76 char *endptr;
77
78                 free(optarg_transfer_buffer);
79                 optarg_transfer_buffer=NULL;
80
81                 transfer_buffer_size=strtol(string,&endptr,0);
82                 if (transfer_buffer_size<=0 || transfer_buffer_size>=LONG_MAX || (endptr && *endptr)) {
83                         g_set_error(errp,CMDLINE_CMD_GET_ERROR,CMDLINE_CMD_GET_ERROR_PARSING_TRANSFER_BUFFER_SIZE,
84                                         _("Error parsing transfer buffer size: %s"),CMD_LOCALE_FROM_UTF8_ALLOCA(string));
85                         return;
86                         }
87                 }
88
89         sourcefile=cmdline_path_from_cwd(cmd_argv[0]);
90         if (cmd_argv[1])
91                 targetfile=captive_strdup_alloca(cmdline_path_from_cwd(cmd_argv[1]));
92         else {
93 char *s;
94
95                 targetfile=captive_strdup_alloca(sourcefile);
96                 if ((s=strrchr(targetfile,G_DIR_SEPARATOR)))
97                         targetfile=s+1;
98                 }
99
100         if (-1==(fdtgt=open(CMD_FILENAME_FROM_UTF8_ALLOCA(targetfile),
101                         O_CREAT|O_EXCL|O_WRONLY /* flags */
102 #ifdef O_BINARY
103                                         | O_BINARY
104 #endif /* O_BINARY */
105 #ifdef O_LARGEFILE
106                                         | O_LARGEFILE
107 #endif /* O_LARGEFILE */
108                                         ,
109                         perm))) {       /* mode */
110                 g_set_error(errp,CMDLINE_CMD_GET_ERROR,CMDLINE_CMD_GET_ERROR_CANNOT_CREATE_TARGET_HOSTOS_FILE,
111                                 _("Cannot create target host-os file '%s': %s"),CMD_LOCALE_FROM_UTF8_ALLOCA(targetfile),g_strerror(errno));
112                 return;
113                 }
114
115         if (!errvfsresult_to_gerr(errp,captive_file_new_open(
116                         &captive_file_object,   /* captive_file_object_return */
117                         cmdline_captive_vfs_object,     /* captive_vfs_object */
118                         sourcefile,     /* pathname */
119                         GNOME_VFS_OPEN_READ))) {        /* mode */
120                 err_cleanup(errp);
121                 g_set_error(errp,CMDLINE_CMD_GET_ERROR,CMDLINE_CMD_GET_ERROR_OPENING_SOURCE_FILE,
122                                 _("Error opening source guest-os file: %s"),CMD_LOCALE_FROM_UTF8_ALLOCA(sourcefile));
123                 goto err_close_fdtgt;
124                 }
125
126         transfer_buffer=g_malloc(transfer_buffer_size);
127         g_assert(transfer_buffer!=NULL);        /* Should not happen. */
128
129         for (;;) {
130 GnomeVFSFileSize bytes_read;
131 GnomeVFSResult errvfsresult;
132 ssize_t gotssize;
133
134                 errvfsresult=captive_file_read(
135                                 captive_file_object,    /* captive_file_object */
136                                 transfer_buffer,        /* buffer */
137                                 transfer_buffer_size,   /* num_bytes */
138                                 &bytes_read);   /* bytes_read_return */
139                 g_assert(errvfsresult==GNOME_VFS_OK || errvfsresult==GNOME_VFS_ERROR_EOF);
140                 g_assert((errvfsresult==GNOME_VFS_ERROR_EOF)==(bytes_read==0));
141                 if (errvfsresult==GNOME_VFS_ERROR_EOF)
142                         break;
143                 if (errvfsresult!=GNOME_VFS_OK) {
144 gboolean errbool;
145
146                         errbool=errvfsresult_to_gerr(errp,errvfsresult);
147                         g_assert(errbool==FALSE);
148                         err_cleanup(errp);
149                         g_set_error(errp,CMDLINE_CMD_GET_ERROR,CMDLINE_CMD_GET_ERROR_READING_SOURCE_FILE,
150                                         _("Error reading source guest-os file '%s': %s"),CMD_LOCALE_FROM_UTF8_ALLOCA(sourcefile),g_strerror(errno));
151                         goto err_free_transfer_buffer;
152                         }
153
154                 if (bytes_read!=(GnomeVFSFileSize)(gotssize=write(fdtgt,transfer_buffer,bytes_read))) {
155                         g_set_error(errp,CMDLINE_CMD_GET_ERROR,CMDLINE_CMD_GET_ERROR_WRITING_TARGET_HOSTOS_FILE,
156                                         _("Error writing target host-os file: %s"),CMD_LOCALE_FROM_UTF8_ALLOCA(targetfile));
157                         goto err_free_transfer_buffer;
158                         }
159                 }
160
161 err_free_transfer_buffer:
162         g_free(transfer_buffer);
163 /* err_unref_captive_file_object: */
164         g_object_unref(captive_file_object);
165 err_close_fdtgt:
166         if (close(fdtgt)) {
167                 err_cleanup(errp);      /* may be clean */
168                 g_set_error(errp,CMDLINE_CMD_GET_ERROR,CMDLINE_CMD_GET_ERROR_CLOSING_TARGET_HOSTOS_FILE,
169                                 _("Error closing target host-os file '%s': %s"),CMD_LOCALE_FROM_UTF8_ALLOCA(targetfile),g_strerror(errno));
170                 }
171 }