5bedd9b6d709af3f86471d1bceda2599c00a3e5e
[captive.git] / src / client / cmdline / cmd_put.c
1 /* $Id$
2  * client cmdline interface command "put" 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
30 #include "cmd_put.h"    /* self */
31 #include "cmd_cd.h"     /* for cmdline_path_from_cwd() */
32 #include "main.h"
33
34
35 /* Config: */
36 #define TRANSFER_BUFFER_SIZE (0x10000)
37
38
39 GQuark cmdline_cmd_put_error_quark(void)
40 {
41 GQuark r=0;
42
43         if (!r)
44                 r=g_quark_from_static_string("cmdline-cmd-put");
45
46         return r;
47 }
48
49
50 const struct poptOption cmd_put_table[]={
51                 POPT_AUTOHELP
52                 POPT_TABLEEND
53                 };
54
55
56 void cmd_put(const char **cmd_argv,GError **errp)
57 {
58 CaptiveFileObject *captive_file_object;
59 const gchar *sourcefile,*targetfile;
60 guint perm=0644;
61 int fdsrc;
62 guint8 transfer_buffer[TRANSFER_BUFFER_SIZE];
63 int got;
64
65         g_return_if_fail(!errp || !*errp);
66
67         sourcefile=cmd_argv[0];
68         if (cmd_argv[1])
69                 targetfile=cmdline_path_from_cwd(cmd_argv[1]);
70         else {
71 gchar *sourcefile_basename;
72
73                 sourcefile_basename=g_path_get_basename(sourcefile);
74                 targetfile=cmdline_path_from_cwd(sourcefile_basename);
75                 g_free(sourcefile_basename);
76                 }
77
78         if (-1==(fdsrc=open(sourcefile,O_RDONLY
79 #ifdef O_BINARY
80                         | O_BINARY
81 #endif /* O_BINARY */
82 #ifdef O_LARGEFILE
83                         | O_LARGEFILE
84 #endif /* O_LARGEFILE */
85                         ))) {
86                 g_set_error(errp,CMDLINE_CMD_PUT_ERROR,CMDLINE_CMD_PUT_ERROR_OPENING_SOURCE_FILE,
87                                 _("Error opening source host-os file '%s': %s"),sourcefile,g_strerror(errno));
88                 return;
89                 }
90
91         if (!errvfsresult_to_gerr(errp,captive_file_new_create(
92                         &captive_file_object,   /* captive_file_object_return */
93                         cmdline_captive_vfs_object,     /* captive_vfs_object */
94                         targetfile,     /* pathname */
95                         GNOME_VFS_OPEN_WRITE,   /* mode */
96                         FALSE,  /* exclusive */
97                         perm))) {       /* perm */
98                 err_cleanup(errp);
99                 g_set_error(errp,CMDLINE_CMD_PUT_ERROR,CMDLINE_CMD_PUT_ERROR_CANNOT_CREATE_TARGET_GUESTOS_FILE,
100                                 _("Cannot create target guest-os file: %s"),targetfile);
101                 goto err_close_fdsrc;
102                 }
103
104         while (0<(got=read(fdsrc,transfer_buffer,sizeof(transfer_buffer)))) {
105 GnomeVFSFileSize bytes_written;
106
107                 if (!errvfsresult_to_gerr(errp,captive_file_write(
108                                 captive_file_object,    /* captive_file_object */
109                                 transfer_buffer,        /* buffer */
110                                 got,    /* num_bytes */
111                                 &bytes_written))) {     /* bytes_written_return */
112                         err_cleanup(errp);
113                         g_set_error(errp,CMDLINE_CMD_PUT_ERROR,CMDLINE_CMD_PUT_ERROR_WRITING_TARGET_GUESTOS_FILE,
114                                         _("Error writing target guest-os file: %s"),targetfile);
115                         goto err_unref_captive_file_object;
116                         }
117                 if (bytes_written!=(GnomeVFSFileSize)got) {
118                         g_set_error(errp,CMDLINE_CMD_PUT_ERROR,CMDLINE_CMD_PUT_ERROR_WRITING_TARGET_GUESTOS_FILE,
119                                         _("Error writing target guest-os file '%s': requested %d, written %Lu"),
120                                         targetfile,got,(unsigned long long)bytes_written);
121                         goto err_unref_captive_file_object;
122                         }
123                 }
124         if (got==-1) {
125                 g_set_error(errp,CMDLINE_CMD_PUT_ERROR,CMDLINE_CMD_PUT_ERROR_READING_SOURCE_FILE,
126                                 _("Error reading source host-os file '%s': %s"),sourcefile,g_strerror(errno));
127                 goto err_unref_captive_file_object;
128                 }
129         g_assert(got==0);
130
131 err_unref_captive_file_object:
132         g_object_unref(captive_file_object);
133 err_close_fdsrc:
134         if (close(fdsrc)) {
135                 err_cleanup(errp);      /* may be clean */
136                 g_set_error(errp,CMDLINE_CMD_PUT_ERROR,CMDLINE_CMD_PUT_ERROR_CLOSING_SOURCE_FILE,
137                                 _("Error closing source host-os file '%s': %s"),sourcefile,g_strerror(errno));
138                 }
139 }