2013-05-08 15:12:01 +04:00
|
|
|
/*
|
|
|
|
* libcsync -- a library to sync a directory with another
|
|
|
|
*
|
|
|
|
* Copyright (c) 2011 by Andreas Schneider <asn@cryptomilk.org>
|
|
|
|
* Copyright (c) 2012 by Klaas Freitag <freitag@owncloud.com>
|
|
|
|
*
|
2013-08-21 15:08:07 +04:00
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
2013-05-08 15:12:01 +04:00
|
|
|
*
|
2013-08-21 15:08:07 +04:00
|
|
|
* This library 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
|
|
|
|
* Lesser General Public License for more details.
|
2013-05-08 15:12:01 +04:00
|
|
|
*
|
2013-08-21 15:08:07 +04:00
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
2013-05-08 15:12:01 +04:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "csync_owncloud.h"
|
2014-05-29 14:02:46 +04:00
|
|
|
#include "csync_owncloud_private.h"
|
2013-05-08 13:57:29 +04:00
|
|
|
|
|
|
|
static void _tree_destructor(void *data) {
|
|
|
|
propfind_recursive_element_t *element = data;
|
|
|
|
resource_free(element->self);
|
|
|
|
resource_free(element->children);
|
|
|
|
SAFE_FREE(element);
|
|
|
|
}
|
2013-05-13 18:28:31 +04:00
|
|
|
|
2014-05-29 14:02:46 +04:00
|
|
|
void clear_propfind_recursive_cache(csync_owncloud_ctx_t *ctx)
|
2013-05-08 13:57:29 +04:00
|
|
|
{
|
2014-05-29 14:02:46 +04:00
|
|
|
if (ctx->propfind_recursive_cache) {
|
2013-10-01 19:31:07 +04:00
|
|
|
DEBUG_WEBDAV("clear_propfind_recursive_cache Invalidating..");
|
2014-05-29 14:02:46 +04:00
|
|
|
c_rbtree_destroy(ctx->propfind_recursive_cache, _tree_destructor);
|
|
|
|
ctx->propfind_recursive_cache = NULL;
|
2013-10-01 19:31:07 +04:00
|
|
|
}
|
2013-05-08 13:57:29 +04:00
|
|
|
}
|
|
|
|
|
2014-05-29 14:02:46 +04:00
|
|
|
struct listdir_context *get_listdir_context_from_recursive_cache(csync_owncloud_ctx_t *ctx, const char *curi)
|
2013-05-08 13:57:29 +04:00
|
|
|
{
|
2013-05-13 18:28:31 +04:00
|
|
|
propfind_recursive_element_t *element = NULL;
|
|
|
|
struct listdir_context *fetchCtx = NULL;
|
|
|
|
struct resource *iterator, *r;
|
|
|
|
|
2014-05-29 14:02:46 +04:00
|
|
|
if (!ctx->propfind_recursive_cache) {
|
2013-10-01 19:31:07 +04:00
|
|
|
DEBUG_WEBDAV("get_listdir_context_from_recursive_cache No cache");
|
2013-05-08 13:57:29 +04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2014-05-29 14:02:46 +04:00
|
|
|
element = c_rbtree_node_data(c_rbtree_find(ctx->propfind_recursive_cache, curi));
|
2013-05-08 13:57:29 +04:00
|
|
|
if (!element) {
|
2013-10-01 19:31:07 +04:00
|
|
|
DEBUG_WEBDAV("get_listdir_context_from_recursive_cache No element %s in cache found", curi);
|
2013-05-08 13:57:29 +04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2013-05-08 15:20:42 +04:00
|
|
|
/* Out of the element, create a listdir_context.. if we could be sure that it is immutable, we could ref instead.. need to investigate */
|
2013-05-13 18:28:31 +04:00
|
|
|
fetchCtx = c_malloc( sizeof( struct listdir_context ));
|
2013-11-28 17:48:47 +04:00
|
|
|
ZERO_STRUCTP(fetchCtx);
|
2013-05-08 13:57:29 +04:00
|
|
|
fetchCtx->list = NULL;
|
|
|
|
fetchCtx->target = c_strdup(curi);
|
|
|
|
fetchCtx->currResource = NULL;
|
|
|
|
fetchCtx->ref = 1;
|
|
|
|
|
2013-05-13 18:28:31 +04:00
|
|
|
iterator = element->children;
|
|
|
|
r = NULL;
|
2013-05-08 13:57:29 +04:00
|
|
|
while (iterator) {
|
|
|
|
r = resource_dup(iterator);
|
|
|
|
r->next = fetchCtx->list;
|
|
|
|
fetchCtx->list = r;
|
|
|
|
iterator = iterator->next;
|
|
|
|
fetchCtx->result_count++;
|
|
|
|
/* DEBUG_WEBDAV("get_listdir_context_from_cache Returning cache for %s element %s", fetchCtx->target, fetchCtx->list->uri); */
|
|
|
|
}
|
2013-05-13 18:28:31 +04:00
|
|
|
|
2013-05-08 13:57:29 +04:00
|
|
|
r = resource_dup(element->self);
|
|
|
|
r->next = fetchCtx->list;
|
|
|
|
fetchCtx->result_count++;
|
|
|
|
fetchCtx->list = r;
|
|
|
|
fetchCtx->currResource = fetchCtx->list;
|
|
|
|
DEBUG_WEBDAV("get_listdir_context_from_cache Returning cache for %s (%d elements)", fetchCtx->target, fetchCtx->result_count);
|
|
|
|
return fetchCtx;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int _key_cmp(const void *key, const void *b) {
|
|
|
|
const char *elementAUri = (char*)key;
|
|
|
|
const propfind_recursive_element_t *elementB = b;
|
|
|
|
return ne_path_compare(elementAUri, elementB->self->uri);
|
|
|
|
}
|
|
|
|
static int _data_cmp(const void *a, const void *b) {
|
|
|
|
const propfind_recursive_element_t *elementA = a;
|
|
|
|
const propfind_recursive_element_t *elementB = b;
|
|
|
|
return ne_path_compare(elementA->self->uri, elementB->self->uri);
|
|
|
|
}
|
2014-06-03 17:01:35 +04:00
|
|
|
static void propfind_results_recursive_callback(void *userdata,
|
2013-05-08 13:57:29 +04:00
|
|
|
const ne_uri *uri,
|
|
|
|
const ne_prop_result_set *set)
|
|
|
|
{
|
|
|
|
struct resource *newres = 0;
|
2013-10-25 15:12:59 +04:00
|
|
|
const char *clength, *modtime, *file_id = NULL;
|
2014-06-03 13:50:13 +04:00
|
|
|
const char *directDownloadUrl = NULL;
|
|
|
|
const char *directDownloadCookies = NULL;
|
2013-05-08 13:57:29 +04:00
|
|
|
const char *resourcetype = NULL;
|
|
|
|
const char *md5sum = NULL;
|
|
|
|
const ne_status *status = NULL;
|
|
|
|
char *path = ne_path_unescape( uri->path );
|
2013-05-13 18:28:31 +04:00
|
|
|
char *parentPath;
|
2013-10-01 19:31:07 +04:00
|
|
|
propfind_recursive_element_t *element = NULL;
|
|
|
|
propfind_recursive_element_t *pElement = NULL;
|
|
|
|
int depth = 0;
|
2014-05-29 14:02:46 +04:00
|
|
|
csync_owncloud_ctx_t *ctx = (csync_owncloud_ctx_t*) userdata;
|
2013-05-08 13:57:29 +04:00
|
|
|
|
2014-06-03 13:50:13 +04:00
|
|
|
|
2013-05-08 13:57:29 +04:00
|
|
|
(void) status;
|
|
|
|
|
2014-05-29 14:02:46 +04:00
|
|
|
if (!ctx->propfind_recursive_cache) {
|
|
|
|
c_rbtree_create(&ctx->propfind_recursive_cache, _key_cmp, _data_cmp);
|
2013-05-08 13:57:29 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Fill the resource structure with the data about the file */
|
|
|
|
newres = c_malloc(sizeof(struct resource));
|
2013-11-28 17:48:47 +04:00
|
|
|
ZERO_STRUCTP(newres);
|
|
|
|
|
2013-05-08 13:57:29 +04:00
|
|
|
newres->uri = path; /* no need to strdup because ne_path_unescape already allocates */
|
|
|
|
newres->name = c_basename( path );
|
|
|
|
|
|
|
|
modtime = ne_propset_value( set, &ls_props[0] );
|
|
|
|
clength = ne_propset_value( set, &ls_props[1] );
|
|
|
|
resourcetype = ne_propset_value( set, &ls_props[2] );
|
|
|
|
md5sum = ne_propset_value( set, &ls_props[3] );
|
2013-10-25 15:12:59 +04:00
|
|
|
file_id = ne_propset_value( set, &ls_props[4] );
|
2014-06-03 13:50:13 +04:00
|
|
|
directDownloadUrl = ne_propset_value( set, &ls_props[5] );
|
|
|
|
directDownloadCookies = ne_propset_value( set, &ls_props[6] );
|
2013-05-08 13:57:29 +04:00
|
|
|
|
|
|
|
newres->type = resr_normal;
|
|
|
|
if( resourcetype && strncmp( resourcetype, "<DAV:collection>", 16 ) == 0) {
|
|
|
|
newres->type = resr_collection;
|
2014-05-29 14:02:46 +04:00
|
|
|
ctx->propfind_recursive_cache_folder_count++;
|
2013-05-08 13:57:29 +04:00
|
|
|
} else {
|
2013-10-01 19:31:07 +04:00
|
|
|
/* DEBUG_WEBDAV("propfind_results_recursive %s [%d]", newres->uri, newres->type); */
|
2014-05-29 14:02:46 +04:00
|
|
|
ctx->propfind_recursive_cache_file_count++;
|
2013-05-08 13:57:29 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (modtime) {
|
|
|
|
newres->modtime = oc_httpdate_parse(modtime);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* DEBUG_WEBDAV("Parsing Modtime: %s -> %llu", modtime, (unsigned long long) newres->modtime ); */
|
2013-10-23 16:50:41 +04:00
|
|
|
newres->size = 0;
|
2013-05-08 13:57:29 +04:00
|
|
|
if (clength) {
|
2013-10-23 16:50:41 +04:00
|
|
|
newres->size = atoll(clength);
|
2013-05-08 13:57:29 +04:00
|
|
|
/* DEBUG_WEBDAV("Parsed File size for %s from %s: %lld", newres->name, clength, (long long)newres->size ); */
|
|
|
|
}
|
|
|
|
|
|
|
|
if( md5sum ) {
|
2013-12-11 14:35:55 +04:00
|
|
|
newres->md5 = csync_normalize_etag(md5sum);
|
2013-05-08 13:57:29 +04:00
|
|
|
}
|
|
|
|
|
2013-10-25 15:12:59 +04:00
|
|
|
csync_vio_set_file_id(newres->file_id, file_id);
|
2013-10-01 19:31:07 +04:00
|
|
|
/*
|
|
|
|
DEBUG_WEBDAV("propfind_results_recursive %s [%s] %s", newres->uri, newres->type == resr_collection ? "collection" : "file", newres->md5);
|
|
|
|
*/
|
2013-05-08 13:57:29 +04:00
|
|
|
|
2014-06-03 13:50:13 +04:00
|
|
|
if (directDownloadUrl) {
|
|
|
|
newres->directDownloadUrl = c_strdup(directDownloadUrl);
|
|
|
|
}
|
|
|
|
if (directDownloadCookies) {
|
|
|
|
newres->directDownloadCookies = c_strdup(directDownloadCookies);
|
|
|
|
}
|
|
|
|
|
2013-05-08 13:57:29 +04:00
|
|
|
/* Create new item in rb tree */
|
|
|
|
if (newres->type == resr_collection) {
|
2013-10-01 19:31:07 +04:00
|
|
|
DEBUG_WEBDAV("propfind_results_recursive %s is a folder", newres->uri);
|
2013-05-08 15:20:42 +04:00
|
|
|
/* Check if in rb tree */
|
2014-05-29 14:02:46 +04:00
|
|
|
element = c_rbtree_node_data(c_rbtree_find(ctx->propfind_recursive_cache,uri->path));
|
2013-05-08 15:20:42 +04:00
|
|
|
/* If not, create a new item and insert it */
|
2013-05-08 13:57:29 +04:00
|
|
|
if (!element) {
|
|
|
|
element = c_malloc(sizeof(propfind_recursive_element_t));
|
|
|
|
element->self = resource_dup(newres);
|
2014-05-28 13:36:07 +04:00
|
|
|
element->self->next = 0;
|
2013-05-08 13:57:29 +04:00
|
|
|
element->children = NULL;
|
2013-10-01 19:31:07 +04:00
|
|
|
element->parent = NULL;
|
2014-05-29 14:02:46 +04:00
|
|
|
c_rbtree_insert(ctx->propfind_recursive_cache, element);
|
2013-05-08 13:57:29 +04:00
|
|
|
/* DEBUG_WEBDAV("results_recursive Added collection %s", newres->uri); */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check for parent in tree. If exists: Insert it into the children elements there */
|
2013-05-13 18:28:31 +04:00
|
|
|
parentPath = ne_path_parent(uri->path);
|
2013-05-08 13:57:29 +04:00
|
|
|
if (parentPath) {
|
2013-10-01 19:31:07 +04:00
|
|
|
propfind_recursive_element_t *parentElement = NULL;
|
2013-05-13 18:28:31 +04:00
|
|
|
|
2014-05-29 14:02:46 +04:00
|
|
|
parentElement = c_rbtree_node_data(c_rbtree_find(ctx->propfind_recursive_cache,parentPath));
|
2013-05-08 13:57:29 +04:00
|
|
|
free(parentPath);
|
2013-05-13 18:28:31 +04:00
|
|
|
|
2013-10-01 19:31:07 +04:00
|
|
|
if (parentElement) {
|
|
|
|
newres->next = parentElement->children;
|
|
|
|
parentElement->children = newres;
|
|
|
|
|
|
|
|
/* If the current result is a collection we also need to set its parent */
|
|
|
|
if (element)
|
|
|
|
element->parent = parentElement;
|
|
|
|
|
|
|
|
pElement = element;
|
|
|
|
while (pElement) {
|
|
|
|
depth++;
|
|
|
|
pElement = pElement->parent;
|
|
|
|
}
|
2014-05-29 14:02:46 +04:00
|
|
|
if (depth > ctx->propfind_recursive_cache_depth) {
|
2013-10-01 19:31:07 +04:00
|
|
|
DEBUG_WEBDAV("propfind_results_recursive %s new maximum tree depth %d", newres->uri, depth);
|
2014-05-29 14:02:46 +04:00
|
|
|
ctx->propfind_recursive_cache_depth = depth;
|
2013-10-01 19:31:07 +04:00
|
|
|
}
|
|
|
|
|
2013-05-08 13:57:29 +04:00
|
|
|
/* DEBUG_WEBDAV("results_recursive Added child %s to collection %s", newres->uri, element->self->uri); */
|
|
|
|
} else {
|
|
|
|
/* DEBUG_WEBDAV("results_recursive No parent %s found for child %s", parentPath, newres->uri); */
|
|
|
|
resource_free(newres);
|
|
|
|
newres = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-05-29 14:02:46 +04:00
|
|
|
void fetch_resource_list_recursive(csync_owncloud_ctx_t *ctx, const char *uri, const char *curi)
|
2013-05-08 13:57:29 +04:00
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
ne_propfind_handler *hdl = NULL;
|
|
|
|
ne_request *request = NULL;
|
|
|
|
const char *content_type = NULL;
|
|
|
|
const ne_status *req_status = NULL;
|
|
|
|
int depth = NE_DEPTH_INFINITE;
|
|
|
|
|
2013-10-01 19:31:07 +04:00
|
|
|
DEBUG_WEBDAV("fetch_resource_list_recursive Starting recursive propfind %s %s", uri, curi);
|
2013-05-08 13:57:29 +04:00
|
|
|
|
|
|
|
/* do a propfind request and parse the results in the results function, set as callback */
|
2014-05-29 14:02:46 +04:00
|
|
|
hdl = ne_propfind_create(ctx->dav_session.ctx, curi, depth);
|
2013-05-08 13:57:29 +04:00
|
|
|
|
|
|
|
if(hdl) {
|
2014-06-03 17:01:35 +04:00
|
|
|
ret = ne_propfind_named(hdl, ls_props, propfind_results_recursive_callback, ctx);
|
2013-05-08 13:57:29 +04:00
|
|
|
request = ne_propfind_get_request( hdl );
|
|
|
|
req_status = ne_get_status( request );
|
|
|
|
}
|
|
|
|
|
|
|
|
if( ret == NE_OK ) {
|
|
|
|
/* Check the request status. */
|
|
|
|
if( req_status && req_status->klass != 2 ) {
|
|
|
|
set_errno_from_http_errcode(req_status->code);
|
|
|
|
DEBUG_WEBDAV("ERROR: Request failed: status %d (%s)", req_status->code,
|
|
|
|
req_status->reason_phrase);
|
|
|
|
ret = NE_CONNECT;
|
2014-05-29 14:02:46 +04:00
|
|
|
set_error_message(ctx, req_status->reason_phrase);
|
2013-05-08 13:57:29 +04:00
|
|
|
}
|
2013-07-04 13:56:37 +04:00
|
|
|
DEBUG_WEBDAV("Recursive propfind result code %d.", req_status ? req_status->code : 0);
|
2013-05-08 13:57:29 +04:00
|
|
|
} else {
|
|
|
|
if( ret == NE_ERROR && req_status->code == 404) {
|
|
|
|
errno = ENOENT;
|
|
|
|
} else {
|
2014-05-29 14:02:46 +04:00
|
|
|
set_errno_from_neon_errcode(ctx, ret);
|
2013-05-08 13:57:29 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if( ret == NE_OK ) {
|
|
|
|
/* Check the content type. If the server has a problem, ie. database is gone or such,
|
|
|
|
* the content type is not xml but a html error message. Stop on processing if it's
|
|
|
|
* not XML.
|
|
|
|
* FIXME: Generate user error message from the reply content.
|
|
|
|
*/
|
|
|
|
content_type = ne_get_response_header( request, "Content-Type" );
|
|
|
|
if( !(content_type && c_streq(content_type, "application/xml; charset=utf-8") ) ) {
|
|
|
|
DEBUG_WEBDAV("ERROR: Content type of propfind request not XML: %s.",
|
|
|
|
content_type ? content_type: "<empty>");
|
|
|
|
errno = ERRNO_WRONG_CONTENT;
|
2014-05-29 14:02:46 +04:00
|
|
|
set_error_message(ctx, "Server error: PROPFIND reply is not XML formatted!");
|
2013-05-08 13:57:29 +04:00
|
|
|
ret = NE_CONNECT;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if( ret != NE_OK ) {
|
|
|
|
const char *err = NULL;
|
|
|
|
|
2014-05-29 14:02:46 +04:00
|
|
|
err = ne_get_error( ctx->dav_session.ctx );
|
2013-05-08 13:57:29 +04:00
|
|
|
DEBUG_WEBDAV("WRN: propfind named failed with %d, request error: %s", ret, err ? err : "<nil>");
|
|
|
|
}
|
|
|
|
|
|
|
|
if( hdl )
|
|
|
|
ne_propfind_destroy(hdl);
|
|
|
|
|
|
|
|
if( ret != NE_OK ) {
|
2013-10-01 19:31:07 +04:00
|
|
|
return;
|
2013-05-08 13:57:29 +04:00
|
|
|
}
|
|
|
|
|
2013-10-01 19:31:07 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Called by owncloud_opendir()->fetch_resource_list() to fill the cache */
|
2014-05-29 14:02:46 +04:00
|
|
|
void fill_recursive_propfind_cache(csync_owncloud_ctx_t *ctx, const char *uri, const char *curi) {
|
|
|
|
fetch_resource_list_recursive(ctx, uri, curi);
|
2013-10-01 19:31:07 +04:00
|
|
|
|
2014-05-29 14:02:46 +04:00
|
|
|
if (ctx->propfind_recursive_cache_depth <= 2) {
|
2013-10-01 19:31:07 +04:00
|
|
|
DEBUG_WEBDAV("fill_recursive_propfind_cache %s Server maybe did not give us an 'infinity' depth result", curi);
|
|
|
|
/* transform the cache to the normal cache in propfind_cache */
|
2014-05-29 14:02:46 +04:00
|
|
|
ctx->propfind_cache = get_listdir_context_from_recursive_cache(ctx, curi);
|
2013-10-01 19:31:07 +04:00
|
|
|
/* clear the cache, it is bogus since the server returned only results for Depth 1 */
|
2014-05-29 14:02:46 +04:00
|
|
|
clear_propfind_recursive_cache(ctx);
|
2013-10-01 19:31:07 +04:00
|
|
|
} else {
|
|
|
|
DEBUG_WEBDAV("fill_recursive_propfind_cache %s We received %d elements deep for 'infinity' depth (%d folders, %d files)",
|
|
|
|
curi,
|
2014-05-29 14:02:46 +04:00
|
|
|
ctx->propfind_recursive_cache_depth,
|
|
|
|
ctx->propfind_recursive_cache_folder_count,
|
|
|
|
ctx->propfind_recursive_cache_file_count);
|
2013-10-01 19:31:07 +04:00
|
|
|
|
|
|
|
}
|
2013-05-08 13:57:29 +04:00
|
|
|
}
|