1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333
|
/*
'ls' for cadaver
Copyright (C) 2000-2004, 2006, 2008, Joe Orton <joe@manyfish.co.uk>,
except where otherwise indicated.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include "config.h"
#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif
#ifdef HAVE_STRING_H
#include <string.h>
#endif
#include <time.h>
#include <ne_request.h>
#include <ne_props.h>
#include <ne_uri.h>
#include <ne_alloc.h>
#include <ne_dates.h>
#include "i18n.h"
#include "commands.h"
#include "cadaver.h"
#include "basename.h"
#include "utils.h"
struct fetch_context {
struct resource **list;
const char *target; /* Request-URI of the PROPFIND */
unsigned int include_target; /* Include resource at href */
};
static const ne_propname ls_props[] = {
{ "DAV:", "getcontentlength" },
{ "DAV:", "getlastmodified" },
{ "http://apache.org/dav/props/", "executable" },
{ "DAV:", "resourcetype" },
{ "DAV:", "checked-in" },
{ "DAV:", "checked-out" },
{ NULL }
};
#define ELM_resourcetype (NE_PROPS_STATE_TOP + 1)
#define ELM_collection (NE_PROPS_STATE_TOP + 2)
static const struct ne_xml_idmap ls_idmap[] = {
{ "DAV:", "resourcetype", ELM_resourcetype },
{ "DAV:", "collection", ELM_collection }
};
static int compare_resource(const struct resource *r1,
const struct resource *r2)
{
/* Sort errors first, then collections, then alphabetically */
if (r1->type == resr_error) {
return -1;
} else if (r2->type == resr_error) {
return 1;
} else if (r1->type == resr_collection) {
if (r2->type != resr_collection) {
return -1;
} else {
return strcmp(r1->uri, r2->uri);
}
} else {
if (r2->type != resr_collection) {
return strcmp(r1->uri, r2->uri);
} else {
return 1;
}
}
}
static void display_ls_line(struct resource *res)
{
const char *restype;
char exec_char, vcr_char, *name;
switch (res->type) {
case resr_normal: restype = ""; break;
case resr_reference: restype = _("Ref:"); break;
case resr_collection: restype = _("Coll:"); break;
default:
restype = "???"; break;
}
if (ne_path_has_trailing_slash(res->uri)) {
res->uri[strlen(res->uri)-1] = '\0';
}
name = strrchr(res->uri, '/');
if (name != NULL && strlen(name+1) > 0) {
name++;
} else {
name = res->uri;
}
name = ne_path_unescape(name);
if (res->type == resr_error) {
printf(_("Error: %-30s %d %s\n"), name, res->error_status,
res->error_reason?res->error_reason:_("unknown"));
} else {
exec_char = res->is_executable ? '*' : ' ';
/* 0: no vcr, 1: checkin, 2: checkout */
vcr_char = res->is_vcr==0 ? ' ' : (res->is_vcr==1? '>' : '<');
printf("%5s %c%c%-29s %10" FMT_DAV_SIZE_T "u %s\n",
restype, vcr_char, exec_char, name,
res->size, format_time(res->modtime));
}
free(name);
}
void execute_ls(const char *remote)
{
int ret;
char *real_remote;
struct resource *reslist = NULL, *current, *next;
if (remote != NULL) {
real_remote = resolve_path(session.uri.path, remote, true);
} else {
real_remote = ne_strdup(session.uri.path);
}
out_start(_("Listing collection"), real_remote);
ret = fetch_resource_list(session.sess, real_remote, 1, 0, &reslist);
if (ret == NE_OK) {
/* Easy this, eh? */
if (reslist == NULL) {
output(o_finish, _("collection is empty.\n"));
} else {
out_success();
for (current = reslist; current!=NULL; current = next) {
next = current->next;
if (strlen(current->uri) > strlen(real_remote)) {
display_ls_line(current);
}
free_resource(current);
}
}
} else {
out_result(ret);
}
free(real_remote);
}
static void results(void *userdata,
const ne_uri *uri,
const ne_prop_result_set *set)
{
struct fetch_context *ctx = userdata;
struct resource *current, *previous, *newres;
const char *clength, *modtime, *isexec;
const char *checkin, *checkout;
const ne_status *status = NULL;
const char *path = uri->path;
newres = ne_propset_private(set);
if (ne_path_compare(ctx->target, path) == 0 && !ctx->include_target) {
/* This is the target URI */
NE_DEBUG(NE_DBG_HTTP, "Skipping target resource.\n");
/* Free the private structure. */
ne_free(newres);
return;
}
newres->uri = ne_strdup(path);
clength = ne_propset_value(set, &ls_props[0]);
modtime = ne_propset_value(set, &ls_props[1]);
isexec = ne_propset_value(set, &ls_props[2]);
checkin = ne_propset_value(set, &ls_props[4]);
checkout = ne_propset_value(set, &ls_props[5]);
if (clength == NULL)
status = ne_propset_status(set, &ls_props[0]);
if (modtime == NULL)
status = ne_propset_status(set, &ls_props[1]);
if (newres->type == resr_normal && status) {
/* It's an error! */
newres->error_status = status->code;
/* Special hack for Apache 1.3/mod_dav */
if (strcmp(status->reason_phrase, "status text goes here") == 0) {
const char *desc;
if (status->code == 401) {
desc = _("Authorization Required");
} else if (status->klass == 3) {
desc = _("Redirect");
} else if (status->klass == 5) {
desc = _("Server Error");
} else {
desc = _("Unknown Error");
}
newres->error_reason = ne_strdup(desc);
} else {
newres->error_reason = ne_strdup(status->reason_phrase);
}
newres->type = resr_error;
}
if (isexec && strcasecmp(isexec, "T") == 0) {
newres->is_executable = 1;
} else {
newres->is_executable = 0;
}
if (modtime)
newres->modtime = ne_httpdate_parse(modtime);
if (clength) {
char *p;
newres->size = DAV_STRTOL(clength, &p, 10);
if (*p) {
newres->size = 0;
}
}
/* is vcr */
if (checkin) {
newres->is_vcr = 1;
} else if (checkout) {
newres->is_vcr = 2;
} else {
newres->is_vcr = 0;
}
NE_DEBUG(NE_DBG_HTTP, "End resource %s\n", newres->uri);
for (current = *ctx->list, previous = NULL; current != NULL;
previous = current, current=current->next) {
if (compare_resource(current, newres) >= 0) {
break;
}
}
if (previous) {
previous->next = newres;
} else {
*ctx->list = newres;
}
newres->next = current;
}
static int ls_startelm(void *userdata, int parent,
const char *nspace, const char *name, const char **atts)
{
ne_propfind_handler *pfh = userdata;
struct resource *r = ne_propfind_current_private(pfh);
int state = ne_xml_mapid(ls_idmap, NE_XML_MAPLEN(ls_idmap),
nspace, name);
if (r == NULL ||
!((parent == NE_207_STATE_PROP && state == ELM_resourcetype) ||
(parent == ELM_resourcetype && state == ELM_collection)))
return NE_XML_DECLINE;
if (state == ELM_collection) {
NE_DEBUG(NE_DBG_HTTP, "This is a collection.\n");
r->type = resr_collection;
}
return state;
}
void free_resource(struct resource *res)
{
if (res->uri) ne_free(res->uri);
if (res->error_reason) ne_free(res->error_reason);
free(res);
}
void free_resource_list(struct resource *res)
{
struct resource *next;
for (; res != NULL; res = next) {
next = res->next;
free_resource(res);
}
}
static void *create_private(void *userdata,
const ne_uri *uri
)
{
return ne_calloc(sizeof(struct resource));
}
int fetch_resource_list(ne_session *sess, const char *uri,
int depth, int include_target,
struct resource **reslist)
{
ne_propfind_handler *pfh = ne_propfind_create(sess, uri, depth);
int ret;
struct fetch_context ctx = {0};
*reslist = NULL;
ctx.list = reslist;
ctx.target = uri;
ctx.include_target = include_target;
ne_xml_push_handler(ne_propfind_get_parser(pfh),
ls_startelm, NULL, NULL, pfh);
ne_propfind_set_private(pfh, create_private, NULL, NULL);
ret = ne_propfind_named(pfh, ls_props, results, &ctx);
ne_propfind_destroy(pfh);
return ret;
}
|