Fixed 'command_name --help' to prevent exit of captive-cmdline(1).
[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
31 #include "cmd_get.h"    /* self */
32 #include "cmd_cd.h"     /* for cmdline_path_from_cwd() */
33 #include "main.h"
34
35
36 /* Config: */
37 #define TRANSFER_BUFFER_SIZE (0x10000)
38
39
40 GQuark cmdline_cmd_get_error_quark(void)
41 {
42 GQuark r=0;
43
44         if (!r)
45                 r=g_quark_from_static_string("cmdline-cmd-get");
46
47         return r;
48 }
49
50
51 const struct poptOption cmd_get_table[]={
52                 CMDLINE_POPT_AUTOHELP
53                 POPT_TABLEEND
54                 };
55
56
57 void cmd_get(const char **cmd_argv,GError **errp)
58 {
59 CaptiveFileObject *captive_file_object;
60 const gchar *sourcefile,*targetfile;
61 guint perm=0644;
62 int fdtgt;
63 guint8 transfer_buffer[TRANSFER_BUFFER_SIZE];
64
65         g_return_if_fail(!errp || !*errp);
66
67         sourcefile=cmdline_path_from_cwd(cmd_argv[0]);
68         if (cmd_argv[1])
69                 targetfile=captive_strdup_alloca(cmdline_path_from_cwd(cmd_argv[1]));
70         else {
71 char *s;
72
73                 targetfile=captive_strdup_alloca(sourcefile);
74                 if ((s=strrchr(targetfile,G_DIR_SEPARATOR)))
75                         targetfile=s+1;
76                 }
77
78         if (-1==(fdtgt=open(targetfile,
79                         O_CREAT|O_EXCL|O_WRONLY /* flags */
80 #ifdef O_BINARY
81                                         | O_BINARY
82 #endif /* O_BINARY */
83 #ifdef O_LARGEFILE
84                                         | O_LARGEFILE
85 #endif /* O_LARGEFILE */
86                                         ,
87                         perm))) {       /* mode */
88                 g_set_error(errp,CMDLINE_CMD_GET_ERROR,CMDLINE_CMD_GET_ERROR_CANNOT_CREATE_TARGET_HOSTOS_FILE,
89                                 _("Cannot create target host-os file: %s"),targetfile);
90                 return;
91                 }
92
93         if (!errvfsresult_to_gerr(errp,captive_file_new_open(
94                         &captive_file_object,   /* captive_file_object_return */
95                         cmdline_captive_vfs_object,     /* captive_vfs_object */
96                         sourcefile,     /* pathname */
97                         GNOME_VFS_OPEN_READ))) {        /* mode */
98                 err_cleanup(errp);
99                 g_set_error(errp,CMDLINE_CMD_GET_ERROR,CMDLINE_CMD_GET_ERROR_OPENING_SOURCE_FILE,
100                                 _("Error opening source guest-os file '%s': %s"),sourcefile,g_strerror(errno));
101                 goto err_close_fdtgt;
102                 }
103
104         for (;;) {
105 GnomeVFSFileSize bytes_read;
106 GnomeVFSResult errvfsresult;
107 ssize_t gotssize;
108
109                 errvfsresult=captive_file_read(
110                                 captive_file_object,    /* captive_file_object */
111                                 transfer_buffer,        /* buffer */
112                                 sizeof(transfer_buffer),        /* num_bytes */
113                                 &bytes_read);   /* bytes_read_return */
114                 g_assert(errvfsresult==GNOME_VFS_OK || errvfsresult==GNOME_VFS_ERROR_EOF);
115                 g_assert((errvfsresult==GNOME_VFS_ERROR_EOF)==(bytes_read==0));
116                 if (errvfsresult==GNOME_VFS_ERROR_EOF)
117                         break;
118                 if (errvfsresult!=GNOME_VFS_OK) {
119 gboolean errbool;
120
121                         errbool=errvfsresult_to_gerr(errp,errvfsresult);
122                         g_assert(errbool==FALSE);
123                         err_cleanup(errp);
124                         g_set_error(errp,CMDLINE_CMD_GET_ERROR,CMDLINE_CMD_GET_ERROR_READING_SOURCE_FILE,
125                                         _("Error reading source guest-os file '%s': %s"),sourcefile,g_strerror(errno));
126                         goto err_unref_captive_file_object;
127                         }
128
129                 if (bytes_read!=(GnomeVFSFileSize)(gotssize=write(fdtgt,transfer_buffer,bytes_read))) {
130                         g_set_error(errp,CMDLINE_CMD_GET_ERROR,CMDLINE_CMD_GET_ERROR_WRITING_TARGET_HOSTOS_FILE,
131                                         _("Error writing target host-os file: %s"),targetfile);
132                         goto err_unref_captive_file_object;
133                         }
134                 }
135
136 err_unref_captive_file_object:
137         g_object_unref(captive_file_object);
138 err_close_fdtgt:
139         if (close(fdtgt)) {
140                 err_cleanup(errp);      /* may be clean */
141                 g_set_error(errp,CMDLINE_CMD_GET_ERROR,CMDLINE_CMD_GET_ERROR_CLOSING_TARGET_HOSTOS_FILE,
142                                 _("Error closing target host-os file '%s': %s"),targetfile,g_strerror(errno));
143                 }
144 }