Initial original import from: fuse-2.4.2-2.fc4
[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 #include <captive/macros.h>
30 #include <stdlib.h>
31
32 #include "cmd_put.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_put_error_quark(void)
43 {
44 GQuark r=0;
45
46         if (!r)
47                 r=g_quark_from_static_string("cmdline-cmd-put");
48
49         return r;
50 }
51
52
53 static gchar *optarg_transfer_buffer;
54
55 const struct poptOption cmd_put_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_put(const char **cmd_argv,GError **errp)
64 {
65 CaptiveFileObject *captive_file_object;
66 const gchar *sourcefile,*targetfile;
67 guint perm=0644;
68 int fdsrc;
69 guint8 *transfer_buffer;
70 long transfer_buffer_size=DEFAULT_TRANSFER_BUFFER_SIZE;
71 int got;
72
73         g_return_if_fail(!errp || !*errp);
74
75         if (optarg_transfer_buffer) {
76 const gchar *string=captive_strdup_alloca(optarg_transfer_buffer);
77 char *endptr;
78
79                 free(optarg_transfer_buffer);
80                 optarg_transfer_buffer=NULL;
81
82                 transfer_buffer_size=strtol(string,&endptr,0);
83                 if (transfer_buffer_size<=0 || transfer_buffer_size>=LONG_MAX || (endptr && *endptr)) {
84                         g_set_error(errp,CMDLINE_CMD_PUT_ERROR,CMDLINE_CMD_PUT_ERROR_PARSING_TRANSFER_BUFFER_SIZE,
85                                         _("Error parsing transfer buffer size: %s"),CMD_LOCALE_FROM_UTF8_ALLOCA(string));
86                         return;
87                         }
88                 }
89
90         sourcefile=cmd_argv[0];
91         if (cmd_argv[1])
92                 targetfile=cmdline_path_from_cwd(cmd_argv[1]);
93         else {
94 gchar *sourcefile_basename;
95
96                 sourcefile_basename=g_path_get_basename(sourcefile);
97                 targetfile=cmdline_path_from_cwd(sourcefile_basename);
98                 g_free(sourcefile_basename);
99                 }
100
101         if (-1==(fdsrc=open(CMD_FILENAME_FROM_UTF8_ALLOCA(sourcefile),O_RDONLY
102 #ifdef O_BINARY
103                         | O_BINARY
104 #endif /* O_BINARY */
105 #ifdef O_LARGEFILE
106                         | O_LARGEFILE
107 #endif /* O_LARGEFILE */
108                         ))) {
109                 g_set_error(errp,CMDLINE_CMD_PUT_ERROR,CMDLINE_CMD_PUT_ERROR_OPENING_SOURCE_FILE,
110                                 _("Error opening source host-os file '%s': %s"),CMD_LOCALE_FROM_UTF8_ALLOCA(sourcefile),g_strerror(errno));
111                 return;
112                 }
113
114         if (!errvfsresult_to_gerr(errp,captive_file_new_create(
115                         &captive_file_object,   /* captive_file_object_return */
116                         cmdline_captive_vfs_object,     /* captive_vfs_object */
117                         targetfile,     /* pathname */
118                         GNOME_VFS_OPEN_WRITE,   /* mode */
119                         FALSE,  /* exclusive */
120                         perm))) {       /* perm */
121                 err_cleanup(errp);
122                 g_set_error(errp,CMDLINE_CMD_PUT_ERROR,CMDLINE_CMD_PUT_ERROR_CANNOT_CREATE_TARGET_GUESTOS_FILE,
123                                 _("Cannot create target guest-os file: %s"),CMD_LOCALE_FROM_UTF8_ALLOCA(targetfile));
124                 goto err_close_fdsrc;
125                 }
126
127         transfer_buffer=g_malloc(transfer_buffer_size);
128         g_assert(transfer_buffer!=NULL);        /* Should not happen. */
129
130         while (0<(got=read(fdsrc,transfer_buffer,transfer_buffer_size))) {
131 GnomeVFSFileSize bytes_written;
132
133                 if (!errvfsresult_to_gerr(errp,captive_file_write(
134                                 captive_file_object,    /* captive_file_object */
135                                 transfer_buffer,        /* buffer */
136                                 got,    /* num_bytes */
137                                 &bytes_written))) {     /* bytes_written_return */
138                         err_cleanup(errp);
139                         g_set_error(errp,CMDLINE_CMD_PUT_ERROR,CMDLINE_CMD_PUT_ERROR_WRITING_TARGET_GUESTOS_FILE,
140                                         _("Error writing target guest-os file: %s"),CMD_LOCALE_FROM_UTF8_ALLOCA(targetfile));
141                         goto err_free_transfer_buffer;
142                         }
143                 if (bytes_written!=(GnomeVFSFileSize)got) {
144                         g_set_error(errp,CMDLINE_CMD_PUT_ERROR,CMDLINE_CMD_PUT_ERROR_WRITING_TARGET_GUESTOS_FILE,
145                                         _("Error writing target guest-os file '%s': requested %d, written %Lu"),
146                                         CMD_LOCALE_FROM_UTF8_ALLOCA(targetfile),got,(unsigned long long)bytes_written);
147                         goto err_free_transfer_buffer;
148                         }
149                 }
150         if (got==-1) {
151                 g_set_error(errp,CMDLINE_CMD_PUT_ERROR,CMDLINE_CMD_PUT_ERROR_READING_SOURCE_FILE,
152                                 _("Error reading source host-os file '%s': %s"),CMD_LOCALE_FROM_UTF8_ALLOCA(sourcefile),g_strerror(errno));
153                 goto err_free_transfer_buffer;
154                 }
155         g_assert(got==0);
156
157 err_free_transfer_buffer:
158         g_free(transfer_buffer);
159 /* err_unref_captive_file_object: */
160         g_object_unref(captive_file_object);
161 err_close_fdsrc:
162         if (close(fdsrc)) {
163                 err_cleanup(errp);      /* may be clean */
164                 g_set_error(errp,CMDLINE_CMD_PUT_ERROR,CMDLINE_CMD_PUT_ERROR_CLOSING_SOURCE_FILE,
165                                 _("Error closing source host-os file '%s': %s"),CMD_LOCALE_FROM_UTF8_ALLOCA(sourcefile),g_strerror(errno));
166                 }
167 }