annotate lib/fsplib/fsplib.c @ 662:be9663e0be00

2005-1-16 Brian Masney <masneyb@gftp.org> * lib/fsplib/COPYING lib/fsplib/Makefile.am lib/fsplib/fsplib.c lib/fsplib/fsplib.h lib/fsplib/lock.c lib/fsplib/lock.h - added FSPLIB This library is written by Radim Kolar <hsn@netmag.cz> and was included with his permission
author masneyb
date Sun, 16 Jan 2005 16:12:13 +0000
parents
children 6b6fbcf76d00
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
662
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1 /*
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
2 This file is part of fsplib - FSP protocol stack implemented in C
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
3 language. See http://fsp.sourceforge.net for more information.
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
4
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
5 Copyright (c) 2003-2004 by Radim HSN Kolar (hsn@netmag.cz)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
6
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
7 You may copy or modify this file in any manner you wish, provided
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
8 that this notice is always included, and that you hold the author
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
9 harmless for any loss or damage resulting from the installation or
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
10 use of this software.
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
11
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
12 This is a free software. Be creative.
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
13 Let me know of any bugs and suggestions.
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
14 */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
15 #include <sys/types.h>
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
16 #include <sys/socket.h>
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
17 #include <sys/time.h>
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
18 #include <netinet/in.h>
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
19 #include <netdb.h>
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
20 #include <stdlib.h>
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
21 #include <stdio.h>
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
22 #include <errno.h>
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
23 #include <unistd.h>
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
24 #include <string.h>
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
25 #include <sys/stat.h>
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
26 #include <dirent.h>
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
27 #include "fsplib.h"
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
28 #include "lock.h"
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
29
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
30 /* ************ Internal functions **************** */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
31
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
32 /* builds filename in packet output buffer, appends password if needed */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
33 static int buildfilename(const FSP_SESSION *s,FSP_PKT *out,const char *dirname)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
34 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
35 int len;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
36
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
37 len=strlen(dirname);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
38 if(len >= FSP_SPACE - 1)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
39 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
40 errno = ENAMETOOLONG;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
41 return -1;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
42 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
43 /* copy name + \0 */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
44 memcpy(out->buf,dirname,len+1);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
45 out->len=len;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
46 if(s->password)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
47 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
48 out->buf[len]='\n';
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
49 out->len++;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
50
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
51 len=strlen(s->password);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
52 if(out->len+ len >= FSP_SPACE -1 )
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
53 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
54 errno = ENAMETOOLONG;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
55 return -1;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
56 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
57 memcpy(out->buf+out->len,s->password,len+1);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
58 out->len+=len;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
59 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
60 /* add terminating \0 */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
61 out->len++;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
62 return 0;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
63 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
64
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
65 /* simple FSP command */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
66 static int simplecommand(FSP_SESSION *s,const char *directory,unsigned char command)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
67 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
68 FSP_PKT in,out;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
69
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
70 if(buildfilename(s,&out,directory))
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
71 return -1;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
72 out.cmd=command;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
73 out.xlen=0;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
74 out.pos=0;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
75
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
76 if(fsp_transaction(s,&out,&in))
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
77 return -1;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
78
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
79 if(in.cmd == FSP_CC_ERR)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
80 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
81 errno = EPERM;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
82 return -1;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
83 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
84
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
85 if(in.cmd != command)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
86 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
87 errno = ENOMSG;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
88 return -1;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
89 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
90
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
91 errno = 0;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
92 return 0;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
93 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
94 /* Get directory part of filename. You must free() the result */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
95 static char * directoryfromfilename(const char *filename)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
96 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
97 char *result;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
98 char *tmp;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
99 int pos;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
100
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
101 result=strrchr(filename,'/');
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
102 if (result == NULL)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
103 return strdup("");
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
104 pos=result-filename;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
105 tmp=malloc(pos+1);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
106 if(!tmp)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
107 return NULL;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
108 memcpy(tmp,filename,pos);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
109 tmp[pos]='\0';
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
110 return tmp;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
111 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
112
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
113 /* ************ Packet encoding / decoding *************** */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
114
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
115 /* write binary representation of FSP packet p into *space. */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
116 /* returns number of bytes used or zero on error */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
117 /* Space must be long enough to hold created packet. */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
118 /* Maximum created packet size is FSP_MAXPACKET */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
119
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
120 size_t fsp_pkt_write(const FSP_PKT *p,void *space)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
121 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
122 size_t used;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
123 unsigned char *ptr;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
124 int checksum;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
125 size_t i;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
126
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
127 if(p->xlen + p->len > FSP_SPACE )
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
128 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
129 /* not enough space */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
130 errno = EMSGSIZE;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
131 return 0;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
132 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
133 ptr=space;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
134 /* pack header */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
135 ptr[FSP_OFFSET_CMD]=p->cmd;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
136 ptr[FSP_OFFSET_SUM]=0;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
137 *(uint16_t *)(ptr+FSP_OFFSET_KEY)=htons(p->key);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
138 *(uint16_t *)(ptr+FSP_OFFSET_SEQ)=htons(p->seq);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
139 *(uint16_t *)(ptr+FSP_OFFSET_LEN)=htons(p->len);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
140 *(uint32_t *)(ptr+FSP_OFFSET_POS)=htonl(p->pos);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
141 used=FSP_HSIZE;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
142 /* copy data block */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
143 memcpy(ptr+FSP_HSIZE,p->buf,p->len);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
144 used+=p->len;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
145 /* copy extra data block */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
146 memcpy(ptr+used,p->buf+p->len,p->xlen);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
147 used+=p->xlen;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
148 /* compute checksum */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
149 checksum = 0;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
150 for(i=0;i<used;i++)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
151 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
152 checksum += ptr[i];
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
153 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
154 checksum +=used;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
155 ptr[FSP_OFFSET_SUM] = checksum + (checksum >> 8);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
156 return used;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
157 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
158
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
159 /* read binary representation of FSP packet received from network into p */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
160 /* return zero on success */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
161 int fsp_pkt_read(FSP_PKT *p,const void *space,size_t recv_len)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
162 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
163 int mysum;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
164 size_t i;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
165 const unsigned char *ptr;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
166
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
167 if(recv_len<FSP_HSIZE)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
168 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
169 /* too short */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
170 errno = ERANGE;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
171 return -1;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
172 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
173 if(recv_len>FSP_MAXPACKET)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
174 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
175 /* too long */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
176 errno = EMSGSIZE;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
177 return -1;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
178 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
179
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
180 ptr=space;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
181 /* check sum */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
182 mysum=-ptr[FSP_OFFSET_SUM];
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
183 for(i=0;i<recv_len;i++)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
184 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
185 mysum+=ptr[i];
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
186 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
187
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
188 mysum = (mysum + (mysum >> 8)) & 0xff;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
189
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
190 if(mysum != ptr[FSP_OFFSET_SUM])
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
191 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
192 /* checksum failed */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
193
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
194 #ifdef MAINTAINER_MODE
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
195 printf("mysum: %x, got %x\n",mysum,ptr[FSP_OFFSET_SUM]);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
196 #endif
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
197 errno = EIO;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
198 return -1;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
199 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
200
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
201 /* unpack header */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
202 p->cmd=ptr[FSP_OFFSET_CMD];
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
203 p->sum=mysum;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
204 p->key=ntohs( *(const uint16_t *)(ptr+FSP_OFFSET_KEY) );
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
205 p->seq=ntohs( *(const uint16_t *)(ptr+FSP_OFFSET_SEQ) );
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
206 p->len=ntohs( *(const uint16_t *)(ptr+FSP_OFFSET_LEN) );
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
207 p->pos=ntohl( *(const uint32_t *)(ptr+FSP_OFFSET_POS) );
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
208 if(p->len > recv_len)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
209 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
210 /* bad length field, should not never happen */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
211 errno = EMSGSIZE;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
212 return -1;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
213 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
214 p->xlen=recv_len - p->len - FSP_HSIZE;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
215 /* now copy data */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
216 memcpy(p->buf,ptr+FSP_HSIZE,recv_len - FSP_HSIZE);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
217 return 0;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
218 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
219
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
220 /* ****************** packet sending functions ************** */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
221
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
222 /* make one send + receive transaction with server */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
223 /* outgoing packet is in p, incomming in rpkt */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
224 int fsp_transaction(FSP_SESSION *s,FSP_PKT *p,FSP_PKT *rpkt)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
225 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
226 char buf[FSP_MAXPACKET];
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
227 size_t l;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
228 ssize_t r;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
229 fd_set mask;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
230 struct timeval start[8],stop;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
231 int i;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
232 unsigned int retry,dupes;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
233 int w_delay; /* how long to wait on next packet */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
234 int f_delay; /* how long to wait after first send */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
235 int l_delay; /* last delay */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
236 unsigned int t_delay; /* time from first send */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
237
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
238
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
239 if(p == rpkt)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
240 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
241 errno = EINVAL;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
242 return -2;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
243 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
244 FD_ZERO(&mask);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
245 /* get the next key */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
246 p->key = client_get_key((FSP_LOCK *)s->lock);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
247
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
248 dupes = retry = 0;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
249 s->seq = (s-> seq + 0x08) & 0xfff8;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
250 t_delay = 0;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
251 /* compute initial delay here */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
252 /* we are using hardcoded value for now */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
253 f_delay = 1340;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
254 for(;;retry++)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
255 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
256 if(t_delay >= s->timeout)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
257 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
258 client_set_key((FSP_LOCK *)s->lock,p->key);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
259 errno = ETIMEDOUT;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
260 return -1;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
261 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
262 /* make a packet */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
263 p->seq = (s->seq) | (retry & 0x7);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
264 l=fsp_pkt_write(p,buf);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
265
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
266 /* We should compute next delay wait time here */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
267 gettimeofday(&start[retry & 0x7],NULL);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
268 if(retry == 0 )
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
269 w_delay=f_delay;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
270 else
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
271 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
272 w_delay=l_delay*3/2;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
273 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
274
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
275 l_delay=w_delay;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
276
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
277 /* send packet */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
278 if( send(s->fd,buf,l,0) < 0 )
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
279 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
280 #ifdef MAINTAINER_MODE
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
281 printf("Send failed.\n");
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
282 #endif
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
283 if(errno == EBADF || errno == ENOTSOCK)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
284 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
285 client_set_key((FSP_LOCK *)s->lock,p->key);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
286 errno = EBADF;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
287 return -1;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
288 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
289 /* io terror */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
290 sleep(1);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
291 /* avoid wasting retry slot */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
292 retry--;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
293 t_delay += 1000;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
294 continue;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
295 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
296
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
297 /* keep delay value within sane limits */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
298 if (w_delay > (int) s->maxdelay)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
299 w_delay=s->maxdelay;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
300 else
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
301 if(w_delay < 1000 )
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
302 w_delay = 1000;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
303
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
304 t_delay += w_delay;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
305 /* receive loop */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
306 while(1)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
307 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
308 if(w_delay <= 0 ) break;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
309 /* convert w_delay to timeval */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
310 stop.tv_sec=w_delay/1000;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
311 stop.tv_usec=(w_delay % 1000)*1000;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
312 FD_SET(s->fd,&mask);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
313 i=select(s->fd+1,&mask,NULL,NULL,&stop);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
314 if(i==0)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
315 break; /* timed out */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
316 if(i<0)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
317 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
318 if(errno==EINTR)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
319 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
320 /* lower w_delay */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
321 gettimeofday(&stop,NULL);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
322 w_delay-=1000*(stop.tv_sec - start[retry & 0x7].tv_sec);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
323 w_delay-= (stop.tv_usec - start[retry & 0x7].tv_usec)/1000;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
324 continue;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
325 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
326 /* hard select error */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
327 client_set_key((FSP_LOCK *)s->lock,p->key);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
328 return -1;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
329 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
330 r=recv(s->fd,buf,FSP_MAXPACKET,0);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
331 if(r < 0 )
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
332 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
333 /* serious recv error */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
334 client_set_key((FSP_LOCK *)s->lock,p->key);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
335 return -1;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
336 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
337
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
338 gettimeofday(&stop,NULL);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
339 w_delay-=1000*(stop.tv_sec - start[retry & 0x7].tv_sec);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
340 w_delay-= (stop.tv_usec - start[retry & 0x7].tv_usec)/1000;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
341
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
342 /* process received packet */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
343 if ( fsp_pkt_read(rpkt,buf,r) < 0)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
344 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
345 /* unpack failed */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
346 continue;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
347 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
348
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
349 /* check sequence number */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
350 if( (rpkt->seq & 0xfff8) != s->seq )
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
351 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
352 #ifdef MAINTAINER_MODE
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
353 printf("dupe\n");
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
354 #endif
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
355 /* duplicate */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
356 dupes++;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
357 continue;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
358 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
359
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
360 /* now we have a correct packet */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
361
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
362 /* compute rtt delay */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
363 w_delay=1000*(stop.tv_sec - start[retry & 0x7].tv_sec);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
364 w_delay+=(stop.tv_usec - start[retry & 0x7].tv_usec)/1000;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
365 /* update last stats */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
366 s->last_rtt=w_delay;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
367 s->last_delay=f_delay;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
368 s->last_dupes=dupes;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
369 s->last_resends=retry;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
370 /* update cumul. stats */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
371 s->dupes+=dupes;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
372 s->resends+=retry;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
373 s->trips++;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
374 s->rtts+=w_delay;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
375
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
376 /* grab a next key */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
377 client_set_key((FSP_LOCK *)s->lock,rpkt->key);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
378 errno = 0;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
379 return 0;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
380 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
381 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
382 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
383
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
384 /* ******************* Session management functions ************ */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
385
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
386 /* initializes a session */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
387 FSP_SESSION * fsp_open_session(const char *host,unsigned short port,const char *password)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
388 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
389 FSP_SESSION *s;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
390 int fd;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
391 struct addrinfo hints,*res;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
392 char port_s[6];
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
393 struct sockaddr_in *addrin;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
394 FSP_LOCK *lock;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
395
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
396 memset (&hints, 0, sizeof (hints));
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
397 /* fspd do not supports inet6 */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
398 hints.ai_family = PF_INET;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
399 hints.ai_socktype = SOCK_DGRAM;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
400
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
401 if (port == 0)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
402 strcpy(port_s,"fsp");
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
403 else
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
404 sprintf(port_s,"%hu",port);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
405
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
406 if ( getaddrinfo(host,port_s,&hints,&res) )
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
407 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
408 return NULL; /* host not found */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
409 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
410
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
411 /* create socket */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
412 fd=socket(res->ai_family,res->ai_socktype,res->ai_protocol);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
413 if ( fd < 0)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
414 return NULL;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
415
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
416 /* connect socket */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
417 if( connect(fd, res->ai_addr, res->ai_addrlen))
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
418 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
419 close(fd);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
420 return NULL;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
421 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
422
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
423 /* allocate memory */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
424 s=calloc(1,sizeof(FSP_SESSION));
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
425 if ( !s )
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
426 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
427 close(fd);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
428 errno = ENOMEM;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
429 return NULL;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
430 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
431
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
432 lock=malloc(sizeof(FSP_LOCK));
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
433
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
434 if ( !lock )
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
435 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
436 close(fd);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
437 free(s);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
438 errno = ENOMEM;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
439 return NULL;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
440 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
441
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
442 s->lock=lock;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
443
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
444 /* init locking subsystem */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
445 addrin = (struct sockaddr_in *)res->ai_addr;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
446 if ( client_init_key( (FSP_LOCK *)s->lock,addrin->sin_addr.s_addr,ntohs(addrin->sin_port)))
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
447 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
448 free(s);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
449 close(fd);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
450 free(lock);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
451 return NULL;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
452 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
453
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
454 s->fd=fd;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
455 s->timeout=300000; /* 5 minutes */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
456 s->maxdelay=60000; /* 1 minute */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
457 s->seq=random();
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
458 if ( password )
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
459 s->password = strdup(password);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
460 return s;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
461 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
462
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
463 /* closes a session */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
464 void fsp_close_session(FSP_SESSION *s)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
465 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
466 FSP_PKT bye,in;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
467
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
468 if( s == NULL)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
469 return;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
470 if ( s->fd == -1)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
471 return;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
472 /* Send bye packet */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
473 bye.cmd=FSP_CC_BYE;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
474 bye.len=bye.xlen=0;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
475 bye.pos=0;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
476 s->timeout=7000;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
477 fsp_transaction(s,&bye,&in);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
478
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
479 close(s->fd);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
480 if (s->password) free(s->password);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
481 client_destroy_key((FSP_LOCK *)s->lock);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
482 free(s->lock);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
483 memset(s,0,sizeof(FSP_SESSION));
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
484 s->fd=-1;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
485 free(s);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
486 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
487
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
488 /* *************** Directory listing functions *************** */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
489
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
490 /* get a directory listing from a server */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
491 FSP_DIR * fsp_opendir(FSP_SESSION *s,const char *dirname)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
492 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
493 FSP_PKT in,out;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
494 int pos;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
495 unsigned short blocksize;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
496 FSP_DIR *dir;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
497 unsigned char *tmp;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
498
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
499 if (s == NULL) return NULL;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
500 if (dirname == NULL) return NULL;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
501
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
502 if(buildfilename(s,&out,dirname))
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
503 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
504 return NULL;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
505 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
506 pos=0;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
507 blocksize=0;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
508 dir=NULL;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
509 out.cmd = FSP_CC_GET_DIR;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
510 out.xlen=0;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
511
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
512 /* load directory listing from the server */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
513 while(1)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
514 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
515 out.pos=pos;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
516 if ( fsp_transaction(s,&out,&in) )
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
517 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
518 pos = -1;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
519 break;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
520 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
521 if ( in.cmd != FSP_CC_GET_DIR )
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
522 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
523 /* bad reply from the server */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
524 pos = -1;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
525 break;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
526 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
527 /* End of directory? */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
528 if ( in.len == 0)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
529 break;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
530 /* set blocksize */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
531 if (blocksize == 0 )
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
532 blocksize = in.len;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
533 /* alloc directory */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
534 if (dir == NULL)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
535 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
536 dir = calloc(1,sizeof(FSP_DIR));
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
537 if (dir == NULL)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
538 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
539 pos = -1;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
540 break;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
541 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
542 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
543 /* append data */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
544 tmp=realloc(dir->data,pos+in.len);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
545 if(tmp == NULL)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
546 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
547 pos = -1;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
548 break;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
549 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
550 dir->data=tmp;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
551 memcpy(dir->data + pos, in.buf,in.len);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
552 pos += in.len;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
553 if (in.len < blocksize)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
554 /* last block is smaller */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
555 break;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
556 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
557 if (pos == -1)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
558 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
559 /* failure */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
560 if (dir)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
561 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
562 if(dir->data)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
563 free(dir->data);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
564 free(dir);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
565 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
566 return NULL;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
567 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
568
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
569 dir->inuse=1;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
570 dir->blocksize=blocksize;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
571 dir->dirname=strdup(dirname);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
572 dir->datasize=pos;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
573
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
574 return dir;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
575 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
576
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
577 int fsp_readdir_r(FSP_DIR *dir,struct dirent *entry, struct dirent **result)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
578 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
579 FSP_RDENTRY fentry,*fresult;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
580 int rc;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
581
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
582 if (dir == NULL || entry == NULL || *result == NULL)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
583 return -EINVAL;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
584 if (dir->dirpos<0 || dir->dirpos % 4)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
585 return -ESPIPE;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
586
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
587 rc=fsp_readdir_native(dir,&fentry,&fresult);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
588
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
589 if (rc != 0)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
590 return rc;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
591
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
592 /* convert FSP dirent to OS dirent */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
593
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
594 if (fentry.type == FSP_RDTYPE_DIR )
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
595 entry->d_type=DT_DIR;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
596 else
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
597 entry->d_type=DT_REG;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
598 entry->d_fileno = 10;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
599 entry->d_reclen = fentry.reclen;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
600 strncpy(entry->d_name,fentry.name,MAXNAMLEN);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
601
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
602 if (fentry.namlen > MAXNAMLEN)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
603 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
604 entry->d_name[MAXNAMLEN + 1 ] = '\0';
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
605 #ifdef HAVE_NAMLEN
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
606 entry->d_namlen = MAXNAMLEN;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
607 } else
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
608 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
609 entry->d_namlen = fentry.namlen;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
610 #endif
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
611 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
612
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
613 if (fresult == &fentry )
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
614 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
615 *result = entry;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
616 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
617 else
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
618 *result = NULL;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
619
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
620 return 0;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
621 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
622
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
623 /* native FSP directory reader */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
624 int fsp_readdir_native(FSP_DIR *dir,FSP_RDENTRY *entry, FSP_RDENTRY **result)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
625 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
626 unsigned char ftype;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
627 int namelen;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
628
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
629 if (dir == NULL || entry == NULL || *result == NULL)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
630 return -EINVAL;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
631 if (dir->dirpos<0 || dir->dirpos % 4)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
632 return -ESPIPE;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
633
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
634 while(1)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
635 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
636 if ( dir->dirpos >= (int)dir->datasize )
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
637 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
638 /* end of the directory */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
639 *result = NULL;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
640 return 0;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
641 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
642 if (dir->blocksize - (dir->dirpos % dir->blocksize) < 9)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
643 ftype= FSP_RDTYPE_SKIP;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
644 else
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
645 /* get the file type */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
646 ftype=dir->data[dir->dirpos+8];
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
647
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
648 if (ftype == FSP_RDTYPE_END )
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
649 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
650 dir->dirpos=dir->datasize;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
651 continue;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
652 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
653 if (ftype == FSP_RDTYPE_SKIP )
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
654 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
655 /* skip to next directory block */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
656 dir->dirpos = ( dir->dirpos / dir->blocksize + 1 ) * dir->blocksize;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
657 #ifdef MAINTAINER_MODE
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
658 printf("new block dirpos: %d\n",dir->dirpos);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
659 #endif
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
660 continue;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
661 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
662 /* extract binary data */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
663 entry->lastmod=ntohl( *(const uint32_t *)( dir->data+ dir->dirpos ));
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
664 entry->size=ntohl( *(const uint32_t *)(dir->data+ dir->dirpos +4 ));
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
665 entry->type=ftype;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
666
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
667 /* skip file date and file size */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
668 dir->dirpos += 9;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
669 /* read file name */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
670 entry->name[255 + 1] = '\0';
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
671 strncpy(entry->name,(char *)( dir->data + dir->dirpos ),MAXNAMLEN);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
672 namelen = strlen( (char *) dir->data+dir->dirpos);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
673 /* skip over file name */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
674 dir->dirpos += namelen +1;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
675
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
676 /* set entry namelen field */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
677 if (namelen > 255)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
678 entry->namlen = 255;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
679 else
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
680 entry->namlen = namelen;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
681 /* set record length */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
682 entry->reclen = 10+namelen;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
683
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
684 /* pad to 4 byte boundary */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
685 while( dir->dirpos & 0x3 )
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
686 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
687 dir->dirpos++;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
688 entry->reclen++;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
689 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
690
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
691 /* and return it */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
692 *result=entry;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
693 return 0;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
694 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
695 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
696
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
697 struct dirent * fsp_readdir(FSP_DIR *dirp)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
698 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
699 static struct dirent entry;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
700 struct dirent *result;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
701
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
702
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
703 if (dirp == NULL) return NULL;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
704 if ( fsp_readdir_r(dirp,&entry,&result) )
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
705 return NULL;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
706 else
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
707 return result;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
708 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
709
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
710 long fsp_telldir(FSP_DIR *dirp)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
711 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
712 return dirp->dirpos;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
713 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
714
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
715 void fsp_seekdir(FSP_DIR *dirp, long loc)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
716 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
717 dirp->dirpos=loc;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
718 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
719
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
720 void fsp_rewinddir(FSP_DIR *dirp)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
721 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
722 dirp->dirpos=0;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
723 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
724
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
725 int fsp_closedir(FSP_DIR *dirp)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
726 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
727 if (dirp == NULL)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
728 return -1;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
729 if(dirp->dirname) free(dirp->dirname);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
730 free(dirp->data);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
731 free(dirp);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
732 return 0;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
733 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
734
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
735 /* ***************** File input/output functions ********* */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
736 FSP_FILE * fsp_fopen(FSP_SESSION *session, const char *path,const char *modeflags)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
737 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
738 FSP_FILE *f;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
739
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
740 if(session == NULL || path == NULL || modeflags == NULL)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
741 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
742 errno = EINVAL;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
743 return NULL;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
744 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
745
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
746 f=calloc(1,sizeof(FSP_FILE));
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
747 if (f == NULL)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
748 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
749 return NULL;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
750 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
751
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
752 /* check and parse flags */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
753 switch (*modeflags++)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
754 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
755 case 'r':
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
756 break;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
757 case 'w':
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
758 f->writing=1;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
759 break;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
760 case 'a':
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
761 /* not supported */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
762 free(f);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
763 errno = ENOTSUP;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
764 return NULL;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
765 default:
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
766 free(f);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
767 errno = EINVAL;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
768 return NULL;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
769 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
770
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
771 if (*modeflags == '+' || ( *modeflags=='b' && modeflags[1]=='+'))
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
772 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
773 free(f);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
774 errno = ENOTSUP;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
775 return NULL;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
776 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
777
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
778 /* build request packet */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
779 if(f->writing)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
780 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
781 f->out.cmd=FSP_CC_UP_LOAD;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
782 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
783 else
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
784 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
785 if(buildfilename(session,&f->out,path))
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
786 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
787 free(f);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
788 return NULL;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
789 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
790 f->bufpos=FSP_SPACE;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
791 f->out.cmd=FSP_CC_GET_FILE;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
792 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
793 f->out.xlen=0;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
794
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
795 /* setup control variables */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
796 f->s=session;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
797 f->name=strdup(path);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
798 if(f->name == NULL)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
799 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
800 free(f);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
801 errno = ENOMEM;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
802 return NULL;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
803 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
804
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
805 return f;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
806 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
807
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
808 size_t fsp_fread(void *dest,size_t size,size_t count,FSP_FILE *file)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
809 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
810 size_t total,done,havebytes;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
811 char *ptr;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
812
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
813 total=count*size;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
814 done=0;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
815 ptr=dest;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
816
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
817 if(file->eof) return 0;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
818
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
819 while(1)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
820 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
821 /* need more data? */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
822 if(file->bufpos>=FSP_SPACE)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
823 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
824 /* fill the buffer */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
825 file->out.pos=file->pos;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
826 if(fsp_transaction(file->s,&file->out,&file->in))
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
827 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
828 file->err=1;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
829 return done/size;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
830 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
831 if(file->in.cmd != FSP_CC_GET_FILE)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
832 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
833 errno = EIO;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
834 file->err=1;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
835 return done/size;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
836 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
837 file->bufpos=FSP_SPACE-file->in.len;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
838 if(file->bufpos > 0)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
839 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
840 memmove(file->in.buf+file->bufpos,file->in.buf,file->in.len);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
841 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
842 file->pos+=file->in.len;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
843 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
844 havebytes=FSP_SPACE - file->bufpos;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
845 if (havebytes == 0 )
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
846 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
847 /* end of file! */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
848 file->eof=1;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
849 errno = 0;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
850 return done/size;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
851 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
852 /* copy ready data to output buffer */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
853 if(havebytes <= total )
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
854 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
855 /* copy all we have */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
856 memcpy(ptr,file->in.buf+file->bufpos,havebytes);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
857 ptr+=havebytes;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
858 file->bufpos=FSP_SPACE;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
859 done+=havebytes;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
860 total-=havebytes;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
861 } else
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
862 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
863 /* copy bytes left */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
864 memcpy(ptr,file->in.buf+file->bufpos,total);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
865 file->bufpos+=total;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
866 errno = 0;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
867 return count;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
868 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
869 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
870 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
871
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
872 size_t fsp_fwrite(const void * source, size_t size, size_t count, FSP_FILE * file)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
873 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
874 size_t total,done,freebytes;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
875 const char *ptr;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
876
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
877 if(file->eof || file->err)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
878 return 0;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
879
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
880 file->out.len=FSP_SPACE;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
881 total=count*size;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
882 done=0;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
883 ptr=source;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
884
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
885 while(1)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
886 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
887 /* need to write some data? */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
888 if(file->bufpos>=FSP_SPACE)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
889 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
890 /* fill the buffer */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
891 file->out.pos=file->pos;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
892 if(fsp_transaction(file->s,&file->out,&file->in))
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
893 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
894 file->err=1;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
895 return done/size;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
896 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
897 if(file->in.cmd != FSP_CC_UP_LOAD)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
898 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
899 errno = EIO;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
900 file->err=1;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
901 return done/size;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
902 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
903 file->bufpos=0;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
904 file->pos+=file->out.len;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
905 done+=file->out.len;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
906 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
907 freebytes=FSP_SPACE - file->bufpos;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
908 /* copy input data to output buffer */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
909 if(freebytes <= total )
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
910 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
911 /* copy all we have */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
912 memcpy(file->out.buf+file->bufpos,ptr,freebytes);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
913 ptr+=freebytes;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
914 file->bufpos=FSP_SPACE;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
915 total-=freebytes;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
916 } else
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
917 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
918 /* copy bytes left */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
919 memcpy(file->out.buf+file->bufpos,ptr,total);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
920 file->bufpos+=total;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
921 errno = 0;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
922 return count;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
923 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
924 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
925 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
926
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
927 int fsp_fpurge(FSP_FILE *file)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
928 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
929 if(file->writing)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
930 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
931 file->bufpos=0;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
932 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
933 else
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
934 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
935 file->bufpos=FSP_SPACE;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
936 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
937 errno = 0;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
938 return 0;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
939 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
940
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
941 int fsp_fflush(FSP_FILE *file)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
942 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
943 if(file == NULL)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
944 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
945 errno = ENOTSUP;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
946 return -1;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
947 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
948 if(!file->writing)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
949 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
950 errno = EBADF;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
951 return -1;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
952 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
953 if(file->eof || file->bufpos==0)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
954 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
955 errno = 0;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
956 return 0;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
957 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
958
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
959 file->out.pos=file->pos;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
960 file->out.len=file->bufpos;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
961 if(fsp_transaction(file->s,&file->out,&file->in))
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
962 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
963 file->err=1;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
964 return -1;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
965 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
966 if(file->in.cmd != FSP_CC_UP_LOAD)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
967 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
968 errno = EIO;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
969 file->err=1;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
970 return -1;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
971 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
972 file->bufpos=0;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
973 file->pos+=file->out.len;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
974
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
975 errno = 0;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
976 return 0;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
977 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
978
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
979
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
980
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
981 int fsp_fclose(FSP_FILE *file)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
982 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
983 int rc;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
984
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
985 rc=0;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
986 errno = 0;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
987 if(file->writing)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
988 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
989 if(fsp_fflush(file))
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
990 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
991 rc=-1;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
992 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
993 else if(fsp_install(file->s,file->name,0))
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
994 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
995 rc=-1;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
996 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
997 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
998 free(file->name);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
999 free(file);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1000 return rc;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1001 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1002
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1003 int fsp_fseek(FSP_FILE *stream, long offset, int whence)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1004 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1005 long newoffset;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1006
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1007 switch(whence)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1008 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1009 case SEEK_SET:
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1010 newoffset = offset;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1011 break;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1012 case SEEK_CUR:
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1013 newoffset = stream->pos + offset;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1014 break;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1015 case SEEK_END:
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1016 errno = ENOTSUP;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1017 return -1;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1018 default:
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1019 errno = EINVAL;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1020 return -1;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1021 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1022 if(stream->writing)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1023 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1024 if(fsp_fflush(stream))
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1025 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1026 return -1;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1027 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1028 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1029 stream->pos=newoffset;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1030 stream->eof=0;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1031 fsp_fpurge(stream);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1032 return 0;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1033 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1034
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1035 long fsp_ftell(FSP_FILE *f)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1036 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1037 return f->pos + f->bufpos;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1038 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1039
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1040 void fsp_rewind(FSP_FILE *f)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1041 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1042 if(f->writing)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1043 fsp_fflush(f);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1044 f->pos=0;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1045 f->err=0;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1046 f->eof=0;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1047 fsp_fpurge(f);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1048 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1049
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1050 /* **************** Utility functions ****************** */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1051
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1052 /* return 0 if user has enough privs for uploading the file */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1053 int fsp_canupload(FSP_SESSION *s,const char *fname)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1054 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1055 char *dir;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1056 unsigned char dirpro;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1057 int rc;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1058 struct stat sb;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1059
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1060 dir=directoryfromfilename(fname);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1061 if(dir == NULL)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1062 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1063 errno = ENOMEM;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1064 return -1;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1065 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1066
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1067 rc=fsp_getpro(s,dir,&dirpro);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1068 free(dir);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1069
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1070 if(rc)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1071 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1072 return -1;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1073 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1074
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1075 if(dirpro & FSP_DIR_OWNER)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1076 return 0;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1077
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1078 if( ! (dirpro & FSP_DIR_ADD))
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1079 return -1;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1080
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1081 if (dirpro & FSP_DIR_DEL)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1082 return 0;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1083
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1084 /* we need to check file existence, because we can not overwrite files */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1085
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1086 rc = fsp_stat(s,fname,&sb);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1087
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1088 if (rc == 0)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1089 return -1;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1090 else
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1091 return 0;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1092 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1093
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1094 /* install file opened for writing */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1095 int fsp_install(FSP_SESSION *s,const char *fname,time_t timestamp)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1096 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1097 int rc;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1098 FSP_PKT in,out;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1099
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1100 /* and install a new file */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1101 out.cmd=FSP_CC_INSTALL;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1102 out.xlen=0;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1103 out.pos=0;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1104 rc=0;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1105 if( buildfilename(s,&out,fname) )
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1106 rc=-1;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1107 else
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1108 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1109 if (timestamp != 0)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1110 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1111 /* add timestamp extra data */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1112 *(uint32_t *)(out.buf+out.len)=htonl(timestamp);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1113 out.xlen=4;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1114 out.pos=4;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1115 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1116 if(fsp_transaction(s,&out,&in))
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1117 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1118 rc=-1;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1119 } else
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1120 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1121 if(in.cmd != FSP_CC_INSTALL)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1122 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1123 rc=-1;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1124 errno = EPERM;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1125 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1126 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1127 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1128
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1129 return rc;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1130 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1131 /* Get protection byte from the directory */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1132 int fsp_getpro(FSP_SESSION *s,const char *directory,unsigned char *result)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1133 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1134 FSP_PKT in,out;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1135
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1136 if(buildfilename(s,&out,directory))
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1137 return -1;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1138 out.cmd=FSP_CC_GET_PRO;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1139 out.xlen=0;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1140 out.pos=0;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1141
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1142 if(fsp_transaction(s,&out,&in))
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1143 return -1;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1144
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1145 if(in.cmd != FSP_CC_GET_PRO || in.pos != FSP_PRO_BYTES)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1146 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1147 errno = ENOMSG;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1148 return -1;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1149 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1150
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1151 if(result)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1152 *result=in.buf[in.len];
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1153 errno = 0;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1154 return 0;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1155 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1156
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1157 int fsp_stat(FSP_SESSION *s,const char *path,struct stat *sb)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1158 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1159 FSP_PKT in,out;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1160 unsigned char ftype;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1161
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1162 if(buildfilename(s,&out,path))
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1163 return -1;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1164 out.cmd=FSP_CC_STAT;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1165 out.xlen=0;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1166 out.pos=0;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1167
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1168 if(fsp_transaction(s,&out,&in))
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1169 return -1;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1170
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1171 if(in.cmd != FSP_CC_STAT)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1172 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1173 errno = ENOTSUP;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1174 return -1;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1175 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1176 /* parse results */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1177 ftype=in.buf[8];
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1178 if(ftype == 0)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1179 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1180 errno = ENOENT;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1181 return -1;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1182 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1183 sb->st_uid=sb->st_gid=0;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1184 sb->st_mtime=sb->st_ctime=sb->st_atime=ntohl( *(const uint32_t *)( in.buf ));
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1185 sb->st_size=ntohl( *(const uint32_t *)(in.buf + 4 ));
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1186 sb->st_blocks=(sb->st_size+511)/512;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1187 if (ftype==FSP_RDTYPE_DIR)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1188 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1189 sb->st_mode=S_IFDIR | 0755;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1190 sb->st_nlink=2;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1191 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1192 else
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1193 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1194 sb->st_mode=S_IFREG | 0644;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1195 sb->st_nlink=1;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1196 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1197
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1198 errno = 0;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1199 return 0;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1200 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1201
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1202 int fsp_mkdir(FSP_SESSION *s,const char *directory)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1203 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1204 return simplecommand(s,directory,FSP_CC_MAKE_DIR);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1205 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1206
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1207 int fsp_rmdir(FSP_SESSION *s,const char *directory)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1208 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1209 return simplecommand(s,directory,FSP_CC_DEL_DIR);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1210 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1211
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1212 int fsp_unlink(FSP_SESSION *s,const char *directory)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1213 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1214 return simplecommand(s,directory,FSP_CC_DEL_FILE);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1215 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1216
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1217 int fsp_rename(FSP_SESSION *s,const char *from, const char *to)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1218 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1219 FSP_PKT in,out;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1220 int l;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1221
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1222 if(buildfilename(s,&out,from))
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1223 return -1;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1224 /* append target name */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1225 l=strlen(to)+1;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1226 if( l + out.len > FSP_SPACE )
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1227 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1228 errno = ENAMETOOLONG;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1229 return -1;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1230 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1231 memcpy(out.buf+out.len,to,l);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1232 out.xlen = l;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1233
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1234 if(s->password)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1235 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1236 l=strlen(s->password)+1;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1237 if(out.len + out.xlen + l > FSP_SPACE)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1238 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1239 errno = ENAMETOOLONG;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1240 return -1;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1241 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1242 out.buf[out.len+out.xlen-1] = '\n';
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1243 memcpy(out.buf+out.len+out.xlen,s->password,l);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1244 out.xlen += l;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1245 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1246
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1247 out.cmd=FSP_CC_RENAME;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1248 out.pos=out.xlen;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1249
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1250 if(fsp_transaction(s,&out,&in))
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1251 return -1;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1252
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1253 if(in.cmd != FSP_CC_RENAME)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1254 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1255 errno = EPERM;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1256 return -1;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1257 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1258
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1259 errno = 0;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1260 return 0;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1261 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1262
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1263 int fsp_access(FSP_SESSION *s,const char *path, int mode)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1264 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1265 struct stat sb;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1266 int rc;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1267 unsigned char dirpro;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1268 char *dir;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1269
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1270 rc=fsp_stat(s,path,&sb);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1271 if(rc == -1)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1272 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1273 /* not found */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1274 /* errno is set by fsp_stat */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1275 return -1;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1276 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1277
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1278 /* just test file existence */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1279 if(mode == F_OK)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1280 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1281 errno = 0;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1282 return 0;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1283 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1284
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1285 /* deny execute access to file */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1286 if (mode & X_OK)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1287 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1288 if(S_ISREG(sb.st_mode))
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1289 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1290 errno = EACCES;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1291 return -1;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1292 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1293 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1294
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1295 /* Need to get ACL of directory */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1296 if(S_ISDIR(sb.st_mode))
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1297 dir=NULL;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1298 else
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1299 dir=directoryfromfilename(path);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1300
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1301 rc=fsp_getpro(s,dir==NULL?path:dir,&dirpro);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1302 /* get pro failure */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1303 if(rc)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1304 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1305 if(dir) free(dir);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1306 errno = EACCES;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1307 return -1;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1308 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1309 /* owner shortcut */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1310 if(dirpro & FSP_DIR_OWNER)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1311 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1312 if(dir) free(dir);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1313 errno = 0;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1314 return 0;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1315 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1316 /* check read access */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1317 if(mode & R_OK)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1318 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1319 if(dir)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1320 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1321 if(! (dirpro & FSP_DIR_GET))
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1322 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1323 free(dir);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1324 errno = EACCES;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1325 return -1;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1326 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1327 } else
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1328 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1329 if(! (dirpro & FSP_DIR_LIST))
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1330 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1331 errno = EACCES;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1332 return -1;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1333 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1334 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1335 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1336 /* check write access */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1337 if(mode & W_OK)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1338 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1339 if(dir)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1340 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1341 if( !(dirpro & FSP_DIR_DEL) || !(dirpro & FSP_DIR_ADD))
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1342 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1343 free(dir);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1344 errno = EACCES;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1345 return -1;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1346 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1347 } else
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1348 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1349 /* when checking directory for write access we are cheating
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1350 by allowing ADD or DEL right */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1351 if( !(dirpro & FSP_DIR_DEL) && !(dirpro & FSP_DIR_ADD))
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1352 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1353 errno = EACCES;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1354 return -1;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1355 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1356 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1357 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1358
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1359 if(dir) free(dir);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1360 errno = 0;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1361 return 0;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1362 }