void ne_set_progress(ne_session *sess, ne_progress progress, void *userdata) { if (progress) { sess->progress_cb = progress; sess->progress_ud = userdata; ne_set_notifier(sess, progress_notifier, sess); } else { ne_set_notifier(sess, NULL, NULL); } }
static int owncloud_close(csync_vio_method_handle_t *fhandle) { struct transfer_context *writeCtx; int ret = 0; enum csync_notify_type_e notify_tag; writeCtx = (struct transfer_context*) fhandle; if (fhandle == NULL) { errno = EBADF; ret = -1; } /* handle the PUT request */ if( ret != -1 && strcmp( writeCtx->method, "PUT" ) == 0 ) { notify_tag = CSYNC_NOTIFY_FINISHED_UPLOAD; ne_request_destroy( writeCtx->req ); _fs.name = NULL; } else { /* Its a GET request. */ notify_tag = CSYNC_NOTIFY_FINISHED_DOWNLOAD; } /* Finish callback */ if (_file_progress_cb) { ne_set_notifier(dav_session.ctx, 0, 0); _file_progress_cb(writeCtx->url, notify_tag, 0, 0, _userdata ); } /* free mem. Note that the request mem is freed by the ne_request_destroy call */ SAFE_FREE( writeCtx->url ); SAFE_FREE( writeCtx ); return ret; }
static char *remote_completion(const char *text, int state) { static struct resource *reslist, *current; static int len; static time_t last_fetch; static char *last_path; char *name; if (state == 0) { /* Check to see if we should refresh the dumb cache. * or, initialize the local cache of remote filenames * The remote resource list persists until a completion * in a new context is requested or the cache expires. */ /* TODO get cache expire time from config, currently from cadaver.h * TODO cache and fetch on deep/absolute paths: (path: /a/b/, text: c/d) */ if (last_fetch < (time(NULL) - COMPLETION_CACHE_EXPIRE) || !last_path || strcmp(session.uri.path, last_path) != 0) { if (last_path != NULL) { free(last_path); } if (reslist != NULL) { free_resource_list(reslist); } /* Hide the connection status */ ne_set_notifier(session.sess, NULL, NULL); if (fetch_resource_list(session.sess, session.uri.path, 1, 0, &reslist) != NE_OK) { reslist = NULL; } /* Restore the session connection printing */ ne_set_notifier(session.sess, notifier, NULL); last_path = ne_strdup(session.uri.path); } current = reslist; len = strlen(text); time(&last_fetch); } while (current) { /* Massage the absolute URI to a URI relative to our path */ /* Copy & paste & search & replace from ls.c */ if (ne_path_has_trailing_slash(current->uri)) { current->uri[strlen(current->uri)-1] = '\0'; } name = strrchr(current->uri, '/'); if (name != NULL && strlen(name+1) > 0) { name++; } else { name = current->uri; } name = ne_path_unescape(name); if (strncmp(text, name, len) == 0) { current = current->next; /* FIXME: readline docs say readline will free() this when * it's finished with, although 'memprof' shows that it * leaks. */ return name; } current = current->next; } return NULL; }
static csync_vio_method_handle_t *owncloud_open(const char *durl, int flags, mode_t mode) { char *dir = NULL; char getUrl[PATH_MAX]; int put = 0; int rc = NE_OK; #ifdef _WIN32 int gtp = 0; char tmpname[13]; mbchar_t winTmp[PATH_MAX]; mbchar_t *winUrlMB = NULL; char *winTmpUtf8 = NULL; csync_stat_t sb; #endif struct transfer_context *writeCtx = NULL; csync_stat_t statBuf; memset( getUrl, '\0', PATH_MAX ); (void) mode; /* unused on webdav server */ DEBUG_WEBDAV( "=> open called for %s", durl ); if( rc == NE_OK ) dav_connect( durl ); if (flags & O_WRONLY) { put = 1; } if (flags & O_RDWR) { put = 1; } if (flags & O_CREAT) { put = 1; } if( rc == NE_OK && put ) { /* check if the dir name exists. Otherwise return ENOENT */ dir = c_dirname( durl ); if (dir == NULL) { errno = ENOMEM; return NULL; } DEBUG_WEBDAV("Stating directory %s", dir ); if( c_streq( dir, _lastDir )) { DEBUG_WEBDAV("Dir %s is there, we know it already.", dir); } else { if( owncloud_stat( dir, (csync_vio_method_handle_t*)(&statBuf) ) == 0 ) { DEBUG_WEBDAV("Directory of file to open exists."); SAFE_FREE( _lastDir ); _lastDir = c_strdup(dir); } else { DEBUG_WEBDAV("Directory %s of file to open does NOT exist.", dir ); /* the directory does not exist. That is an ENOENT */ errno = ENOENT; SAFE_FREE( dir ); return NULL; } } } writeCtx = c_malloc( sizeof(struct transfer_context) ); writeCtx->url = _cleanPath( durl ); if( ! writeCtx->url ) { DEBUG_WEBDAV("Failed to clean path for %s", durl ); errno = EACCES; rc = NE_ERROR; } if( rc == NE_OK && put) { DEBUG_WEBDAV("PUT request on %s!", writeCtx->url); writeCtx->req = ne_request_create(dav_session.ctx, "PUT", writeCtx->url); writeCtx->method = "PUT"; } if( rc == NE_OK && ! put ) { writeCtx->req = 0; writeCtx->method = "GET"; /* the download via the get function requires a full uri */ snprintf( getUrl, PATH_MAX, "%s://%s%s", ne_get_scheme( dav_session.ctx), ne_get_server_hostport( dav_session.ctx ), writeCtx->url ); DEBUG_WEBDAV("GET request on %s", getUrl ); writeCtx->req = ne_request_create( dav_session.ctx, "GET", getUrl ); /* Call the progress callback */ if (_file_progress_cb) { ne_set_notifier(dav_session.ctx, ne_notify_status_cb, writeCtx); _file_progress_cb( writeCtx->url, CSYNC_NOTIFY_START_DOWNLOAD, 0 , 0, _userdata); } } if( rc != NE_OK ) { SAFE_FREE( writeCtx ); writeCtx = NULL; } SAFE_FREE( dir ); return (csync_vio_method_handle_t *) writeCtx; }
/* FIXME: Leaky as a bucket */ void open_connection(const char *url) { char *proxy_host = get_option(opt_proxy), *pnt; ne_server_capabilities caps; int ret, use_ssl = 0; ne_session *sess; if (session.connected) { close_connection(); } else { ne_uri_free(&session.uri); if (session.lastwp) { ne_free(session.lastwp); session.lastwp = NULL; } } /* Single argument: see whether we have a path or scheme */ if (strchr(url, '/') == NULL) { /* No path, no scheme -> just a hostname */ pnt = strchr(url, ':'); if (pnt != NULL) { *pnt++ = '\0'; session.uri.port = atoi(pnt); } else { session.uri.port = 80; } session.uri.host = ne_strdup(url); session.uri.scheme = ne_strdup("http"); } else { /* Parse the URL */ if (ne_uri_parse(url, &session.uri) || session.uri.host == NULL) { printf(_("Could not parse URL `%s'\n"), url); return; } if (session.uri.scheme == NULL) session.uri.scheme = ne_strdup("http"); if (!session.uri.port) session.uri.port = ne_uri_defaultport(session.uri.scheme); if (strcasecmp(session.uri.scheme, "https") == 0) { if (!ne_has_support(NE_FEATURE_SSL)) { printf(_("SSL is not enabled.\n")); return; } use_ssl = 1; } } session.sess = ne_session_create(session.uri.scheme, session.uri.host, session.uri.port); sess = session.sess; if (use_ssl && setup_ssl()) { return; } ne_lockstore_register(session.locks, sess); ne_redirect_register(sess); ne_set_notifier(sess, notifier, NULL); if (session.uri.path == NULL) { session.uri.path = ne_strdup("/"); } else { if (!ne_path_has_trailing_slash(session.uri.path)) { pnt = ne_concat(session.uri.path, "/", NULL); free(session.uri.path); session.uri.path = pnt; } } /* Get the proxy details */ if (proxy_host != NULL) { if (get_option(opt_proxy_port) != NULL) { proxy_port = atoi(get_option(opt_proxy_port)); } else { proxy_port = 8080; } proxy_hostname = proxy_host; } #ifdef ENABLE_NETRC { netrc_entry *found; found = search_netrc(netrc_list, session.uri.host); if (found != NULL) { if (found->account && found->password) { server_username = found->account; server_password = found->password; } } } #endif /* ENABLE_NETRC */ #ifdef NE_SESSFLAG_EXPECT100 ne_set_session_flag(session.sess, NE_SESSFLAG_EXPECT100, get_bool_option(opt_expect100)); #endif /* NE_SESSFLAG_EXPECT100 */ session.connected = 0; ne_set_useragent(session.sess, "cadaver/" PACKAGE_VERSION); ne_set_server_auth(session.sess, supply_creds_server, NULL); ne_set_proxy_auth(session.sess, supply_creds_proxy, NULL); if (proxy_host) { ne_session_proxy(session.sess, proxy_hostname, proxy_port); } ret = ne_options(session.sess, session.uri.path, &caps); switch (ret) { case NE_OK: session.connected = true; if (set_path(session.uri.path)) { close_connection(); } break; case NE_CONNECT: if (proxy_host) { printf(_("Could not connect to `%s' on port %d:\n%s\n"), proxy_hostname, proxy_port, ne_get_error(session.sess)); } else { printf(_("Could not connect to `%s' on port %d:\n%s\n"), session.uri.host, session.uri.port, ne_get_error(session.sess)); } break; case NE_LOOKUP: puts(ne_get_error(session.sess)); break; default: printf(_("Could not open collection:\n%s\n"), ne_get_error(session.sess)); break; } }