| 1 | /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */ |
|---|
| 2 | /* |
|---|
| 3 | * Copyright (C) 2000 Eazel, Inc |
|---|
| 4 | * |
|---|
| 5 | * This program is free software; you can redistribute it and/or |
|---|
| 6 | * modify it under the terms of the GNU General Public License as |
|---|
| 7 | * published by the Free Software Foundation; either version 2 of the |
|---|
| 8 | * License, or (at your option) any later version. |
|---|
| 9 | * |
|---|
| 10 | * This program is distributed in the hope that it will be useful, |
|---|
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|---|
| 13 | * General Public License for more details. |
|---|
| 14 | * |
|---|
| 15 | * You should have received a copy of the GNU General Public |
|---|
| 16 | * License along with this program; if not, write to the |
|---|
| 17 | * Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
|---|
| 18 | * Boston, MA 02111-1307, USA. |
|---|
| 19 | * |
|---|
| 20 | * Authors: Ian McKellar <yakk@yakk.net.au> |
|---|
| 21 | * |
|---|
| 22 | */ |
|---|
| 23 | |
|---|
| 24 | #include <config.h> |
|---|
| 25 | #include <string.h> |
|---|
| 26 | #include <unistd.h> |
|---|
| 27 | #include <glib.h> |
|---|
| 28 | #include <string.h> |
|---|
| 29 | #include "vault-operations.h" |
|---|
| 30 | |
|---|
| 31 | static GnomeVFSResult vault_list(GList *args, gchar *uri, gboolean debug, gchar **error_context); |
|---|
| 32 | static GnomeVFSResult vault_upload(GList *args, gchar *uri, gboolean debug, gchar **error_context); |
|---|
| 33 | static GnomeVFSResult vault_download(GList *args, gchar *uri, gboolean debug, gchar **error_context); |
|---|
| 34 | static GnomeVFSResult vault_move(GList *args, gchar *uri, gboolean debug, gchar **error_context); |
|---|
| 35 | static GnomeVFSResult vault_delete(GList *args, gchar *uri, gboolean debug, gchar **error_context); |
|---|
| 36 | |
|---|
| 37 | struct VaultOperation vault_operations[] = { |
|---|
| 38 | {"list", "list [<remote path>]", 0, 1, vault_list}, |
|---|
| 39 | {"ls", "ls [<remote path>]", 0, 1, vault_list}, |
|---|
| 40 | {"upload", "upload <local path> [<remote path>]", 1, 2, vault_upload}, |
|---|
| 41 | {"download", "download <remote path> [<local path>]", 1, 2, vault_download}, |
|---|
| 42 | {"move", "move <from> <to>", 2, 2, vault_move}, |
|---|
| 43 | {"mv", "mv <from> <to>", 2, 2, vault_move}, |
|---|
| 44 | {"rename", "rename <from> <to>", 2, 2, vault_move}, |
|---|
| 45 | {"delete", "delete <file>", 1, 1, vault_delete}, |
|---|
| 46 | {NULL, NULL, 0, 0, NULL} |
|---|
| 47 | }; |
|---|
| 48 | |
|---|
| 49 | |
|---|
| 50 | static GnomeVFSResult vault_list(GList *args, gchar *uri, gboolean debug, gchar **error_context) { |
|---|
| 51 | GnomeVFSResult result; |
|---|
| 52 | GnomeVFSDirectoryList *list; |
|---|
| 53 | GnomeVFSFileInfo *info; |
|---|
| 54 | gchar *text_uri; |
|---|
| 55 | |
|---|
| 56 | if(args != NULL) { |
|---|
| 57 | text_uri = g_strconcat(uri, args->data, NULL); |
|---|
| 58 | } else { |
|---|
| 59 | text_uri = uri; |
|---|
| 60 | } |
|---|
| 61 | |
|---|
| 62 | result = gnome_vfs_directory_list_load (&list, text_uri, (GNOME_VFS_FILE_INFO_GET_MIME_TYPE | GNOME_VFS_FILE_INFO_FORCE_FAST_MIME_TYPE | GNOME_VFS_FILE_INFO_FOLLOW_LINKS), NULL); |
|---|
| 63 | |
|---|
| 64 | if(result == GNOME_VFS_OK) { |
|---|
| 65 | |
|---|
| 66 | info = gnome_vfs_directory_list_first(list); |
|---|
| 67 | |
|---|
| 68 | while( info != NULL ) { |
|---|
| 69 | g_print("%s %10d %20s %s\n", |
|---|
| 70 | info->type==GNOME_VFS_FILE_TYPE_DIRECTORY?"Folder":"File ", |
|---|
| 71 | (gint)info->size, |
|---|
| 72 | info->mime_type, |
|---|
| 73 | info->name); |
|---|
| 74 | |
|---|
| 75 | info = gnome_vfs_directory_list_next(list); |
|---|
| 76 | } |
|---|
| 77 | } |
|---|
| 78 | |
|---|
| 79 | return result; |
|---|
| 80 | } |
|---|
| 81 | |
|---|
| 82 | |
|---|
| 83 | |
|---|
| 84 | static gchar *make_local_uri(gchar *fn, gchar *remote) { |
|---|
| 85 | gchar buffer[PATH_MAX+1]; |
|---|
| 86 | |
|---|
| 87 | g_assert(fn || remote); |
|---|
| 88 | |
|---|
| 89 | if(fn == NULL && remote != NULL) fn = g_basename(remote); |
|---|
| 90 | if(fn[0] != '/') { |
|---|
| 91 | getcwd(buffer, PATH_MAX); |
|---|
| 92 | fn = g_strconcat(buffer, "/", fn, NULL); |
|---|
| 93 | } |
|---|
| 94 | |
|---|
| 95 | if(fn[strlen(fn)-1] == '/' && remote) |
|---|
| 96 | fn = g_strconcat(fn, g_basename(remote), NULL); |
|---|
| 97 | |
|---|
| 98 | return g_strconcat("file://", fn, NULL); |
|---|
| 99 | } |
|---|
| 100 | |
|---|
| 101 | static gchar *make_remote_uri(gchar *uri, gchar *fn, gchar *local) { |
|---|
| 102 | if(fn) |
|---|
| 103 | uri = g_strconcat(uri, fn, NULL); |
|---|
| 104 | |
|---|
| 105 | if(uri[strlen(uri)-1] == '/' && local) |
|---|
| 106 | uri = g_strconcat(uri, g_basename(local), NULL); |
|---|
| 107 | |
|---|
| 108 | return uri; |
|---|
| 109 | } |
|---|
| 110 | |
|---|
| 111 | |
|---|
| 112 | static GnomeVFSResult vault_upload(GList *args, gchar *uri, gboolean debug, gchar **error_context) { |
|---|
| 113 | GnomeVFSResult result; |
|---|
| 114 | GnomeVFSHandle *whandle, *rhandle; |
|---|
| 115 | gchar *remote_uri; |
|---|
| 116 | gchar *local_uri; |
|---|
| 117 | gchar buffer[1025]; |
|---|
| 118 | |
|---|
| 119 | local_uri = make_local_uri(args->data, NULL); |
|---|
| 120 | |
|---|
| 121 | args = args->next; |
|---|
| 122 | |
|---|
| 123 | remote_uri = make_remote_uri(uri, args?args->data:NULL, local_uri); |
|---|
| 124 | |
|---|
| 125 | g_print("local: %s\n", local_uri); |
|---|
| 126 | g_print("remote: %s\n", remote_uri); |
|---|
| 127 | |
|---|
| 128 | result = gnome_vfs_open (&rhandle, local_uri, GNOME_VFS_OPEN_READ); |
|---|
| 129 | if(result != GNOME_VFS_OK) return result; |
|---|
| 130 | |
|---|
| 131 | result = gnome_vfs_open (&whandle, remote_uri, GNOME_VFS_OPEN_WRITE); |
|---|
| 132 | if(result != GNOME_VFS_OK) return result; |
|---|
| 133 | |
|---|
| 134 | while(result == GNOME_VFS_OK) { |
|---|
| 135 | GnomeVFSFileSize bytes = 1024, bytes_read; |
|---|
| 136 | |
|---|
| 137 | result = gnome_vfs_read (rhandle, buffer, bytes, &bytes_read); |
|---|
| 138 | if(result != GNOME_VFS_OK) return result; |
|---|
| 139 | |
|---|
| 140 | if(bytes_read == 0) break; |
|---|
| 141 | |
|---|
| 142 | g_print("%d=\n", (gint)bytes_read); |
|---|
| 143 | |
|---|
| 144 | result = gnome_vfs_write (whandle, buffer, bytes_read, &bytes); |
|---|
| 145 | if(result != GNOME_VFS_OK) return result; |
|---|
| 146 | |
|---|
| 147 | g_print("%d-\n", (gint)bytes); |
|---|
| 148 | } |
|---|
| 149 | |
|---|
| 150 | result = gnome_vfs_close(rhandle); |
|---|
| 151 | result = gnome_vfs_close(whandle); /* we especially need to do this for HTTP */ |
|---|
| 152 | |
|---|
| 153 | |
|---|
| 154 | return result; |
|---|
| 155 | } |
|---|
| 156 | |
|---|
| 157 | |
|---|
| 158 | static GnomeVFSResult vault_download(GList *args, gchar *uri, gboolean debug, gchar **error_context) { |
|---|
| 159 | GnomeVFSResult result; |
|---|
| 160 | GnomeVFSHandle *whandle, *rhandle; |
|---|
| 161 | gchar *remote_uri; |
|---|
| 162 | gchar *local_uri; |
|---|
| 163 | gchar buffer[1025]; |
|---|
| 164 | |
|---|
| 165 | remote_uri = make_remote_uri(uri, args->data, NULL); |
|---|
| 166 | |
|---|
| 167 | args = args->next; |
|---|
| 168 | |
|---|
| 169 | local_uri = make_local_uri(args?args->data:NULL, remote_uri); |
|---|
| 170 | |
|---|
| 171 | g_print("local: %s\n", local_uri); |
|---|
| 172 | g_print("remote: %s\n", remote_uri); |
|---|
| 173 | |
|---|
| 174 | result = gnome_vfs_open (&rhandle, remote_uri, GNOME_VFS_OPEN_READ); |
|---|
| 175 | if(result != GNOME_VFS_OK) { |
|---|
| 176 | *error_context = remote_uri; |
|---|
| 177 | return result; |
|---|
| 178 | } |
|---|
| 179 | |
|---|
| 180 | result = gnome_vfs_create (&whandle, local_uri, GNOME_VFS_OPEN_WRITE, TRUE, 0600); |
|---|
| 181 | if(result != GNOME_VFS_OK) { |
|---|
| 182 | *error_context = local_uri; |
|---|
| 183 | return result; |
|---|
| 184 | } |
|---|
| 185 | |
|---|
| 186 | while(result == GNOME_VFS_OK) { |
|---|
| 187 | GnomeVFSFileSize bytes = 1024, bytes_read; |
|---|
| 188 | |
|---|
| 189 | result = gnome_vfs_read (rhandle, buffer, bytes, &bytes_read); |
|---|
| 190 | if(result != GNOME_VFS_OK) { |
|---|
| 191 | *error_context = "read"; |
|---|
| 192 | return result; |
|---|
| 193 | } |
|---|
| 194 | |
|---|
| 195 | if(bytes_read == 0) break; |
|---|
| 196 | |
|---|
| 197 | g_print("%d=\n", (gint)bytes_read); |
|---|
| 198 | |
|---|
| 199 | result = gnome_vfs_write (whandle, buffer, bytes_read, &bytes); |
|---|
| 200 | if(result != GNOME_VFS_OK) { |
|---|
| 201 | *error_context = "write"; |
|---|
| 202 | return result; |
|---|
| 203 | } |
|---|
| 204 | |
|---|
| 205 | g_print("%d-\n", (gint)bytes); |
|---|
| 206 | } |
|---|
| 207 | |
|---|
| 208 | result = gnome_vfs_close(rhandle); |
|---|
| 209 | result = gnome_vfs_close(whandle); /* we especially need to do this for HTTP */ |
|---|
| 210 | |
|---|
| 211 | |
|---|
| 212 | return result; |
|---|
| 213 | } |
|---|
| 214 | |
|---|
| 215 | |
|---|
| 216 | static GnomeVFSResult vault_move(GList *args, gchar *uri, gboolean debug, gchar **error_context) { |
|---|
| 217 | GnomeVFSResult result; |
|---|
| 218 | gchar *uri1, *uri2; |
|---|
| 219 | |
|---|
| 220 | uri1 = make_remote_uri(uri, args->data, NULL); |
|---|
| 221 | |
|---|
| 222 | args = args->next; |
|---|
| 223 | |
|---|
| 224 | uri2 = make_remote_uri(uri, args->data, NULL); |
|---|
| 225 | |
|---|
| 226 | g_print("from: %s\n", uri1); |
|---|
| 227 | g_print("to: %s\n", uri2); |
|---|
| 228 | |
|---|
| 229 | result = gnome_vfs_move (uri1, uri2, TRUE); |
|---|
| 230 | if(result != GNOME_VFS_OK) { |
|---|
| 231 | *error_context = g_strdup_printf("move %s to %s", uri1, uri2); |
|---|
| 232 | return result; |
|---|
| 233 | } |
|---|
| 234 | return result; |
|---|
| 235 | } |
|---|
| 236 | |
|---|
| 237 | |
|---|
| 238 | static GnomeVFSResult vault_delete(GList *args, gchar *uri, gboolean debug, gchar **error_context) { |
|---|
| 239 | GnomeVFSResult result; |
|---|
| 240 | gchar *uri1; |
|---|
| 241 | |
|---|
| 242 | uri1 = make_remote_uri(uri, args->data, NULL); |
|---|
| 243 | |
|---|
| 244 | g_print("deleting: %s\n", uri1); |
|---|
| 245 | |
|---|
| 246 | result = gnome_vfs_unlink (uri1); |
|---|
| 247 | if(result != GNOME_VFS_OK) { |
|---|
| 248 | *error_context = uri1; |
|---|
| 249 | return result; |
|---|
| 250 | } |
|---|
| 251 | return result; |
|---|
| 252 | } |
|---|