On Mon, Aug 4, 2008 at 7:34 PM, Henning Garus
<[EMAIL PROTECTED]> wrote:
>
> If my understanding of the libdownload code is correct "Command okay"
> occurs, because the server sends a 200 when libdownload expects
> something different. But I can't say where.
>
> I've been spending some time trying to reproduce this error, but
> syncing to a local ftp just works, with and without trailing slash.
>
>> I am a bit confused though now, it looks like the code
>> already handles multiple slashes. But well, I will need to
>> investigate it more.
>
> C is not my strong point, but I think the following lines in _ftp_cwd()
> should take care of multiple slashes:
>
>        while (*beg == '/')
>                        ++beg, ++i;

Note that you need to download at least 2 files in a row for this to
happen. Doing pacman -Sy <package> is enough because a first
connection is made for the -Sy, then a second for downloading the
package and it breaks there.

If you prefer, I have a simpler test case. I just took libdownload
source code (for helping debugging, I set debug=true in the Makefile
and then installed that), then I extended the samples/dl.c program to
reproduce the issue. I attach it here.
Then you just need to run it on one ftp server with two files :
./dl ftp://ftp.archlinux.org/core/os/i686/lastsync
ftp://ftp.archlinux.org/core/os/i686//packages.txt
But it is probably better to run your own ftp server for debugging /
playing purpose.
./dl ftp://localhost/foo ftp://localhost//bar

The relevant part of the code is indeed the one you showed, but you
have to look around it too:
        for (beg = file + i; beg < end; beg = file + i + 1) {
                while (*beg == '/')
                        ++beg, ++i;
                for (++i; file + i < end && file[i] != '/'; ++i)
                        /* nothing */;
                if(beg < end)
                        e = _ftp_cmd(conn, "CWD %.*s", file + i - beg, beg);
                if (e != FTP_FILE_ACTION_OK) {
                        _ftp_seterr(e);
                        return (-1);
                }
        }

In non trailing case, we have :
file : /core/os/i686/packages.txt
pwd : /core/os/i686

"end" points to the last / in file
"beg" points to the first character which differs between file and
pwd, which turns out to be exactly the same / as "end".

So here we don't have beg < end (we have beg == end) so the whole code
above is skipped and everything works fine.

In trailing case :
file : /core/os/i686//packages.txt
pwd : /core/os/i686

end points to the last / in file, and beg to the first different char,
which is the slash just before.
So here we have beg < end (beg == end - 1 ), so the above code is run,
the beg pointer skips the successive /, so eventually points to the
"p" of packages
So then we don't have beg < end anymore (beg == end + 1), so the
ftp_cmd command is not run.
But the final check is still run, and here is where our error appears :
                if (e != FTP_FILE_ACTION_OK) {
                        _ftp_seterr(e);
                        return (-1);
                }

So as you said, at this point e was set to 200 (FTP_OK), but from a
previous part of the code.
It looks very weird to check for the e error here, while on the
previous line, e was only set conditionally (only if beg < end).

And about finding where e was last set (to 200), I am not sure, maybe
at the beggining of _ftp_cwd :
        if ((e = _ftp_cmd(conn, "PWD")) != FTP_WORKING_DIRECTORY ||

Anyway, this was a very long explanation for a small thing in the end
:P Maybe it is easier to just run libdownload in debug mode, and play
with dl.c and gdb to get sense of it.
I still didn't figure out the whole thing yet, and I have still no
idea if there is something to fix and how.
/*
 *  dl.c : proof-of-concept URL downloader
 * 
 *  Copyright (c) 2006 by Aaron Griffin <[EMAIL PROTECTED]>
 * 
 *  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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 
 *  USA.
 */

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <libgen.h>

#include "../download.h"

#define DLBUF_SIZE 512

int download_url(struct url *u)
{
	int nread = 0, nwritten;
	FILE *f = NULL;
	char buffer[DLBUF_SIZE];
	struct url_stat ust;

	downloadTimeout = 10000;

#ifdef DEBUG
	downloadDebug = 1;
#endif

	downloadLastErrCode = 0;

	u->offset = 0;
#ifdef DEBUG
	f = downloadXGet(u, &ust, "pv");
#else
	f = downloadXGet(u, &ust, "p");
#endif

	if(downloadLastErrCode != 0 || f == NULL) {
		fprintf(stderr, "failed to connect to %s: %s\n", u->host, downloadLastErrString);
		return 1;
	}

	while((nread = fread(buffer, 1, DLBUF_SIZE, f)) > 0) {
		printf("read %u bytes\n", nread);
		if(downloadLastErrCode != 0 || ferror(f)) {
			fprintf(stderr, "error downloading %s: %s\n", u->doc, downloadLastErrString);
			fclose(f);
			return 1;
		}

		nwritten = nread;
		while(nwritten < nread) {
			nwritten += fwrite(buffer, 1, (nread - nwritten), stdout);
		}
	}
	fclose(f);

	return 0;
}

int download(char *url) {
	int ret = 0;
	struct url *u;

	u = downloadParseURL(url);

	if(!u) {
		fprintf(stderr, "invalid URL '%s'\n", url);
		exit(1);
	}

	if(strlen(u->scheme) == 0) {
		fprintf(stderr, "warning: scheme not specified, assuming http://\n";);
		strcpy(u->scheme, "http");
	}


	ret = download_url(u);
	if(ret != 0) fprintf(stderr, "errors occured\n");

	//downloadFreeURL(u);
	return(ret);
}

int main(int argc, char **argv)
{
	int ret = 0;
	const char *progname;
	int i;

	progname = basename(argv[0]);

	if(argc < 2) {
		fprintf(stderr, "usage: %s <url1> ...\n", progname);
		exit(1);
	}

	for(i = 1; i < argc; i++) {
		ret += download(argv[i]);
	}


	return ret;
}
_______________________________________________
pacman-dev mailing list
[email protected]
http://archlinux.org/mailman/listinfo/pacman-dev

Reply via email to