annotate lib/fsplib/fsplib.c @ 693:fd44f94c14ef

2005-2-13 Brian Masney <masneyb@gftp.org> * lib/fsplib/fsplib.c - include stdint.h if HAVE_STDINT_H is defined
author masneyb
date Sun, 13 Feb 2005 14:47:41 +0000
parents 81172373727f
children ba82724da370
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
fd44f94c14ef 2005-2-13 Brian Masney <masneyb@gftp.org>
masneyb
parents: 692
diff changeset
29 # include <stdint.h>
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 if(in.cmd != command)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
91 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
92 errno = ENOMSG;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
93 return -1;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
94 }
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 errno = 0;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
97 return 0;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
98 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
99 /* Get directory part of filename. You must free() the result */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
100 static char * directoryfromfilename(const char *filename)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
101 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
102 char *result;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
103 char *tmp;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
104 int pos;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
105
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
106 result=strrchr(filename,'/');
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
107 if (result == NULL)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
108 return strdup("");
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
109 pos=result-filename;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
110 tmp=malloc(pos+1);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
111 if(!tmp)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
112 return NULL;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
113 memcpy(tmp,filename,pos);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
114 tmp[pos]='\0';
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
115 return tmp;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
116 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
117
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
118 /* ************ Packet encoding / decoding *************** */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
119
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
120 /* write binary representation of FSP packet p into *space. */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
121 /* returns number of bytes used or zero on error */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
122 /* Space must be long enough to hold created packet. */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
123 /* Maximum created packet size is FSP_MAXPACKET */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
124
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
125 size_t fsp_pkt_write(const FSP_PKT *p,void *space)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
126 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
127 size_t used;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
128 unsigned char *ptr;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
129 int checksum;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
130 size_t i;
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 if(p->xlen + p->len > FSP_SPACE )
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
133 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
134 /* not enough space */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
135 errno = EMSGSIZE;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
136 return 0;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
137 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
138 ptr=space;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
139 /* pack header */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
140 ptr[FSP_OFFSET_CMD]=p->cmd;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
141 ptr[FSP_OFFSET_SUM]=0;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
142 *(uint16_t *)(ptr+FSP_OFFSET_KEY)=htons(p->key);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
143 *(uint16_t *)(ptr+FSP_OFFSET_SEQ)=htons(p->seq);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
144 *(uint16_t *)(ptr+FSP_OFFSET_LEN)=htons(p->len);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
145 *(uint32_t *)(ptr+FSP_OFFSET_POS)=htonl(p->pos);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
146 used=FSP_HSIZE;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
147 /* copy data block */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
148 memcpy(ptr+FSP_HSIZE,p->buf,p->len);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
149 used+=p->len;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
150 /* copy extra data block */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
151 memcpy(ptr+used,p->buf+p->len,p->xlen);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
152 used+=p->xlen;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
153 /* compute checksum */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
154 checksum = 0;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
155 for(i=0;i<used;i++)
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 checksum += ptr[i];
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
158 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
159 checksum +=used;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
160 ptr[FSP_OFFSET_SUM] = checksum + (checksum >> 8);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
161 return used;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
162 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
163
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
164 /* read binary representation of FSP packet received from network into p */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
165 /* return zero on success */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
166 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
167 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
168 int mysum;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
169 size_t i;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
170 const unsigned char *ptr;
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_HSIZE)
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 short */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
175 errno = ERANGE;
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 if(recv_len>FSP_MAXPACKET)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
179 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
180 /* too long */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
181 errno = EMSGSIZE;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
182 return -1;
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
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
185 ptr=space;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
186 /* check sum */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
187 mysum=-ptr[FSP_OFFSET_SUM];
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
188 for(i=0;i<recv_len;i++)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
189 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
190 mysum+=ptr[i];
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
191 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
192
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
193 mysum = (mysum + (mysum >> 8)) & 0xff;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
194
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
195 if(mysum != ptr[FSP_OFFSET_SUM])
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
196 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
197 /* checksum failed */
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 #ifdef MAINTAINER_MODE
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
200 printf("mysum: %x, got %x\n",mysum,ptr[FSP_OFFSET_SUM]);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
201 #endif
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
202 errno = EIO;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
203 return -1;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
204 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
205
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
206 /* unpack header */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
207 p->cmd=ptr[FSP_OFFSET_CMD];
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
208 p->sum=mysum;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
209 p->key=ntohs( *(const uint16_t *)(ptr+FSP_OFFSET_KEY) );
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
210 p->seq=ntohs( *(const uint16_t *)(ptr+FSP_OFFSET_SEQ) );
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
211 p->len=ntohs( *(const uint16_t *)(ptr+FSP_OFFSET_LEN) );
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
212 p->pos=ntohl( *(const uint32_t *)(ptr+FSP_OFFSET_POS) );
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
213 if(p->len > recv_len)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
214 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
215 /* bad length field, should not never happen */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
216 errno = EMSGSIZE;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
217 return -1;
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 p->xlen=recv_len - p->len - FSP_HSIZE;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
220 /* now copy data */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
221 memcpy(p->buf,ptr+FSP_HSIZE,recv_len - FSP_HSIZE);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
222 return 0;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
223 }
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 /* ****************** packet sending functions ************** */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
226
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
227 /* make one send + receive transaction with server */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
228 /* outgoing packet is in p, incomming in rpkt */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
229 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
230 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
231 char buf[FSP_MAXPACKET];
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
232 size_t l;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
233 ssize_t r;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
234 fd_set mask;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
235 struct timeval start[8],stop;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
236 int i;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
237 unsigned int retry,dupes;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
238 int w_delay; /* how long to wait on next packet */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
239 int f_delay; /* how long to wait after first send */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
240 int l_delay; /* last delay */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
241 unsigned int t_delay; /* time from first send */
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
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
244 if(p == rpkt)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
245 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
246 errno = EINVAL;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
247 return -2;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
248 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
249 FD_ZERO(&mask);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
250 /* get the next key */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
251 p->key = client_get_key((FSP_LOCK *)s->lock);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
252
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
253 dupes = retry = 0;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
254 s->seq = (s-> seq + 0x08) & 0xfff8;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
255 t_delay = 0;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
256 /* compute initial delay here */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
257 /* we are using hardcoded value for now */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
258 f_delay = 1340;
667
6b6fbcf76d00 2005-1-18 Brian Masney <masneyb@gftp.org>
masneyb
parents: 662
diff changeset
259 l_delay = 0;
662
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
260 for(;;retry++)
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 if(t_delay >= s->timeout)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
263 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
264 client_set_key((FSP_LOCK *)s->lock,p->key);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
265 errno = ETIMEDOUT;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
266 return -1;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
267 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
268 /* make a packet */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
269 p->seq = (s->seq) | (retry & 0x7);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
270 l=fsp_pkt_write(p,buf);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
271
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
272 /* We should compute next delay wait time here */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
273 gettimeofday(&start[retry & 0x7],NULL);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
274 if(retry == 0 )
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
275 w_delay=f_delay;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
276 else
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 w_delay=l_delay*3/2;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
279 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
280
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
281 l_delay=w_delay;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
282
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
283 /* send packet */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
284 if( send(s->fd,buf,l,0) < 0 )
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
285 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
286 #ifdef MAINTAINER_MODE
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
287 printf("Send failed.\n");
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
288 #endif
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
289 if(errno == EBADF || errno == ENOTSOCK)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
290 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
291 client_set_key((FSP_LOCK *)s->lock,p->key);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
292 errno = EBADF;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
293 return -1;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
294 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
295 /* io terror */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
296 sleep(1);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
297 /* avoid wasting retry slot */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
298 retry--;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
299 t_delay += 1000;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
300 continue;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
301 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
302
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
303 /* keep delay value within sane limits */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
304 if (w_delay > (int) s->maxdelay)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
305 w_delay=s->maxdelay;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
306 else
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
307 if(w_delay < 1000 )
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
308 w_delay = 1000;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
309
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
310 t_delay += w_delay;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
311 /* receive loop */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
312 while(1)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
313 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
314 if(w_delay <= 0 ) break;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
315 /* convert w_delay to timeval */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
316 stop.tv_sec=w_delay/1000;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
317 stop.tv_usec=(w_delay % 1000)*1000;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
318 FD_SET(s->fd,&mask);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
319 i=select(s->fd+1,&mask,NULL,NULL,&stop);
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 break; /* timed out */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
322 if(i<0)
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 if(errno==EINTR)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
325 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
326 /* lower w_delay */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
327 gettimeofday(&stop,NULL);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
328 w_delay-=1000*(stop.tv_sec - start[retry & 0x7].tv_sec);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
329 w_delay-= (stop.tv_usec - start[retry & 0x7].tv_usec)/1000;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
330 continue;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
331 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
332 /* hard select error */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
333 client_set_key((FSP_LOCK *)s->lock,p->key);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
334 return -1;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
335 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
336 r=recv(s->fd,buf,FSP_MAXPACKET,0);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
337 if(r < 0 )
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
338 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
339 /* serious recv error */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
340 client_set_key((FSP_LOCK *)s->lock,p->key);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
341 return -1;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
342 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
343
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
344 gettimeofday(&stop,NULL);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
345 w_delay-=1000*(stop.tv_sec - start[retry & 0x7].tv_sec);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
346 w_delay-= (stop.tv_usec - start[retry & 0x7].tv_usec)/1000;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
347
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
348 /* process received packet */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
349 if ( fsp_pkt_read(rpkt,buf,r) < 0)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
350 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
351 /* unpack failed */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
352 continue;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
353 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
354
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
355 /* check sequence number */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
356 if( (rpkt->seq & 0xfff8) != s->seq )
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
357 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
358 #ifdef MAINTAINER_MODE
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
359 printf("dupe\n");
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
360 #endif
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
361 /* duplicate */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
362 dupes++;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
363 continue;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
364 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
365
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
366 /* now we have a correct packet */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
367
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
368 /* compute rtt delay */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
369 w_delay=1000*(stop.tv_sec - start[retry & 0x7].tv_sec);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
370 w_delay+=(stop.tv_usec - start[retry & 0x7].tv_usec)/1000;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
371 /* update last stats */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
372 s->last_rtt=w_delay;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
373 s->last_delay=f_delay;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
374 s->last_dupes=dupes;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
375 s->last_resends=retry;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
376 /* update cumul. stats */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
377 s->dupes+=dupes;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
378 s->resends+=retry;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
379 s->trips++;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
380 s->rtts+=w_delay;
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 /* grab a next key */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
383 client_set_key((FSP_LOCK *)s->lock,rpkt->key);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
384 errno = 0;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
385 return 0;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
386 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
387 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
388 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
389
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
390 /* ******************* Session management functions ************ */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
391
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
392 /* initializes a session */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
393 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
394 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
395 FSP_SESSION *s;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
396 int fd;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
397 struct addrinfo hints,*res;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
398 char port_s[6];
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
399 struct sockaddr_in *addrin;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
400 FSP_LOCK *lock;
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 memset (&hints, 0, sizeof (hints));
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
403 /* fspd do not supports inet6 */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
404 hints.ai_family = PF_INET;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
405 hints.ai_socktype = SOCK_DGRAM;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
406
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
407 if (port == 0)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
408 strcpy(port_s,"fsp");
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
409 else
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
410 sprintf(port_s,"%hu",port);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
411
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
412 if ( getaddrinfo(host,port_s,&hints,&res) )
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
413 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
414 return NULL; /* host not found */
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
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
417 /* create socket */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
418 fd=socket(res->ai_family,res->ai_socktype,res->ai_protocol);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
419 if ( fd < 0)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
420 return NULL;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
421
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
422 /* connect socket */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
423 if( connect(fd, res->ai_addr, res->ai_addrlen))
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
424 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
425 close(fd);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
426 return NULL;
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
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
429 /* allocate memory */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
430 s=calloc(1,sizeof(FSP_SESSION));
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
431 if ( !s )
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
432 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
433 close(fd);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
434 errno = ENOMEM;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
435 return NULL;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
436 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
437
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
438 lock=malloc(sizeof(FSP_LOCK));
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
439
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
440 if ( !lock )
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 close(fd);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
443 free(s);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
444 errno = ENOMEM;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
445 return NULL;
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
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
448 s->lock=lock;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
449
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
450 /* init locking subsystem */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
451 addrin = (struct sockaddr_in *)res->ai_addr;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
452 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
453 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
454 free(s);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
455 close(fd);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
456 free(lock);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
457 return NULL;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
458 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
459
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
460 s->fd=fd;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
461 s->timeout=300000; /* 5 minutes */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
462 s->maxdelay=60000; /* 1 minute */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
463 s->seq=random();
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
464 if ( password )
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
465 s->password = strdup(password);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
466 return s;
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
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
469 /* closes a session */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
470 void fsp_close_session(FSP_SESSION *s)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
471 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
472 FSP_PKT bye,in;
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 if( s == NULL)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
475 return;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
476 if ( s->fd == -1)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
477 return;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
478 /* Send bye packet */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
479 bye.cmd=FSP_CC_BYE;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
480 bye.len=bye.xlen=0;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
481 bye.pos=0;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
482 s->timeout=7000;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
483 fsp_transaction(s,&bye,&in);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
484
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
485 close(s->fd);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
486 if (s->password) free(s->password);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
487 client_destroy_key((FSP_LOCK *)s->lock);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
488 free(s->lock);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
489 memset(s,0,sizeof(FSP_SESSION));
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
490 s->fd=-1;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
491 free(s);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
492 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
493
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
494 /* *************** Directory listing functions *************** */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
495
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
496 /* get a directory listing from a server */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
497 FSP_DIR * fsp_opendir(FSP_SESSION *s,const char *dirname)
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 FSP_PKT in,out;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
500 int pos;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
501 unsigned short blocksize;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
502 FSP_DIR *dir;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
503 unsigned char *tmp;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
504
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
505 if (s == NULL) return NULL;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
506 if (dirname == NULL) return NULL;
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 if(buildfilename(s,&out,dirname))
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 return NULL;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
511 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
512 pos=0;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
513 blocksize=0;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
514 dir=NULL;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
515 out.cmd = FSP_CC_GET_DIR;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
516 out.xlen=0;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
517
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
518 /* load directory listing from the server */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
519 while(1)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
520 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
521 out.pos=pos;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
522 if ( fsp_transaction(s,&out,&in) )
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 pos = -1;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
525 break;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
526 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
527 if ( in.cmd != FSP_CC_GET_DIR )
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
528 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
529 /* bad reply from the server */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
530 pos = -1;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
531 break;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
532 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
533 /* End of directory? */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
534 if ( in.len == 0)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
535 break;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
536 /* set blocksize */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
537 if (blocksize == 0 )
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
538 blocksize = in.len;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
539 /* alloc directory */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
540 if (dir == NULL)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
541 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
542 dir = calloc(1,sizeof(FSP_DIR));
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
543 if (dir == NULL)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
544 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
545 pos = -1;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
546 break;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
547 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
548 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
549 /* append data */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
550 tmp=realloc(dir->data,pos+in.len);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
551 if(tmp == NULL)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
552 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
553 pos = -1;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
554 break;
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->data=tmp;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
557 memcpy(dir->data + pos, in.buf,in.len);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
558 pos += in.len;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
559 if (in.len < blocksize)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
560 /* last block is smaller */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
561 break;
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 if (pos == -1)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
564 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
565 /* failure */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
566 if (dir)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
567 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
568 if(dir->data)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
569 free(dir->data);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
570 free(dir);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
571 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
572 return NULL;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
573 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
574
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
575 dir->inuse=1;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
576 dir->blocksize=blocksize;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
577 dir->dirname=strdup(dirname);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
578 dir->datasize=pos;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
579
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
580 return 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
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
583 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
584 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
585 FSP_RDENTRY fentry,*fresult;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
586 int rc;
667
6b6fbcf76d00 2005-1-18 Brian Masney <masneyb@gftp.org>
masneyb
parents: 662
diff changeset
587 char *c;
662
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
588
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
589 if (dir == NULL || entry == NULL || *result == NULL)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
590 return -EINVAL;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
591 if (dir->dirpos<0 || dir->dirpos % 4)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
592 return -ESPIPE;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
593
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
594 rc=fsp_readdir_native(dir,&fentry,&fresult);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
595
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
596 if (rc != 0)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
597 return rc;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
598
692
81172373727f 2005-2-13 Brian Masney <masneyb@gftp.org>
masneyb
parents: 667
diff changeset
599 #ifdef HAVE_DIRENT_TYPE
662
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
600 /* convert FSP dirent to OS dirent */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
601
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
602 if (fentry.type == FSP_RDTYPE_DIR )
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
603 entry->d_type=DT_DIR;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
604 else
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
605 entry->d_type=DT_REG;
692
81172373727f 2005-2-13 Brian Masney <masneyb@gftp.org>
masneyb
parents: 667
diff changeset
606 #endif
81172373727f 2005-2-13 Brian Masney <masneyb@gftp.org>
masneyb
parents: 667
diff changeset
607
667
6b6fbcf76d00 2005-1-18 Brian Masney <masneyb@gftp.org>
masneyb
parents: 662
diff changeset
608 /* remove symlink destination */
6b6fbcf76d00 2005-1-18 Brian Masney <masneyb@gftp.org>
masneyb
parents: 662
diff changeset
609 c=strchr(fentry.name,'\n');
6b6fbcf76d00 2005-1-18 Brian Masney <masneyb@gftp.org>
masneyb
parents: 662
diff changeset
610 if (c)
6b6fbcf76d00 2005-1-18 Brian Masney <masneyb@gftp.org>
masneyb
parents: 662
diff changeset
611 {
6b6fbcf76d00 2005-1-18 Brian Masney <masneyb@gftp.org>
masneyb
parents: 662
diff changeset
612 *c='\0';
6b6fbcf76d00 2005-1-18 Brian Masney <masneyb@gftp.org>
masneyb
parents: 662
diff changeset
613 rc=fentry.namlen-strlen(fentry.name);
6b6fbcf76d00 2005-1-18 Brian Masney <masneyb@gftp.org>
masneyb
parents: 662
diff changeset
614 fentry.reclen-=rc;
6b6fbcf76d00 2005-1-18 Brian Masney <masneyb@gftp.org>
masneyb
parents: 662
diff changeset
615 fentry.namlen-=rc;
6b6fbcf76d00 2005-1-18 Brian Masney <masneyb@gftp.org>
masneyb
parents: 662
diff changeset
616 }
6b6fbcf76d00 2005-1-18 Brian Masney <masneyb@gftp.org>
masneyb
parents: 662
diff changeset
617
692
81172373727f 2005-2-13 Brian Masney <masneyb@gftp.org>
masneyb
parents: 667
diff changeset
618 #ifdef HAVE_DIRENT_FILENO
662
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
619 entry->d_fileno = 10;
692
81172373727f 2005-2-13 Brian Masney <masneyb@gftp.org>
masneyb
parents: 667
diff changeset
620 #endif
662
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
621 entry->d_reclen = fentry.reclen;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
622 strncpy(entry->d_name,fentry.name,MAXNAMLEN);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
623
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
624 if (fentry.namlen > MAXNAMLEN)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
625 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
626 entry->d_name[MAXNAMLEN + 1 ] = '\0';
692
81172373727f 2005-2-13 Brian Masney <masneyb@gftp.org>
masneyb
parents: 667
diff changeset
627 #ifdef HAVE_DIRENT_NAMLEN
662
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
628 entry->d_namlen = MAXNAMLEN;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
629 } else
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
630 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
631 entry->d_namlen = fentry.namlen;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
632 #endif
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
633 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
634
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
635 if (fresult == &fentry )
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
636 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
637 *result = entry;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
638 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
639 else
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
640 *result = NULL;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
641
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
642 return 0;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
643 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
644
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
645 /* native FSP directory reader */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
646 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
647 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
648 unsigned char ftype;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
649 int namelen;
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 (dir == NULL || entry == NULL || *result == NULL)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
652 return -EINVAL;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
653 if (dir->dirpos<0 || dir->dirpos % 4)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
654 return -ESPIPE;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
655
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
656 while(1)
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 if ( dir->dirpos >= (int)dir->datasize )
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 /* end of the directory */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
661 *result = NULL;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
662 return 0;
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 if (dir->blocksize - (dir->dirpos % dir->blocksize) < 9)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
665 ftype= FSP_RDTYPE_SKIP;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
666 else
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
667 /* get the file type */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
668 ftype=dir->data[dir->dirpos+8];
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
669
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
670 if (ftype == FSP_RDTYPE_END )
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 dir->dirpos=dir->datasize;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
673 continue;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
674 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
675 if (ftype == FSP_RDTYPE_SKIP )
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
676 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
677 /* skip to next directory block */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
678 dir->dirpos = ( dir->dirpos / dir->blocksize + 1 ) * dir->blocksize;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
679 #ifdef MAINTAINER_MODE
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
680 printf("new block dirpos: %d\n",dir->dirpos);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
681 #endif
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
682 continue;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
683 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
684 /* extract binary data */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
685 entry->lastmod=ntohl( *(const uint32_t *)( dir->data+ dir->dirpos ));
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
686 entry->size=ntohl( *(const uint32_t *)(dir->data+ dir->dirpos +4 ));
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
687 entry->type=ftype;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
688
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
689 /* skip file date and file size */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
690 dir->dirpos += 9;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
691 /* read file name */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
692 entry->name[255 + 1] = '\0';
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
693 strncpy(entry->name,(char *)( dir->data + dir->dirpos ),MAXNAMLEN);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
694 namelen = strlen( (char *) dir->data+dir->dirpos);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
695 /* skip over file name */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
696 dir->dirpos += namelen +1;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
697
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
698 /* set entry namelen field */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
699 if (namelen > 255)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
700 entry->namlen = 255;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
701 else
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
702 entry->namlen = namelen;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
703 /* set record length */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
704 entry->reclen = 10+namelen;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
705
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
706 /* pad to 4 byte boundary */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
707 while( dir->dirpos & 0x3 )
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
708 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
709 dir->dirpos++;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
710 entry->reclen++;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
711 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
712
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
713 /* and return it */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
714 *result=entry;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
715 return 0;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
716 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
717 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
718
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
719 struct dirent * fsp_readdir(FSP_DIR *dirp)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
720 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
721 static struct dirent entry;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
722 struct dirent *result;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
723
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
724
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
725 if (dirp == NULL) return NULL;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
726 if ( fsp_readdir_r(dirp,&entry,&result) )
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
727 return NULL;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
728 else
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
729 return result;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
730 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
731
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
732 long fsp_telldir(FSP_DIR *dirp)
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 return dirp->dirpos;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
735 }
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 void fsp_seekdir(FSP_DIR *dirp, long loc)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
738 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
739 dirp->dirpos=loc;
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
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
742 void fsp_rewinddir(FSP_DIR *dirp)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
743 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
744 dirp->dirpos=0;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
745 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
746
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
747 int fsp_closedir(FSP_DIR *dirp)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
748 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
749 if (dirp == NULL)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
750 return -1;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
751 if(dirp->dirname) free(dirp->dirname);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
752 free(dirp->data);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
753 free(dirp);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
754 return 0;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
755 }
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 /* ***************** File input/output functions ********* */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
758 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
759 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
760 FSP_FILE *f;
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 if(session == NULL || path == NULL || modeflags == NULL)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
763 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
764 errno = EINVAL;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
765 return NULL;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
766 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
767
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
768 f=calloc(1,sizeof(FSP_FILE));
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
769 if (f == NULL)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
770 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
771 return NULL;
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
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
774 /* check and parse flags */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
775 switch (*modeflags++)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
776 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
777 case 'r':
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
778 break;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
779 case 'w':
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
780 f->writing=1;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
781 break;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
782 case 'a':
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
783 /* not supported */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
784 free(f);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
785 errno = ENOTSUP;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
786 return NULL;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
787 default:
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
788 free(f);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
789 errno = EINVAL;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
790 return NULL;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
791 }
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 if (*modeflags == '+' || ( *modeflags=='b' && modeflags[1]=='+'))
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
794 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
795 free(f);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
796 errno = ENOTSUP;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
797 return NULL;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
798 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
799
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
800 /* build request packet */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
801 if(f->writing)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
802 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
803 f->out.cmd=FSP_CC_UP_LOAD;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
804 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
805 else
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
806 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
807 if(buildfilename(session,&f->out,path))
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 free(f);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
810 return NULL;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
811 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
812 f->bufpos=FSP_SPACE;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
813 f->out.cmd=FSP_CC_GET_FILE;
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 f->out.xlen=0;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
816
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
817 /* setup control variables */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
818 f->s=session;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
819 f->name=strdup(path);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
820 if(f->name == NULL)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
821 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
822 free(f);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
823 errno = ENOMEM;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
824 return NULL;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
825 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
826
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
827 return f;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
828 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
829
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
830 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
831 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
832 size_t total,done,havebytes;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
833 char *ptr;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
834
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
835 total=count*size;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
836 done=0;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
837 ptr=dest;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
838
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
839 if(file->eof) return 0;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
840
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
841 while(1)
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 /* need more data? */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
844 if(file->bufpos>=FSP_SPACE)
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 /* fill the buffer */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
847 file->out.pos=file->pos;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
848 if(fsp_transaction(file->s,&file->out,&file->in))
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
849 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
850 file->err=1;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
851 return done/size;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
852 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
853 if(file->in.cmd != FSP_CC_GET_FILE)
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 errno = EIO;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
856 file->err=1;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
857 return done/size;
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 file->bufpos=FSP_SPACE-file->in.len;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
860 if(file->bufpos > 0)
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 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
863 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
864 file->pos+=file->in.len;
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 havebytes=FSP_SPACE - file->bufpos;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
867 if (havebytes == 0 )
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
868 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
869 /* end of file! */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
870 file->eof=1;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
871 errno = 0;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
872 return done/size;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
873 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
874 /* copy ready data to output buffer */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
875 if(havebytes <= total )
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
876 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
877 /* copy all we have */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
878 memcpy(ptr,file->in.buf+file->bufpos,havebytes);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
879 ptr+=havebytes;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
880 file->bufpos=FSP_SPACE;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
881 done+=havebytes;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
882 total-=havebytes;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
883 } else
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 /* copy bytes left */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
886 memcpy(ptr,file->in.buf+file->bufpos,total);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
887 file->bufpos+=total;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
888 errno = 0;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
889 return count;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
890 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
891 }
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
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
894 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
895 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
896 size_t total,done,freebytes;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
897 const char *ptr;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
898
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
899 if(file->eof || file->err)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
900 return 0;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
901
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
902 file->out.len=FSP_SPACE;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
903 total=count*size;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
904 done=0;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
905 ptr=source;
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 while(1)
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 /* need to write some data? */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
910 if(file->bufpos>=FSP_SPACE)
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 /* fill the buffer */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
913 file->out.pos=file->pos;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
914 if(fsp_transaction(file->s,&file->out,&file->in))
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
915 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
916 file->err=1;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
917 return done/size;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
918 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
919 if(file->in.cmd != FSP_CC_UP_LOAD)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
920 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
921 errno = EIO;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
922 file->err=1;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
923 return done/size;
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 file->bufpos=0;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
926 file->pos+=file->out.len;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
927 done+=file->out.len;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
928 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
929 freebytes=FSP_SPACE - file->bufpos;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
930 /* copy input data to output buffer */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
931 if(freebytes <= total )
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
932 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
933 /* copy all we have */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
934 memcpy(file->out.buf+file->bufpos,ptr,freebytes);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
935 ptr+=freebytes;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
936 file->bufpos=FSP_SPACE;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
937 total-=freebytes;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
938 } else
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
939 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
940 /* copy bytes left */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
941 memcpy(file->out.buf+file->bufpos,ptr,total);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
942 file->bufpos+=total;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
943 errno = 0;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
944 return count;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
945 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
946 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
947 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
948
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
949 int fsp_fpurge(FSP_FILE *file)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
950 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
951 if(file->writing)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
952 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
953 file->bufpos=0;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
954 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
955 else
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
956 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
957 file->bufpos=FSP_SPACE;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
958 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
959 errno = 0;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
960 return 0;
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 int fsp_fflush(FSP_FILE *file)
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 if(file == NULL)
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 errno = ENOTSUP;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
968 return -1;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
969 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
970 if(!file->writing)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
971 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
972 errno = EBADF;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
973 return -1;
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 if(file->eof || file->bufpos==0)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
976 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
977 errno = 0;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
978 return 0;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
979 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
980
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
981 file->out.pos=file->pos;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
982 file->out.len=file->bufpos;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
983 if(fsp_transaction(file->s,&file->out,&file->in))
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
984 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
985 file->err=1;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
986 return -1;
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 if(file->in.cmd != FSP_CC_UP_LOAD)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
989 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
990 errno = EIO;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
991 file->err=1;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
992 return -1;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
993 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
994 file->bufpos=0;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
995 file->pos+=file->out.len;
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 errno = 0;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
998 return 0;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
999 }
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
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1002
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1003 int fsp_fclose(FSP_FILE *file)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1004 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1005 int rc;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1006
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1007 rc=0;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1008 errno = 0;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1009 if(file->writing)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1010 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1011 if(fsp_fflush(file))
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 rc=-1;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1014 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1015 else if(fsp_install(file->s,file->name,0))
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 rc=-1;
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 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1020 free(file->name);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1021 free(file);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1022 return rc;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1023 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1024
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1025 int fsp_fseek(FSP_FILE *stream, long offset, int whence)
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 long newoffset;
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 switch(whence)
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 case SEEK_SET:
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1032 newoffset = offset;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1033 break;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1034 case SEEK_CUR:
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1035 newoffset = stream->pos + offset;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1036 break;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1037 case SEEK_END:
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1038 errno = ENOTSUP;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1039 return -1;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1040 default:
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1041 errno = EINVAL;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1042 return -1;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1043 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1044 if(stream->writing)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1045 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1046 if(fsp_fflush(stream))
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1047 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1048 return -1;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1049 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1050 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1051 stream->pos=newoffset;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1052 stream->eof=0;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1053 fsp_fpurge(stream);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1054 return 0;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1055 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1056
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1057 long fsp_ftell(FSP_FILE *f)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1058 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1059 return f->pos + f->bufpos;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1060 }
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 void fsp_rewind(FSP_FILE *f)
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 if(f->writing)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1065 fsp_fflush(f);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1066 f->pos=0;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1067 f->err=0;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1068 f->eof=0;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1069 fsp_fpurge(f);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1070 }
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 /* **************** Utility functions ****************** */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1073
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1074 /* return 0 if user has enough privs for uploading the file */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1075 int fsp_canupload(FSP_SESSION *s,const char *fname)
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 char *dir;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1078 unsigned char dirpro;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1079 int rc;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1080 struct stat sb;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1081
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1082 dir=directoryfromfilename(fname);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1083 if(dir == NULL)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1084 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1085 errno = ENOMEM;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1086 return -1;
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
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1089 rc=fsp_getpro(s,dir,&dirpro);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1090 free(dir);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1091
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1092 if(rc)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1093 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1094 return -1;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1095 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1096
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1097 if(dirpro & FSP_DIR_OWNER)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1098 return 0;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1099
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1100 if( ! (dirpro & FSP_DIR_ADD))
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1101 return -1;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1102
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1103 if (dirpro & FSP_DIR_DEL)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1104 return 0;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1105
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1106 /* 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
1107
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1108 rc = fsp_stat(s,fname,&sb);
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 if (rc == 0)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1111 return -1;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1112 else
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1113 return 0;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1114 }
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 /* install file opened for writing */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1117 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
1118 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1119 int rc;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1120 FSP_PKT in,out;
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 /* and install a new file */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1123 out.cmd=FSP_CC_INSTALL;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1124 out.xlen=0;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1125 out.pos=0;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1126 rc=0;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1127 if( buildfilename(s,&out,fname) )
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1128 rc=-1;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1129 else
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 if (timestamp != 0)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1132 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1133 /* add timestamp extra data */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1134 *(uint32_t *)(out.buf+out.len)=htonl(timestamp);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1135 out.xlen=4;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1136 out.pos=4;
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 if(fsp_transaction(s,&out,&in))
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1139 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1140 rc=-1;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1141 } else
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1142 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1143 if(in.cmd != FSP_CC_INSTALL)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1144 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1145 rc=-1;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1146 errno = EPERM;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1147 }
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 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1150
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1151 return rc;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1152 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1153 /* Get protection byte from the directory */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1154 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
1155 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1156 FSP_PKT in,out;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1157
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1158 if(buildfilename(s,&out,directory))
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1159 return -1;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1160 out.cmd=FSP_CC_GET_PRO;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1161 out.xlen=0;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1162 out.pos=0;
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 if(fsp_transaction(s,&out,&in))
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1165 return -1;
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 if(in.cmd != FSP_CC_GET_PRO || in.pos != FSP_PRO_BYTES)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1168 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1169 errno = ENOMSG;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1170 return -1;
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
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1173 if(result)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1174 *result=in.buf[in.len];
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1175 errno = 0;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1176 return 0;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1177 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1178
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1179 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
1180 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1181 FSP_PKT in,out;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1182 unsigned char ftype;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1183
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1184 if(buildfilename(s,&out,path))
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1185 return -1;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1186 out.cmd=FSP_CC_STAT;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1187 out.xlen=0;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1188 out.pos=0;
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 if(fsp_transaction(s,&out,&in))
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 if(in.cmd != FSP_CC_STAT)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1194 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1195 errno = ENOTSUP;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1196 return -1;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1197 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1198 /* parse results */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1199 ftype=in.buf[8];
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1200 if(ftype == 0)
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 errno = ENOENT;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1203 return -1;
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 sb->st_uid=sb->st_gid=0;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1206 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
1207 sb->st_size=ntohl( *(const uint32_t *)(in.buf + 4 ));
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1208 sb->st_blocks=(sb->st_size+511)/512;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1209 if (ftype==FSP_RDTYPE_DIR)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1210 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1211 sb->st_mode=S_IFDIR | 0755;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1212 sb->st_nlink=2;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1213 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1214 else
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 sb->st_mode=S_IFREG | 0644;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1217 sb->st_nlink=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
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1220 errno = 0;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1221 return 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
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1224 int fsp_mkdir(FSP_SESSION *s,const char *directory)
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 return simplecommand(s,directory,FSP_CC_MAKE_DIR);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1227 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1228
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1229 int fsp_rmdir(FSP_SESSION *s,const char *directory)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1230 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1231 return simplecommand(s,directory,FSP_CC_DEL_DIR);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1232 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1233
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1234 int fsp_unlink(FSP_SESSION *s,const char *directory)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1235 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1236 return simplecommand(s,directory,FSP_CC_DEL_FILE);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1237 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1238
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1239 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
1240 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1241 FSP_PKT in,out;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1242 int l;
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 if(buildfilename(s,&out,from))
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1245 return -1;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1246 /* append target name */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1247 l=strlen(to)+1;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1248 if( l + out.len > FSP_SPACE )
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 errno = ENAMETOOLONG;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1251 return -1;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1252 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1253 memcpy(out.buf+out.len,to,l);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1254 out.xlen = l;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1255
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1256 if(s->password)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1257 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1258 l=strlen(s->password)+1;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1259 if(out.len + out.xlen + l > FSP_SPACE)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1260 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1261 errno = ENAMETOOLONG;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1262 return -1;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1263 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1264 out.buf[out.len+out.xlen-1] = '\n';
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1265 memcpy(out.buf+out.len+out.xlen,s->password,l);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1266 out.xlen += l;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1267 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1268
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1269 out.cmd=FSP_CC_RENAME;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1270 out.pos=out.xlen;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1271
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1272 if(fsp_transaction(s,&out,&in))
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1273 return -1;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1274
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1275 if(in.cmd != FSP_CC_RENAME)
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 errno = EPERM;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1278 return -1;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1279 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1280
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1281 errno = 0;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1282 return 0;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1283 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1284
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1285 int fsp_access(FSP_SESSION *s,const char *path, int mode)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1286 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1287 struct stat sb;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1288 int rc;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1289 unsigned char dirpro;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1290 char *dir;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1291
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1292 rc=fsp_stat(s,path,&sb);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1293 if(rc == -1)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1294 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1295 /* not found */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1296 /* errno is set by fsp_stat */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1297 return -1;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1298 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1299
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1300 /* just test file existence */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1301 if(mode == F_OK)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1302 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1303 errno = 0;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1304 return 0;
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
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1307 /* deny execute access to file */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1308 if (mode & X_OK)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1309 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1310 if(S_ISREG(sb.st_mode))
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1311 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1312 errno = EACCES;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1313 return -1;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1314 }
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
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1317 /* Need to get ACL of directory */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1318 if(S_ISDIR(sb.st_mode))
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1319 dir=NULL;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1320 else
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1321 dir=directoryfromfilename(path);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1322
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1323 rc=fsp_getpro(s,dir==NULL?path:dir,&dirpro);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1324 /* get pro failure */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1325 if(rc)
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 if(dir) free(dir);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1328 errno = EACCES;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1329 return -1;
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 /* owner shortcut */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1332 if(dirpro & FSP_DIR_OWNER)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1333 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1334 if(dir) free(dir);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1335 errno = 0;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1336 return 0;
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 /* check read access */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1339 if(mode & R_OK)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1340 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1341 if(dir)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1342 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1343 if(! (dirpro & FSP_DIR_GET))
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1344 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1345 free(dir);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1346 errno = EACCES;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1347 return -1;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1348 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1349 } else
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1350 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1351 if(! (dirpro & FSP_DIR_LIST))
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1352 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1353 errno = EACCES;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1354 return -1;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1355 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1356 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1357 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1358 /* check write access */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1359 if(mode & W_OK)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1360 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1361 if(dir)
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1362 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1363 if( !(dirpro & FSP_DIR_DEL) || !(dirpro & FSP_DIR_ADD))
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1364 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1365 free(dir);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1366 errno = EACCES;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1367 return -1;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1368 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1369 } else
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1370 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1371 /* when checking directory for write access we are cheating
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1372 by allowing ADD or DEL right */
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1373 if( !(dirpro & FSP_DIR_DEL) && !(dirpro & FSP_DIR_ADD))
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1374 {
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1375 errno = EACCES;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1376 return -1;
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 }
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1380
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1381 if(dir) free(dir);
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1382 errno = 0;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1383 return 0;
be9663e0be00 2005-1-16 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1384 }