annotate lib/fsplib/fsplib.c @ 739:ba82724da370

2006-5-14 Brian Masney <masneyb@gftp.org> * lib/fsplib/fsplib.c lib/fsplib/fsplib.h - updated with FSPLIB 0.8
author masneyb
date Sun, 14 May 2006 16:40:53 +0000
parents fd44f94c14ef
children 62222927016c
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
667
6b6fbcf76d00 2005-1-18 Brian Masney <masneyb@gftp.org>
masneyb
parents: 662
diff changeset
5 Copyright (c) 2003-2005 by Radim HSN Kolar (hsn@netmag.cz)
662
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>
693
fd44f94c14ef 2005-2-13 Brian Masney <masneyb@gftp.org>
masneyb
parents: 692
diff changeset
27
fd44f94c14ef 2005-2-13 Brian Masney <masneyb@gftp.org>
masneyb
parents: 692
diff changeset
28 #ifdef HAVE_STDINT_H
739
ba82724da370 2006-5-14 Brian Masney <masneyb@gftp.org>
masneyb
parents: 693
diff changeset
29 #include <stdint.h>
693
fd44f94c14ef 2005-2-13 Brian Masney <masneyb@gftp.org>
masneyb
parents: 692
diff changeset
30 #endif
fd44f94c14ef 2005-2-13 Brian Masney <masneyb@gftp.org>
masneyb
parents: 692
diff changeset
31
662
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
32 #include "fsplib.h"
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
33 #include "lock.h"
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 /* ************ Internal functions **************** */
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 /* builds filename in packet output buffer, appends password if needed */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
38 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
39 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
40 int len;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
41
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
42 len=strlen(dirname);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
43 if(len >= FSP_SPACE - 1)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
44 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
45 errno = ENAMETOOLONG;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
46 return -1;
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 /* copy name + \0 */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
49 memcpy(out->buf,dirname,len+1);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
50 out->len=len;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
51 if(s->password)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
52 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
53 out->buf[len]='\n';
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
54 out->len++;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
55
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
56 len=strlen(s->password);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
57 if(out->len+ len >= FSP_SPACE -1 )
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
58 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
59 errno = ENAMETOOLONG;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
60 return -1;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
61 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
62 memcpy(out->buf+out->len,s->password,len+1);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
63 out->len+=len;
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 /* add terminating \0 */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
66 out->len++;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
67 return 0;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
68 }
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 /* simple FSP command */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
71 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
72 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
73 FSP_PKT in,out;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
74
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
75 if(buildfilename(s,&out,directory))
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
76 return -1;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
77 out.cmd=command;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
78 out.xlen=0;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
79 out.pos=0;
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 if(fsp_transaction(s,&out,&in))
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 if(in.cmd == FSP_CC_ERR)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
85 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
86 errno = EPERM;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
87 return -1;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
88 }
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 errno = 0;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
91 return 0;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
92 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
93 /* Get directory part of filename. You must free() the result */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
94 static char * directoryfromfilename(const char *filename)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
95 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
96 char *result;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
97 char *tmp;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
98 int pos;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
99
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
100 result=strrchr(filename,'/');
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
101 if (result == NULL)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
102 return strdup("");
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
103 pos=result-filename;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
104 tmp=malloc(pos+1);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
105 if(!tmp)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
106 return NULL;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
107 memcpy(tmp,filename,pos);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
108 tmp[pos]='\0';
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
109 return tmp;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
110 }
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 /* ************ Packet encoding / decoding *************** */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
113
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
114 /* write binary representation of FSP packet p into *space. */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
115 /* returns number of bytes used or zero on error */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
116 /* Space must be long enough to hold created packet. */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
117 /* Maximum created packet size is FSP_MAXPACKET */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
118
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
119 size_t fsp_pkt_write(const FSP_PKT *p,void *space)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
120 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
121 size_t used;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
122 unsigned char *ptr;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
123 int checksum;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
124 size_t i;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
125
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
126 if(p->xlen + p->len > FSP_SPACE )
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
127 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
128 /* not enough space */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
129 errno = EMSGSIZE;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
130 return 0;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
131 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
132 ptr=space;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
133 /* pack header */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
134 ptr[FSP_OFFSET_CMD]=p->cmd;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
135 ptr[FSP_OFFSET_SUM]=0;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
136 *(uint16_t *)(ptr+FSP_OFFSET_KEY)=htons(p->key);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
137 *(uint16_t *)(ptr+FSP_OFFSET_SEQ)=htons(p->seq);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
138 *(uint16_t *)(ptr+FSP_OFFSET_LEN)=htons(p->len);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
139 *(uint32_t *)(ptr+FSP_OFFSET_POS)=htonl(p->pos);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
140 used=FSP_HSIZE;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
141 /* copy data block */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
142 memcpy(ptr+FSP_HSIZE,p->buf,p->len);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
143 used+=p->len;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
144 /* copy extra data block */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
145 memcpy(ptr+used,p->buf+p->len,p->xlen);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
146 used+=p->xlen;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
147 /* compute checksum */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
148 checksum = 0;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
149 for(i=0;i<used;i++)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
150 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
151 checksum += ptr[i];
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
152 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
153 checksum +=used;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
154 ptr[FSP_OFFSET_SUM] = checksum + (checksum >> 8);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
155 return used;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
156 }
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 /* read binary representation of FSP packet received from network into p */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
159 /* return zero on success */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
160 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
161 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
162 int mysum;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
163 size_t i;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
164 const unsigned char *ptr;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
165
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
166 if(recv_len<FSP_HSIZE)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
167 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
168 /* too short */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
169 errno = ERANGE;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
170 return -1;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
171 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
172 if(recv_len>FSP_MAXPACKET)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
173 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
174 /* too long */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
175 errno = EMSGSIZE;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
176 return -1;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
177 }
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 ptr=space;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
180 /* check sum */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
181 mysum=-ptr[FSP_OFFSET_SUM];
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
182 for(i=0;i<recv_len;i++)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
183 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
184 mysum+=ptr[i];
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
185 }
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 mysum = (mysum + (mysum >> 8)) & 0xff;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
188
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
189 if(mysum != ptr[FSP_OFFSET_SUM])
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
190 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
191 /* checksum failed */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
192
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
193 #ifdef MAINTAINER_MODE
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
194 printf("mysum: %x, got %x\n",mysum,ptr[FSP_OFFSET_SUM]);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
195 #endif
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
196 errno = EIO;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
197 return -1;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
198 }
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 /* unpack header */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
201 p->cmd=ptr[FSP_OFFSET_CMD];
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
202 p->sum=mysum;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
203 p->key=ntohs( *(const uint16_t *)(ptr+FSP_OFFSET_KEY) );
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
204 p->seq=ntohs( *(const uint16_t *)(ptr+FSP_OFFSET_SEQ) );
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
205 p->len=ntohs( *(const uint16_t *)(ptr+FSP_OFFSET_LEN) );
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
206 p->pos=ntohl( *(const uint32_t *)(ptr+FSP_OFFSET_POS) );
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
207 if(p->len > recv_len)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
208 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
209 /* bad length field, should not never happen */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
210 errno = EMSGSIZE;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
211 return -1;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
212 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
213 p->xlen=recv_len - p->len - FSP_HSIZE;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
214 /* now copy data */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
215 memcpy(p->buf,ptr+FSP_HSIZE,recv_len - FSP_HSIZE);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
216 return 0;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
217 }
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 /* ****************** packet sending functions ************** */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
220
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
221 /* make one send + receive transaction with server */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
222 /* outgoing packet is in p, incomming in rpkt */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
223 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
224 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
225 char buf[FSP_MAXPACKET];
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
226 size_t l;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
227 ssize_t r;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
228 fd_set mask;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
229 struct timeval start[8],stop;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
230 int i;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
231 unsigned int retry,dupes;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
232 int w_delay; /* how long to wait on next packet */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
233 int f_delay; /* how long to wait after first send */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
234 int l_delay; /* last delay */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
235 unsigned int t_delay; /* time from first send */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
236
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 if(p == rpkt)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
239 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
240 errno = EINVAL;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
241 return -2;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
242 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
243 FD_ZERO(&mask);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
244 /* get the next key */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
245 p->key = client_get_key((FSP_LOCK *)s->lock);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
246
739
ba82724da370 2006-5-14 Brian Masney <masneyb@gftp.org>
masneyb
parents: 693
diff changeset
247 retry = random() & 0xfff8;
ba82724da370 2006-5-14 Brian Masney <masneyb@gftp.org>
masneyb
parents: 693
diff changeset
248 if (s->seq == retry)
ba82724da370 2006-5-14 Brian Masney <masneyb@gftp.org>
masneyb
parents: 693
diff changeset
249 s->seq ^= 0x1080;
ba82724da370 2006-5-14 Brian Masney <masneyb@gftp.org>
masneyb
parents: 693
diff changeset
250 else
ba82724da370 2006-5-14 Brian Masney <masneyb@gftp.org>
masneyb
parents: 693
diff changeset
251 s->seq = retry;
662
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
252 dupes = retry = 0;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
253 t_delay = 0;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
254 /* compute initial delay here */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
255 /* we are using hardcoded value for now */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
256 f_delay = 1340;
667
6b6fbcf76d00 2005-1-18 Brian Masney <masneyb@gftp.org>
masneyb
parents: 662
diff changeset
257 l_delay = 0;
662
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
258 for(;;retry++)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
259 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
260 if(t_delay >= s->timeout)
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 client_set_key((FSP_LOCK *)s->lock,p->key);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
263 errno = ETIMEDOUT;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
264 return -1;
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 /* make a packet */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
267 p->seq = (s->seq) | (retry & 0x7);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
268 l=fsp_pkt_write(p,buf);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
269
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
270 /* We should compute next delay wait time here */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
271 gettimeofday(&start[retry & 0x7],NULL);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
272 if(retry == 0 )
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
273 w_delay=f_delay;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
274 else
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
275 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
276 w_delay=l_delay*3/2;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
277 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
278
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
279 l_delay=w_delay;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
280
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
281 /* send packet */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
282 if( send(s->fd,buf,l,0) < 0 )
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
283 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
284 #ifdef MAINTAINER_MODE
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
285 printf("Send failed.\n");
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
286 #endif
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
287 if(errno == EBADF || errno == ENOTSOCK)
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 client_set_key((FSP_LOCK *)s->lock,p->key);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
290 errno = EBADF;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
291 return -1;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
292 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
293 /* io terror */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
294 sleep(1);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
295 /* avoid wasting retry slot */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
296 retry--;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
297 t_delay += 1000;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
298 continue;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
299 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
300
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
301 /* keep delay value within sane limits */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
302 if (w_delay > (int) s->maxdelay)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
303 w_delay=s->maxdelay;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
304 else
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
305 if(w_delay < 1000 )
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
306 w_delay = 1000;
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 t_delay += w_delay;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
309 /* receive loop */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
310 while(1)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
311 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
312 if(w_delay <= 0 ) break;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
313 /* convert w_delay to timeval */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
314 stop.tv_sec=w_delay/1000;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
315 stop.tv_usec=(w_delay % 1000)*1000;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
316 FD_SET(s->fd,&mask);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
317 i=select(s->fd+1,&mask,NULL,NULL,&stop);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
318 if(i==0)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
319 break; /* timed out */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
320 if(i<0)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
321 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
322 if(errno==EINTR)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
323 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
324 /* lower w_delay */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
325 gettimeofday(&stop,NULL);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
326 w_delay-=1000*(stop.tv_sec - start[retry & 0x7].tv_sec);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
327 w_delay-= (stop.tv_usec - start[retry & 0x7].tv_usec)/1000;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
328 continue;
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 /* hard select error */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
331 client_set_key((FSP_LOCK *)s->lock,p->key);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
332 return -1;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
333 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
334 r=recv(s->fd,buf,FSP_MAXPACKET,0);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
335 if(r < 0 )
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 /* serious recv error */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
338 client_set_key((FSP_LOCK *)s->lock,p->key);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
339 return -1;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
340 }
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 gettimeofday(&stop,NULL);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
343 w_delay-=1000*(stop.tv_sec - start[retry & 0x7].tv_sec);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
344 w_delay-= (stop.tv_usec - start[retry & 0x7].tv_usec)/1000;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
345
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
346 /* process received packet */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
347 if ( fsp_pkt_read(rpkt,buf,r) < 0)
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 /* unpack failed */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
350 continue;
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
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
353 /* check sequence number */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
354 if( (rpkt->seq & 0xfff8) != s->seq )
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
355 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
356 #ifdef MAINTAINER_MODE
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
357 printf("dupe\n");
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
358 #endif
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
359 /* duplicate */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
360 dupes++;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
361 continue;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
362 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
363
739
ba82724da370 2006-5-14 Brian Masney <masneyb@gftp.org>
masneyb
parents: 693
diff changeset
364 /* check command code */
ba82724da370 2006-5-14 Brian Masney <masneyb@gftp.org>
masneyb
parents: 693
diff changeset
365 if( (rpkt->cmd != p->cmd) && (rpkt->cmd != FSP_CC_ERR))
ba82724da370 2006-5-14 Brian Masney <masneyb@gftp.org>
masneyb
parents: 693
diff changeset
366 {
ba82724da370 2006-5-14 Brian Masney <masneyb@gftp.org>
masneyb
parents: 693
diff changeset
367 dupes++;
ba82724da370 2006-5-14 Brian Masney <masneyb@gftp.org>
masneyb
parents: 693
diff changeset
368 continue;
ba82724da370 2006-5-14 Brian Masney <masneyb@gftp.org>
masneyb
parents: 693
diff changeset
369 }
ba82724da370 2006-5-14 Brian Masney <masneyb@gftp.org>
masneyb
parents: 693
diff changeset
370
ba82724da370 2006-5-14 Brian Masney <masneyb@gftp.org>
masneyb
parents: 693
diff changeset
371 /* check correct filepos */
ba82724da370 2006-5-14 Brian Masney <masneyb@gftp.org>
masneyb
parents: 693
diff changeset
372 if( (rpkt->pos != p->pos) && ( p->cmd == FSP_CC_GET_DIR ||
ba82724da370 2006-5-14 Brian Masney <masneyb@gftp.org>
masneyb
parents: 693
diff changeset
373 p->cmd == FSP_CC_GET_FILE || p->cmd == FSP_CC_UP_LOAD ||
ba82724da370 2006-5-14 Brian Masney <masneyb@gftp.org>
masneyb
parents: 693
diff changeset
374 p->cmd == FSP_CC_GRAB_FILE || p->cmd == FSP_CC_INFO) )
ba82724da370 2006-5-14 Brian Masney <masneyb@gftp.org>
masneyb
parents: 693
diff changeset
375 {
ba82724da370 2006-5-14 Brian Masney <masneyb@gftp.org>
masneyb
parents: 693
diff changeset
376 dupes++;
ba82724da370 2006-5-14 Brian Masney <masneyb@gftp.org>
masneyb
parents: 693
diff changeset
377 continue;
ba82724da370 2006-5-14 Brian Masney <masneyb@gftp.org>
masneyb
parents: 693
diff changeset
378 }
ba82724da370 2006-5-14 Brian Masney <masneyb@gftp.org>
masneyb
parents: 693
diff changeset
379
662
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
380 /* now we have a correct packet */
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 /* compute rtt delay */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
383 w_delay=1000*(stop.tv_sec - start[retry & 0x7].tv_sec);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
384 w_delay+=(stop.tv_usec - start[retry & 0x7].tv_usec)/1000;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
385 /* update last stats */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
386 s->last_rtt=w_delay;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
387 s->last_delay=f_delay;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
388 s->last_dupes=dupes;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
389 s->last_resends=retry;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
390 /* update cumul. stats */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
391 s->dupes+=dupes;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
392 s->resends+=retry;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
393 s->trips++;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
394 s->rtts+=w_delay;
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 /* grab a next key */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
397 client_set_key((FSP_LOCK *)s->lock,rpkt->key);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
398 errno = 0;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
399 return 0;
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 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
402 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
403
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
404 /* ******************* Session management functions ************ */
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 /* initializes a session */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
407 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
408 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
409 FSP_SESSION *s;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
410 int fd;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
411 struct addrinfo hints,*res;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
412 char port_s[6];
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
413 struct sockaddr_in *addrin;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
414 FSP_LOCK *lock;
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 memset (&hints, 0, sizeof (hints));
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
417 /* fspd do not supports inet6 */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
418 hints.ai_family = PF_INET;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
419 hints.ai_socktype = SOCK_DGRAM;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
420
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
421 if (port == 0)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
422 strcpy(port_s,"fsp");
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
423 else
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
424 sprintf(port_s,"%hu",port);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
425
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
426 if ( getaddrinfo(host,port_s,&hints,&res) )
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
427 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
428 return NULL; /* host not found */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
429 }
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 /* create socket */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
432 fd=socket(res->ai_family,res->ai_socktype,res->ai_protocol);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
433 if ( fd < 0)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
434 return NULL;
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 /* connect socket */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
437 if( connect(fd, res->ai_addr, res->ai_addrlen))
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
438 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
439 close(fd);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
440 return NULL;
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
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
443 /* allocate memory */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
444 s=calloc(1,sizeof(FSP_SESSION));
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
445 if ( !s )
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
446 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
447 close(fd);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
448 errno = ENOMEM;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
449 return NULL;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
450 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
451
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
452 lock=malloc(sizeof(FSP_LOCK));
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 if ( !lock )
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
455 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
456 close(fd);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
457 free(s);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
458 errno = ENOMEM;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
459 return NULL;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
460 }
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 s->lock=lock;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
463
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
464 /* init locking subsystem */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
465 addrin = (struct sockaddr_in *)res->ai_addr;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
466 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
467 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
468 free(s);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
469 close(fd);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
470 free(lock);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
471 return NULL;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
472 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
473
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
474 s->fd=fd;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
475 s->timeout=300000; /* 5 minutes */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
476 s->maxdelay=60000; /* 1 minute */
739
ba82724da370 2006-5-14 Brian Masney <masneyb@gftp.org>
masneyb
parents: 693
diff changeset
477 s->seq=random() & 0xfff8;
662
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
478 if ( password )
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
479 s->password = strdup(password);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
480 return s;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
481 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
482
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
483 /* closes a session */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
484 void fsp_close_session(FSP_SESSION *s)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
485 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
486 FSP_PKT bye,in;
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 if( s == NULL)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
489 return;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
490 if ( s->fd == -1)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
491 return;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
492 /* Send bye packet */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
493 bye.cmd=FSP_CC_BYE;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
494 bye.len=bye.xlen=0;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
495 bye.pos=0;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
496 s->timeout=7000;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
497 fsp_transaction(s,&bye,&in);
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 close(s->fd);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
500 if (s->password) free(s->password);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
501 client_destroy_key((FSP_LOCK *)s->lock);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
502 free(s->lock);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
503 memset(s,0,sizeof(FSP_SESSION));
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
504 s->fd=-1;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
505 free(s);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
506 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
507
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
508 /* *************** Directory listing functions *************** */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
509
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
510 /* get a directory listing from a server */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
511 FSP_DIR * fsp_opendir(FSP_SESSION *s,const char *dirname)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
512 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
513 FSP_PKT in,out;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
514 int pos;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
515 unsigned short blocksize;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
516 FSP_DIR *dir;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
517 unsigned char *tmp;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
518
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
519 if (s == NULL) return NULL;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
520 if (dirname == NULL) return NULL;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
521
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
522 if(buildfilename(s,&out,dirname))
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
523 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
524 return NULL;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
525 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
526 pos=0;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
527 blocksize=0;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
528 dir=NULL;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
529 out.cmd = FSP_CC_GET_DIR;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
530 out.xlen=0;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
531
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
532 /* load directory listing from the server */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
533 while(1)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
534 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
535 out.pos=pos;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
536 if ( fsp_transaction(s,&out,&in) )
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
537 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
538 pos = -1;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
539 break;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
540 }
739
ba82724da370 2006-5-14 Brian Masney <masneyb@gftp.org>
masneyb
parents: 693
diff changeset
541 if ( in.cmd == FSP_CC_ERR )
662
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 /* bad reply from the server */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
544 pos = -1;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
545 break;
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 /* End of directory? */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
548 if ( in.len == 0)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
549 break;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
550 /* set blocksize */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
551 if (blocksize == 0 )
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
552 blocksize = in.len;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
553 /* alloc directory */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
554 if (dir == NULL)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
555 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
556 dir = calloc(1,sizeof(FSP_DIR));
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
557 if (dir == NULL)
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 pos = -1;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
560 break;
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 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
563 /* append data */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
564 tmp=realloc(dir->data,pos+in.len);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
565 if(tmp == NULL)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
566 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
567 pos = -1;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
568 break;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
569 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
570 dir->data=tmp;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
571 memcpy(dir->data + pos, in.buf,in.len);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
572 pos += in.len;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
573 if (in.len < blocksize)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
574 /* last block is smaller */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
575 break;
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 if (pos == -1)
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 /* failure */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
580 if (dir)
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->data)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
583 free(dir->data);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
584 free(dir);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
585 }
739
ba82724da370 2006-5-14 Brian Masney <masneyb@gftp.org>
masneyb
parents: 693
diff changeset
586 errno = EPERM;
662
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
587 return NULL;
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
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
590 dir->inuse=1;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
591 dir->blocksize=blocksize;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
592 dir->dirname=strdup(dirname);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
593 dir->datasize=pos;
739
ba82724da370 2006-5-14 Brian Masney <masneyb@gftp.org>
masneyb
parents: 693
diff changeset
594
ba82724da370 2006-5-14 Brian Masney <masneyb@gftp.org>
masneyb
parents: 693
diff changeset
595 errno = 0;
662
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
596 return dir;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
597 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
598
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
599 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
600 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
601 FSP_RDENTRY fentry,*fresult;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
602 int rc;
667
6b6fbcf76d00 2005-1-18 Brian Masney <masneyb@gftp.org>
masneyb
parents: 662
diff changeset
603 char *c;
662
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
604
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
605 if (dir == NULL || entry == NULL || *result == NULL)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
606 return -EINVAL;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
607 if (dir->dirpos<0 || dir->dirpos % 4)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
608 return -ESPIPE;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
609
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
610 rc=fsp_readdir_native(dir,&fentry,&fresult);
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 if (rc != 0)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
613 return rc;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
614
692
81172373727f 2005-2-13 Brian Masney <masneyb@gftp.org>
masneyb
parents: 667
diff changeset
615 #ifdef HAVE_DIRENT_TYPE
662
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
616 /* convert FSP dirent to OS dirent */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
617
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
618 if (fentry.type == FSP_RDTYPE_DIR )
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
619 entry->d_type=DT_DIR;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
620 else
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
621 entry->d_type=DT_REG;
692
81172373727f 2005-2-13 Brian Masney <masneyb@gftp.org>
masneyb
parents: 667
diff changeset
622 #endif
81172373727f 2005-2-13 Brian Masney <masneyb@gftp.org>
masneyb
parents: 667
diff changeset
623
667
6b6fbcf76d00 2005-1-18 Brian Masney <masneyb@gftp.org>
masneyb
parents: 662
diff changeset
624 /* remove symlink destination */
6b6fbcf76d00 2005-1-18 Brian Masney <masneyb@gftp.org>
masneyb
parents: 662
diff changeset
625 c=strchr(fentry.name,'\n');
6b6fbcf76d00 2005-1-18 Brian Masney <masneyb@gftp.org>
masneyb
parents: 662
diff changeset
626 if (c)
6b6fbcf76d00 2005-1-18 Brian Masney <masneyb@gftp.org>
masneyb
parents: 662
diff changeset
627 {
6b6fbcf76d00 2005-1-18 Brian Masney <masneyb@gftp.org>
masneyb
parents: 662
diff changeset
628 *c='\0';
6b6fbcf76d00 2005-1-18 Brian Masney <masneyb@gftp.org>
masneyb
parents: 662
diff changeset
629 rc=fentry.namlen-strlen(fentry.name);
6b6fbcf76d00 2005-1-18 Brian Masney <masneyb@gftp.org>
masneyb
parents: 662
diff changeset
630 fentry.reclen-=rc;
6b6fbcf76d00 2005-1-18 Brian Masney <masneyb@gftp.org>
masneyb
parents: 662
diff changeset
631 fentry.namlen-=rc;
6b6fbcf76d00 2005-1-18 Brian Masney <masneyb@gftp.org>
masneyb
parents: 662
diff changeset
632 }
6b6fbcf76d00 2005-1-18 Brian Masney <masneyb@gftp.org>
masneyb
parents: 662
diff changeset
633
692
81172373727f 2005-2-13 Brian Masney <masneyb@gftp.org>
masneyb
parents: 667
diff changeset
634 #ifdef HAVE_DIRENT_FILENO
662
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
635 entry->d_fileno = 10;
692
81172373727f 2005-2-13 Brian Masney <masneyb@gftp.org>
masneyb
parents: 667
diff changeset
636 #endif
662
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
637 entry->d_reclen = fentry.reclen;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
638 strncpy(entry->d_name,fentry.name,MAXNAMLEN);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
639
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
640 if (fentry.namlen > MAXNAMLEN)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
641 {
739
ba82724da370 2006-5-14 Brian Masney <masneyb@gftp.org>
masneyb
parents: 693
diff changeset
642 entry->d_name[MAXNAMLEN] = '\0';
692
81172373727f 2005-2-13 Brian Masney <masneyb@gftp.org>
masneyb
parents: 667
diff changeset
643 #ifdef HAVE_DIRENT_NAMLEN
662
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
644 entry->d_namlen = MAXNAMLEN;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
645 } else
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
646 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
647 entry->d_namlen = fentry.namlen;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
648 #endif
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
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
651 if (fresult == &fentry )
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 *result = entry;
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 else
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
656 *result = NULL;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
657
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
658 return 0;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
659 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
660
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
661 /* native FSP directory reader */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
662 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
663 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
664 unsigned char ftype;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
665 int namelen;
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 if (dir == NULL || entry == NULL || *result == NULL)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
668 return -EINVAL;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
669 if (dir->dirpos<0 || dir->dirpos % 4)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
670 return -ESPIPE;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
671
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
672 while(1)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
673 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
674 if ( dir->dirpos >= (int)dir->datasize )
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 /* end of the directory */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
677 *result = NULL;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
678 return 0;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
679 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
680 if (dir->blocksize - (dir->dirpos % dir->blocksize) < 9)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
681 ftype= FSP_RDTYPE_SKIP;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
682 else
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
683 /* get the file type */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
684 ftype=dir->data[dir->dirpos+8];
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
685
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
686 if (ftype == FSP_RDTYPE_END )
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
687 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
688 dir->dirpos=dir->datasize;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
689 continue;
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 if (ftype == FSP_RDTYPE_SKIP )
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
692 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
693 /* skip to next directory block */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
694 dir->dirpos = ( dir->dirpos / dir->blocksize + 1 ) * dir->blocksize;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
695 #ifdef MAINTAINER_MODE
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
696 printf("new block dirpos: %d\n",dir->dirpos);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
697 #endif
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
698 continue;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
699 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
700 /* extract binary data */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
701 entry->lastmod=ntohl( *(const uint32_t *)( dir->data+ dir->dirpos ));
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
702 entry->size=ntohl( *(const uint32_t *)(dir->data+ dir->dirpos +4 ));
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
703 entry->type=ftype;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
704
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
705 /* skip file date and file size */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
706 dir->dirpos += 9;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
707 /* read file name */
739
ba82724da370 2006-5-14 Brian Masney <masneyb@gftp.org>
masneyb
parents: 693
diff changeset
708 entry->name[255] = '\0';
662
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
709 strncpy(entry->name,(char *)( dir->data + dir->dirpos ),MAXNAMLEN);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
710 namelen = strlen( (char *) dir->data+dir->dirpos);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
711 /* skip over file name */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
712 dir->dirpos += namelen +1;
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 /* set entry namelen field */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
715 if (namelen > 255)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
716 entry->namlen = 255;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
717 else
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
718 entry->namlen = namelen;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
719 /* set record length */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
720 entry->reclen = 10+namelen;
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 /* pad to 4 byte boundary */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
723 while( dir->dirpos & 0x3 )
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 dir->dirpos++;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
726 entry->reclen++;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
727 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
728
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
729 /* and return it */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
730 *result=entry;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
731 return 0;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
732 }
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 struct dirent * fsp_readdir(FSP_DIR *dirp)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
736 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
737 static struct dirent entry;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
738 struct dirent *result;
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
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
741 if (dirp == NULL) return NULL;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
742 if ( fsp_readdir_r(dirp,&entry,&result) )
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 else
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
745 return result;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
746 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
747
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
748 long fsp_telldir(FSP_DIR *dirp)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
749 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
750 return dirp->dirpos;
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
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
753 void fsp_seekdir(FSP_DIR *dirp, long loc)
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 dirp->dirpos=loc;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
756 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
757
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
758 void fsp_rewinddir(FSP_DIR *dirp)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
759 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
760 dirp->dirpos=0;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
761 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
762
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
763 int fsp_closedir(FSP_DIR *dirp)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
764 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
765 if (dirp == NULL)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
766 return -1;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
767 if(dirp->dirname) free(dirp->dirname);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
768 free(dirp->data);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
769 free(dirp);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
770 return 0;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
771 }
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 /* ***************** File input/output functions ********* */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
774 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
775 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
776 FSP_FILE *f;
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 if(session == NULL || path == NULL || modeflags == NULL)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
779 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
780 errno = EINVAL;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
781 return NULL;
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
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
784 f=calloc(1,sizeof(FSP_FILE));
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
785 if (f == NULL)
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 return NULL;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
788 }
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 /* check and parse flags */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
791 switch (*modeflags++)
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 case 'r':
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
794 break;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
795 case 'w':
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
796 f->writing=1;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
797 break;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
798 case 'a':
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
799 /* not supported */
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 = ENOTSUP;
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 default:
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
804 free(f);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
805 errno = EINVAL;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
806 return NULL;
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
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
809 if (*modeflags == '+' || ( *modeflags=='b' && modeflags[1]=='+'))
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
810 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
811 free(f);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
812 errno = ENOTSUP;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
813 return NULL;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
814 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
815
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
816 /* build request packet */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
817 if(f->writing)
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 f->out.cmd=FSP_CC_UP_LOAD;
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 else
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
822 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
823 if(buildfilename(session,&f->out,path))
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
824 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
825 free(f);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
826 return NULL;
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 f->bufpos=FSP_SPACE;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
829 f->out.cmd=FSP_CC_GET_FILE;
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 f->out.xlen=0;
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 /* setup control variables */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
834 f->s=session;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
835 f->name=strdup(path);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
836 if(f->name == NULL)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
837 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
838 free(f);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
839 errno = ENOMEM;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
840 return NULL;
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
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
843 return f;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
844 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
845
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
846 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
847 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
848 size_t total,done,havebytes;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
849 char *ptr;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
850
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
851 total=count*size;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
852 done=0;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
853 ptr=dest;
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 if(file->eof) return 0;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
856
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
857 while(1)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
858 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
859 /* need more data? */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
860 if(file->bufpos>=FSP_SPACE)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
861 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
862 /* fill the buffer */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
863 file->out.pos=file->pos;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
864 if(fsp_transaction(file->s,&file->out,&file->in))
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
865 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
866 file->err=1;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
867 return done/size;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
868 }
739
ba82724da370 2006-5-14 Brian Masney <masneyb@gftp.org>
masneyb
parents: 693
diff changeset
869 if(file->in.cmd == FSP_CC_ERR)
662
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 errno = EIO;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
872 file->err=1;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
873 return done/size;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
874 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
875 file->bufpos=FSP_SPACE-file->in.len;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
876 if(file->bufpos > 0)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
877 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
878 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
879 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
880 file->pos+=file->in.len;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
881 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
882 havebytes=FSP_SPACE - file->bufpos;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
883 if (havebytes == 0 )
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 /* end of file! */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
886 file->eof=1;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
887 errno = 0;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
888 return done/size;
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 /* copy ready data to output buffer */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
891 if(havebytes <= total )
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
892 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
893 /* copy all we have */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
894 memcpy(ptr,file->in.buf+file->bufpos,havebytes);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
895 ptr+=havebytes;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
896 file->bufpos=FSP_SPACE;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
897 done+=havebytes;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
898 total-=havebytes;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
899 } else
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
900 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
901 /* copy bytes left */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
902 memcpy(ptr,file->in.buf+file->bufpos,total);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
903 file->bufpos+=total;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
904 errno = 0;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
905 return count;
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 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
908 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
909
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
910 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
911 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
912 size_t total,done,freebytes;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
913 const char *ptr;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
914
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
915 if(file->eof || file->err)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
916 return 0;
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 file->out.len=FSP_SPACE;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
919 total=count*size;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
920 done=0;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
921 ptr=source;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
922
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
923 while(1)
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 /* need to write some data? */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
926 if(file->bufpos>=FSP_SPACE)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
927 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
928 /* fill the buffer */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
929 file->out.pos=file->pos;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
930 if(fsp_transaction(file->s,&file->out,&file->in))
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
931 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
932 file->err=1;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
933 return done/size;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
934 }
739
ba82724da370 2006-5-14 Brian Masney <masneyb@gftp.org>
masneyb
parents: 693
diff changeset
935 if(file->in.cmd == FSP_CC_ERR)
662
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 = EIO;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
938 file->err=1;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
939 return done/size;
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 file->bufpos=0;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
942 file->pos+=file->out.len;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
943 done+=file->out.len;
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 freebytes=FSP_SPACE - file->bufpos;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
946 /* copy input data to output buffer */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
947 if(freebytes <= total )
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
948 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
949 /* copy all we have */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
950 memcpy(file->out.buf+file->bufpos,ptr,freebytes);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
951 ptr+=freebytes;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
952 file->bufpos=FSP_SPACE;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
953 total-=freebytes;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
954 } else
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
955 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
956 /* copy bytes left */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
957 memcpy(file->out.buf+file->bufpos,ptr,total);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
958 file->bufpos+=total;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
959 errno = 0;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
960 return count;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
961 }
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 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
964
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
965 int fsp_fpurge(FSP_FILE *file)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
966 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
967 if(file->writing)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
968 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
969 file->bufpos=0;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
970 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
971 else
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
972 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
973 file->bufpos=FSP_SPACE;
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 int fsp_fflush(FSP_FILE *file)
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 if(file == NULL)
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 errno = ENOTSUP;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
984 return -1;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
985 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
986 if(!file->writing)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
987 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
988 errno = EBADF;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
989 return -1;
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 if(file->eof || file->bufpos==0)
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 errno = 0;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
994 return 0;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
995 }
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 file->out.pos=file->pos;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
998 file->out.len=file->bufpos;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
999 if(fsp_transaction(file->s,&file->out,&file->in))
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1000 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1001 file->err=1;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1002 return -1;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1003 }
739
ba82724da370 2006-5-14 Brian Masney <masneyb@gftp.org>
masneyb
parents: 693
diff changeset
1004 if(file->in.cmd == FSP_CC_ERR)
662
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1005 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1006 errno = EIO;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1007 file->err=1;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1008 return -1;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1009 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1010 file->bufpos=0;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1011 file->pos+=file->out.len;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1012
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1013 errno = 0;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1014 return 0;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1015 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1016
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1017
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1018
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1019 int fsp_fclose(FSP_FILE *file)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1020 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1021 int rc;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1022
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1023 rc=0;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1024 errno = 0;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1025 if(file->writing)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1026 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1027 if(fsp_fflush(file))
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 rc=-1;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1030 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1031 else if(fsp_install(file->s,file->name,0))
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1032 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1033 rc=-1;
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 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1036 free(file->name);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1037 free(file);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1038 return rc;
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
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1041 int fsp_fseek(FSP_FILE *stream, long offset, int whence)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1042 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1043 long newoffset;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1044
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1045 switch(whence)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1046 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1047 case SEEK_SET:
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1048 newoffset = offset;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1049 break;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1050 case SEEK_CUR:
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1051 newoffset = stream->pos + offset;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1052 break;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1053 case SEEK_END:
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1054 errno = ENOTSUP;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1055 return -1;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1056 default:
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1057 errno = EINVAL;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1058 return -1;
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 if(stream->writing)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1061 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1062 if(fsp_fflush(stream))
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1063 {
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 stream->pos=newoffset;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1068 stream->eof=0;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1069 fsp_fpurge(stream);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1070 return 0;
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
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1073 long fsp_ftell(FSP_FILE *f)
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 return f->pos + f->bufpos;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1076 }
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 void fsp_rewind(FSP_FILE *f)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1079 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1080 if(f->writing)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1081 fsp_fflush(f);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1082 f->pos=0;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1083 f->err=0;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1084 f->eof=0;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1085 fsp_fpurge(f);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1086 }
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 /* **************** Utility functions ****************** */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1089
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1090 /* return 0 if user has enough privs for uploading the file */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1091 int fsp_canupload(FSP_SESSION *s,const char *fname)
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 char *dir;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1094 unsigned char dirpro;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1095 int rc;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1096 struct stat sb;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1097
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1098 dir=directoryfromfilename(fname);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1099 if(dir == NULL)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1100 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1101 errno = ENOMEM;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1102 return -1;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1103 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1104
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1105 rc=fsp_getpro(s,dir,&dirpro);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1106 free(dir);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1107
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1108 if(rc)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1109 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1110 return -1;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1111 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1112
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1113 if(dirpro & FSP_DIR_OWNER)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1114 return 0;
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( ! (dirpro & FSP_DIR_ADD))
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1117 return -1;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1118
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1119 if (dirpro & FSP_DIR_DEL)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1120 return 0;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1121
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1122 /* 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
1123
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1124 rc = fsp_stat(s,fname,&sb);
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 if (rc == 0)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1127 return -1;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1128 else
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1129 return 0;
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
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1132 /* install file opened for writing */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1133 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
1134 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1135 int rc;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1136 FSP_PKT in,out;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1137
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1138 /* and install a new file */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1139 out.cmd=FSP_CC_INSTALL;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1140 out.xlen=0;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1141 out.pos=0;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1142 rc=0;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1143 if( buildfilename(s,&out,fname) )
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1144 rc=-1;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1145 else
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 if (timestamp != 0)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1148 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1149 /* add timestamp extra data */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1150 *(uint32_t *)(out.buf+out.len)=htonl(timestamp);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1151 out.xlen=4;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1152 out.pos=4;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1153 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1154 if(fsp_transaction(s,&out,&in))
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 rc=-1;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1157 } else
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1158 {
739
ba82724da370 2006-5-14 Brian Masney <masneyb@gftp.org>
masneyb
parents: 693
diff changeset
1159 if(in.cmd == FSP_CC_ERR)
662
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1160 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1161 rc=-1;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1162 errno = EPERM;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1163 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1164 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1165 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1166
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1167 return rc;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1168 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1169 /* Get protection byte from the directory */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1170 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
1171 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1172 FSP_PKT in,out;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1173
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1174 if(buildfilename(s,&out,directory))
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1175 return -1;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1176 out.cmd=FSP_CC_GET_PRO;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1177 out.xlen=0;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1178 out.pos=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 if(fsp_transaction(s,&out,&in))
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
739
ba82724da370 2006-5-14 Brian Masney <masneyb@gftp.org>
masneyb
parents: 693
diff changeset
1183 if(in.cmd == FSP_CC_ERR)
ba82724da370 2006-5-14 Brian Masney <masneyb@gftp.org>
masneyb
parents: 693
diff changeset
1184 {
ba82724da370 2006-5-14 Brian Masney <masneyb@gftp.org>
masneyb
parents: 693
diff changeset
1185 errno = ENOENT;
ba82724da370 2006-5-14 Brian Masney <masneyb@gftp.org>
masneyb
parents: 693
diff changeset
1186 return -1;
ba82724da370 2006-5-14 Brian Masney <masneyb@gftp.org>
masneyb
parents: 693
diff changeset
1187 }
ba82724da370 2006-5-14 Brian Masney <masneyb@gftp.org>
masneyb
parents: 693
diff changeset
1188 if(in.pos != FSP_PRO_BYTES)
662
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1189 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1190 errno = ENOMSG;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1191 return -1;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1192 }
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 if(result)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1195 *result=in.buf[in.len];
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1196 errno = 0;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1197 return 0;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1198 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1199
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1200 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
1201 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1202 FSP_PKT in,out;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1203 unsigned char ftype;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1204
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1205 if(buildfilename(s,&out,path))
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1206 return -1;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1207 out.cmd=FSP_CC_STAT;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1208 out.xlen=0;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1209 out.pos=0;
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 if(fsp_transaction(s,&out,&in))
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1212 return -1;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1213
739
ba82724da370 2006-5-14 Brian Masney <masneyb@gftp.org>
masneyb
parents: 693
diff changeset
1214 if(in.cmd == FSP_CC_ERR)
662
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 errno = ENOTSUP;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1217 return -1;
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 /* parse results */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1220 ftype=in.buf[8];
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1221 if(ftype == 0)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1222 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1223 errno = ENOENT;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1224 return -1;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1225 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1226 sb->st_uid=sb->st_gid=0;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1227 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
1228 sb->st_size=ntohl( *(const uint32_t *)(in.buf + 4 ));
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1229 sb->st_blocks=(sb->st_size+511)/512;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1230 if (ftype==FSP_RDTYPE_DIR)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1231 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1232 sb->st_mode=S_IFDIR | 0755;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1233 sb->st_nlink=2;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1234 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1235 else
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1236 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1237 sb->st_mode=S_IFREG | 0644;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1238 sb->st_nlink=1;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1239 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1240
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1241 errno = 0;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1242 return 0;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1243 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1244
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1245 int fsp_mkdir(FSP_SESSION *s,const char *directory)
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 return simplecommand(s,directory,FSP_CC_MAKE_DIR);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1248 }
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 int fsp_rmdir(FSP_SESSION *s,const char *directory)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1251 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1252 return simplecommand(s,directory,FSP_CC_DEL_DIR);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1253 }
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 int fsp_unlink(FSP_SESSION *s,const char *directory)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1256 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1257 return simplecommand(s,directory,FSP_CC_DEL_FILE);
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
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1260 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
1261 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1262 FSP_PKT in,out;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1263 int l;
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 if(buildfilename(s,&out,from))
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1266 return -1;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1267 /* append target name */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1268 l=strlen(to)+1;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1269 if( l + out.len > FSP_SPACE )
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1270 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1271 errno = ENAMETOOLONG;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1272 return -1;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1273 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1274 memcpy(out.buf+out.len,to,l);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1275 out.xlen = l;
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 if(s->password)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1278 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1279 l=strlen(s->password)+1;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1280 if(out.len + out.xlen + l > FSP_SPACE)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1281 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1282 errno = ENAMETOOLONG;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1283 return -1;
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 out.buf[out.len+out.xlen-1] = '\n';
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1286 memcpy(out.buf+out.len+out.xlen,s->password,l);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1287 out.xlen += l;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1288 }
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 out.cmd=FSP_CC_RENAME;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1291 out.pos=out.xlen;
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 if(fsp_transaction(s,&out,&in))
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1294 return -1;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1295
739
ba82724da370 2006-5-14 Brian Masney <masneyb@gftp.org>
masneyb
parents: 693
diff changeset
1296 if(in.cmd == FSP_CC_ERR)
662
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1297 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1298 errno = EPERM;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1299 return -1;
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
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1302 errno = 0;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1303 return 0;
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
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1306 int fsp_access(FSP_SESSION *s,const char *path, int mode)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1307 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1308 struct stat sb;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1309 int rc;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1310 unsigned char dirpro;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1311 char *dir;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1312
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1313 rc=fsp_stat(s,path,&sb);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1314 if(rc == -1)
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 /* not found */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1317 /* errno is set by fsp_stat */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1318 return -1;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1319 }
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 /* just test file existence */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1322 if(mode == F_OK)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1323 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1324 errno = 0;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1325 return 0;
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
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1328 /* deny execute access to file */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1329 if (mode & X_OK)
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 if(S_ISREG(sb.st_mode))
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1332 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1333 errno = EACCES;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1334 return -1;
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 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1337
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1338 /* Need to get ACL of directory */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1339 if(S_ISDIR(sb.st_mode))
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1340 dir=NULL;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1341 else
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1342 dir=directoryfromfilename(path);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1343
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1344 rc=fsp_getpro(s,dir==NULL?path:dir,&dirpro);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1345 /* get pro failure */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1346 if(rc)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1347 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1348 if(dir) free(dir);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1349 errno = EACCES;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1350 return -1;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1351 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1352 /* owner shortcut */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1353 if(dirpro & FSP_DIR_OWNER)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1354 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1355 if(dir) free(dir);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1356 errno = 0;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1357 return 0;
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 /* check read access */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1360 if(mode & R_OK)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1361 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1362 if(dir)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1363 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1364 if(! (dirpro & FSP_DIR_GET))
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1365 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1366 free(dir);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1367 errno = EACCES;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1368 return -1;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1369 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1370 } else
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1371 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1372 if(! (dirpro & FSP_DIR_LIST))
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1373 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1374 errno = EACCES;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1375 return -1;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1376 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1377 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1378 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1379 /* check write access */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1380 if(mode & W_OK)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1381 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1382 if(dir)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1383 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1384 if( !(dirpro & FSP_DIR_DEL) || !(dirpro & FSP_DIR_ADD))
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1385 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1386 free(dir);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1387 errno = EACCES;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1388 return -1;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1389 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1390 } else
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1391 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1392 /* when checking directory for write access we are cheating
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1393 by allowing ADD or DEL right */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1394 if( !(dirpro & FSP_DIR_DEL) && !(dirpro & FSP_DIR_ADD))
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1395 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1396 errno = EACCES;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1397 return -1;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1398 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1399 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1400 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1401
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1402 if(dir) free(dir);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1403 errno = 0;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1404 return 0;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1405 }