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