File: rdirectory.c

package info (click to toggle)
yafc 1.1.1.dfsg.1-4
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 3,248 kB
  • ctags: 1,679
  • sloc: ansic: 19,338; sh: 10,365; makefile: 155; perl: 38; ruby: 33
file content (97 lines) | stat: -rw-r--r-- 2,181 bytes parent folder | download | duplicates (4)
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
/* $Id: rdirectory.c,v 1.6 2003/07/12 10:25:41 mhe Exp $
 *
 * rdirectory.c -- representation of a remote directory
 *
 * Yet Another FTP Client
 * Copyright (C) 1998-2001, Martin Hedenfalk <mhe@stacken.kth.se>
 *
 * 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. See COPYING for more details.
 */

#include "syshdr.h"
#include "ftp.h"
#include "strq.h"

rdirectory *rdir_create(void)
{
	rdirectory *rdir;

	rdir = (rdirectory *)xmalloc(sizeof(rdirectory));
	rdir->files = list_new((listfunc)rfile_destroy);
	rdir->timestamp = time(0);

	return rdir;
}

void rdir_destroy(rdirectory *rdir)
{
	if(!rdir)
		return;
	list_free(rdir->files);
	free(rdir->path);
	free(rdir);
}

unsigned long int rdir_size(rdirectory *rdir)
{
	return rglob_size(rdir->files);
}

int rdir_parse(rdirectory *rdir, FILE *fp, const char *path, bool is_mlsd)
{
	char tmp[257];
	rfile *f;
	int r;
	bool failed = false;

	free(rdir->path);
	rdir->path = 0;
	list_clear(rdir->files);
	rdir->timestamp = time(0);

	f = rfile_create();

	ftp_trace("*** start parsing directory listing of '%s' ***\n", path);

	while(!feof(fp)) {
		if(fgets(tmp, 256, fp) == 0)
			break;
		strip_trailing_chars(tmp, "\r\n");
		if(tmp[0] == 0)
			break;
		ftp_trace("%s\n", tmp);

		rfile_clear(f);
		r = rfile_parse(f, tmp, path, is_mlsd);
		if(r == -1) {
			ftp_err("parsing failed on '%s'\n", tmp);
			list_clear(rdir->files);
			failed = true;
		} else if(r == 0)
			list_additem(rdir->files, (void *)rfile_clone(f));
		/* else r == 1, ie a 'total ###' line, which isn't an error */
	}
	ftp_trace("*** end parsing directory listing ***\n");
	if(failed) {
		if(list_numitem(rdir->files) == 0) {
			ftp_err("directory parsing failed completely\n");
			return -1;
		}
	}
	rdir->path = xstrdup(path);
	return 0;
}

rfile *rdir_get_file(rdirectory *rdir, const char *filename)
{
	listitem *li;

	li = list_search(rdir->files,
					 (listsearchfunc)rfile_search_filename, filename);
	if(li)
		return (rfile *)li->data;
	return 0;
}