[libsoup/new-io: 2/2] SoupRequestFTP, etc
- From: Dan Winship <danw src gnome org>
- To: svn-commits-list gnome org
- Cc:
- Subject: [libsoup/new-io: 2/2] SoupRequestFTP, etc
- Date: Sun, 20 Dec 2009 10:19:03 +0000 (UTC)
commit 4c0136aac0e935fcf5c1b65def7928ab59808398
Author: Dan Winship <danw gnome org>
Date: Sun Dec 20 10:51:16 2009 +0100
SoupRequestFTP, etc
libsoup/Makefile.am | 8 +
libsoup/ParseFTPList.c | 1842 +++++++++++++++++++++++++++++++++++++++
libsoup/ParseFTPList.h | 127 +++
libsoup/soup-ftp-connection.c | 1778 +++++++++++++++++++++++++++++++++++++
libsoup/soup-ftp-connection.h | 54 ++
libsoup/soup-ftp-input-stream.c | 178 ++++
libsoup/soup-ftp-input-stream.h | 43 +
libsoup/soup-request-file.c | 1 -
libsoup/soup-request-file.h | 2 +-
libsoup/soup-request-ftp.c | 204 +++++
libsoup/soup-request-ftp.h | 33 +
libsoup/soup-session.c | 3 +-
12 files changed, 4270 insertions(+), 3 deletions(-)
---
diff --git a/libsoup/Makefile.am b/libsoup/Makefile.am
index 3e7161f..9c2c3ce 100644
--- a/libsoup/Makefile.am
+++ b/libsoup/Makefile.am
@@ -107,6 +107,8 @@ libsoup_2_4_la_LIBADD = \
libsoup_2_4_la_SOURCES = \
$(BUILT_SOURCES) \
+ ParseFTPList.h \
+ ParseFTPList.c \
soup-address.c \
soup-auth.c \
soup-auth-basic.h \
@@ -135,6 +137,10 @@ libsoup_2_4_la_SOURCES = \
soup-cookie-jar-text.c \
soup-date.c \
soup-form.c \
+ soup-ftp-connection.h \
+ soup-ftp-connection.c \
+ soup-ftp-input-stream.h \
+ soup-ftp-input-stream.c \
soup-gnutls.c \
soup-headers.c \
soup-logger.c \
@@ -161,6 +167,8 @@ libsoup_2_4_la_SOURCES = \
soup-request.c \
soup-request-file.h \
soup-request-file.c \
+ soup-request-ftp.h \
+ soup-request-ftp.c \
soup-server.c \
soup-session.c \
soup-session-async.c \
diff --git a/libsoup/ParseFTPList.c b/libsoup/ParseFTPList.c
new file mode 100644
index 0000000..3b261a2
--- /dev/null
+++ b/libsoup/ParseFTPList.c
@@ -0,0 +1,1842 @@
+/* ***** BEGIN LICENSE BLOCK *****
+ * Version: MPL 1.1/GPL 2.0/LGPL 2.1
+ *
+ * The contents of this file are subject to the Mozilla Public License Version
+ * 1.1 (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ * http://www.mozilla.org/MPL/
+ *
+ * Software distributed under the License is distributed on an "AS IS" basis,
+ * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
+ * for the specific language governing rights and limitations under the
+ * License.
+ *
+ * The Original Code is mozilla.org Code.
+ *
+ * The Initial Developer of the Original Code is
+ * Cyrus Patel <cyp fb14 uni-mainz de>.
+ * Portions created by the Initial Developer are Copyright (C) 2002
+ * the Initial Developer. All Rights Reserved.
+ *
+ * Contributor(s):
+ * Doug Turner <dougt netscape com>
+ *
+ * Alternatively, the contents of this file may be used under the terms of
+ * either the GNU General Public License Version 2 or later (the "GPL"), or
+ * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
+ * in which case the provisions of the GPL or the LGPL are applicable instead
+ * of those above. If you wish to allow use of your version of this file only
+ * under the terms of either the GPL or the LGPL, and not to allow others to
+ * use your version of this file under the terms of the MPL, indicate your
+ * decision by deleting the provisions above and replace them with the notice
+ * and other provisions required by the GPL or the LGPL. If you do not delete
+ * the provisions above, a recipient may use your version of this file under
+ * the terms of any one of the MPL, the GPL or the LGPL.
+ *
+ * ***** END LICENSE BLOCK ***** */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <ctype.h>
+
+#include "ParseFTPList.h"
+
+/* ==================================================================== */
+
+int ParseFTPList(const char *line, struct list_state *state,
+ struct list_result *result )
+{
+ unsigned int carry_buf_len; /* copy of state->carry_buf_len */
+ unsigned int linelen, pos;
+ const char *p;
+
+ if (!line || !state || !result)
+ return 0;
+
+ memset( result, 0, sizeof(*result) );
+ if (state->magic != ((void *)ParseFTPList))
+ {
+ memset( state, 0, sizeof(*state) );
+ state->magic = ((void *)ParseFTPList);
+ }
+ state->numlines++;
+
+ /* carry buffer is only valid from one line to the next */
+ carry_buf_len = state->carry_buf_len;
+ state->carry_buf_len = 0;
+
+ linelen = 0;
+
+ /* strip leading whitespace */
+ while (*line == ' ' || *line == '\t')
+ line++;
+
+ /* line is terminated at first '\0' or '\n' */
+ p = line;
+ while (*p && *p != '\n')
+ p++;
+ linelen = p - line;
+
+ if (linelen > 0 && *p == '\n' && *(p-1) == '\r')
+ linelen--;
+
+ /* DON'T strip trailing whitespace. */
+
+ if (linelen > 0)
+ {
+ static const char *month_names = "JanFebMarAprMayJunJulAugSepOctNovDec";
+ const char *tokens[16]; /* 16 is more than enough */
+ unsigned int toklen[(sizeof(tokens)/sizeof(tokens[0]))];
+ unsigned int linelen_sans_wsp; // line length sans whitespace
+ unsigned int numtoks = 0;
+ unsigned int tokmarker = 0; /* extra info for lstyle handler */
+ unsigned int month_num = 0;
+ char tbuf[4];
+ int lstyle = 0;
+
+ if (carry_buf_len) /* VMS long filename carryover buffer */
+ {
+ tokens[0] = state->carry_buf;
+ toklen[0] = carry_buf_len;
+ numtoks++;
+ }
+
+ pos = 0;
+ while (pos < linelen && numtoks < (sizeof(tokens)/sizeof(tokens[0])) )
+ {
+ while (pos < linelen &&
+ (line[pos] == ' ' || line[pos] == '\t' || line[pos] == '\r'))
+ pos++;
+ if (pos < linelen)
+ {
+ tokens[numtoks] = &line[pos];
+ while (pos < linelen &&
+ (line[pos] != ' ' && line[pos] != '\t' && line[pos] != '\r'))
+ pos++;
+ if (tokens[numtoks] != &line[pos])
+ {
+ toklen[numtoks] = (&line[pos] - tokens[numtoks]);
+ numtoks++;
+ }
+ }
+ }
+
+ linelen_sans_wsp = &(tokens[numtoks-1][toklen[numtoks-1]]) - tokens[0];
+ if (numtoks == (sizeof(tokens)/sizeof(tokens[0])) )
+ {
+ pos = linelen;
+ while (pos > 0 && (line[pos-1] == ' ' || line[pos-1] == '\t'))
+ pos--;
+ linelen_sans_wsp = pos;
+ }
+
+ /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
+
+#if defined(SUPPORT_EPLF)
+ /* EPLF handling must come somewhere before /bin/dls handling. */
+ if (!lstyle && (!state->lstyle || state->lstyle == 'E'))
+ {
+ if (*line == '+' && linelen > 4 && numtoks >= 2)
+ {
+ pos = 1;
+ while (pos < (linelen-1))
+ {
+ p = &line[pos++];
+ if (*p == '/')
+ result->fe_type = 'd'; /* its a dir */
+ else if (*p == 'r')
+ result->fe_type = 'f'; /* its a file */
+ else if (*p == 'm')
+ {
+ if (isdigit(line[pos]))
+ {
+ while (pos < linelen && isdigit(line[pos]))
+ pos++;
+ if (pos < linelen && line[pos] == ',')
+ {
+ guint64 seconds;
+ time_t t;
+ sscanf(p+1, "%"G_GUINT64_FORMAT, &seconds);
+ t = seconds;
+ result->fe_time = *localtime (&t);
+ }
+ }
+ }
+ else if (*p == 's')
+ {
+ if (isdigit(line[pos]))
+ {
+ while (pos < linelen && isdigit(line[pos]))
+ pos++;
+ if (pos < linelen && line[pos] == ',' &&
+ ((&line[pos]) - (p+1)) < (int) (sizeof(result->fe_size)-1) )
+ {
+ memcpy( result->fe_size, p+1, (unsigned)(&line[pos] - (p+1)) );
+ result->fe_size[(&line[pos] - (p+1))] = '\0';
+ }
+ }
+ }
+ else if (isalpha(*p)) /* 'i'/'up' or unknown "fact" (property) */
+ {
+ while (pos < linelen && *++p != ',')
+ pos++;
+ }
+ else if (*p != '\t' || (p+1) != tokens[1])
+ {
+ break; /* its not EPLF after all */
+ }
+ else
+ {
+ state->parsed_one = 1;
+ state->lstyle = lstyle = 'E';
+
+ p = &(line[linelen_sans_wsp]);
+ result->fe_fname = tokens[1];
+ result->fe_fnlen = p - tokens[1];
+
+ if (!result->fe_type) /* access denied */
+ {
+ result->fe_type = 'f'; /* is assuming 'f'ile correct? */
+ return '?'; /* NO! junk it. */
+ }
+ return result->fe_type;
+ }
+ if (pos >= (linelen-1) || line[pos] != ',')
+ break;
+ pos++;
+ } /* while (pos < linelen) */
+ memset( result, 0, sizeof(*result) );
+ } /* if (*line == '+' && linelen > 4 && numtoks >= 2) */
+ } /* if (!lstyle && (!state->lstyle || state->lstyle == 'E')) */
+#endif /* SUPPORT_EPLF */
+
+ /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
+
+#if defined(SUPPORT_VMS)
+ if (!lstyle && (!state->lstyle || state->lstyle == 'V'))
+ { /* try VMS Multinet/UCX/CMS server */
+ /*
+ * Legal characters in a VMS file/dir spec are [A-Z0-9$.-_~].
+ * '$' cannot begin a filename and `-' cannot be used as the first
+ * or last character. '.' is only valid as a directory separator
+ * and <file>.<type> separator. A canonical filename spec might look
+ * like this: DISK$VOL:[DIR1.DIR2.DIR3]FILE.TYPE;123
+ * All VMS FTP servers LIST in uppercase.
+ *
+ * We need to be picky about this in order to support
+ * multi-line listings correctly.
+ */
+ if (!state->parsed_one &&
+ (numtoks == 1 || (numtoks == 2 && toklen[0] == 9 &&
+ memcmp(tokens[0], "Directory", 9)==0 )))
+ {
+ /* If no dirstyle has been detected yet, and this line is a
+ * VMS list's dirname, then turn on VMS dirstyle.
+ * eg "ACA:[ANONYMOUS]", "DISK$FTP:[ANONYMOUS]", "SYS$ANONFTP:"
+ */
+ p = tokens[0];
+ pos = toklen[0];
+ if (numtoks == 2)
+ {
+ p = tokens[1];
+ pos = toklen[1];
+ }
+ pos--;
+ if (pos >= 3)
+ {
+ while (pos > 0 && p[pos] != '[')
+ {
+ pos--;
+ if (p[pos] == '-' || p[pos] == '$')
+ {
+ if (pos == 0 || p[pos-1] == '[' || p[pos-1] == '.' ||
+ (p[pos] == '-' && (p[pos+1] == ']' || p[pos+1] == '.')))
+ break;
+ }
+ else if (p[pos] != '.' && p[pos] != '~' &&
+ !isdigit(p[pos]) && !isalpha(p[pos]))
+ break;
+ else if (isalpha(p[pos]) && p[pos] != toupper(p[pos]))
+ break;
+ }
+ if (pos > 0)
+ {
+ pos--;
+ if (p[pos] != ':' || p[pos+1] != '[')
+ pos = 0;
+ }
+ }
+ if (pos > 0 && p[pos] == ':')
+ {
+ while (pos > 0)
+ {
+ pos--;
+ if (p[pos] != '$' && p[pos] != '_' && p[pos] != '-' &&
+ p[pos] != '~' && !isdigit(p[pos]) && !isalpha(p[pos]))
+ break;
+ else if (isalpha(p[pos]) && p[pos] != toupper(p[pos]))
+ break;
+ }
+ if (pos == 0)
+ {
+ state->lstyle = 'V';
+ return '?'; /* its junk */
+ }
+ }
+ /* fallthrough */
+ }
+ else if ((tokens[0][toklen[0]-1]) != ';')
+ {
+ if (numtoks == 1 && (state->lstyle == 'V' && !carry_buf_len))
+ lstyle = 'V';
+ else if (numtoks < 4)
+ ;
+ else if (toklen[1] >= 10 && memcmp(tokens[1], "%RMS-E-PRV", 10) == 0)
+ lstyle = 'V';
+ else if ((&line[linelen] - tokens[1]) >= 22 &&
+ memcmp(tokens[1], "insufficient privilege", 22) == 0)
+ lstyle = 'V';
+ else if (numtoks != 4 && numtoks != 6)
+ ;
+ else if (numtoks == 6 && (
+ toklen[5] < 4 || *tokens[5] != '(' || /* perms */
+ (tokens[5][toklen[5]-1]) != ')' ))
+ ;
+ else if ( (toklen[2] == 10 || toklen[2] == 11) &&
+ (tokens[2][toklen[2]-5]) == '-' &&
+ (tokens[2][toklen[2]-9]) == '-' &&
+ (((toklen[3]==4 || toklen[3]==5 || toklen[3]==7 || toklen[3]==8) &&
+ (tokens[3][toklen[3]-3]) == ':' ) ||
+ ((toklen[3]==10 || toklen[3]==11 ) &&
+ (tokens[3][toklen[3]-3]) == '.' )
+ ) && /* time in [H]H:MM[:SS[.CC]] format */
+ isdigit(*tokens[1]) && /* size */
+ isdigit(*tokens[2]) && /* date */
+ isdigit(*tokens[3]) /* time */
+ )
+ {
+ lstyle = 'V';
+ }
+ if (lstyle == 'V')
+ {
+ /*
+ * MultiNet FTP:
+ * LOGIN.COM;2 1 4-NOV-1994 04:09 [ANONYMOUS] (RWE,RWE,,)
+ * PUB.DIR;1 1 27-JAN-1994 14:46 [ANONYMOUS] (RWE,RWE,RE,RWE)
+ * README.FTP;1 %RMS-E-PRV, insufficient privilege or file protection violation
+ * ROUSSOS.DIR;1 1 27-JAN-1994 14:48 [CS,ROUSSOS] (RWE,RWE,RE,R)
+ * S67-50903.JPG;1 328 22-SEP-1998 16:19 [ANONYMOUS] (RWED,RWED,,)
+ * UCX FTP:
+ * CII-MANUAL.TEX;1 213/216 29-JAN-1996 03:33:12 [ANONYMOU,ANONYMOUS] (RWED,RWED,,)
+ * CMU/VMS-IP FTP
+ * [VMSSERV.FILES]ALARM.DIR;1 1/3 5-MAR-1993 18:09
+ * TCPware FTP
+ * FOO.BAR;1 4 5-MAR-1993 18:09:01.12
+ * Long filename example:
+ * THIS-IS-A-LONG-VMS-FILENAME.AND-THIS-IS-A-LONG-VMS-FILETYPE\r\n
+ * 213[/nnn] 29-JAN-1996 03:33[:nn] [ANONYMOU,ANONYMOUS] (RWED,RWED,,)
+ */
+ tokmarker = 0;
+ p = tokens[0];
+ pos = 0;
+ if (*p == '[' && toklen[0] >= 4) /* CMU style */
+ {
+ if (p[1] != ']')
+ {
+ p++;
+ pos++;
+ }
+ while (lstyle && pos < toklen[0] && *p != ']')
+ {
+ if (*p != '$' && *p != '.' && *p != '_' && *p != '-' &&
+ *p != '~' && !isdigit(*p) && !isalpha(*p))
+ lstyle = 0;
+ pos++;
+ p++;
+ }
+ if (lstyle && pos < (toklen[0]-1) && *p == ']')
+ {
+ pos++;
+ p++;
+ tokmarker = pos; /* length of leading "[DIR1.DIR2.etc]" */
+ }
+ }
+ while (lstyle && pos < toklen[0] && *p != ';')
+ {
+ if (*p != '$' && *p != '.' && *p != '_' && *p != '-' &&
+ *p != '~' && !isdigit(*p) && !isalpha(*p))
+ lstyle = 0;
+ else if (isalpha(*p) && *p != toupper(*p))
+ lstyle = 0;
+ p++;
+ pos++;
+ }
+ if (lstyle && *p == ';')
+ {
+ if (pos == 0 || pos == (toklen[0]-1))
+ lstyle = 0;
+ for (pos++;lstyle && pos < toklen[0];pos++)
+ {
+ if (!isdigit(tokens[0][pos]))
+ lstyle = 0;
+ }
+ }
+ pos = (p - tokens[0]); /* => fnlength sans ";####" */
+ pos -= tokmarker; /* => fnlength sans "[DIR1.DIR2.etc]" */
+ p = &(tokens[0][tokmarker]); /* offset of basename */
+
+ if (!lstyle || pos > 80) /* VMS filenames can't be longer than that */
+ {
+ lstyle = 0;
+ }
+ else if (numtoks == 1)
+ {
+ /* if VMS has been detected and there is only one token and that
+ * token was a VMS filename then this is a multiline VMS LIST entry.
+ */
+ if (pos >= (sizeof(state->carry_buf)-1))
+ pos = (sizeof(state->carry_buf)-1); /* shouldn't happen */
+ memcpy( state->carry_buf, p, pos );
+ state->carry_buf_len = pos;
+ return '?'; /* tell caller to treat as junk */
+ }
+ else if (isdigit(*tokens[1])) /* not no-privs message */
+ {
+ for (pos = 0; lstyle && pos < (toklen[1]); pos++)
+ {
+ if (!isdigit((tokens[1][pos])) && (tokens[1][pos]) != '/')
+ lstyle = 0;
+ }
+ if (lstyle && numtoks > 4) /* Multinet or UCX but not CMU */
+ {
+ for (pos = 1; lstyle && pos < (toklen[5]-1); pos++)
+ {
+ p = &(tokens[5][pos]);
+ if (*p!='R' && *p!='W' && *p!='E' && *p!='D' && *p!=',')
+ lstyle = 0;
+ }
+ }
+ }
+ } /* passed initial tests */
+ } /* else if ((tokens[0][toklen[0]-1]) != ';') */
+
+ if (lstyle == 'V')
+ {
+ state->parsed_one = 1;
+ state->lstyle = lstyle;
+
+ if (isdigit(*tokens[1])) /* not permission denied etc */
+ {
+ /* strip leading directory name */
+ if (*tokens[0] == '[') /* CMU server */
+ {
+ pos = toklen[0]-1;
+ p = tokens[0]+1;
+ while (*p != ']')
+ {
+ p++;
+ pos--;
+ }
+ toklen[0] = --pos;
+ tokens[0] = ++p;
+ }
+ pos = 0;
+ while (pos < toklen[0] && (tokens[0][pos]) != ';')
+ pos++;
+
+ result->fe_cinfs = 1;
+ result->fe_type = 'f';
+ result->fe_fname = tokens[0];
+ result->fe_fnlen = pos;
+
+ if (pos > 4)
+ {
+ p = &(tokens[0][pos-4]);
+ if (p[0] == '.' && p[1] == 'D' && p[2] == 'I' && p[3] == 'R')
+ {
+ result->fe_fnlen -= 4;
+ result->fe_type = 'd';
+ }
+ }
+
+ if (result->fe_type != 'd')
+ {
+ /* #### or used/allocated form. If used/allocated form, then
+ * 'used' is the size in bytes if and only if 'used'<=allocated.
+ * If 'used' is size in bytes then it can be > 2^32
+ * If 'used' is not size in bytes then it is size in blocks.
+ */
+ pos = 0;
+ while (pos < toklen[1] && (tokens[1][pos]) != '/')
+ pos++;
+
+/*
+ * I've never seen size come back in bytes, its always in blocks, and
+ * the following test fails. So, always perform the "size in blocks".
+ * I'm leaving the "size in bytes" code if'd out in case we ever need
+ * to re-instate it.
+*/
+#if 0
+ if (pos < toklen[1] && ( (pos<<1) > (toklen[1]-1) ||
+ (strtoul(tokens[1], (char **)0, 10) >
+ strtoul(tokens[1]+pos+1, (char **)0, 10)) ))
+ { /* size is in bytes */
+ if (pos > (sizeof(result->fe_size)-1))
+ pos = sizeof(result->fe_size)-1;
+ memcpy( result->fe_size, tokens[1], pos );
+ result->fe_size[pos] = '\0';
+ }
+ else /* size is in blocks */
+#endif
+ {
+ /* size requires multiplication by blocksize.
+ *
+ * We could assume blocksize is 512 (like Lynx does) and
+ * shift by 9, but that might not be right. Even if it
+ * were, doing that wouldn't reflect what the file's
+ * real size was. The sanest thing to do is not use the
+ * LISTing's filesize, so we won't (like ftpmirror).
+ *
+ * ulltoa(((unsigned long long)fsz)<<9, result->fe_size, 10);
+ *
+ * A block is always 512 bytes on OpenVMS, compute size.
+ * So its rounded up to the next block, so what, its better
+ * than not showing the size at all.
+ * A block is always 512 bytes on OpenVMS, compute size.
+ * So its rounded up to the next block, so what, its better
+ * than not showing the size at all.
+ */
+ guint64 fsz;
+ fsz = g_ascii_strtoull (tokens[1], (char **)0, 10);
+ fsz *= 512;
+ g_snprintf(result->fe_size, sizeof(result->fe_size),
+ "%"G_GUINT64_FORMAT, fsz);
+ }
+
+ } /* if (result->fe_type != 'd') */
+
+ p = tokens[2] + 2;
+ if (*p == '-')
+ p++;
+ tbuf[0] = p[0];
+ tbuf[1] = tolower(p[1]);
+ tbuf[2] = tolower(p[2]);
+ month_num = 0;
+ for (pos = 0; pos < (12*3); pos+=3)
+ {
+ if (tbuf[0] == month_names[pos+0] &&
+ tbuf[1] == month_names[pos+1] &&
+ tbuf[2] == month_names[pos+2])
+ break;
+ month_num++;
+ }
+ if (month_num >= 12)
+ month_num = 0;
+ result->fe_time.tm_mon = month_num;
+ result->fe_time.tm_mday = atoi(tokens[2]);
+ result->fe_time.tm_year = atoi(p+4); // NSPR wants year as XXXX
+
+ p = tokens[3] + 2;
+ if (*p == ':')
+ p++;
+ if (p[2] == ':')
+ result->fe_time.tm_sec = atoi(p+3);
+ result->fe_time.tm_hour = atoi(tokens[3]);
+ result->fe_time.tm_min = atoi(p);
+
+ return result->fe_type;
+
+ } /* if (isdigit(*tokens[1])) */
+
+ return '?'; /* junk */
+
+ } /* if (lstyle == 'V') */
+ } /* if (!lstyle && (!state->lstyle || state->lstyle == 'V')) */
+#endif
+
+ /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
+
+#if defined(SUPPORT_CMS)
+ /* Virtual Machine/Conversational Monitor System (IBM Mainframe) */
+ if (!lstyle && (!state->lstyle || state->lstyle == 'C')) /* VM/CMS */
+ {
+ /* LISTing according to mirror.pl
+ * Filename FileType Fm Format Lrecl Records Blocks Date Time
+ * LASTING GLOBALV A1 V 41 21 1 9/16/91 15:10:32
+ * J43401 NETLOG A0 V 77 1 1 9/12/91 12:36:04
+ * PROFILE EXEC A1 V 17 3 1 9/12/91 12:39:07
+ * DIRUNIX SCRIPT A1 V 77 1216 17 1/04/93 20:30:47
+ * MAIL PROFILE A2 F 80 1 1 10/14/92 16:12:27
+ * BADY2K TEXT A0 V 1 1 1 1/03/102 10:11:12
+ * AUTHORS A1 DIR - - - 9/20/99 10:31:11
+ *
+ * LISTing from vm.marist.edu and vm.sc.edu
+ * 220-FTPSERVE IBM VM Level 420 at VM.MARIST.EDU, 04:58:12 EDT WEDNESDAY 2002-07-10
+ * AUTHORS DIR - - - 1999-09-20 10:31:11 -
+ * HARRINGTON DIR - - - 1997-02-12 15:33:28 -
+ * PICS DIR - - - 2000-10-12 15:43:23 -
+ * SYSFILE DIR - - - 2000-07-20 17:48:01 -
+ * WELCNVT EXEC V 72 9 1 1999-09-20 17:16:18 -
+ * WELCOME EREADME F 80 21 1 1999-12-27 16:19:00 -
+ * WELCOME README V 82 21 1 1999-12-27 16:19:04 -
+ * README ANONYMOU V 71 26 1 1997-04-02 12:33:20 TCP291
+ * README ANONYOLD V 71 15 1 1995-08-25 16:04:27 TCP291
+ */
+ if (numtoks >= 7 && (toklen[0]+toklen[1]) <= 16)
+ {
+ for (pos = 1; !lstyle && (pos+5) < numtoks; pos++)
+ {
+ p = tokens[pos];
+ if ((toklen[pos] == 1 && (*p == 'F' || *p == 'V')) ||
+ (toklen[pos] == 3 && *p == 'D' && p[1] == 'I' && p[2] == 'R'))
+ {
+ if (toklen[pos+5] == 8 && (tokens[pos+5][2]) == ':' &&
+ (tokens[pos+5][5]) == ':' )
+ {
+ p = tokens[pos+4];
+ if ((toklen[pos+4] == 10 && p[4] == '-' && p[7] == '-') ||
+ (toklen[pos+4] >= 7 && toklen[pos+4] <= 9 &&
+ p[((p[1]!='/')?(2):(1))] == '/' &&
+ p[((p[1]!='/')?(5):(4))] == '/'))
+ /* Y2K bugs possible ("7/06/102" or "13/02/101") */
+ {
+ if ( (*tokens[pos+1] == '-' &&
+ *tokens[pos+2] == '-' &&
+ *tokens[pos+3] == '-') ||
+ (isdigit(*tokens[pos+1]) &&
+ isdigit(*tokens[pos+2]) &&
+ isdigit(*tokens[pos+3])) )
+ {
+ lstyle = 'C';
+ tokmarker = pos;
+ }
+ }
+ }
+ }
+ } /* for (pos = 1; !lstyle && (pos+5) < numtoks; pos++) */
+ } /* if (numtoks >= 7) */
+
+ /* extra checking if first pass */
+ if (lstyle && !state->lstyle)
+ {
+ for (pos = 0, p = tokens[0]; lstyle && pos < toklen[0]; pos++, p++)
+ {
+ if (isalpha(*p) && toupper(*p) != *p)
+ lstyle = 0;
+ }
+ for (pos = tokmarker+1; pos <= tokmarker+3; pos++)
+ {
+ if (!(toklen[pos] == 1 && *tokens[pos] == '-'))
+ {
+ for (p = tokens[pos]; lstyle && p<(tokens[pos]+toklen[pos]); p++)
+ {
+ if (!isdigit(*p))
+ lstyle = 0;
+ }
+ }
+ }
+ for (pos = 0, p = tokens[tokmarker+4];
+ lstyle && pos < toklen[tokmarker+4]; pos++, p++)
+ {
+ if (*p == '/')
+ {
+ /* There may be Y2K bugs in the date. Don't simplify to
+ * pos != (len-3) && pos != (len-6) like time is done.
+ */
+ if ((tokens[tokmarker+4][1]) == '/')
+ {
+ if (pos != 1 && pos != 4)
+ lstyle = 0;
+ }
+ else if (pos != 2 && pos != 5)
+ lstyle = 0;
+ }
+ else if (*p != '-' && !isdigit(*p))
+ lstyle = 0;
+ else if (*p == '-' && pos != 4 && pos != 7)
+ lstyle = 0;
+ }
+ for (pos = 0, p = tokens[tokmarker+5];
+ lstyle && pos < toklen[tokmarker+5]; pos++, p++)
+ {
+ if (*p != ':' && !isdigit(*p))
+ lstyle = 0;
+ else if (*p == ':' && pos != (toklen[tokmarker+5]-3)
+ && pos != (toklen[tokmarker+5]-6))
+ lstyle = 0;
+ }
+ } /* initial if() */
+
+ if (lstyle == 'C')
+ {
+ state->parsed_one = 1;
+ state->lstyle = lstyle;
+
+ p = tokens[tokmarker+4];
+ if (toklen[tokmarker+4] == 10) /* newstyle: YYYY-MM-DD format */
+ {
+ result->fe_time.tm_year = atoi(p+0) - 1900;
+ result->fe_time.tm_mon = atoi(p+5) - 1;
+ result->fe_time.tm_mday = atoi(p+8);
+ }
+ else /* oldstyle: [M]M/DD/YY format */
+ {
+ pos = toklen[tokmarker+4];
+ result->fe_time.tm_mon = atoi(p) - 1;
+ result->fe_time.tm_mday = atoi((p+pos)-5);
+ result->fe_time.tm_year = atoi((p+pos)-2);
+ if (result->fe_time.tm_year < 70)
+ result->fe_time.tm_year += 100;
+ }
+
+ p = tokens[tokmarker+5];
+ pos = toklen[tokmarker+5];
+ result->fe_time.tm_hour = atoi(p);
+ result->fe_time.tm_min = atoi((p+pos)-5);
+ result->fe_time.tm_sec = atoi((p+pos)-2);
+
+ result->fe_cinfs = 1;
+ result->fe_fname = tokens[0];
+ result->fe_fnlen = toklen[0];
+ result->fe_type = 'f';
+
+ p = tokens[tokmarker];
+ if (toklen[tokmarker] == 3 && *p=='D' && p[1]=='I' && p[2]=='R')
+ result->fe_type = 'd';
+
+ if ((/*newstyle*/ toklen[tokmarker+4] == 10 && tokmarker > 1) ||
+ (/*oldstyle*/ toklen[tokmarker+4] != 10 && tokmarker > 2))
+ { /* have a filetype column */
+ char *dot;
+ p = &(tokens[0][toklen[0]]);
+ memcpy( &dot, &p, sizeof(dot) ); /* NASTY! */
+ *dot++ = '.';
+ p = tokens[1];
+ for (pos = 0; pos < toklen[1]; pos++)
+ *dot++ = *p++;
+ result->fe_fnlen += 1 + toklen[1];
+ }
+
+ /* oldstyle LISTING:
+ * files/dirs not on the 'A' minidisk are not RETRievable/CHDIRable
+ if (toklen[tokmarker+4] != 10 && *tokens[tokmarker-1] != 'A')
+ return '?';
+ */
+
+ /* VM/CMS LISTings have no usable filesize field.
+ * Have to use the 'SIZE' command for that.
+ */
+ return result->fe_type;
+
+ } /* if (lstyle == 'C' && (!state->lstyle || state->lstyle == lstyle)) */
+ } /* VM/CMS */
+#endif
+
+ /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
+
+#if defined(SUPPORT_DOS) /* WinNT DOS dirstyle */
+ if (!lstyle && (!state->lstyle || state->lstyle == 'W'))
+ {
+ /*
+ * "10-23-00 01:27PM <DIR> veronist"
+ * "06-15-00 07:37AM <DIR> zoe"
+ * "07-14-00 01:35PM 2094926 canprankdesk.tif"
+ * "07-21-00 01:19PM 95077 Jon Kauffman Enjoys the Good Life.jpg"
+ * "07-21-00 01:19PM 52275 Name Plate.jpg"
+ * "07-14-00 01:38PM 2250540 Valentineoffprank-HiRes.jpg"
+ */
+ if ((numtoks >= 4) && toklen[0] == 8 && toklen[1] == 7 &&
+ (*tokens[2] == '<' || isdigit(*tokens[2])) )
+ {
+ p = tokens[0];
+ if ( isdigit(p[0]) && isdigit(p[1]) && p[2]=='-' &&
+ isdigit(p[3]) && isdigit(p[4]) && p[5]=='-' &&
+ isdigit(p[6]) && isdigit(p[7]) )
+ {
+ p = tokens[1];
+ if ( isdigit(p[0]) && isdigit(p[1]) && p[2]==':' &&
+ isdigit(p[3]) && isdigit(p[4]) &&
+ (p[5]=='A' || p[5]=='P') && p[6]=='M')
+ {
+ lstyle = 'W';
+ if (!state->lstyle)
+ {
+ p = tokens[2];
+ /* <DIR> or <JUNCTION> */
+ if (*p != '<' || p[toklen[2]-1] != '>')
+ {
+ for (pos = 1; (lstyle && pos < toklen[2]); pos++)
+ {
+ if (!isdigit(*++p))
+ lstyle = 0;
+ }
+ }
+ }
+ }
+ }
+ }
+
+ if (lstyle == 'W')
+ {
+ state->parsed_one = 1;
+ state->lstyle = lstyle;
+
+ p = &(line[linelen_sans_wsp]); /* line end sans wsp */
+ result->fe_cinfs = 1;
+ result->fe_fname = tokens[3];
+ result->fe_fnlen = p - tokens[3];
+ result->fe_type = 'd';
+
+ if (*tokens[2] != '<') /* not <DIR> or <JUNCTION> */
+ {
+ result->fe_type = 'f';
+ pos = toklen[2];
+ while (pos > (sizeof(result->fe_size)-1))
+ pos = (sizeof(result->fe_size)-1);
+ memcpy( result->fe_size, tokens[2], pos );
+ result->fe_size[pos] = '\0';
+ }
+ else if ((tokens[2][1]) != 'D') /* not <DIR> */
+ {
+ result->fe_type = '?'; /* unknown until junc for sure */
+ if (result->fe_fnlen > 4)
+ {
+ p = result->fe_fname;
+ for (pos = result->fe_fnlen - 4; pos > 0; pos--)
+ {
+ if (p[0] == ' ' && p[3] == ' ' && p[2] == '>' &&
+ (p[1] == '=' || p[1] == '-'))
+ {
+ result->fe_type = 'l';
+ result->fe_fnlen = p - result->fe_fname;
+ result->fe_lname = p + 4;
+ result->fe_lnlen = &(line[linelen_sans_wsp])
+ - result->fe_lname;
+ break;
+ }
+ p++;
+ }
+ }
+ }
+
+ result->fe_time.tm_mon = atoi(tokens[0]+0);
+ if (result->fe_time.tm_mon != 0)
+ {
+ result->fe_time.tm_mon--;
+ result->fe_time.tm_mday = atoi(tokens[0]+3);
+ result->fe_time.tm_year = atoi(tokens[0]+6);
+ if (result->fe_time.tm_year < 80)
+ result->fe_time.tm_year += 100;
+ }
+
+ result->fe_time.tm_hour = atoi(tokens[1]+0);
+ result->fe_time.tm_min = atoi(tokens[1]+3);
+ if ((tokens[1][5]) == 'P' && result->fe_time.tm_hour < 12)
+ result->fe_time.tm_hour += 12;
+
+ /* the caller should do this (if dropping "." and ".." is desired)
+ if (result->fe_type == 'd' && result->fe_fname[0] == '.' &&
+ (result->fe_fnlen == 1 || (result->fe_fnlen == 2 &&
+ result->fe_fname[1] == '.')))
+ return '?';
+ */
+
+ return result->fe_type;
+ } /* if (lstyle == 'W' && (!state->lstyle || state->lstyle == lstyle)) */
+ } /* if (!lstyle && (!state->lstyle || state->lstyle == 'W')) */
+#endif
+
+ /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
+
+#if defined(SUPPORT_OS2)
+ if (!lstyle && (!state->lstyle || state->lstyle == 'O')) /* OS/2 test */
+ {
+ /* 220 server IBM TCP/IP for OS/2 - FTP Server ver 23:04:36 on Jan 15 1997 ready.
+ * fixed position, space padded columns. I have only a vague idea
+ * of what the contents between col 18 and 34 might be: All I can infer
+ * is that there may be attribute flags in there and there may be
+ * a " DIR" in there.
+ *
+ * 1 2 3 4 5 6
+ *0123456789012345678901234567890123456789012345678901234567890123456789
+ *----- size -------|??????????????? MM-DD-YY| HH:MM| nnnnnnnnn....
+ * 0 DIR 04-11-95 16:26 .
+ * 0 DIR 04-11-95 16:26 ..
+ * 0 DIR 04-11-95 16:26 ADDRESS
+ * 612 RHSA 07-28-95 16:45 air_tra1.bag
+ * 195 A 08-09-95 10:23 Alfa1.bag
+ * 0 RHS DIR 04-11-95 16:26 ATTACH
+ * 372 A 08-09-95 10:26 Aussie_1.bag
+ * 310992 06-28-94 09:56 INSTALL.EXE
+ * 1 2 3 4
+ * 01234567890123456789012345678901234567890123456789
+ * dirlist from the mirror.pl project, col positions from Mozilla.
+ */
+ p = &(line[toklen[0]]);
+ /* \s(\d\d-\d\d-\d\d)\s+(\d\d:\d\d)\s */
+ if (numtoks >= 4 && toklen[0] <= 18 && isdigit(*tokens[0]) &&
+ (linelen - toklen[0]) >= (53-18) &&
+ p[18-18] == ' ' && p[34-18] == ' ' &&
+ p[37-18] == '-' && p[40-18] == '-' && p[43-18] == ' ' &&
+ p[45-18] == ' ' && p[48-18] == ':' && p[51-18] == ' ' &&
+ isdigit(p[35-18]) && isdigit(p[36-18]) &&
+ isdigit(p[38-18]) && isdigit(p[39-18]) &&
+ isdigit(p[41-18]) && isdigit(p[42-18]) &&
+ isdigit(p[46-18]) && isdigit(p[47-18]) &&
+ isdigit(p[49-18]) && isdigit(p[50-18])
+ )
+ {
+ lstyle = 'O'; /* OS/2 */
+ if (!state->lstyle)
+ {
+ for (pos = 1; lstyle && pos < toklen[0]; pos++)
+ {
+ if (!isdigit(tokens[0][pos]))
+ lstyle = 0;
+ }
+ }
+ }
+
+ if (lstyle == 'O')
+ {
+ state->parsed_one = 1;
+ state->lstyle = lstyle;
+
+ p = &(line[toklen[0]]);
+
+ result->fe_cinfs = 1;
+ result->fe_fname = &p[53-18];
+ result->fe_fnlen = (&(line[linelen_sans_wsp]))
+ - (result->fe_fname);
+ result->fe_type = 'f';
+
+ /* I don't have a real listing to determine exact pos, so scan. */
+ for (pos = (18-18); pos < ((35-18)-4); pos++)
+ {
+ if (p[pos+0] == ' ' && p[pos+1] == 'D' &&
+ p[pos+2] == 'I' && p[pos+3] == 'R')
+ {
+ result->fe_type = 'd';
+ break;
+ }
+ }
+
+ if (result->fe_type != 'd')
+ {
+ pos = toklen[0];
+ if (pos > (sizeof(result->fe_size)-1))
+ pos = (sizeof(result->fe_size)-1);
+ memcpy( result->fe_size, tokens[0], pos );
+ result->fe_size[pos] = '\0';
+ }
+
+ result->fe_time.tm_mon = atoi(&p[35-18]) - 1;
+ result->fe_time.tm_mday = atoi(&p[38-18]);
+ result->fe_time.tm_year = atoi(&p[41-18]);
+ if (result->fe_time.tm_year < 80)
+ result->fe_time.tm_year += 100;
+ result->fe_time.tm_hour = atoi(&p[46-18]);
+ result->fe_time.tm_min = atoi(&p[49-18]);
+
+ /* the caller should do this (if dropping "." and ".." is desired)
+ if (result->fe_type == 'd' && result->fe_fname[0] == '.' &&
+ (result->fe_fnlen == 1 || (result->fe_fnlen == 2 &&
+ result->fe_fname[1] == '.')))
+ return '?';
+ */
+
+ return result->fe_type;
+ } /* if (lstyle == 'O') */
+
+ } /* if (!lstyle && (!state->lstyle || state->lstyle == 'O')) */
+#endif
+
+ /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
+
+#if defined(SUPPORT_LSL)
+ if (!lstyle && (!state->lstyle || state->lstyle == 'U')) /* /bin/ls & co. */
+ {
+ /* UNIX-style listing, without inum and without blocks
+ * "-rw-r--r-- 1 root other 531 Jan 29 03:26 README"
+ * "dr-xr-xr-x 2 root other 512 Apr 8 1994 etc"
+ * "dr-xr-xr-x 2 root 512 Apr 8 1994 etc"
+ * "lrwxrwxrwx 1 root other 7 Jan 25 00:17 bin -> usr/bin"
+ * Also produced by Microsoft's FTP servers for Windows:
+ * "---------- 1 owner group 1803128 Jul 10 10:18 ls-lR.Z"
+ * "d--------- 1 owner group 0 May 9 19:45 Softlib"
+ * Also WFTPD for MSDOS:
+ * "-rwxrwxrwx 1 noone nogroup 322 Aug 19 1996 message.ftp"
+ * Hellsoft for NetWare:
+ * "d[RWCEMFA] supervisor 512 Jan 16 18:53 login"
+ * "-[RWCEMFA] rhesus 214059 Oct 20 15:27 cx.exe"
+ * Newer Hellsoft for NetWare: (netlab2.usu.edu)
+ * - [RWCEAFMS] NFAUUser 192 Apr 27 15:21 HEADER.html
+ * d [RWCEAFMS] jrd 512 Jul 11 03:01 allupdates
+ * Also NetPresenz for the Mac:
+ * "-------r-- 326 1391972 1392298 Nov 22 1995 MegaPhone.sit"
+ * "drwxrwxr-x folder 2 May 10 1996 network"
+ * Protected directory:
+ * "drwx-wx-wt 2 root wheel 512 Jul 1 02:15 incoming"
+ * uid/gid instead of username/groupname:
+ * "drwxr-xr-x 2 0 0 512 May 28 22:17 etc"
+ */
+
+ if (numtoks >= 6)
+ {
+ /* there are two perm formats (Hellsoft/NetWare and *IX strmode(3)).
+ * Scan for size column only if the perm format is one or the other.
+ */
+ if (toklen[0] == 1 || (tokens[0][1]) == '[')
+ {
+ if (*tokens[0] == 'd' || *tokens[0] == '-')
+ {
+ pos = toklen[0]-1;
+ p = tokens[0] + 1;
+ if (pos == 0)
+ {
+ p = tokens[1];
+ pos = toklen[1];
+ }
+ if ((pos == 9 || pos == 10) &&
+ (*p == '[' && p[pos-1] == ']') &&
+ (p[1] == 'R' || p[1] == '-') &&
+ (p[2] == 'W' || p[2] == '-') &&
+ (p[3] == 'C' || p[3] == '-') &&
+ (p[4] == 'E' || p[4] == '-'))
+ {
+ /* rest is FMA[S] or AFM[S] */
+ lstyle = 'U'; /* very likely one of the NetWare servers */
+ }
+ }
+ }
+ else if ((toklen[0] == 10 || toklen[0] == 11)
+ && strchr("-bcdlpsw?DFam", *tokens[0]))
+ {
+ p = &(tokens[0][1]);
+ if ((p[0] == 'r' || p[0] == '-') &&
+ (p[1] == 'w' || p[1] == '-') &&
+ (p[3] == 'r' || p[3] == '-') &&
+ (p[4] == 'w' || p[4] == '-') &&
+ (p[6] == 'r' || p[6] == '-') &&
+ (p[7] == 'w' || p[7] == '-'))
+ /* 'x'/p[9] can be S|s|x|-|T|t or implementation specific */
+ {
+ lstyle = 'U'; /* very likely /bin/ls */
+ }
+ }
+ }
+ if (lstyle == 'U') /* first token checks out */
+ {
+ lstyle = 0;
+ for (pos = (numtoks-5); !lstyle && pos > 1; pos--)
+ {
+ /* scan for: (\d+)\s+([A-Z][a-z][a-z])\s+
+ * (\d\d\d\d|\d\:\d\d|\d\d\:\d\d|\d\:\d\d\:\d\d|\d\d\:\d\d\:\d\d)
+ * \s+(.+)$
+ */
+ if (isdigit(*tokens[pos]) /* size */
+ /* (\w\w\w) */
+ && toklen[pos+1] == 3 && isalpha(*tokens[pos+1]) &&
+ isalpha(tokens[pos+1][1]) && isalpha(tokens[pos+1][2])
+ /* (\d|\d\d) */
+ && isdigit(*tokens[pos+2]) &&
+ (toklen[pos+2] == 1 ||
+ (toklen[pos+2] == 2 && isdigit(tokens[pos+2][1])))
+ && toklen[pos+3] >= 4 && isdigit(*tokens[pos+3])
+ /* (\d\:\d\d\:\d\d|\d\d\:\d\d\:\d\d) */
+ && (toklen[pos+3] <= 5 || (
+ (toklen[pos+3] == 7 || toklen[pos+3] == 8) &&
+ (tokens[pos+3][toklen[pos+3]-3]) == ':'))
+ && isdigit(tokens[pos+3][toklen[pos+3]-2])
+ && isdigit(tokens[pos+3][toklen[pos+3]-1])
+ && (
+ /* (\d\d\d\d) */
+ ((toklen[pos+3] == 4 || toklen[pos+3] == 5) &&
+ isdigit(tokens[pos+3][1]) &&
+ isdigit(tokens[pos+3][2]) )
+ /* (\d\:\d\d|\d\:\d\d\:\d\d) */
+ || ((toklen[pos+3] == 4 || toklen[pos+3] == 7) &&
+ (tokens[pos+3][1]) == ':' &&
+ isdigit(tokens[pos+3][2]) && isdigit(tokens[pos+3][3]))
+ /* (\d\d\:\d\d|\d\d\:\d\d\:\d\d) */
+ || ((toklen[pos+3] == 5 || toklen[pos+3] == 8) &&
+ isdigit(tokens[pos+3][1]) && (tokens[pos+3][2]) == ':' &&
+ isdigit(tokens[pos+3][3]) && isdigit(tokens[pos+3][4]))
+ )
+ )
+ {
+ unsigned int i;
+ lstyle = 'U'; /* assume /bin/ls or variant format */
+ tokmarker = pos;
+
+ /* check that size is numeric */
+ p = tokens[tokmarker];
+ for (i = 0; i < toklen[tokmarker]; i++)
+ {
+ if (!isdigit(*p++))
+ {
+ lstyle = 0;
+ break;
+ }
+ }
+ if (lstyle)
+ {
+ month_num = 0;
+ p = tokens[tokmarker+1];
+ for (i = 0; i < (12*3); i+=3)
+ {
+ if (p[0] == month_names[i+0] &&
+ p[1] == month_names[i+1] &&
+ p[2] == month_names[i+2])
+ break;
+ month_num++;
+ }
+ if (month_num >= 12)
+ lstyle = 0;
+ }
+ } /* relative position test */
+ } /* for (pos = (numtoks-5); !lstyle && pos > 1; pos--) */
+ } /* if (lstyle == 'U') */
+
+ if (lstyle == 'U')
+ {
+ state->parsed_one = 1;
+ state->lstyle = lstyle;
+
+ result->fe_cinfs = 0;
+ result->fe_type = '?';
+ if (*tokens[0] == 'd' || *tokens[0] == 'l')
+ result->fe_type = *tokens[0];
+ else if (*tokens[0] == 'D')
+ result->fe_type = 'd';
+ else if (*tokens[0] == '-' || *tokens[0] == 'F')
+ result->fe_type = 'f'; /* (hopefully a regular file) */
+
+ if (result->fe_type != 'd')
+ {
+ pos = toklen[tokmarker];
+ if (pos > (sizeof(result->fe_size)-1))
+ pos = (sizeof(result->fe_size)-1);
+ memcpy( result->fe_size, tokens[tokmarker], pos );
+ result->fe_size[pos] = '\0';
+ }
+
+ result->fe_time.tm_mon = month_num;
+ result->fe_time.tm_mday = atoi(tokens[tokmarker+2]);
+ if (result->fe_time.tm_mday == 0)
+ result->fe_time.tm_mday++;
+
+ p = tokens[tokmarker+3];
+ pos = (unsigned int)atoi(p);
+ if (p[1] == ':') /* one digit hour */
+ p--;
+ if (p[2] != ':') /* year */
+ {
+ result->fe_time.tm_year = pos;
+ }
+ else
+ {
+ result->fe_time.tm_hour = pos;
+ result->fe_time.tm_min = atoi(p+3);
+ if (p[5] == ':')
+ result->fe_time.tm_sec = atoi(p+6);
+
+ if (!state->now_time)
+ {
+ state->now_time = time (NULL);
+ state->now_tm = *localtime (&state->now_time);
+ }
+
+ result->fe_time.tm_year = state->now_tm.tm_year;
+ if ( (( state->now_tm.tm_mon << 5) + state->now_tm.tm_mday) <
+ ((result->fe_time.tm_mon << 5) + result->fe_time.tm_mday) )
+ result->fe_time.tm_year--;
+
+ } /* time/year */
+
+ result->fe_fname = tokens[tokmarker+4];
+ result->fe_fnlen = (&(line[linelen_sans_wsp]))
+ - (result->fe_fname);
+
+ if (result->fe_type == 'l' && result->fe_fnlen > 4)
+ {
+ p = result->fe_fname + 1;
+ for (pos = 1; pos < (result->fe_fnlen - 4); pos++)
+ {
+ if (*p == ' ' && p[1] == '-' && p[2] == '>' && p[3] == ' ')
+ {
+ result->fe_lname = p + 4;
+ result->fe_lnlen = (&(line[linelen_sans_wsp]))
+ - (result->fe_lname);
+ result->fe_fnlen = pos;
+ break;
+ }
+ p++;
+ }
+ }
+
+#if defined(SUPPORT_LSLF) /* some (very rare) servers return ls -lF */
+ if (result->fe_fnlen > 1)
+ {
+ p = result->fe_fname[result->fe_fnlen-1];
+ pos = result->fe_type;
+ if (pos == 'd') {
+ if (*p == '/') result->fe_fnlen--; /* directory */
+ } else if (pos == 'l') {
+ if (*p == '@') result->fe_fnlen--; /* symlink */
+ } else if (pos == 'f') {
+ if (*p == '*') result->fe_fnlen--; /* executable */
+ } else if (*p == '=' || *p == '%' || *p == '|') {
+ result->fe_fnlen--; /* socket, whiteout, fifo */
+ }
+ }
+#endif
+
+ /* the caller should do this (if dropping "." and ".." is desired)
+ if (result->fe_type == 'd' && result->fe_fname[0] == '.' &&
+ (result->fe_fnlen == 1 || (result->fe_fnlen == 2 &&
+ result->fe_fname[1] == '.')))
+ return '?';
+ */
+
+ return result->fe_type;
+
+ } /* if (lstyle == 'U') */
+
+ } /* if (!lstyle && (!state->lstyle || state->lstyle == 'U')) */
+#endif
+
+ /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
+
+#if defined(SUPPORT_W16) /* 16bit Windows */
+ if (!lstyle && (!state->lstyle || state->lstyle == 'w'))
+ { /* old SuperTCP suite FTP server for Win3.1 */
+ /* old NetManage Chameleon TCP/IP suite FTP server for Win3.1 */
+ /*
+ * SuperTCP dirlist from the mirror.pl project
+ * mon/day/year separator may be '/' or '-'.
+ * . <DIR> 11-16-94 17:16
+ * .. <DIR> 11-16-94 17:16
+ * INSTALL <DIR> 11-16-94 17:17
+ * CMT <DIR> 11-21-94 10:17
+ * DESIGN1.DOC 11264 05-11-95 14:20
+ * README.TXT 1045 05-10-95 11:01
+ * WPKIT1.EXE 960338 06-21-95 17:01
+ * CMT.CSV 0 07-06-95 14:56
+ *
+ * Chameleon dirlist guessed from lynx
+ * . <DIR> Nov 16 1994 17:16
+ * .. <DIR> Nov 16 1994 17:16
+ * INSTALL <DIR> Nov 16 1994 17:17
+ * CMT <DIR> Nov 21 1994 10:17
+ * DESIGN1.DOC 11264 May 11 1995 14:20 A
+ * README.TXT 1045 May 10 1995 11:01
+ * WPKIT1.EXE 960338 Jun 21 1995 17:01 R
+ * CMT.CSV 0 Jul 06 1995 14:56 RHA
+ */
+ if (numtoks >= 4 && toklen[0] < 13 &&
+ ((toklen[1] == 5 && *tokens[1] == '<') || isdigit(*tokens[1])) )
+ {
+ if (numtoks == 4
+ && (toklen[2] == 8 || toklen[2] == 9)
+ && (((tokens[2][2]) == '/' && (tokens[2][5]) == '/') ||
+ ((tokens[2][2]) == '-' && (tokens[2][5]) == '-'))
+ && (toklen[3] == 4 || toklen[3] == 5)
+ && (tokens[3][toklen[3]-3]) == ':'
+ && isdigit(tokens[2][0]) && isdigit(tokens[2][1])
+ && isdigit(tokens[2][3]) && isdigit(tokens[2][4])
+ && isdigit(tokens[2][6]) && isdigit(tokens[2][7])
+ && (toklen[2] < 9 || isdigit(tokens[2][8]))
+ && isdigit(tokens[3][toklen[3]-1]) && isdigit(tokens[3][toklen[3]-2])
+ && isdigit(tokens[3][toklen[3]-4]) && isdigit(*tokens[3])
+ )
+ {
+ lstyle = 'w';
+ }
+ else if ((numtoks == 6 || numtoks == 7)
+ && toklen[2] == 3 && toklen[3] == 2
+ && toklen[4] == 4 && toklen[5] == 5
+ && (tokens[5][2]) == ':'
+ && isalpha(tokens[2][0]) && isalpha(tokens[2][1])
+ && isalpha(tokens[2][2])
+ && isdigit(tokens[3][0]) && isdigit(tokens[3][1])
+ && isdigit(tokens[4][0]) && isdigit(tokens[4][1])
+ && isdigit(tokens[4][2]) && isdigit(tokens[4][3])
+ && isdigit(tokens[5][0]) && isdigit(tokens[5][1])
+ && isdigit(tokens[5][3]) && isdigit(tokens[5][4])
+ /* could also check that (&(tokens[5][5]) - tokens[2]) == 17 */
+ )
+ {
+ lstyle = 'w';
+ }
+ if (lstyle && state->lstyle != lstyle) /* first time */
+ {
+ p = tokens[1];
+ if (toklen[1] != 5 || p[0] != '<' || p[1] != 'D' ||
+ p[2] != 'I' || p[3] != 'R' || p[4] != '>')
+ {
+ for (pos = 0; lstyle && pos < toklen[1]; pos++)
+ {
+ if (!isdigit(*p++))
+ lstyle = 0;
+ }
+ } /* not <DIR> */
+ } /* if (first time) */
+ } /* if (numtoks == ...) */
+
+ if (lstyle == 'w')
+ {
+ state->parsed_one = 1;
+ state->lstyle = lstyle;
+
+ result->fe_cinfs = 1;
+ result->fe_fname = tokens[0];
+ result->fe_fnlen = toklen[0];
+ result->fe_type = 'd';
+
+ p = tokens[1];
+ if (isdigit(*p))
+ {
+ result->fe_type = 'f';
+ pos = toklen[1];
+ if (pos > (sizeof(result->fe_size)-1))
+ pos = sizeof(result->fe_size)-1;
+ memcpy( result->fe_size, p, pos );
+ result->fe_size[pos] = '\0';
+ }
+
+ p = tokens[2];
+ if (toklen[2] == 3) /* Chameleon */
+ {
+ tbuf[0] = toupper(p[0]);
+ tbuf[1] = tolower(p[1]);
+ tbuf[2] = tolower(p[2]);
+ for (pos = 0; pos < (12*3); pos+=3)
+ {
+ if (tbuf[0] == month_names[pos+0] &&
+ tbuf[1] == month_names[pos+1] &&
+ tbuf[2] == month_names[pos+2])
+ {
+ result->fe_time.tm_mon = pos/3;
+ result->fe_time.tm_mday = atoi(tokens[3]);
+ result->fe_time.tm_year = atoi(tokens[4]) - 1900;
+ break;
+ }
+ }
+ pos = 5; /* Chameleon toknum of date field */
+ }
+ else
+ {
+ result->fe_time.tm_mon = atoi(p+0)-1;
+ result->fe_time.tm_mday = atoi(p+3);
+ result->fe_time.tm_year = atoi(p+6);
+ if (result->fe_time.tm_year < 80) /* SuperTCP */
+ result->fe_time.tm_year += 100;
+
+ pos = 3; /* SuperTCP toknum of date field */
+ }
+
+ result->fe_time.tm_hour = atoi(tokens[pos]);
+ result->fe_time.tm_min = atoi(&(tokens[pos][toklen[pos]-2]));
+
+ /* the caller should do this (if dropping "." and ".." is desired)
+ if (result->fe_type == 'd' && result->fe_fname[0] == '.' &&
+ (result->fe_fnlen == 1 || (result->fe_fnlen == 2 &&
+ result->fe_fname[1] == '.')))
+ return '?';
+ */
+
+ return result->fe_type;
+ } /* (lstyle == 'w') */
+
+ } /* if (!lstyle && (!state->lstyle || state->lstyle == 'w')) */
+#endif
+
+ /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
+
+#if defined(SUPPORT_DLS) /* dls -dtR */
+ if (!lstyle &&
+ (state->lstyle == 'D' || (!state->lstyle && state->numlines == 1)))
+ /* /bin/dls lines have to be immediately recognizable (first line) */
+ {
+ /* I haven't seen an FTP server that delivers a /bin/dls listing,
+ * but can infer the format from the lynx and mirror.pl projects.
+ * Both formats are supported.
+ *
+ * Lynx says:
+ * README 763 Information about this server\0
+ * bin/ - \0
+ * etc/ = \0
+ * ls-lR 0 \0
+ * ls-lR.Z 3 \0
+ * pub/ = Public area\0
+ * usr/ - \0
+ * morgan 14 -> ../real/morgan\0
+ * TIMIT.mostlikely.Z\0
+ * 79215 \0
+ *
+ * mirror.pl says:
+ * filename: ^(\S*)\s+
+ * size: (\-|\=|\d+)\s+
+ * month/day: ((\w\w\w\s+\d+|\d+\s+\w\w\w)\s+
+ * time/year: (\d+:\d+|\d\d\d\d))\s+
+ * rest: (.+)
+ *
+ * README 763 Jul 11 21:05 Information about this server
+ * bin/ - Apr 28 1994
+ * etc/ = 11 Jul 21:04
+ * ls-lR 0 6 Aug 17:14
+ * ls-lR.Z 3 05 Sep 1994
+ * pub/ = Jul 11 21:04 Public area
+ * usr/ - Sep 7 09:39
+ * morgan 14 Apr 18 09:39 -> ../real/morgan
+ * TIMIT.mostlikely.Z
+ * 79215 Jul 11 21:04
+ */
+ if (!state->lstyle && line[linelen-1] == ':' &&
+ linelen >= 2 && toklen[numtoks-1] != 1)
+ {
+ /* code in mirror.pl suggests that a listing may be preceded
+ * by a PWD line in the form "/some/dir/names/here:"
+ * but does not necessarily begin with '/'. *sigh*
+ */
+ pos = 0;
+ p = line;
+ while (pos < (linelen-1))
+ {
+ /* illegal (or extremely unusual) chars in a dirspec */
+ if (*p == '<' || *p == '|' || *p == '>' ||
+ *p == '?' || *p == '*' || *p == '\\')
+ break;
+ if (*p == '/' && pos < (linelen-2) && p[1] == '/')
+ break;
+ pos++;
+ p++;
+ }
+ if (pos == (linelen-1))
+ {
+ state->lstyle = 'D';
+ return '?';
+ }
+ }
+
+ if (!lstyle && numtoks >= 2)
+ {
+ pos = 22; /* pos of (\d+|-|=) if this is not part of a multiline */
+ if (state->lstyle && carry_buf_len) /* first is from previous line */
+ pos = toklen[1]-1; /* and is 'as-is' (may contain whitespace) */
+
+ if (linelen > pos)
+ {
+ p = &line[pos];
+ if ((*p == '-' || *p == '=' || isdigit(*p)) &&
+ ((linelen == (pos+1)) ||
+ (linelen >= (pos+3) && p[1] == ' ' && p[2] == ' ')) )
+ {
+ tokmarker = 1;
+ if (!carry_buf_len)
+ {
+ pos = 1;
+ while (pos < numtoks && (tokens[pos]+toklen[pos]) < (&line[23]))
+ pos++;
+ tokmarker = 0;
+ if ((tokens[pos]+toklen[pos]) == (&line[23]))
+ tokmarker = pos;
+ }
+ if (tokmarker)
+ {
+ lstyle = 'D';
+ if (*tokens[tokmarker] == '-' || *tokens[tokmarker] == '=')
+ {
+ if (toklen[tokmarker] != 1 ||
+ (tokens[tokmarker-1][toklen[tokmarker-1]-1]) != '/')
+ lstyle = 0;
+ }
+ else
+ {
+ for (pos = 0; lstyle && pos < toklen[tokmarker]; pos++)
+ {
+ if (!isdigit(tokens[tokmarker][pos]))
+ lstyle = 0;
+ }
+ }
+ if (lstyle && !state->lstyle) /* first time */
+ {
+ /* scan for illegal (or incredibly unusual) chars in fname */
+ for (p = tokens[0]; lstyle &&
+ p < &(tokens[tokmarker-1][toklen[tokmarker-1]]); p++)
+ {
+ if (*p == '<' || *p == '|' || *p == '>' ||
+ *p == '?' || *p == '*' || *p == '/' || *p == '\\')
+ lstyle = 0;
+ }
+ }
+
+ } /* size token found */
+ } /* expected chars behind expected size token */
+ } /* if (linelen > pos) */
+ } /* if (!lstyle && numtoks >= 2) */
+
+ if (!lstyle && state->lstyle == 'D' && !carry_buf_len)
+ {
+ /* the filename of a multi-line entry can be identified
+ * correctly only if dls format had been previously established.
+ * This should always be true because there should be entries
+ * for '.' and/or '..' and/or CWD that precede the rest of the
+ * listing.
+ */
+ pos = linelen;
+ if (pos > (sizeof(state->carry_buf)-1))
+ pos = sizeof(state->carry_buf)-1;
+ memcpy( state->carry_buf, line, pos );
+ state->carry_buf_len = pos;
+ return '?';
+ }
+
+ if (lstyle == 'D')
+ {
+ state->parsed_one = 1;
+ state->lstyle = lstyle;
+
+ p = &(tokens[tokmarker-1][toklen[tokmarker-1]]);
+ result->fe_fname = tokens[0];
+ result->fe_fnlen = p - tokens[0];
+ result->fe_type = 'f';
+
+ if (result->fe_fname[result->fe_fnlen-1] == '/')
+ {
+ if (result->fe_lnlen == 1)
+ result->fe_type = '?';
+ else
+ {
+ result->fe_fnlen--;
+ result->fe_type = 'd';
+ }
+ }
+ else if (isdigit(*tokens[tokmarker]))
+ {
+ pos = toklen[tokmarker];
+ if (pos > (sizeof(result->fe_size)-1))
+ pos = sizeof(result->fe_size)-1;
+ memcpy( result->fe_size, tokens[tokmarker], pos );
+ result->fe_size[pos] = '\0';
+ }
+
+ if ((tokmarker+3) < numtoks &&
+ (&(tokens[numtoks-1][toklen[numtoks-1]]) -
+ tokens[tokmarker+1]) >= (1+1+3+1+4) )
+ {
+ pos = (tokmarker+3);
+ p = tokens[pos];
+ pos = toklen[pos];
+
+ if ((pos == 4 || pos == 5)
+ && isdigit(*p) && isdigit(p[pos-1]) && isdigit(p[pos-2])
+ && ((pos == 5 && p[2] == ':') ||
+ (pos == 4 && (isdigit(p[1]) || p[1] == ':')))
+ )
+ {
+ month_num = tokmarker+1; /* assumed position of month field */
+ pos = tokmarker+2; /* assumed position of mday field */
+ if (isdigit(*tokens[month_num])) /* positions are reversed */
+ {
+ month_num++;
+ pos--;
+ }
+ p = tokens[month_num];
+ if (isdigit(*tokens[pos])
+ && (toklen[pos] == 1 ||
+ (toklen[pos] == 2 && isdigit(tokens[pos][1])))
+ && toklen[month_num] == 3
+ && isalpha(*p) && isalpha(p[1]) && isalpha(p[2]) )
+ {
+ pos = atoi(tokens[pos]);
+ if (pos > 0 && pos <= 31)
+ {
+ result->fe_time.tm_mday = pos;
+ month_num = 1;
+ for (pos = 0; pos < (12*3); pos+=3)
+ {
+ if (p[0] == month_names[pos+0] &&
+ p[1] == month_names[pos+1] &&
+ p[2] == month_names[pos+2])
+ break;
+ month_num++;
+ }
+ if (month_num > 12)
+ result->fe_time.tm_mday = 0;
+ else
+ result->fe_time.tm_mon = month_num - 1;
+ }
+ }
+ if (result->fe_time.tm_mday)
+ {
+ tokmarker += 3; /* skip mday/mon/yrtime (to find " -> ") */
+ p = tokens[tokmarker];
+
+ pos = atoi(p);
+ if (pos > 24)
+ result->fe_time.tm_year = pos-1900;
+ else
+ {
+ if (p[1] == ':')
+ p--;
+ result->fe_time.tm_hour = pos;
+ result->fe_time.tm_min = atoi(p+3);
+ if (!state->now_time)
+ {
+ state->now_time = time (NULL);
+ state->now_tm = *localtime (&state->now_time);
+ }
+ result->fe_time.tm_year = state->now_tm.tm_year;
+ if ( (( state->now_tm.tm_mon << 4) + state->now_tm.tm_mday) <
+ ((result->fe_time.tm_mon << 4) + result->fe_time.tm_mday) )
+ result->fe_time.tm_year--;
+ } /* got year or time */
+ } /* got month/mday */
+ } /* may have year or time */
+ } /* enough remaining to possibly have date/time */
+
+ if (numtoks > (tokmarker+2))
+ {
+ pos = tokmarker+1;
+ p = tokens[pos];
+ if (toklen[pos] == 2 && *p == '-' && p[1] == '>')
+ {
+ p = &(tokens[numtoks-1][toklen[numtoks-1]]);
+ result->fe_type = 'l';
+ result->fe_lname = tokens[pos+1];
+ result->fe_lnlen = p - result->fe_lname;
+ if (result->fe_lnlen > 1 &&
+ result->fe_lname[result->fe_lnlen-1] == '/')
+ result->fe_lnlen--;
+ }
+ } /* if (numtoks > (tokmarker+2)) */
+
+ /* the caller should do this (if dropping "." and ".." is desired)
+ if (result->fe_type == 'd' && result->fe_fname[0] == '.' &&
+ (result->fe_fnlen == 1 || (result->fe_fnlen == 2 &&
+ result->fe_fname[1] == '.')))
+ return '?';
+ */
+
+ return result->fe_type;
+
+ } /* if (lstyle == 'D') */
+ } /* if (!lstyle && (!state->lstyle || state->lstyle == 'D')) */
+#endif
+
+ /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
+
+ } /* if (linelen > 0) */
+
+ if (state->parsed_one || state->lstyle) /* junk if we fail to parse */
+ return '?'; /* this time but had previously parsed successfully */
+ return '"'; /* its part of a comment or error message */
+}
+
+/* ==================================================================== */
+/* standalone testing */
+/* ==================================================================== */
+#if 0
+
+#include <stdio.h>
+
+static int do_it(FILE *outfile,
+ char *line, size_t linelen, struct list_state *state,
+ char **cmnt_buf, unsigned int *cmnt_buf_sz,
+ char **list_buf, unsigned int *list_buf_sz )
+{
+ struct list_result result;
+ char *p;
+ int rc;
+
+ rc = ParseFTPLIST( line, state, &result );
+
+ if (!outfile)
+ {
+ outfile = stdout;
+ if (rc == '?')
+ fprintf(outfile, "junk: %.*s\n", (int)linelen, line );
+ else if (rc == '"')
+ fprintf(outfile, "cmnt: %.*s\n", (int)linelen, line );
+ else
+ fprintf(outfile,
+ "list: %02u-%02u-%02u %02u:%02u%cM %20s %.*s%s%.*s\n",
+ (result.fe_time.tm_mday ? (result.fe_time.tm_mon + 1) : 0),
+ result.fe_time.tm_mday,
+ (result.fe_time.tm_mday ? (result.fe_time.tm_year % 100) : 0),
+ result.fe_time.tm_hour -
+ ((result.fe_time.tm_hour > 12)?(12):(0)),
+ result.fe_time.tm_min,
+ ((result.fe_time.tm_hour >= 12) ? 'P' : 'A'),
+ (rc == 'd' ? "<DIR> " :
+ (rc == 'l' ? "<JUNCTION> " : result.fe_size)),
+ (int)result.fe_fnlen, result.fe_fname,
+ ((rc == 'l' && result.fe_lnlen) ? " -> " : ""),
+ (int)((rc == 'l' && result.fe_lnlen) ? result.fe_lnlen : 0),
+ ((rc == 'l' && result.fe_lnlen) ? result.fe_lname : "") );
+ }
+ else if (rc != '?') /* NOT junk */
+ {
+ char **bufp = list_buf;
+ unsigned int *bufz = list_buf_sz;
+
+ if (rc == '"') /* comment - make it a 'result' */
+ {
+ memset( &result, 0, sizeof(result));
+ result.fe_fname = line;
+ result.fe_fnlen = linelen;
+ result.fe_type = 'f';
+ if (line[linelen-1] == '/')
+ {
+ result.fe_type = 'd';
+ result.fe_fnlen--;
+ }
+ bufp = cmnt_buf;
+ bufz = cmnt_buf_sz;
+ rc = result.fe_type;
+ }
+
+ linelen = 80 + result.fe_fnlen + result.fe_lnlen;
+ p = (char *)realloc( *bufp, *bufz + linelen );
+ if (!p)
+ return -1;
+ sprintf( &p[*bufz],
+ "%02u-%02u-%04u %02u:%02u:%02u %20s %.*s%s%.*s\n",
+ (result.fe_time.tm_mday ? (result.fe_time.tm_mon + 1) : 0),
+ result.fe_time.tm_mday,
+ (result.fe_time.tm_mday ? (result.fe_time.tm_year + 1900) : 0),
+ result.fe_time.tm_hour,
+ result.fe_time.tm_min,
+ result.fe_time.tm_sec,
+ (rc == 'd' ? "<DIR> " :
+ (rc == 'l' ? "<JUNCTION> " : result.fe_size)),
+ (int)result.fe_fnlen, result.fe_fname,
+ ((rc == 'l' && result.fe_lnlen) ? " -> " : ""),
+ (int)((rc == 'l' && result.fe_lnlen) ? result.fe_lnlen : 0),
+ ((rc == 'l' && result.fe_lnlen) ? result.fe_lname : "") );
+ linelen = strlen(&p[*bufz]);
+ *bufp = p;
+ *bufz = *bufz + linelen;
+ }
+ return 0;
+}
+
+int main(int argc, char *argv[])
+{
+ FILE *infile = (FILE *)0;
+ FILE *outfile = (FILE *)0;
+ int need_close_in = 0;
+ int need_close_out = 0;
+
+ if (argc > 1)
+ {
+ infile = stdin;
+ if (strcmp(argv[1], "-") == 0)
+ need_close_in = 0;
+ else if ((infile = fopen(argv[1], "r")) != ((FILE *)0))
+ need_close_in = 1;
+ else
+ fprintf(stderr, "Unable to open input file '%s'\n", argv[1]);
+ }
+ if (infile && argc > 2)
+ {
+ outfile = stdout;
+ if (strcmp(argv[2], "-") == 0)
+ need_close_out = 0;
+ else if ((outfile = fopen(argv[2], "w")) != ((FILE *)0))
+ need_close_out = 1;
+ else
+ {
+ fprintf(stderr, "Unable to open output file '%s'\n", argv[2]);
+ fclose(infile);
+ infile = (FILE *)0;
+ }
+ }
+
+ if (!infile)
+ {
+ char *appname = &(argv[0][strlen(argv[0])]);
+ while (appname > argv[0])
+ {
+ appname--;
+ if (*appname == '/' || *appname == '\\' || *appname == ':')
+ {
+ appname++;
+ break;
+ }
+ }
+ fprintf(stderr,
+ "Usage: %s <inputfilename> [<outputfilename>]\n"
+ "\nIf an outout file is specified the results will be"
+ "\nbe post-processed, and only the file entries will appear"
+ "\n(or all comments if there are no file entries)."
+ "\nNot specifying an output file causes %s to run in \"debug\""
+ "\nmode, ie results are printed as lines are parsed."
+ "\nIf a filename is a single dash ('-'), stdin/stdout is used."
+ "\n", appname, appname );
+ }
+ else
+ {
+ char *cmnt_buf = (char *)0;
+ unsigned int cmnt_buf_sz = 0;
+ char *list_buf = (char *)0;
+ unsigned int list_buf_sz = 0;
+
+ struct list_state state;
+ char line[512];
+
+ memset( &state, 0, sizeof(state) );
+ while (fgets(line, sizeof(line), infile))
+ {
+ size_t linelen = strlen(line);
+ if (linelen < (sizeof(line)-1))
+ {
+ if (linelen > 0 && line[linelen-1] == '\n')
+ linelen--;
+ if (do_it( outfile, line, linelen, &state,
+ &cmnt_buf, &cmnt_buf_sz, &list_buf, &list_buf_sz) != 0)
+ {
+ fprintf(stderr, "Insufficient memory. Listing may be incomplete.\n");
+ break;
+ }
+ }
+ else
+ {
+ /* no '\n' found. drop this and everything up to the next '\n' */
+ fprintf(stderr, "drop: %.*s", (int)linelen, line );
+ while (linelen == sizeof(line))
+ {
+ if (!fgets(line, sizeof(line), infile))
+ break;
+ linelen = 0;
+ while (linelen < sizeof(line) && line[linelen] != '\n')
+ linelen++;
+ fprintf(stderr, "%.*s", (int)linelen, line );
+ }
+ fprintf(stderr, "\n");
+ }
+ }
+ if (outfile)
+ {
+ if (list_buf)
+ fwrite( list_buf, 1, list_buf_sz, outfile );
+ else if (cmnt_buf)
+ fwrite( cmnt_buf, 1, cmnt_buf_sz, outfile );
+ }
+ if (list_buf)
+ free(list_buf);
+ if (cmnt_buf)
+ free(cmnt_buf);
+
+ if (need_close_in)
+ fclose(infile);
+ if (outfile && need_close_out)
+ fclose(outfile);
+ }
+
+ return 0;
+}
+#endif
diff --git a/libsoup/ParseFTPList.h b/libsoup/ParseFTPList.h
new file mode 100644
index 0000000..5580657
--- /dev/null
+++ b/libsoup/ParseFTPList.h
@@ -0,0 +1,127 @@
+/* ***** BEGIN LICENSE BLOCK *****
+ * Version: MPL 1.1/GPL 2.0/LGPL 2.1
+ *
+ * The contents of this file are subject to the Mozilla Public License Version
+ * 1.1 (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ * http://www.mozilla.org/MPL/
+ *
+ * Software distributed under the License is distributed on an "AS IS" basis,
+ * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
+ * for the specific language governing rights and limitations under the
+ * License.
+ *
+ * The Original Code is mozilla.org Code.
+ *
+ * The Initial Developer of the Original Code is
+ * Cyrus Patel <cyp fb14 uni-mainz de>.
+ * Portions created by the Initial Developer are Copyright (C) 2002
+ * the Initial Developer. All Rights Reserved.
+ *
+ * Contributor(s):
+ * Doug Turner <dougt netscape com>
+ *
+ * Alternatively, the contents of this file may be used under the terms of
+ * either the GNU General Public License Version 2 or later (the "GPL"), or
+ * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
+ * in which case the provisions of the GPL or the LGPL are applicable instead
+ * of those above. If you wish to allow use of your version of this file only
+ * under the terms of either the GPL or the LGPL, and not to allow others to
+ * use your version of this file under the terms of the MPL, indicate your
+ * decision by deleting the provisions above and replace them with the notice
+ * and other provisions required by the GPL or the LGPL. If you do not delete
+ * the provisions above, a recipient may use your version of this file under
+ * the terms of any one of the MPL, the GPL or the LGPL.
+ *
+ * ***** END LICENSE BLOCK ***** */
+
+#include <glib.h>
+
+/* ParseFTPList() parses lines from an FTP LIST command.
+**
+** Written July 2002 by Cyrus Patel <cyp fb14 uni-mainz de>
+** with acknowledgements to squid, lynx, wget and ftpmirror.
+**
+** Arguments:
+** 'line': line of FTP data connection output. The line is assumed
+** to end at the first '\0' or '\n' or '\r\n'.
+** 'state': a structure used internally to track state between
+** lines. Needs to be bzero()'d at LIST begin.
+** 'result': where ParseFTPList will store the results of the parse
+** if 'line' is not a comment and is not junk.
+**
+** Returns one of the following:
+** 'd' - LIST line is a directory entry ('result' is valid)
+** 'f' - LIST line is a file's entry ('result' is valid)
+** 'l' - LIST line is a symlink's entry ('result' is valid)
+** '?' - LIST line is junk. (cwd, non-file/dir/link, etc)
+** '"' - its not a LIST line (its a "comment")
+**
+** It may be advisable to let the end-user see "comments" (particularly when
+** the listing results in ONLY such lines) because such a listing may be:
+** - an unknown LIST format (NLST or "custom" format for example)
+** - an error msg (EPERM,ENOENT,ENFILE,EMFILE,ENOTDIR,ENOTBLK,EEXDEV etc).
+** - an empty directory and the 'comment' is a "total 0" line or similar.
+** (warning: a "total 0" can also mean the total size is unknown).
+**
+** ParseFTPList() supports all known FTP LISTing formats:
+** - '/bin/ls -l' and all variants (including Hellsoft FTP for NetWare);
+** - EPLF (Easily Parsable List Format);
+** - Windows NT's default "DOS-dirstyle";
+** - OS/2 basic server format LIST format;
+** - VMS (MultiNet, UCX, and CMU) LIST format (including multi-line format);
+** - IBM VM/CMS, VM/ESA LIST format (two known variants);
+** - SuperTCP FTP Server for Win16 LIST format;
+** - NetManage Chameleon (NEWT) for Win16 LIST format;
+** - '/bin/dls' (two known variants, plus multi-line) LIST format;
+** If there are others, then I'd like to hear about them (send me a sample).
+**
+** NLSTings are not supported explicitely because they cannot be machine
+** parsed consistently: NLSTings do not have unique characteristics - even
+** the assumption that there won't be whitespace on the line does not hold
+** because some nlistings have more than one filename per line and/or
+** may have filenames that have spaces in them. Moreover, distinguishing
+** between an error message and an NLST line would require ParseList() to
+** recognize all the possible strerror() messages in the world.
+*/
+
+
+/* #undef anything you don't want to support */
+#define SUPPORT_LSL /* /bin/ls -l and dozens of variations therof */
+#define SUPPORT_DLS /* /bin/dls format (very, Very, VERY rare) */
+#define SUPPORT_EPLF /* Extraordinarily Pathetic List Format */
+#define SUPPORT_DOS /* WinNT server in 'site dirstyle' dos */
+#define SUPPORT_VMS /* VMS (all: MultiNet, UCX, CMU-IP) */
+#define SUPPORT_CMS /* IBM VM/CMS,VM/ESA (z/VM and LISTING forms) */
+#define SUPPORT_OS2 /* IBM TCP/IP for OS/2 - FTP Server */
+#define SUPPORT_W16 /* win16 hosts: SuperTCP or NetManage Chameleon */
+
+struct list_state
+{
+ void *magic; /* to determine if previously initialized */
+ time_t now_time; /* current date for year calculation */
+ struct tm now_tm; /* current date for year calculation */
+ gint32 lstyle; /* LISTing style */
+ gint32 parsed_one; /* returned anything yet? */
+ char carry_buf[84]; /* for VMS multiline */
+ guint32 carry_buf_len; /* length of name in carry_buf */
+ guint32 numlines; /* number of lines seen */
+};
+
+struct list_result
+{
+ gint32 fe_type; /* 'd'(dir) or 'l'(link) or 'f'(file) */
+ const char * fe_fname; /* pointer to filename */
+ guint32 fe_fnlen; /* length of filename */
+ const char * fe_lname; /* pointer to symlink name */
+ guint32 fe_lnlen; /* length of symlink name */
+ char fe_size[40]; /* size of file in bytes (<= (2^128 - 1)) */
+ struct tm fe_time; /* last-modified time */
+ gint32 fe_cinfs; /* file system is definitely case insensitive */
+ /* (converting all-upcase names may be desirable) */
+};
+
+int ParseFTPList(const char *line,
+ struct list_state *state,
+ struct list_result *result );
+
diff --git a/libsoup/soup-ftp-connection.c b/libsoup/soup-ftp-connection.c
new file mode 100644
index 0000000..ece6e27
--- /dev/null
+++ b/libsoup/soup-ftp-connection.c
@@ -0,0 +1,1778 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
+/*
+ * soup-ftp-connection.c: ftp connection
+ *
+ * Copyright (C) 2009 FIXME
+ * Copyright (C) 2009 Red Hat, Inc.
+ */
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <string.h>
+#include <stdlib.h>
+#include <time.h>
+
+#include "soup-ftp-connection.h"
+#include "soup-ftp-input-stream.h"
+#include "soup-misc.h"
+#include "soup-uri.h"
+#include "ParseFTPList.h"
+
+/* TODO:
+ * Remove GCancellable parameter from internal methods and use
+ * context->cancellable
+ */
+
+struct _SoupFTPConnectionPrivate
+{
+ SoupURI *uri;
+
+ guint16 features;
+
+ /* not needed but, if unref, it close control_input & control_output streams */
+ GSocketConnection *control;
+ GDataInputStream *control_input;
+ GOutputStream *control_output;
+
+ /* not needed but, if unref, it close input returned to caller */
+ GSocketConnection *data;
+
+ char *working_directory;
+
+ GCancellable *cancellable;
+};
+
+typedef enum {
+ SOUP_FTP_CONNECTION_FEATURE_MDTM = 1 << 0,
+ SOUP_FTP_CONNECTION_FEATURE_SIZE = 1 << 1,
+ SOUP_FTP_CONNECTION_FEATURE_REST = 1 << 2,
+ SOUP_FTP_CONNECTION_FEATURE_TVFS = 1 << 3,
+ SOUP_FTP_CONNECTION_FEATURE_MLST = 1 << 4,
+ SOUP_FTP_CONNECTION_FEATURE_MLSD = 1 << 5,
+ SOUP_FTP_CONNECTION_FEATURE_EPRT = 1 << 6,
+ SOUP_FTP_CONNECTION_FEATURE_EPSV = 1 << 7,
+ SOUP_FTP_CONNECTION_FEATURE_UTF8 = 1 << 8
+} SoupFTPConnectionFeature;
+
+typedef struct {
+ guint16 code;
+ char *message;
+} SoupFTPConnectionReply;
+
+typedef enum {
+ SOUP_FTP_NONE,
+ SOUP_FTP_BAD_ANSWER,
+ SOUP_FTP_INVALID_PATH,
+ SOUP_FTP_ACTIVE_NOT_IMPLEMENTED,
+ SOUP_FTP_LOGIN_ERROR,
+ SOUP_FTP_SERVICE_UNAVAILABLE
+} SoupFTPConnectionError;
+
+#define SOUP_PARSE_FTP_STATUS(buffer) (g_ascii_digit_value (buffer[0]) * 100 \
+ + g_ascii_digit_value (buffer[1]) * 10 \
+ + g_ascii_digit_value (buffer[2]))
+#define REPLY_IS_POSITIVE_PRELIMINARY(reply) (reply->code / 100 == 1 ? TRUE : FALSE)
+#define REPLY_IS_POSITIVE_COMPLETION(reply) (reply->code / 100 == 2 ? TRUE : FALSE)
+#define REPLY_IS_POSITIVE_INTERMEDIATE(reply) (reply->code / 100 == 3 ? TRUE : FALSE)
+#define REPLY_IS_NEGATIVE_TRANSIENT(reply) (reply->code / 100 == 4 ? TRUE : FALSE)
+#define REPLY_IS_NEGATIVE_PERMANENT(reply) (reply->code / 100 == 5 ? TRUE : FALSE)
+#define REPLY_IS_ABOUT_SYNTAX(reply) ((reply->code % 100) / 10 == 0 ? TRUE : FALSE)
+#define REPLY_IS_ABOUT_INFORMATION(reply) ((reply->code % 100) / 10 == 1 ? TRUE : FALSE)
+#define REPLY_IS_ABOUT_CONNECTION(reply) ((reply->code % 100) / 10 == 2 ? TRUE : FALSE)
+#define REPLY_IS_ABOUT_AUTHENTICATION(reply) ((reply->code % 100) / 10 == 3 ? TRUE : FALSE)
+#define REPLY_IS_ABOUT_UNSPECIFIED(reply) ((reply->code % 100) / 10 == 4 ? TRUE : FALSE)
+#define REPLY_IS_ABOUT_FILE_SYSTEM(reply) ((reply->code % 100) / 10 == 5 ? TRUE : FALSE)
+
+G_DEFINE_TYPE (SoupFTPConnection, soup_ftp_connection, G_TYPE_OBJECT);
+
+/* async callbacks */
+void ftp_callback_pass (GObject *source_object,
+ GAsyncResult *res,
+ gpointer user_data);
+void ftp_callback_feat (GObject *source_object,
+ GAsyncResult *res,
+ gpointer user_data);
+void ftp_callback_pasv (GObject *source_object,
+ GAsyncResult *res,
+ gpointer user_data);
+void ftp_callback_data (GObject *source_object,
+ GAsyncResult *res,
+ gpointer user_data);
+void ftp_callback_retr (GObject *source_object,
+ GAsyncResult *res,
+ gpointer user_data);
+
+static void
+soup_ftp_connection_finalize (GObject *object)
+{
+ SoupFTPConnection *ftp = SOUP_FTP_CONNECTION (object);
+
+ // TODO : close correctly the connection (QUIT)
+
+ if (ftp->priv->uri)
+ soup_uri_free (ftp->priv->uri);
+ if (ftp->priv->control)
+ g_object_unref (ftp->priv->control);
+ if (ftp->priv->data)
+ g_object_unref (ftp->priv->data);
+
+ G_OBJECT_CLASS (soup_ftp_connection_parent_class)->finalize (object);
+}
+
+static void
+soup_ftp_connection_class_init (SoupFTPConnectionClass *klass)
+{
+ GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
+
+ g_type_class_add_private (klass, sizeof (SoupFTPConnectionPrivate));
+
+ gobject_class->finalize = soup_ftp_connection_finalize;
+}
+
+static void
+soup_ftp_connection_init (SoupFTPConnection *ftp)
+{
+
+ ftp->priv = G_TYPE_INSTANCE_GET_PRIVATE (ftp, SOUP_TYPE_FTP_CONNECTION, SoupFTPConnectionPrivate);
+}
+
+SoupFTPConnection *
+soup_ftp_connection_new (void)
+{
+ return g_object_new (SOUP_TYPE_FTP_CONNECTION, NULL);
+}
+
+static void
+ftp_connection_reply_free (SoupFTPConnectionReply *reply)
+{
+ g_free (reply->message);
+ g_free (reply);
+}
+
+static SoupFTPConnectionReply *
+ftp_connection_reply_copy (SoupFTPConnectionReply *reply)
+{
+ SoupFTPConnectionReply *dup;
+
+ dup = g_malloc0 (sizeof (SoupFTPConnectionReply));
+ dup->message = g_strdup (reply->message);
+ dup->code = reply->code;
+
+ return dup;
+}
+
+static gboolean
+ftp_connection_check_reply (SoupFTPConnection *ftp,
+ SoupFTPConnectionReply *reply,
+ GError **error)
+{
+ g_return_val_if_fail (SOUP_IS_FTP_CONNECTION (ftp), FALSE);
+ g_return_val_if_fail (reply != NULL, FALSE);
+
+ if (REPLY_IS_POSITIVE_PRELIMINARY (reply) ||
+ REPLY_IS_POSITIVE_COMPLETION (reply) ||
+ REPLY_IS_POSITIVE_INTERMEDIATE (reply))
+ return TRUE;
+ else if (REPLY_IS_NEGATIVE_TRANSIENT (reply)) {
+ if (REPLY_IS_ABOUT_CONNECTION (reply)) {
+ g_set_error_literal (error,
+ G_IO_ERROR,
+ G_IO_ERROR_FAILED,
+ "FTP : Try again later (connection)");
+ } else if (REPLY_IS_ABOUT_FILE_SYSTEM (reply)) {
+ g_set_error_literal (error,
+ G_IO_ERROR,
+ G_IO_ERROR_FAILED,
+ "FTP : Try again later (file system)");
+ } else {
+ g_set_error (error,
+ G_IO_ERROR,
+ G_IO_ERROR_FAILED,
+ "FTP : Try again later (%u - %s)",
+ reply->code,
+ reply->message);
+ }
+ return FALSE;
+ } else if (REPLY_IS_NEGATIVE_PERMANENT (reply)) {
+ if (REPLY_IS_ABOUT_SYNTAX (reply)) {
+ g_set_error_literal (error,
+ G_IO_ERROR,
+ G_IO_ERROR_FAILED,
+ "FTP : Command failed (syntax)");
+ }
+ if (REPLY_IS_ABOUT_AUTHENTICATION (reply)) {
+ g_set_error_literal (error,
+ G_IO_ERROR,
+ G_IO_ERROR_FAILED,
+ "FTP : Authentication failed");
+ }
+ if (REPLY_IS_ABOUT_FILE_SYSTEM (reply)) {
+ g_set_error_literal (error,
+ G_IO_ERROR,
+ G_IO_ERROR_FAILED,
+ "FTP : File action failed (invalid path or no access allowed)");
+ } else {
+ g_set_error (error,
+ G_IO_ERROR,
+ G_IO_ERROR_FAILED,
+ "FTP : Fatal error (%u - %s)",
+ reply->code,
+ reply->message);
+ }
+ return FALSE;
+ } else {
+ g_set_error (error,
+ G_IO_ERROR,
+ G_IO_ERROR_FAILED,
+ "FTP : Fatal error (%u - %s)",
+ reply->code,
+ reply->message);
+ return FALSE;
+ }
+}
+
+static gboolean
+ftp_parse_feat_reply (SoupFTPConnection *ftp,
+ SoupFTPConnectionReply *reply)
+{
+ char **split, *feature;
+ int i, j;
+ const struct {
+ const char *name;
+ SoupFTPConnectionFeature enable;
+ } features [] = {
+ { "MDTM", SOUP_FTP_CONNECTION_FEATURE_MDTM },
+ { "SIZE", SOUP_FTP_CONNECTION_FEATURE_SIZE },
+ { "REST", SOUP_FTP_CONNECTION_FEATURE_REST },
+ { "TVFS", SOUP_FTP_CONNECTION_FEATURE_TVFS },
+ { "MLST", SOUP_FTP_CONNECTION_FEATURE_MLST },
+ { "MLSD", SOUP_FTP_CONNECTION_FEATURE_MLSD },
+ { "EPRT", SOUP_FTP_CONNECTION_FEATURE_EPRT },
+ { "EPSV", SOUP_FTP_CONNECTION_FEATURE_EPSV },
+ { "UTF8", SOUP_FTP_CONNECTION_FEATURE_UTF8 },
+ };
+
+ g_return_val_if_fail (SOUP_IS_FTP_CONNECTION (ftp), FALSE);
+ g_return_val_if_fail (reply != NULL, FALSE);
+
+ if (reply->code != 211)
+ return FALSE;
+ split = g_strsplit (reply->message, "\n", 0);
+ for (i = 1; split[i + 1]; ++i) {
+ if (!g_ascii_isspace (split[i][0])) {
+ ftp->priv->features = 0;
+ g_strfreev (split);
+ return FALSE;
+ }
+ feature = g_strstrip (split[i]);
+ for (j = 0; j < G_N_ELEMENTS (features); ++j) {
+ if (g_ascii_strncasecmp (feature, features[j].name, 4) == 0)
+ ftp->priv->features |= features[j].enable;
+ }
+ }
+ g_strfreev (split);
+
+ return TRUE;
+}
+
+static GSocketConnectable *
+ftp_connection_parse_pasv_reply (SoupFTPConnection *ftp,
+ SoupFTPConnectionReply *reply,
+ GError **error)
+{
+ GSocketConnectable *conn;
+ char **split;
+ char *hostname;
+ guint16 port;
+
+ if (!ftp_connection_check_reply (ftp, reply, error))
+ return NULL;
+
+ if (reply->code != 227) {
+ g_set_error (error,
+ G_IO_ERROR,
+ G_IO_ERROR_FAILED,
+ "FTP : Unexpected reply (%u - %s)",
+ reply->code,
+ reply->message);
+ return NULL;
+ } else {
+ // TODO : how to check if the split is fine
+ split = g_regex_split_simple ("([0-9]*),([0-9]*),([0-9]*),([0-9]*),([0-9]*),([0-9]*)",
+ reply->message,
+ G_REGEX_CASELESS,
+ G_REGEX_MATCH_NOTEMPTY);
+ hostname = g_strdup_printf ("%s.%s.%s.%s", split[1], split[2], split[3], split[4]);
+ port = 256 * atoi (split[5]) + atoi (split[6]);
+ conn = g_network_address_new (hostname, port);
+ g_strfreev (split);
+ g_free (hostname);
+
+ return conn;
+ }
+}
+
+static char *
+ftp_connection_parse_pwd_reply (SoupFTPConnection *ftp,
+ SoupFTPConnectionReply *reply,
+ GError **error)
+{
+ char *current_path;
+
+ if (!ftp_connection_check_reply (ftp, reply, error))
+ return NULL;
+
+ if (reply->code != 257) {
+ g_set_error (error,
+ G_IO_ERROR,
+ G_IO_ERROR_FAILED,
+ "FTP : Unexpected reply (%u - %s)",
+ reply->code,
+ reply->message);
+ return NULL;
+ }
+ current_path = g_strndup (reply->message + 1,
+ g_strrstr (reply->message, "\"") - reply->message - 1);
+
+ return current_path;
+}
+
+static gboolean
+ftp_connection_parse_welcome_reply (SoupFTPConnection *ftp,
+ SoupFTPConnectionReply *reply,
+ GError **error)
+{
+ if (!ftp_connection_check_reply (ftp, reply, error))
+ return FALSE;
+
+ if (reply->code == 120) {
+ g_set_error_literal (error,
+ G_IO_ERROR,
+ G_IO_ERROR_FAILED,
+ "FTP : Try again later (connection)");
+ return FALSE;
+ } else if (reply->code != 220) {
+ g_set_error (error,
+ G_IO_ERROR,
+ G_IO_ERROR_FAILED,
+ "FTP : Unexpected reply (%u - %s)",
+ reply->code,
+ reply->message);
+ return FALSE;
+ } else
+ return TRUE;
+}
+
+static gboolean
+ftp_connection_parse_user_reply (SoupFTPConnection *ftp,
+ SoupFTPConnectionReply *reply,
+ GError **error)
+{
+ if (!ftp_connection_check_reply (ftp, reply, error))
+ return FALSE;
+
+ if (reply->code == 332) {
+ g_set_error_literal (error,
+ G_IO_ERROR,
+ G_IO_ERROR_NOT_SUPPORTED,
+ "FTP : Account not implemented");
+ return FALSE;
+ } else if (reply->code != 230 && reply->code != 331) {
+ g_set_error (error,
+ G_IO_ERROR,
+ G_IO_ERROR_FAILED,
+ "FTP : Unexpected reply (%u - %s)",
+ reply->code,
+ reply->message);
+ return FALSE;
+ } else
+ return TRUE;
+}
+
+static gboolean
+ftp_connection_parse_pass_reply (SoupFTPConnection *ftp,
+ SoupFTPConnectionReply *reply,
+ GError **error)
+{
+ if (!ftp_connection_check_reply (ftp, reply, error))
+ return FALSE;
+
+ if (reply->code == 332) {
+ g_set_error_literal (error,
+ G_IO_ERROR,
+ G_IO_ERROR_NOT_SUPPORTED,
+ "FTP : Account not implemented");
+ return FALSE;
+ } else if (reply->code != 202 && reply->code != 230) {
+ g_set_error (error,
+ G_IO_ERROR,
+ G_IO_ERROR_FAILED,
+ "FTP : Unexpected reply (%u - %s)",
+ reply->code,
+ reply->message);
+ return FALSE;
+ } else
+ return TRUE;
+}
+
+static gboolean
+ftp_connection_parse_cwd_reply (SoupFTPConnection *ftp,
+ SoupFTPConnectionReply *reply,
+ GError **error)
+{
+ if (!ftp_connection_check_reply (ftp, reply, error))
+ return FALSE;
+
+ if (reply->code != 250) {
+ g_set_error (error,
+ G_IO_ERROR,
+ G_IO_ERROR_FAILED,
+ "FTP : Unexpected reply (%u - %s)",
+ reply->code,
+ reply->message);
+ return FALSE;
+ } else
+ return TRUE;
+}
+
+static gboolean
+ftp_connection_parse_retr_reply (SoupFTPConnection *ftp,
+ SoupFTPConnectionReply *reply,
+ GError **error)
+{
+ if (!ftp_connection_check_reply (ftp, reply, error))
+ return FALSE;
+
+ if (reply->code != 226 && reply->code != 250) {
+ g_set_error (error,
+ G_IO_ERROR,
+ G_IO_ERROR_FAILED,
+ "FTP : Unexpected reply (%u - %s)",
+ reply->code,
+ reply->message);
+ return FALSE;
+ } else
+ return TRUE;
+}
+
+static gboolean
+ftp_connection_parse_quit_reply (SoupFTPConnection *ftp,
+ SoupFTPConnectionReply *reply,
+ GError **error)
+{
+ if (!ftp_connection_check_reply (ftp, reply, error))
+ return FALSE;
+
+ if (reply->code != 221) {
+ g_set_error (error,
+ G_IO_ERROR,
+ G_IO_ERROR_FAILED,
+ "FTP : Unexpected reply (%u - %s)",
+ reply->code,
+ reply->message);
+ return FALSE;
+ } else
+ return TRUE;
+}
+
+static SoupFTPConnectionReply *
+ftp_connection_receive_reply (SoupFTPConnection *ftp,
+ GError **error)
+{
+ SoupFTPConnectionReply *reply = g_malloc0 (sizeof (SoupFTPConnectionReply));
+ char *buffer, *tmp;
+ gsize len;
+ gboolean multi = FALSE;
+
+ buffer = g_data_input_stream_read_line (ftp->priv->control_input, &len, ftp->priv->cancellable, error);
+ if (buffer == NULL)
+ return NULL;
+ if (len < 4) {
+ g_set_error_literal (error,
+ SOUP_FTP_CONNECTION_ERROR,
+ SOUP_FTP_BAD_ANSWER,
+ "Bad FTP answer (less than 4 character)");
+ return NULL;
+ }
+ reply->code = SOUP_PARSE_FTP_STATUS (buffer);
+ if (buffer[3] == '-')
+ multi = TRUE;
+ reply->message = g_strdup (buffer + 4);
+ g_free (buffer);
+ while (multi) {
+ buffer = g_data_input_stream_read_line (ftp->priv->control_input, &len, ftp->priv->cancellable, error);
+ tmp = reply->message;
+ if (SOUP_PARSE_FTP_STATUS (buffer) == reply->code) {
+ if (g_ascii_isspace (buffer[3])) {
+ multi = FALSE;
+ reply->message = g_strjoin ("\n", tmp, buffer + 4, NULL);
+ } else if (buffer[3] == '-')
+ reply->message = g_strjoin ("\n", tmp, buffer + 4, NULL);
+ else
+ reply->message = g_strjoin ("\n", tmp, buffer, NULL);
+ } else
+ reply->message = g_strjoin ("\n", tmp, buffer, NULL);
+ g_free (tmp);
+ g_free (buffer);
+ }
+
+ return reply;
+}
+
+static void
+ftp_connection_receive_reply_async_cb (GObject *source_object,
+ GAsyncResult *read_res,
+ gpointer user_data)
+{
+ SoupFTPConnection *ftp;
+ SoupFTPConnectionReply *reply;
+ GSimpleAsyncResult *simple;
+ GError *error = NULL;
+ gsize len;
+ char *buffer, *tmp;
+ gboolean multi = FALSE;
+
+ simple = G_SIMPLE_ASYNC_RESULT (user_data);
+ ftp = SOUP_FTP_CONNECTION (g_async_result_get_source_object (G_ASYNC_RESULT (simple)));
+ buffer = g_data_input_stream_read_line_finish (ftp->priv->control_input,
+ read_res,
+ &len,
+ &error);
+ if (buffer) {
+ reply = g_simple_async_result_get_op_res_gpointer (simple);
+ if (reply->message == NULL) {
+ if (len < 4) {
+ g_simple_async_result_set_error (simple,
+ SOUP_FTP_CONNECTION_ERROR,
+ SOUP_FTP_BAD_ANSWER,
+ "Server answer too short");
+ g_simple_async_result_complete (simple);
+ g_object_unref (simple);
+ g_free (buffer);
+ return;
+ } else if (!g_ascii_isdigit (buffer[0]) ||
+ g_ascii_digit_value (buffer[0]) > 5 ||
+ g_ascii_digit_value (buffer[0]) == 0 ||
+ !g_ascii_isdigit (buffer[1]) ||
+ g_ascii_digit_value (buffer[1]) > 5 ||
+ !g_ascii_isdigit (buffer[2])) {
+ g_simple_async_result_set_error (simple,
+ SOUP_FTP_CONNECTION_ERROR,
+ SOUP_FTP_BAD_ANSWER,
+ "Server answer code not recognized");
+ g_simple_async_result_complete (simple);
+ g_object_unref (simple);
+ g_free (buffer);
+ return;
+ } else {
+ reply->code = SOUP_PARSE_FTP_STATUS (buffer);
+ reply->message = g_strdup (buffer + 4);
+ if (buffer[3] == '-')
+ multi = TRUE;
+ g_free (buffer);
+ }
+ } else {
+ multi = TRUE;
+ if (SOUP_PARSE_FTP_STATUS (buffer) == reply->code &&
+ g_ascii_isspace (buffer[3]))
+ multi = FALSE;
+ tmp = reply->message;
+ reply->message = g_strjoin ("\n", tmp, buffer, NULL);
+ g_free (tmp);
+ g_free (buffer);
+ }
+ if (multi) {
+ g_data_input_stream_read_line_async (ftp->priv->control_input,
+ G_PRIORITY_DEFAULT,
+ ftp->priv->cancellable,
+ ftp_connection_receive_reply_async_cb,
+ simple);
+ return;
+ } else {
+ g_simple_async_result_complete (simple);
+ g_object_unref (simple);
+ return;
+ }
+ } else {
+ g_simple_async_result_set_from_error (simple, error);
+ g_simple_async_result_complete (simple);
+ g_object_unref (simple);
+ g_error_free (error);
+ return;
+ }
+}
+
+static void
+ftp_connection_receive_reply_async (SoupFTPConnection *ftp,
+ GAsyncReadyCallback callback,
+ gpointer user_data)
+{
+ SoupFTPConnectionReply *reply;
+ GSimpleAsyncResult *simple;
+
+ reply = g_malloc0 (sizeof (SoupFTPConnectionReply));
+ simple = g_simple_async_result_new (G_OBJECT (ftp),
+ callback,
+ user_data,
+ ftp_connection_receive_reply_async);
+ g_simple_async_result_set_op_res_gpointer (simple,
+ reply,
+ (GDestroyNotify) ftp_connection_reply_free);
+ g_data_input_stream_read_line_async (ftp->priv->control_input,
+ G_PRIORITY_DEFAULT,
+ ftp->priv->cancellable,
+ ftp_connection_receive_reply_async_cb,
+ simple);
+}
+
+static SoupFTPConnectionReply *
+ftp_connection_receive_reply_finish (SoupFTPConnection *ftp,
+ GAsyncResult *result,
+ GError **error)
+{
+ SoupFTPConnectionReply *reply;
+
+ reply = ftp_connection_reply_copy (g_simple_async_result_get_op_res_gpointer ((GSimpleAsyncResult *) result));
+
+ return reply;
+}
+
+static gboolean
+ftp_connection_send_command (SoupFTPConnection *ftp,
+ const char *str,
+ GError **error)
+{
+ char *request;
+ gssize bytes_written;
+ gboolean success;
+
+ g_return_val_if_fail (SOUP_IS_FTP_CONNECTION (ftp), FALSE);
+ g_return_val_if_fail (str != NULL, FALSE);
+
+ request = g_strconcat (str, "\r\n", NULL);
+ bytes_written = g_output_stream_write (ftp->priv->control_output,
+ request,
+ strlen (request),
+ ftp->priv->cancellable,
+ error);
+ success = bytes_written == strlen (request);
+ g_free (request);
+
+ return success;
+}
+
+static void
+ftp_connection_send_command_cb (GObject *source_object,
+ GAsyncResult *result,
+ gpointer user_data)
+{
+ SoupFTPConnection *ftp;
+ GSimpleAsyncResult *simple;
+ GError *error = NULL;
+ gboolean success;
+ gssize bytes_to_write, bytes_written;
+
+ g_return_if_fail (G_IS_OUTPUT_STREAM (source_object));
+ g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (result));
+ g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (user_data));
+
+ simple = G_SIMPLE_ASYNC_RESULT (user_data);
+ ftp = SOUP_FTP_CONNECTION (g_async_result_get_source_object (result));
+ g_object_unref (ftp);
+ bytes_to_write = g_simple_async_result_get_op_res_gssize (simple);
+ bytes_written = g_output_stream_write_finish (G_OUTPUT_STREAM (source_object),
+ result,
+ &error);
+ success = (bytes_to_write == bytes_written);
+ if (bytes_written == -1) {
+ g_simple_async_result_set_from_error (simple, error);
+ g_error_free (error);
+ g_simple_async_result_complete (simple);
+ g_object_unref (simple);
+ } else {
+ g_simple_async_result_set_op_res_gboolean (simple, success);
+ g_simple_async_result_complete (simple);
+ g_object_unref (simple);
+ }
+}
+
+static void
+ftp_connection_send_command_async (SoupFTPConnection *ftp,
+ const char *str,
+ GAsyncReadyCallback callback,
+ gpointer user_data)
+{
+ GSimpleAsyncResult *simple;
+ char *request;
+
+ g_return_if_fail (SOUP_IS_FTP_CONNECTION (ftp));
+ g_return_if_fail (str != NULL);
+
+ simple = g_simple_async_result_new (G_OBJECT (ftp),
+ callback,
+ user_data,
+ ftp_connection_send_command_async);
+ request = g_strconcat (str, "\r\n", NULL);
+ g_simple_async_result_set_op_res_gssize (simple, strlen (request));
+ g_output_stream_write_async (G_OUTPUT_STREAM (ftp->priv->control_output),
+ request,
+ strlen (request),
+ G_PRIORITY_DEFAULT,
+ ftp->priv->cancellable,
+ ftp_connection_send_command_cb,
+ simple);
+ g_free (request);
+}
+
+static gboolean
+ftp_connection_send_command_finish (SoupFTPConnection *ftp,
+ GAsyncResult *result,
+ GError **error)
+{
+ GSimpleAsyncResult *simple;
+ gboolean success;
+
+ g_return_val_if_fail (SOUP_IS_FTP_CONNECTION (ftp), FALSE);
+ g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (result), FALSE);
+
+ simple = G_SIMPLE_ASYNC_RESULT (result);
+ if (!g_simple_async_result_is_valid (result,
+ G_OBJECT (ftp),
+ ftp_connection_send_command_async))
+ g_critical ("ftp_send_command_finish FAILED");
+ success = g_simple_async_result_get_op_res_gboolean (simple);
+
+ return success;
+}
+
+static SoupFTPConnectionReply *
+ftp_connection_send_and_recv (SoupFTPConnection *ftp,
+ const char *str,
+ GError **error)
+{
+ gboolean success;
+ SoupFTPConnectionReply *reply;
+
+ success = ftp_connection_send_command (ftp, str, error);
+ if (success) {
+ reply = ftp_connection_receive_reply (ftp, error);
+ if (reply)
+ return reply;
+ else
+ return NULL;
+ } else
+ return NULL;
+}
+
+static void
+ftp_connection_send_and_recv_async_cb_b (GObject *source_object,
+ GAsyncResult *result,
+ gpointer user_data)
+{
+ SoupFTPConnection *ftp;
+ SoupFTPConnectionReply *reply;
+ GError *error = NULL;
+ GSimpleAsyncResult *simple;
+
+ ftp = SOUP_FTP_CONNECTION (source_object);
+ reply = ftp_connection_receive_reply_finish (ftp, result, &error);
+ simple = G_SIMPLE_ASYNC_RESULT (user_data);
+ if (reply) {
+ g_simple_async_result_set_op_res_gpointer (simple, reply, (GDestroyNotify) ftp_connection_reply_free);
+ g_simple_async_result_complete (simple);
+ g_object_unref (simple);
+ } else {
+ g_simple_async_result_set_from_error (simple, error);
+ g_simple_async_result_complete (simple);
+ g_object_unref (simple);
+ g_error_free (error);
+ }
+}
+
+static void
+ftp_connection_send_and_recv_async_cb_a (GObject *source_object,
+ GAsyncResult *result,
+ gpointer user_data)
+{
+ SoupFTPConnection *ftp;
+ GSimpleAsyncResult *simple;
+ GError *error = NULL;
+ gboolean success;
+
+ g_warn_if_fail (SOUP_IS_FTP_CONNECTION (source_object));
+ g_warn_if_fail (G_IS_ASYNC_RESULT (result));
+
+ ftp = SOUP_FTP_CONNECTION (source_object);
+ simple = G_SIMPLE_ASYNC_RESULT (user_data);
+ success = ftp_connection_send_command_finish (ftp, result, &error);
+ if (success) {
+ ftp_connection_receive_reply_async (ftp,
+ ftp_connection_send_and_recv_async_cb_b,
+ simple);
+ } else {
+ g_simple_async_result_set_from_error (simple, error);
+ g_simple_async_result_complete (simple);
+ g_object_unref (simple);
+ g_error_free (error);
+ }
+}
+
+static void
+ftp_connection_send_and_recv_async (SoupFTPConnection *ftp,
+ const char *str,
+ GAsyncReadyCallback callback,
+ gpointer user_data)
+{
+ GSimpleAsyncResult *simple;
+
+ g_return_if_fail (SOUP_IS_FTP_CONNECTION (ftp));
+
+ simple = g_simple_async_result_new (G_OBJECT (ftp),
+ callback,
+ user_data,
+ ftp_connection_send_and_recv_async);
+ ftp_connection_send_command_async (ftp, str, ftp_connection_send_and_recv_async_cb_a, simple);
+}
+
+static SoupFTPConnectionReply *
+ftp_connection_send_and_recv_finish (SoupFTPConnection *ftp,
+ GAsyncResult *result,
+ GError **error)
+{
+ SoupFTPConnectionReply *reply;
+ GSimpleAsyncResult *simple;
+
+ simple = G_SIMPLE_ASYNC_RESULT (result);
+ if (g_simple_async_result_propagate_error (simple, error))
+ return NULL;
+ reply = ftp_connection_reply_copy (g_simple_async_result_get_op_res_gpointer (simple));
+ return reply;
+}
+
+/**
+ * gboolean ftp_connection_auth (SoupFTPConnection *ftp, GError **error)
+ * void ftp_connection_auth_async (SoupFTPConnection *ftp, GCancellable *cancellable, GAsyncReadyCallback callback)
+ * gboolean ftp_connection_auth_finish (SoupFTPConnection *ftp, GAsyncResult *result, GError **error)
+ *
+ * GInputStream * ftp_connection_list (SoupFTPConnection *ftp, char *path, GError **error)
+ * void ftp_connection_list_async (SoupFTPConnection *ftp, char *path, GCancellable *cancellable, GAsyncReadyCallback callback)
+ * GInputStream * ftp_connection_list_finish (SoupFTPConnection *ftp, GAsyncResult *result, GError **error)
+ *
+ * GInputStream * ftp_connection_retr (SoupFTPConnection *ftp, char *path, GError **error)
+ * void ftp_connection_retr_async (SoupFTPConnection *ftp, char *path, GCancellable *cancellable, GAsyncReadyCallback callback)
+ * GInputStream * ftp_connection_retr_finish (SoupFTPConnection *ftp, GAsyncResult *result, GError **error)
+ *
+ * ftp_connection_cd (SoupFTPConnection *ftp, char *path)
+ * char * ftp_connection_cwd (SoupFTPConnection *ftp)
+ **/
+
+static gboolean
+ftp_connection_auth (SoupFTPConnection *ftp,
+ GError **error)
+{
+ SoupFTPConnectionReply *reply;
+ char *msg;
+
+ g_return_val_if_fail (SOUP_IS_FTP_CONNECTION (ftp), FALSE);
+
+ if (ftp->priv->uri->user == NULL)
+ msg = "USER anonymous";
+ else
+ msg = g_strdup_printf ("USER %s", ftp->priv->uri->user);
+ reply = ftp_connection_send_and_recv (ftp, msg, error);
+ if (ftp->priv->uri->user != NULL)
+ g_free (msg);
+ if (reply == NULL)
+ return FALSE;
+ else if (!ftp_connection_parse_user_reply (ftp, reply, error)) {
+ ftp_connection_reply_free (reply);
+ return FALSE;
+ } else if (reply->code == 230) {
+ ftp_connection_reply_free (reply);
+ return TRUE;
+ }
+ ftp_connection_reply_free (reply);
+ if (ftp->priv->uri->user == NULL)
+ msg = "PASS libsoup example com";
+ else
+ msg = g_strdup_printf ("PASS %s", ftp->priv->uri->password);
+ reply = ftp_connection_send_and_recv (ftp, msg, error);
+ if (ftp->priv->uri->user != NULL)
+ g_free (msg);
+ if (!reply)
+ return FALSE;
+ if (!ftp_connection_parse_pass_reply (ftp, reply, error)) {
+ ftp_connection_reply_free (reply);
+ return FALSE;
+ } else
+ return TRUE;
+}
+
+static void
+ftp_connection_auth_pass_cb (GObject *source,
+ GAsyncResult *res,
+ gpointer user_data)
+{
+ SoupFTPConnection *ftp;
+ SoupFTPConnectionReply *reply;
+ GSimpleAsyncResult *simple;
+ GError *error = NULL;
+
+ g_return_if_fail (SOUP_IS_FTP_CONNECTION (source));
+ g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (res));
+
+ simple = G_SIMPLE_ASYNC_RESULT (user_data);
+ ftp = SOUP_FTP_CONNECTION (source);
+ reply = ftp_connection_send_and_recv_finish (ftp, res, &error);
+
+ if (!reply) {
+ g_simple_async_result_set_from_error (simple, error);
+ g_simple_async_result_set_op_res_gboolean (simple, FALSE);
+ g_simple_async_result_complete (simple);
+ return;
+ }
+ if (!ftp_connection_check_reply (ftp, reply, &error)) {
+ ftp_connection_reply_free (reply);
+ g_simple_async_result_set_from_error (simple, error);
+ g_simple_async_result_set_op_res_gboolean (simple, FALSE);
+ g_simple_async_result_complete (simple);
+ return;
+ } else if (reply->code == 230) {
+ ftp_connection_reply_free (reply);
+ g_simple_async_result_set_op_res_gboolean (simple, TRUE);
+ g_simple_async_result_complete (simple);
+ return;
+ } else if (reply->code == 332) {
+ ftp_connection_reply_free (reply);
+ g_simple_async_result_set_error (simple,
+ SOUP_FTP_CONNECTION_ERROR,
+ 0,
+ "Authentication : ACCT not implemented");
+ g_simple_async_result_set_op_res_gboolean (simple, FALSE);
+ g_simple_async_result_complete (simple);
+ return;
+ } else {
+ ftp_connection_reply_free (reply);
+ g_simple_async_result_set_error (simple,
+ SOUP_FTP_CONNECTION_ERROR,
+ 0,
+ "Authentication : Unexpected reply received");
+ g_simple_async_result_set_op_res_gboolean (simple, FALSE);
+ g_simple_async_result_complete (simple);
+ return;
+ }
+}
+
+static void
+ftp_connection_auth_user_cb (GObject *source,
+ GAsyncResult *result,
+ gpointer user_data)
+{
+ SoupFTPConnection *ftp;
+ SoupFTPConnectionReply *reply;
+ GSimpleAsyncResult *simple;
+ GError *error = NULL;
+ char *msg;
+
+ ftp = SOUP_FTP_CONNECTION (source);
+ simple = G_SIMPLE_ASYNC_RESULT (user_data);
+ reply = ftp_connection_send_and_recv_finish (ftp, result, &error);
+ if (reply == NULL) {
+ g_simple_async_result_set_from_error (simple, error);
+ g_simple_async_result_complete (simple);
+ g_object_unref (simple);
+ g_error_free (error);
+ }
+ if (!ftp_connection_parse_user_reply (ftp, reply, &error)) {
+ g_simple_async_result_set_from_error (simple, error);
+ g_simple_async_result_complete (simple);
+ g_object_unref (simple);
+ ftp_connection_reply_free (reply);
+ }
+ if (reply->code == 230) {
+ ftp_connection_reply_free (reply);
+ g_simple_async_result_set_op_res_gboolean (simple, TRUE);
+ g_simple_async_result_complete (simple);
+ g_object_unref (simple);
+ } else if (reply->code == 331) {
+ ftp_connection_reply_free (reply);
+ if (ftp->priv->uri->user == NULL)
+ msg = "PASS libsoup example com";
+ else
+ msg = g_strdup_printf ("PASS %s", ftp->priv->uri->password);
+ ftp_connection_send_and_recv_async (ftp, msg, ftp_connection_auth_pass_cb, simple);
+ if (ftp->priv->uri->user != NULL)
+ g_free (msg);
+ }
+}
+
+static void
+ftp_connection_auth_async (SoupFTPConnection *ftp,
+ GCancellable *cancellable,
+ GAsyncReadyCallback callback,
+ gpointer user_data)
+{
+ GSimpleAsyncResult *simple;
+ char *msg;
+
+ g_return_if_fail (SOUP_IS_FTP_CONNECTION (ftp));
+
+ simple = g_simple_async_result_new (G_OBJECT (ftp),
+ callback,
+ user_data,
+ ftp_connection_auth_async);
+ if (ftp->priv->uri->user == NULL)
+ msg = "USER anonymous";
+ else
+ msg = g_strdup_printf ("USER %s", ftp->priv->uri->user);
+ ftp_connection_send_and_recv_async (ftp, msg, ftp_connection_auth_user_cb, simple);
+ if (ftp->priv->uri->user != NULL)
+ g_free (msg);
+}
+
+static gboolean
+ftp_connection_auth_finish (SoupFTPConnection *ftp,
+ GAsyncResult *result,
+ GError **error)
+{
+ GSimpleAsyncResult *simple;
+ gboolean res;
+
+ g_return_val_if_fail (SOUP_IS_FTP_CONNECTION (ftp), FALSE);
+ g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (result), FALSE);
+
+ simple = G_SIMPLE_ASYNC_RESULT (result);
+ if (g_simple_async_result_propagate_error (simple, error))
+ res = FALSE;
+ else
+ res = g_simple_async_result_get_op_res_gboolean (simple);
+
+ return res;
+}
+
+/**
+ * GInputStream * ftp_connection_list (SoupFTPConnection *ftp, char *path, GError **error)
+ * void ftp_connection_list_async (SoupFTPConnection *ftp, char *path, GCancellable *cancellable, GAsyncReadyCallback callback)
+ * GInputStream * ftp_connection_list_finish (SoupFTPConnection *ftp, GAsyncResult *result, GError **error)
+ **/
+
+static void
+ftp_connection_list_complete (SoupFTPInputStream *sfstream,
+ gpointer user_data)
+{
+ SoupFTPConnection *ftp;
+ SoupFTPConnectionReply *reply;
+
+ /* FIXME : need to clean ftp->priv->data */
+
+ g_return_if_fail (SOUP_IS_FTP_INPUT_STREAM (sfstream));
+ g_return_if_fail (SOUP_IS_FTP_CONNECTION (user_data));
+
+ ftp = SOUP_FTP_CONNECTION (user_data);
+ g_signal_handlers_disconnect_by_func (sfstream,
+ ftp_connection_list_complete,
+ ftp);
+
+ reply = ftp_connection_receive_reply (ftp, NULL);
+ if (reply)
+ ftp_connection_reply_free (reply);
+}
+
+static int
+ftp_connection_file_info_list_compare (gconstpointer data,
+ gconstpointer user_data)
+{
+ GFileInfo *info;
+ char *name;
+
+ g_return_val_if_fail (G_IS_FILE_INFO (data), -1);
+ g_return_val_if_fail (user_data != NULL,-1);
+
+ info = G_FILE_INFO (data);
+ name = (char *) user_data;
+
+ return g_strcmp0 (g_file_info_get_name (info), name);
+}
+
+static int
+ftp_connection_info_list_sort (gconstpointer data1,
+ gconstpointer data2)
+{
+ /* FIXME : This code is duplicated (see protocol_file) */
+ GFileInfo *info1, *info2;
+
+ g_return_val_if_fail (G_IS_FILE_INFO (data1), -1);
+ g_return_val_if_fail (G_IS_FILE_INFO (data2), -1);
+
+ info1 = G_FILE_INFO (data1);
+ info2 = G_FILE_INFO (data2);
+
+ if (g_file_info_get_file_type (info1) == G_FILE_TYPE_DIRECTORY &&
+ g_file_info_get_file_type (info2) != G_FILE_TYPE_DIRECTORY)
+ return -1;
+ else if (g_file_info_get_file_type (info1) != G_FILE_TYPE_DIRECTORY &&
+ g_file_info_get_file_type (info2) == G_FILE_TYPE_DIRECTORY)
+ return 1;
+ else
+ return g_ascii_strcasecmp (g_file_info_get_name (info1),
+ g_file_info_get_name (info2));
+}
+
+static GList *
+ftp_connection_list_parse (SoupFTPConnection *ftp,
+ GInputStream *stream)
+{
+ GDataInputStream *dstream;
+ struct list_state state = { 0, };
+ GList *file_list = NULL;
+ GFileInfo *file_info;
+ char *buffer;
+ GError *error = NULL;
+ gsize len = 0;
+ int type;
+
+ g_return_val_if_fail (SOUP_IS_FTP_CONNECTION (ftp), NULL);
+ g_return_val_if_fail (G_IS_INPUT_STREAM (stream), NULL);
+
+ dstream = g_data_input_stream_new (stream);
+ g_data_input_stream_set_newline_type (dstream, G_DATA_STREAM_NEWLINE_TYPE_CR_LF);
+
+ while ((buffer = g_data_input_stream_read_line (dstream, &len, NULL, &error))) {
+ struct list_result result = { 0, };
+ GTimeVal tv = { 0, 0 };
+ type = ParseFTPList (buffer, &state, &result);
+ file_info = g_file_info_new();
+ if (result.fe_type == 'f') {
+ g_file_info_set_file_type (file_info, G_FILE_TYPE_REGULAR);
+ g_file_info_set_name (file_info, g_strdup (result.fe_fname));
+ } else if (result.fe_type == 'd') {
+ g_file_info_set_file_type (file_info, G_FILE_TYPE_DIRECTORY);
+ g_file_info_set_name (file_info, g_strdup (result.fe_fname));
+ } else if (result.fe_type == 'l' && result.fe_lname) {
+ g_file_info_set_file_type (file_info, G_FILE_TYPE_SYMBOLIC_LINK);
+ g_file_info_set_name (file_info, g_strndup (result.fe_fname,
+ result.fe_lname - result.fe_fname - 4));
+ g_file_info_set_symlink_target (file_info, g_strdup (result.fe_lname));
+ } else {
+ g_object_unref (file_info);
+ continue;
+ }
+ g_file_info_set_size (file_info, atoi (result.fe_size));
+ if (result.fe_time.tm_year >= 1900)
+ result.fe_time.tm_year -= 1900;
+ tv.tv_sec = mktime (&result.fe_time);
+ if (tv.tv_sec != -1)
+ g_file_info_set_modification_time (file_info, &tv);
+ file_list = g_list_prepend (file_list, file_info);
+ }
+
+ g_object_unref (dstream);
+ file_list = g_list_sort (file_list, ftp_connection_info_list_sort);
+
+ return file_list;
+}
+
+static GInputStream *
+ftp_connection_list (SoupFTPConnection *ftp,
+ char *path,
+ GError **error)
+{
+ SoupFTPConnectionReply *reply;
+ GInputStream *istream, *sfstream;
+ GSocketConnectable *conn;
+ GSocketClient *client;
+ GList *file_list = NULL;
+ GFileInfo *dir_info;
+ char *msg;
+
+ g_return_val_if_fail (SOUP_IS_FTP_CONNECTION (ftp), NULL);
+ g_return_val_if_fail (path != NULL, NULL);
+
+ reply = ftp_connection_send_and_recv (ftp, "PASV", error);
+ if (!reply)
+ return NULL;
+ if (!ftp_connection_check_reply (ftp, reply, error)) {
+ ftp_connection_reply_free (reply);
+ return NULL;
+ }
+ if (reply->code != 227) {
+ ftp_connection_reply_free (reply);
+ g_set_error_literal (error,
+ SOUP_FTP_CONNECTION_ERROR,
+ 0,
+ "Directory listing : Unexpected reply received");
+ return NULL;
+ }
+ conn = ftp_connection_parse_pasv_reply (ftp, reply, error);
+ ftp_connection_reply_free (reply);
+ client = g_socket_client_new ();
+ ftp->priv->data = g_socket_client_connect (client,
+ conn,
+ ftp->priv->cancellable,
+ error);
+ g_object_unref (client);
+ g_object_unref (conn);
+ if (!ftp->priv->data)
+ return NULL;
+ msg = g_strdup_printf ("LIST -a %s", path);
+ reply = ftp_connection_send_and_recv (ftp, msg, error);
+ g_free (msg);
+ if (!reply)
+ return NULL;
+ if (!ftp_connection_check_reply (ftp, reply, error)) {
+ ftp_connection_reply_free (reply);
+ return NULL;
+ }
+ if (reply->code != 125 && reply->code != 150) {
+ ftp_connection_reply_free (reply);
+ g_set_error_literal (error,
+ SOUP_FTP_CONNECTION_ERROR,
+ 0,
+ "Directory listing : Unexpected reply received");
+ return NULL;
+ }
+ ftp_connection_reply_free (reply);
+
+ istream = g_io_stream_get_input_stream (G_IO_STREAM (ftp->priv->data));
+
+ dir_info = g_file_info_new ();
+ g_file_info_set_name (dir_info, path);
+ g_file_info_set_file_type (dir_info, G_FILE_TYPE_DIRECTORY);
+ sfstream = soup_ftp_input_stream_new (istream, dir_info, NULL);
+ g_object_unref (dir_info);
+ g_signal_connect (sfstream, "eof",
+ G_CALLBACK (ftp_connection_list_complete), ftp);
+
+ file_list = ftp_connection_list_parse (ftp, sfstream);
+ g_object_set (sfstream, "children", file_list, NULL);
+
+ return sfstream;
+}
+
+static void
+ftp_connection_retr_complete (SoupFTPInputStream *sfstream,
+ gpointer user_data)
+{
+ SoupFTPConnection *ftp;
+ SoupFTPConnectionReply *reply;
+
+ g_return_if_fail (SOUP_IS_FTP_INPUT_STREAM (sfstream));
+ g_return_if_fail (SOUP_IS_FTP_CONNECTION (user_data));
+
+ /* FIXME, load_uri_complete, clean up ftp_connection */
+
+ ftp = user_data;
+ g_signal_handlers_disconnect_by_func (sfstream, ftp_connection_retr_complete, ftp);
+
+ g_object_unref (ftp->priv->data);
+ ftp->priv->data = NULL;
+ reply = ftp_connection_receive_reply (ftp, NULL);
+ if (reply)
+ ftp_connection_reply_free (reply);
+}
+
+static GInputStream *
+ftp_connection_retr (SoupFTPConnection *ftp,
+ char *path,
+ GFileInfo *info,
+ GError **error)
+{
+ SoupFTPConnectionReply *reply;
+ GInputStream *istream, *sfstream;
+ GSocketConnectable *conn;
+ GSocketClient *client;
+ char *msg;
+
+ g_return_val_if_fail (SOUP_IS_FTP_CONNECTION (ftp), NULL);
+ g_return_val_if_fail (path != NULL, NULL);
+
+ reply = ftp_connection_send_and_recv (ftp, "PASV", error);
+ if (!reply)
+ return NULL;
+ if (!ftp_connection_check_reply (ftp, reply, error)) {
+ ftp_connection_reply_free (reply);
+ return NULL;
+ }
+ if (reply->code != 227) {
+ ftp_connection_reply_free (reply);
+ g_set_error_literal (error,
+ SOUP_FTP_CONNECTION_ERROR,
+ 0,
+ "Directory listing : Unexpected reply received");
+ return NULL;
+ }
+ conn = ftp_connection_parse_pasv_reply (ftp, reply, error);
+ ftp_connection_reply_free (reply);
+ client = g_socket_client_new ();
+ ftp->priv->data = g_socket_client_connect (client,
+ conn,
+ ftp->priv->cancellable,
+ error);
+ g_object_unref (client);
+ g_object_unref (conn);
+ if (!ftp->priv->data)
+ return NULL;
+ msg = g_strdup_printf ("RETR %s", path);
+ reply = ftp_connection_send_and_recv (ftp, msg, error);
+ g_free (msg);
+ if (!reply)
+ return NULL;
+ if (!ftp_connection_check_reply (ftp, reply, error)) {
+ ftp_connection_reply_free (reply);
+ return NULL;
+ }
+ if (reply->code != 125 && reply->code != 150) {
+ ftp_connection_reply_free (reply);
+ g_set_error_literal (error,
+ SOUP_FTP_CONNECTION_ERROR,
+ 0,
+ "Directory listing : Unexpected reply received");
+ return NULL;
+ }
+ ftp_connection_reply_free (reply);
+ istream = g_io_stream_get_input_stream (G_IO_STREAM (ftp->priv->data));
+ sfstream = soup_ftp_input_stream_new (istream, info, NULL);
+ g_signal_connect (sfstream, "eof",
+ G_CALLBACK (ftp_connection_retr_complete), ftp);
+
+ return sfstream;
+}
+
+GInputStream *
+soup_ftp_connection_load_uri (SoupFTPConnection *ftp,
+ SoupURI *uri,
+ GCancellable *cancellable,
+ GError **error)
+{
+ SoupFTPConnectionReply *reply;
+ GInputStream *istream;
+ GList *listing = NULL;
+ GFileInfo *info;
+ char *needed_directory, *msg;
+ GSocketClient *client;
+
+ g_return_val_if_fail (SOUP_IS_FTP_CONNECTION (ftp), NULL);
+ g_return_val_if_fail (uri != NULL, NULL);
+
+ ftp->priv->uri = soup_uri_copy (uri);
+ ftp->priv->cancellable = cancellable;
+ if (ftp->priv->control == NULL) {
+ client = g_socket_client_new ();
+ ftp->priv->control = g_socket_client_connect_to_host (client,
+ uri->host,
+ uri->port,
+ ftp->priv->cancellable,
+ error);
+ g_object_unref (client);
+ if (ftp->priv->control == NULL)
+ return NULL;
+ ftp->priv->control_input = g_data_input_stream_new (g_io_stream_get_input_stream (G_IO_STREAM (ftp->priv->control)));
+ g_data_input_stream_set_newline_type (ftp->priv->control_input, G_DATA_STREAM_NEWLINE_TYPE_CR_LF);
+ ftp->priv->control_output = g_io_stream_get_output_stream (G_IO_STREAM (ftp->priv->control));
+ reply = ftp_connection_receive_reply (ftp, error);
+ if (reply == NULL)
+ return NULL;
+ else if (!ftp_connection_parse_welcome_reply (ftp, reply, error)) {
+ ftp_connection_reply_free (reply);
+ return NULL;
+ }
+ ftp_connection_reply_free (reply);
+ if (!ftp_connection_auth (ftp, error))
+ return NULL;
+ }
+ if (ftp->priv->working_directory == NULL) {
+ reply = ftp_connection_send_and_recv (ftp, "PWD", error);
+ if (reply == NULL)
+ return NULL;
+ ftp->priv->working_directory = ftp_connection_parse_pwd_reply (ftp, reply, error);
+ if (ftp->priv->working_directory == NULL) {
+ ftp_connection_reply_free (reply);
+ return NULL;
+ }
+ }
+ needed_directory = g_strndup (uri->path, g_strrstr (uri->path, "/") - uri->path + 1);
+ if (g_strcmp0 (ftp->priv->working_directory, needed_directory)) {
+ msg = g_strdup_printf ("CWD %s", needed_directory);
+ reply = ftp_connection_send_and_recv (ftp, msg, error);
+ g_free (msg);
+ if (reply == NULL)
+ return NULL;
+ if (!ftp_connection_parse_cwd_reply (ftp, reply, error)) {
+ ftp_connection_reply_free (reply);
+ return NULL;
+ }
+ g_free (ftp->priv->working_directory);
+ ftp->priv->working_directory = g_strdup (needed_directory);
+ }
+ g_free (needed_directory);
+ istream = ftp_connection_list (ftp, ".", error);
+ if (istream == NULL)
+ return NULL;
+ if (g_str_has_suffix (uri->path, "/"))
+ return istream;
+ else {
+ g_object_get (istream,
+ "file-info", &info,
+ "children", &listing,
+ NULL);
+ listing = g_list_find_custom (listing,
+ g_strrstr (uri->path, "/") + 1,
+ ftp_connection_file_info_list_compare);
+ if (listing == NULL) {
+ g_object_unref (istream);
+ g_set_error_literal (error,
+ G_IO_ERROR,
+ G_IO_ERROR_NOT_FOUND,
+ "FTP : File or directory not found");
+ return NULL;
+ }
+ if (g_file_info_get_file_type (listing->data) == G_FILE_TYPE_DIRECTORY) {
+ msg = g_strconcat ("CWD ", ftp->priv->working_directory, g_file_info_get_name (listing->data), NULL);
+ reply = ftp_connection_send_and_recv (ftp, msg, error);
+ if (reply == NULL) {
+ g_object_unref (istream);
+ g_free (msg);
+ return NULL;
+ }
+ g_free (ftp->priv->working_directory);
+ ftp->priv->working_directory = msg;
+ g_object_unref (istream);
+ istream = ftp_connection_list (ftp, ".", error);
+ if (istream == NULL)
+ return NULL;
+ return istream;
+ } else if (g_file_info_get_file_type (listing->data) == G_FILE_TYPE_REGULAR) {
+ g_object_unref (istream);
+ istream = ftp_connection_retr (ftp, uri->path, listing->data, error);
+ if (istream == NULL)
+ return NULL;
+ return istream;
+ } else {
+ g_object_unref (istream);
+ return NULL;
+ }
+ // FIXME : add the symlink case
+ }
+}
+
+static void
+ftp_connection_welcome_cb (GObject *source_object,
+ GAsyncResult *res,
+ gpointer user_data)
+{
+ SoupFTPConnection *ftp;
+ SoupFTPConnectionReply *reply;
+ GSimpleAsyncResult *simple;
+ GError *error = NULL;
+
+ ftp = SOUP_FTP_CONNECTION (source_object);
+ simple = G_SIMPLE_ASYNC_RESULT (user_data);
+ reply = ftp_connection_receive_reply_finish (ftp,
+ res,
+ &error);
+ if (reply == NULL) {
+ g_simple_async_result_set_from_error (simple, error);
+ g_simple_async_result_complete (simple);
+ g_object_unref (simple);
+ g_error_free (error);
+ }
+ if (!ftp_connection_parse_welcome_reply (ftp, reply, &error)) {
+ g_simple_async_result_set_from_error (simple, error);
+ g_simple_async_result_complete (simple);
+ g_object_unref (simple);
+ ftp_connection_reply_free (reply);
+ }
+ ftp_connection_auth_async (ftp, ftp->priv->cancellable, ftp_callback_pass, simple);
+}
+
+static void
+ftp_connection_connection_cb (GObject *source_object,
+ GAsyncResult *result,
+ gpointer user_data)
+{
+ SoupFTPConnection *ftp;
+ GSocketClient *client;
+ GSimpleAsyncResult *simple;
+ GError *error = NULL;
+
+ g_return_if_fail (G_IS_SOCKET_CLIENT (source_object));
+ g_return_if_fail (G_IS_ASYNC_RESULT (result));
+ g_return_if_fail (G_IS_SIMPLE_ASYNC_RESULT (user_data));
+
+ client = G_SOCKET_CLIENT (source_object);
+ simple = G_SIMPLE_ASYNC_RESULT (user_data);
+ ftp = SOUP_FTP_CONNECTION (g_async_result_get_source_object (G_ASYNC_RESULT (simple)));
+ ftp->priv->control = g_socket_client_connect_to_host_finish (client,
+ result,
+ &error);
+ if (ftp->priv->control == NULL) {
+ g_simple_async_result_set_from_error (simple, error);
+ g_simple_async_result_complete (simple);
+ g_object_unref (simple);
+ g_error_free (error);
+ }
+ ftp->priv->control_input = g_data_input_stream_new (g_io_stream_get_input_stream (G_IO_STREAM (ftp->priv->control)));
+ g_data_input_stream_set_newline_type (ftp->priv->control_input, G_DATA_STREAM_NEWLINE_TYPE_CR_LF);
+ ftp->priv->control_output = g_io_stream_get_output_stream (G_IO_STREAM (ftp->priv->control));
+ ftp_connection_receive_reply_async (ftp,
+ ftp_connection_welcome_cb,
+ simple);
+}
+
+void
+soup_ftp_connection_load_uri_async (SoupFTPConnection *ftp,
+ SoupURI *uri,
+ GCancellable *cancellable,
+ GAsyncReadyCallback callback,
+ gpointer user_data)
+{
+ GSimpleAsyncResult *simple;
+ GSocketClient *client;
+
+ g_return_if_fail (SOUP_IS_FTP_CONNECTION (ftp));
+ g_return_if_fail (uri != NULL);
+
+ ftp->priv->uri = soup_uri_copy (uri);
+ ftp->priv->cancellable = cancellable;
+ simple = g_simple_async_result_new (G_OBJECT (ftp),
+ callback,
+ user_data,
+ soup_ftp_connection_load_uri_async);
+ if (ftp->priv->control == NULL) {
+ client = g_socket_client_new ();
+ g_socket_client_connect_to_host_async (client,
+ ftp->priv->uri->host,
+ ftp->priv->uri->port,
+ ftp->priv->cancellable,
+ ftp_connection_connection_cb,
+ simple);
+ }
+ /* needed_directory = g_strndup (uri->path, g_strrstr (uri->path, "/") - uri->path + 1); */
+}
+
+GInputStream *
+soup_ftp_connection_load_uri_finish (SoupFTPConnection *ftp,
+ GAsyncResult *result,
+ GError **error)
+{
+ GInputStream *input_stream;
+ GSimpleAsyncResult *simple;
+ GInputStream *sfstream;
+
+ g_return_val_if_fail (SOUP_IS_FTP_CONNECTION (ftp), NULL);
+
+ simple = G_SIMPLE_ASYNC_RESULT (result);
+
+ if (g_simple_async_result_propagate_error (simple, error))
+ return NULL;
+ input_stream = g_io_stream_get_input_stream (G_IO_STREAM (ftp->priv->data));
+ sfstream = soup_ftp_input_stream_new (input_stream, NULL, NULL);
+ g_signal_connect (sfstream, "eof",
+ G_CALLBACK (ftp_connection_retr_complete), ftp);
+
+ return sfstream;
+}
+
+GQuark
+soup_ftp_connection_error_quark (void)
+{
+ static GQuark error;
+ if (!error)
+ error = g_quark_from_static_string ("soup_ftp_connection_error_quark");
+ return error;
+}
+
+GQuark
+soup_ftp_error_quark (void)
+{
+ static GQuark error;
+ if (!error)
+ error = g_quark_from_static_string ("soup_ftp_error_quark");
+ return error;
+}
+
+/**
+ * async callbacks
+ **/
+
+void
+ftp_callback_pass (GObject *source_object,
+ GAsyncResult *res,
+ gpointer user_data)
+{
+ SoupFTPConnection *ftp;
+ GSimpleAsyncResult *simple;
+ GError *error = NULL;
+
+ g_warn_if_fail (SOUP_IS_FTP_CONNECTION (source_object));
+ g_warn_if_fail (G_IS_ASYNC_RESULT (res));
+
+ ftp = SOUP_FTP_CONNECTION (source_object);
+ simple = G_SIMPLE_ASYNC_RESULT (user_data);
+
+ if (ftp_connection_auth_finish (ftp, res, &error))
+ ftp_connection_send_and_recv_async (ftp, "FEAT", ftp_callback_feat, simple);
+}
+
+void
+ftp_callback_feat (GObject *source_object,
+ GAsyncResult *res,
+ gpointer user_data)
+{
+ SoupFTPConnection *ftp;
+ SoupFTPConnectionReply *reply;
+ GSimpleAsyncResult *simple;
+ GError *error = NULL;
+
+ g_warn_if_fail (SOUP_IS_FTP_CONNECTION (source_object));
+ g_warn_if_fail (G_IS_ASYNC_RESULT (res));
+
+ ftp = SOUP_FTP_CONNECTION (source_object);
+ simple = G_SIMPLE_ASYNC_RESULT (user_data);
+ reply = ftp_connection_receive_reply_finish (ftp, res, &error);
+ if (reply) {
+ if (ftp_connection_check_reply (ftp, reply, &error)) {
+ if (REPLY_IS_POSITIVE_COMPLETION (reply)) {
+ ftp_parse_feat_reply (ftp, reply);
+ ftp_connection_send_and_recv_async (ftp,
+ "PASV",
+ ftp_callback_pasv,
+ simple);
+ }
+ } else {
+ g_simple_async_result_set_from_error (simple, error);
+ g_simple_async_result_complete (simple);
+ g_error_free (error);
+ }
+ ftp_connection_reply_free (reply);
+ } else {
+ g_simple_async_result_set_from_error (simple, error);
+ g_simple_async_result_complete (simple);
+ g_error_free (error);
+ }
+}
+
+void
+ftp_callback_pasv (GObject *source_object,
+ GAsyncResult *res,
+ gpointer user_data)
+{
+ SoupFTPConnection *ftp;
+ SoupFTPConnectionReply *reply;
+ GSocketConnectable *conn;
+ GSimpleAsyncResult *simple;
+ GError *error = NULL;
+
+ g_warn_if_fail (SOUP_IS_FTP_CONNECTION (source_object));
+ g_warn_if_fail (G_IS_ASYNC_RESULT (res));
+
+ simple = G_SIMPLE_ASYNC_RESULT (user_data);
+ ftp = SOUP_FTP_CONNECTION (source_object);
+ reply = ftp_connection_receive_reply_finish (ftp, res, &error);
+ if (reply) {
+ if (ftp_connection_check_reply (ftp, reply, &error)) {
+ if (REPLY_IS_POSITIVE_COMPLETION (reply)) {
+ conn = ftp_connection_parse_pasv_reply (ftp, reply, &error);
+ if (conn) {
+ g_socket_client_connect_async (g_socket_client_new (),
+ conn,
+ ftp->priv->cancellable,
+ ftp_callback_data,
+ simple);
+ } else {
+ g_simple_async_result_set_error (simple,
+ SOUP_FTP_CONNECTION_ERROR,
+ 0,
+ "Passive failed");
+ g_simple_async_result_complete (simple);
+ }
+ }
+ } else {
+ g_simple_async_result_set_from_error (simple, error);
+ g_simple_async_result_complete (simple);
+ g_error_free (error);
+ }
+ ftp_connection_reply_free (reply);
+ } else {
+ g_simple_async_result_set_from_error (simple, error);
+ g_simple_async_result_complete (simple);
+ g_error_free (error);
+ }
+}
+
+void
+ftp_callback_data (GObject *source_object,
+ GAsyncResult *res,
+ gpointer user_data)
+{
+ GSocketClient *client;
+ SoupFTPConnection *ftp;
+ GSimpleAsyncResult *simple;
+ GError *error = NULL;
+ char *uri_decode, *msg;
+
+ g_warn_if_fail (G_IS_SOCKET_CLIENT (source_object));
+ g_warn_if_fail (G_IS_ASYNC_RESULT (res));
+ g_warn_if_fail (user_data != NULL);
+
+ simple = G_SIMPLE_ASYNC_RESULT (user_data);
+ client = G_SOCKET_CLIENT (source_object);
+ ftp = SOUP_FTP_CONNECTION (g_async_result_get_source_object (G_ASYNC_RESULT (simple)));
+ ftp->priv->data = g_socket_client_connect_finish (client,
+ res,
+ &error);
+ if (ftp->priv->data) {
+ uri_decode = soup_uri_decode (ftp->priv->uri->path);
+ if (uri_decode) {
+ msg = g_strdup_printf ("RETR %s", uri_decode);
+ ftp_connection_send_and_recv_async (ftp, msg, ftp_callback_retr, simple);
+ g_free (uri_decode);
+ g_free (msg);
+ } else {
+ g_simple_async_result_set_error (simple,
+ SOUP_FTP_CONNECTION_ERROR,
+ SOUP_FTP_INVALID_PATH,
+ "Path decode failed");
+ g_simple_async_result_complete (simple);
+ g_object_unref (simple);
+ }
+ } else {
+ g_simple_async_result_set_from_error (simple, error);
+ g_simple_async_result_complete (simple);
+ g_object_unref (simple);
+ g_error_free (error);
+ }
+}
+
+void
+ftp_callback_retr (GObject *source_object,
+ GAsyncResult *result,
+ gpointer user_data)
+{
+ SoupFTPConnection *ftp;
+ SoupFTPConnectionReply *reply;
+ GSimpleAsyncResult *simple;
+ GError *error = NULL;
+
+ g_warn_if_fail (SOUP_IS_FTP_CONNECTION (source_object));
+ g_warn_if_fail (G_IS_ASYNC_RESULT (result));
+
+ simple = G_SIMPLE_ASYNC_RESULT (user_data);
+ ftp = SOUP_FTP_CONNECTION (source_object);
+ reply = ftp_connection_receive_reply_finish (ftp, result, &error);
+ if (reply == NULL) {
+ g_simple_async_result_set_from_error (simple, error);
+ g_simple_async_result_complete (simple);
+ g_object_unref (simple);
+ g_error_free (error);
+ } else if (!ftp_connection_parse_retr_reply (ftp, reply, &error)) {
+ g_simple_async_result_set_from_error (simple, error);
+ g_simple_async_result_complete (simple);
+ g_object_unref (simple);
+ ftp_connection_reply_free (reply);
+ }
+ g_simple_async_result_complete (simple);
+ g_object_unref (simple);
+}
diff --git a/libsoup/soup-ftp-connection.h b/libsoup/soup-ftp-connection.h
new file mode 100644
index 0000000..7b613cb
--- /dev/null
+++ b/libsoup/soup-ftp-connection.h
@@ -0,0 +1,54 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
+/*
+ * Copyright (C) 2009 Red Hat, Inc.
+ */
+
+#ifndef SOUP_FTP_CONNECTION_H
+#define SOUP_FTP_CONNECTION_H 1
+
+#include <libsoup/soup-types.h>
+#include <gio/gio.h>
+
+#define SOUP_TYPE_FTP_CONNECTION (soup_ftp_connection_get_type ())
+#define SOUP_FTP_CONNECTION(object) (G_TYPE_CHECK_INSTANCE_CAST ((object), SOUP_TYPE_FTP_CONNECTION, SoupFTPConnection))
+#define SOUP_FTP_CONNECTION_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), SOUP_TYPE_FTP_CONNECTION, SoupFTPConnectionInterface))
+#define SOUP_IS_FTP_CONNECTION(object) (G_TYPE_CHECK_INSTANCE_TYPE ((object), SOUP_TYPE_FTP_CONNECTION))
+#define SOUP_IS_FTP_CONNECTION_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), SOUP_TYPE_FTP_CONNECTION))
+#define SOUP_FTP_CONNECTION_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_INTERFACE ((obj), SOUP_TYPE_FTP_CONNECTION, SoupFTPConnectionInterface))
+
+typedef struct _SoupFTPConnectionPrivate SoupFTPConnectionPrivate;
+
+typedef struct {
+ GObject parent;
+
+ SoupFTPConnectionPrivate *priv;
+} SoupFTPConnection;
+
+typedef struct {
+ GObjectClass parent;
+
+} SoupFTPConnectionClass;
+
+GType soup_ftp_connection_get_type (void);
+SoupFTPConnection *soup_ftp_connection_new (void);
+
+GInputStream *soup_ftp_connection_load_uri (SoupFTPConnection *ftp,
+ SoupURI *uri,
+ GCancellable *cancellable,
+ GError **error);
+void soup_ftp_connection_load_uri_async (SoupFTPConnection *ftp,
+ SoupURI *uri,
+ GCancellable *cancellable,
+ GAsyncReadyCallback callback,
+ gpointer user_data);
+GInputStream *soup_ftp_connection_load_uri_finish (SoupFTPConnection *ftp,
+ GAsyncResult *result,
+ GError **error);
+
+#define SOUP_FTP_ERROR (soup_ftp_error_quark ())
+GQuark soup_ftp_error_quark (void);
+
+#define SOUP_FTP_CONNECTION_ERROR (soup_ftp_connection_error_quark ())
+GQuark soup_ftp_connection_error_quark (void);
+
+#endif /* SOUP_FTP_CONNECTION_H */
diff --git a/libsoup/soup-ftp-input-stream.c b/libsoup/soup-ftp-input-stream.c
new file mode 100644
index 0000000..d02512b
--- /dev/null
+++ b/libsoup/soup-ftp-input-stream.c
@@ -0,0 +1,178 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
+/*
+ * soup-ftp-input-stream.c: ftp GInputStream wrapper
+ *
+ * Copyright (C) 2009 FIXME
+ * Copyright (C) 2009 Red Hat, Inc.
+ */
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include "soup-ftp-input-stream.h"
+
+G_DEFINE_TYPE (SoupFTPInputStream, soup_ftp_input_stream, G_TYPE_FILTER_INPUT_STREAM);
+
+enum {
+ PROP_0,
+
+ PROP_FILE_INFO,
+ PROP_CHILDREN
+};
+
+enum {
+ EOF,
+
+ LAST_SIGNAL
+};
+
+static guint signals[LAST_SIGNAL] = { 0 };
+
+struct _SoupFTPInputStreamPrivate {
+ GFileInfo *info;
+ GList *children;
+};
+
+static gssize
+soup_ftp_input_stream_read (GInputStream *stream,
+ void *buffer,
+ gsize count,
+ GCancellable *cancellable,
+ GError **error)
+{
+ gssize nread = 0;
+
+ nread = G_INPUT_STREAM_CLASS (soup_ftp_input_stream_parent_class)->
+ read_fn (stream, buffer, count, cancellable, error);
+
+ if (nread == 0)
+ g_signal_emit (stream, signals[EOF], 0);
+
+ return nread;
+}
+
+static gboolean
+soup_ftp_input_stream_close (GInputStream *stream,
+ GCancellable *cancellable,
+ GError **error)
+{
+ g_signal_emit (stream, signals[EOF], 0);
+
+ return G_INPUT_STREAM_CLASS (soup_ftp_input_stream_parent_class)->
+ close_fn (stream, cancellable, error);
+}
+
+static void
+soup_ftp_input_stream_set_property (GObject *object,
+ guint prop_id,
+ const GValue *value,
+ GParamSpec *pspec)
+{
+ SoupFTPInputStream *sfstream = SOUP_FTP_INPUT_STREAM (object);
+
+ switch (prop_id) {
+ case PROP_FILE_INFO:
+ sfstream->priv->info = g_value_dup_object (value);
+ break;
+ case PROP_CHILDREN:
+ sfstream->priv->children = g_value_get_pointer (value);
+ break;
+ default:
+ G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+ break;
+ }
+}
+
+static void
+soup_ftp_input_stream_get_property (GObject *object,
+ guint prop_id,
+ GValue *value,
+ GParamSpec *pspec)
+{
+ SoupFTPInputStream *sfstream = SOUP_FTP_INPUT_STREAM (object);
+
+ switch (prop_id) {
+ case PROP_FILE_INFO:
+ g_value_set_object (value, sfstream->priv->info);
+ break;
+ case PROP_CHILDREN:
+ g_value_set_pointer (value, sfstream->priv->children);
+ break;
+ default:
+ G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+ break;
+ }
+}
+
+static void
+soup_ftp_input_stream_finalize (GObject *object)
+{
+ SoupFTPInputStream *sfstream = SOUP_FTP_INPUT_STREAM (object);
+
+ if (sfstream->priv->info)
+ g_object_unref (sfstream->priv->info);
+
+ G_OBJECT_CLASS (soup_ftp_input_stream_parent_class)->finalize (object);
+}
+
+static void
+soup_ftp_input_stream_class_init (SoupFTPInputStreamClass *klass)
+{
+ GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
+ GInputStreamClass *input_stream_class = G_INPUT_STREAM_CLASS (klass);
+
+ g_type_class_add_private (klass, sizeof (SoupFTPInputStreamPrivate));
+
+ gobject_class->set_property = soup_ftp_input_stream_set_property;
+ gobject_class->get_property = soup_ftp_input_stream_get_property;
+ gobject_class->finalize = soup_ftp_input_stream_finalize;
+
+ input_stream_class->read_fn = soup_ftp_input_stream_read;
+ input_stream_class->close_fn = soup_ftp_input_stream_close;
+
+ signals[EOF] =
+ g_signal_new ("eof",
+ SOUP_TYPE_FTP_INPUT_STREAM,
+ G_SIGNAL_RUN_FIRST,
+ 0,
+ NULL,
+ NULL,
+ g_cclosure_marshal_VOID__VOID,
+ G_TYPE_NONE, 0);
+
+ g_object_class_install_property (gobject_class,
+ PROP_FILE_INFO,
+ g_param_spec_object ("file-info",
+ "File Information",
+ "Stores information about a file referenced by a SoupFTPInputStream.",
+ G_TYPE_FILE_INFO,
+ G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
+ G_PARAM_STATIC_STRINGS));
+
+ g_object_class_install_property (gobject_class,
+ PROP_CHILDREN,
+ g_param_spec_pointer ("children",
+ "Directory's Children",
+ "Stores a list of GFileInfo",
+ G_PARAM_READWRITE |
+ G_PARAM_STATIC_STRINGS));
+}
+
+static void
+soup_ftp_input_stream_init (SoupFTPInputStream *sfstream)
+{
+ sfstream->priv = G_TYPE_INSTANCE_GET_PRIVATE (sfstream, SOUP_TYPE_FTP_INPUT_STREAM, SoupFTPInputStreamPrivate);
+}
+
+GInputStream *
+soup_ftp_input_stream_new (GInputStream *base_stream,
+ GFileInfo *file_info,
+ GList *children)
+{
+ return g_object_new (SOUP_TYPE_FTP_INPUT_STREAM,
+ "base-stream", base_stream,
+ "file-info", file_info,
+ "children", children,
+ NULL);
+}
diff --git a/libsoup/soup-ftp-input-stream.h b/libsoup/soup-ftp-input-stream.h
new file mode 100644
index 0000000..e279413
--- /dev/null
+++ b/libsoup/soup-ftp-input-stream.h
@@ -0,0 +1,43 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
+/*
+ * Copyright (C) 2009 Red Hat, Inc.
+ */
+
+#ifndef SOUP_FTP_INPUT_STREAM_H
+#define SOUP_FTP_INPUT_STREAM_H
+
+#include <gio/gio.h>
+
+G_BEGIN_DECLS
+
+#define SOUP_TYPE_FTP_INPUT_STREAM (soup_ftp_input_stream_get_type ())
+#define SOUP_FTP_INPUT_STREAM(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), SOUP_TYPE_FTP_INPUT_STREAM, SoupFTPInputStream))
+#define SOUP_FTP_INPUT_STREAM_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), SOUP_TYPE_FTP_INPUT_STREAM, SoupFTPInputStreamClass))
+#define SOUP_IS_FTP_INPUT_STREAM(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), SOUP_TYPE_FTP_INPUT_STREAM))
+#define SOUP_IS_FTP_INPUT_STREAM_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((obj), SOUP_TYPE_FTP_INPUT_STREAM))
+#define SOUP_FTP_INPUT_STREAM_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), SOUP_TYPE_FTP_INPUT_STREAM, SoupFTPInputStreamClass))
+
+typedef struct _SoupFTPInputStream SoupFTPInputStream;
+typedef struct _SoupFTPInputStreamClass SoupFTPInputStreamClass;
+typedef struct _SoupFTPInputStreamPrivate SoupFTPInputStreamPrivate;
+
+struct _SoupFTPInputStream {
+ GFilterInputStream parent;
+
+ SoupFTPInputStreamPrivate *priv;
+};
+
+struct _SoupFTPInputStreamClass {
+ GFilterInputStreamClass parent;
+
+};
+
+GType soup_ftp_input_stream_get_type (void);
+
+GInputStream *soup_ftp_input_stream_new (GInputStream *base_stream,
+ GFileInfo *file_info,
+ GList *children);
+
+G_END_DECLS
+
+#endif /*SOUP_FTP_INPUT_STREAM_H*/
diff --git a/libsoup/soup-request-file.c b/libsoup/soup-request-file.c
index 13f2002..55168af 100644
--- a/libsoup/soup-request-file.c
+++ b/libsoup/soup-request-file.c
@@ -12,7 +12,6 @@
#include <glib/gi18n.h>
#include "soup-request-file.h"
-#include "soup-request.h"
#include "soup-session-feature.h"
#include "soup-session.h"
#include "soup-uri.h"
diff --git a/libsoup/soup-request-file.h b/libsoup/soup-request-file.h
index 7e486a2..0507132 100644
--- a/libsoup/soup-request-file.h
+++ b/libsoup/soup-request-file.h
@@ -6,7 +6,7 @@
#ifndef SOUP_REQUEST_FILE_H
#define SOUP_REQUEST_FILE_H 1
-#include <libsoup/soup-types.h>
+#include "soup-request.h"
#define SOUP_TYPE_REQUEST_FILE (soup_request_file_get_type ())
#define SOUP_REQUEST_FILE(object) (G_TYPE_CHECK_INSTANCE_CAST ((object), SOUP_TYPE_REQUEST_FILE, SoupRequestFile))
diff --git a/libsoup/soup-request-ftp.c b/libsoup/soup-request-ftp.c
new file mode 100644
index 0000000..45442bd
--- /dev/null
+++ b/libsoup/soup-request-ftp.c
@@ -0,0 +1,204 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
+/*
+ * soup-request-ftp.c: ftp: URI request object
+ *
+ * Copyright (C) 2009 Red Hat, Inc.
+ */
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <glib/gi18n.h>
+
+#include "soup-request-ftp.h"
+#include "soup-ftp-connection.h"
+#include "soup-session-feature.h"
+#include "soup-session.h"
+#include "soup-uri.h"
+
+static void soup_request_ftp_request_interface_init (SoupRequestInterface *request_interface);
+static void soup_request_ftp_initable_interface_init (GInitableIface *initable_interface);
+
+G_DEFINE_TYPE_WITH_CODE (SoupRequestFtp, soup_request_ftp, G_TYPE_OBJECT,
+ G_IMPLEMENT_INTERFACE (SOUP_TYPE_REQUEST,
+ soup_request_ftp_request_interface_init)
+ G_IMPLEMENT_INTERFACE (G_TYPE_INITABLE,
+ soup_request_ftp_initable_interface_init))
+
+struct _SoupRequestFtpPrivate {
+ SoupURI *uri;
+ SoupFTPConnection *conn;
+};
+
+enum {
+ PROP_0,
+
+ PROP_URI
+};
+
+static void soup_request_ftp_set_property (GObject *object, guint prop_id,
+ const GValue *value, GParamSpec *pspec);
+static void soup_request_ftp_get_property (GObject *object, guint prop_id,
+ GValue *value, GParamSpec *pspec);
+static void soup_request_ftp_finalize (GObject *object);
+
+static gboolean soup_request_ftp_initable_init (GInitable *initable,
+ GCancellable *cancellable,
+ GError **error);
+
+static GInputStream *soup_request_ftp_send (SoupRequest *request,
+ GCancellable *cancellable,
+ GError **error);
+static void soup_request_ftp_send_async (SoupRequest *request,
+ GCancellable *cancellable,
+ GAsyncReadyCallback callback,
+ gpointer user_data);
+static GInputStream *soup_request_ftp_send_finish (SoupRequest *request,
+ GAsyncResult *result,
+ GError **error);
+
+static void
+soup_request_ftp_class_init (SoupRequestFtpClass *request_ftp_class)
+{
+ GObjectClass *object_class = G_OBJECT_CLASS (request_ftp_class);
+
+ g_type_class_add_private (request_ftp_class, sizeof (SoupRequestFtpPrivate));
+
+ object_class->finalize = soup_request_ftp_finalize;
+ object_class->set_property = soup_request_ftp_set_property;
+ object_class->get_property = soup_request_ftp_get_property;
+
+ g_object_class_override_property (object_class, PROP_URI, "uri");
+}
+
+static void
+soup_request_ftp_request_interface_init (SoupRequestInterface *request_interface)
+{
+ request_interface->send = soup_request_ftp_send;
+ request_interface->send_async = soup_request_ftp_send_async;
+ request_interface->send_finish = soup_request_ftp_send_finish;
+}
+
+static void
+soup_request_ftp_initable_interface_init (GInitableIface *initable_interface)
+{
+ initable_interface->init = soup_request_ftp_initable_init;
+}
+
+static void
+soup_request_ftp_init (SoupRequestFtp *ftp)
+{
+ ftp->priv = G_TYPE_INSTANCE_GET_PRIVATE (ftp, SOUP_TYPE_REQUEST_FTP, SoupRequestFtpPrivate);
+}
+
+static gboolean
+soup_request_ftp_initable_init (GInitable *initable,
+ GCancellable *cancellable,
+ GError **error)
+{
+ SoupRequestFtp *ftp = SOUP_REQUEST_FTP (initable);
+ const char *host;
+
+ host = ftp->priv->uri->host;
+ if (!host) {
+ if (error) {
+ char *uri_string = soup_uri_to_string (ftp->priv->uri, FALSE);
+ g_set_error (error, SOUP_ERROR, SOUP_ERROR_BAD_URI,
+ _("Invalid 'ftp' URI: %s"), uri_string);
+ g_free (uri_string);
+ }
+ return FALSE;
+ }
+
+ ftp->priv->conn = soup_ftp_connection_new ();
+
+ return TRUE;
+}
+
+static void
+soup_request_ftp_finalize (GObject *object)
+{
+ SoupRequestFtp *ftp = SOUP_REQUEST_FTP (object);
+
+ if (ftp->priv->uri)
+ soup_uri_free (ftp->priv->uri);
+
+ G_OBJECT_CLASS (soup_request_ftp_parent_class)->finalize (object);
+}
+
+static void
+soup_request_ftp_set_property (GObject *object, guint prop_id,
+ const GValue *value, GParamSpec *pspec)
+{
+ SoupRequestFtp *ftp = SOUP_REQUEST_FTP (object);
+
+ switch (prop_id) {
+ case PROP_URI:
+ if (ftp->priv->uri)
+ soup_uri_free (ftp->priv->uri);
+ ftp->priv->uri = g_value_dup_boxed (value);
+ break;
+ default:
+ G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+ break;
+ }
+}
+
+static void
+soup_request_ftp_get_property (GObject *object, guint prop_id,
+ GValue *value, GParamSpec *pspec)
+{
+ SoupRequestFtp *ftp = SOUP_REQUEST_FTP (object);
+
+ switch (prop_id) {
+ case PROP_URI:
+ g_value_set_boxed (value, ftp->priv->uri);
+ break;
+ default:
+ G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+ break;
+ }
+}
+
+/* FIXME: cache SoupFTPConnection objects! */
+
+static GInputStream *
+soup_request_ftp_send (SoupRequest *request,
+ GCancellable *cancellable,
+ GError **error)
+{
+ SoupRequestFtp *ftp = SOUP_REQUEST_FTP (request);
+
+ return soup_ftp_connection_load_uri (ftp->priv->conn,
+ ftp->priv->uri,
+ cancellable,
+ error);
+}
+
+static void
+soup_request_ftp_send_async (SoupRequest *request,
+ GCancellable *cancellable,
+ GAsyncReadyCallback callback,
+ gpointer user_data)
+{
+ SoupRequestFtp *ftp = SOUP_REQUEST_FTP (request);
+
+ soup_ftp_connection_load_uri_async (ftp->priv->conn,
+ ftp->priv->uri,
+ cancellable,
+ callback,
+ user_data);
+}
+
+static GInputStream *
+soup_request_ftp_send_finish (SoupRequest *request,
+ GAsyncResult *result,
+ GError **error)
+{
+ SoupRequestFtp *ftp = SOUP_REQUEST_FTP (request);
+
+ return soup_ftp_connection_load_uri_finish (ftp->priv->conn,
+ result,
+ error);
+}
diff --git a/libsoup/soup-request-ftp.h b/libsoup/soup-request-ftp.h
new file mode 100644
index 0000000..a69a856
--- /dev/null
+++ b/libsoup/soup-request-ftp.h
@@ -0,0 +1,33 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
+/*
+ * Copyright (C) 2009 Red Hat, Inc.
+ */
+
+#ifndef SOUP_REQUEST_FTP_H
+#define SOUP_REQUEST_FTP_H 1
+
+#include "soup-request.h"
+
+#define SOUP_TYPE_REQUEST_FTP (soup_request_ftp_get_type ())
+#define SOUP_REQUEST_FTP(object) (G_TYPE_CHECK_INSTANCE_CAST ((object), SOUP_TYPE_REQUEST_FTP, SoupRequestFtp))
+#define SOUP_REQUEST_FTP_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), SOUP_TYPE_REQUEST_FTP, SoupRequestFtpInterface))
+#define SOUP_IS_REQUEST_FTP(object) (G_TYPE_CHECK_INSTANCE_TYPE ((object), SOUP_TYPE_REQUEST_FTP))
+#define SOUP_IS_REQUEST_FTP_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), SOUP_TYPE_REQUEST_FTP))
+#define SOUP_REQUEST_FTP_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_INTERFACE ((obj), SOUP_TYPE_REQUEST_FTP, SoupRequestFtpInterface))
+
+typedef struct _SoupRequestFtpPrivate SoupRequestFtpPrivate;
+
+typedef struct {
+ GObject parent;
+
+ SoupRequestFtpPrivate *priv;
+} SoupRequestFtp;
+
+typedef struct {
+ GObjectClass parent;
+
+} SoupRequestFtpClass;
+
+GType soup_request_ftp_get_type (void);
+
+#endif /* SOUP_REQUEST_FTP_H */
diff --git a/libsoup/soup-session.c b/libsoup/soup-session.c
index 1e44adf..03ae46f 100644
--- a/libsoup/soup-session.c
+++ b/libsoup/soup-session.c
@@ -28,6 +28,7 @@
#include "soup-proxy-resolver-static.h"
#include "soup-proxy-uri-resolver.h"
#include "soup-request-file.h"
+#include "soup-request-ftp.h"
#include "soup-session.h"
#include "soup-session-feature.h"
#include "soup-session-private.h"
@@ -2003,9 +2004,9 @@ init_request_types (SoupSessionPrivate *priv)
GSIZE_TO_POINTER (SOUP_TYPE_REQUEST_HTTP));
g_hash_table_insert (priv->request_types, "https",
GSIZE_TO_POINTER (SOUP_TYPE_REQUEST_HTTP));
+#endif
g_hash_table_insert (priv->request_types, "ftp",
GSIZE_TO_POINTER (SOUP_TYPE_REQUEST_FTP));
-#endif
}
void
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]