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