]> arthur.barton.de Git - netatalk.git/blob - libatalk/adouble/ad_lock.c
Ignore EAs on symlinks
[netatalk.git] / libatalk / adouble / ad_lock.c
1 /* 
2  * Copyright (c) 1998,1999 Adrian Sun (asun@zoology.washington.edu)
3  * All Rights Reserved. See COPYRIGHT for more information.
4  *
5  * Because fcntl locks are
6  * process-oriented, we need to keep around a list of file descriptors
7  * that refer to the same file.
8  *
9  * TODO: fix the race when reading/writing.
10  *       keep a pool of both locks and reference counters around so that
11  *       we can save on mallocs. we should also use a tree to keep things
12  *       sorted.
13  */
14
15 #ifdef HAVE_CONFIG_H
16 #include "config.h"
17 #endif /* HAVE_CONFIG_H */
18
19 #include <atalk/adouble.h>
20 #include <atalk/logger.h>
21 #include <atalk/compat.h>
22 #include <atalk/errchk.h>
23 #include <atalk/util.h>
24
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <errno.h>
28 #include <inttypes.h>
29
30 #include <string.h>
31
32 #include "ad_lock.h"
33
34 static const char *shmdstrfromoff(off_t off)
35 {
36     switch (off) {
37     case AD_FILELOCK_OPEN_WR:
38         return "OPEN_WR_DATA";
39     case AD_FILELOCK_OPEN_RD:
40         return "OPEN_RD_DATA";
41     case AD_FILELOCK_RSRC_OPEN_WR:
42         return "OPEN_WR_RSRC";
43     case AD_FILELOCK_RSRC_OPEN_RD:
44         return "OPEN_RD_RSRC";
45     case AD_FILELOCK_DENY_WR:
46         return "DENY_WR_DATA";
47     case AD_FILELOCK_DENY_RD:
48         return "DENY_RD_DATA";
49     case AD_FILELOCK_RSRC_DENY_WR:
50         return "DENY_WR_RSRC";
51     case AD_FILELOCK_RSRC_DENY_RD:
52         return "DENY_RD_RSRC";
53     case AD_FILELOCK_OPEN_NONE:
54         return "OPEN_NONE_DATA";
55     case AD_FILELOCK_RSRC_OPEN_NONE:
56         return "OPEN_NONE_RSRC";
57     default:
58         return "-";
59     }
60 }
61
62 /* ----------------------- */
63 static int set_lock(int fd, int cmd,  struct flock *lock)
64 {
65     EC_INIT;
66
67     LOG(log_debug, logtype_default, "set_lock(fd: %d, %s, %s, off: %jd (%s), len: %jd): BEGIN",
68         fd, cmd == F_SETLK ? "F_SETLK" : "F_GETLK",
69         lock->l_type == F_RDLCK ? "F_RDLCK" : lock->l_type == F_WRLCK ? "F_WRLCK" : "F_UNLCK",
70         (intmax_t)lock->l_start,
71         shmdstrfromoff(lock->l_start),
72         (intmax_t)lock->l_len);
73
74     if (fd == -2) {
75         /* We assign fd = -2 for symlinks -> do nothing */
76         if (cmd == F_GETLK)
77             lock->l_type = F_UNLCK;
78         return 0;
79     }
80
81     EC_NEG1( fcntl(fd, cmd, lock) );
82
83 EC_CLEANUP:
84     EC_EXIT;
85 }
86
87 /* ----------------------- */
88 static int XLATE_FCNTL_LOCK(int type) 
89 {
90     switch(type) {
91     case ADLOCK_RD:
92         return F_RDLCK;
93     case ADLOCK_WR:
94          return F_WRLCK;
95     case ADLOCK_CLR:
96          return F_UNLCK;
97     }
98     return -1;
99 }
100
101 /* ----------------------- */
102 static int OVERLAP(off_t a, off_t alen, off_t b, off_t blen) 
103 {
104     return (!alen && a <= b) || 
105         (!blen && b <= a) || 
106         ( (a + alen > b) && (b + blen > a) );
107 }
108
109 /* allocation for lock regions. we allocate aggressively and shrink
110  * only in large chunks. */
111 #define ARRAY_BLOCK_SIZE 10 
112 #define ARRAY_FREE_DELTA 100
113
114 /* remove a lock and compact space if necessary */
115 static void adf_freelock(struct ad_fd *ad, const int i)
116 {
117     adf_lock_t *lock = ad->adf_lock + i;
118
119     if (--(*lock->refcount) < 1) {
120         free(lock->refcount); 
121     lock->lock.l_type = F_UNLCK;
122     set_lock(ad->adf_fd, F_SETLK, &lock->lock); /* unlock */
123     }
124
125     ad->adf_lockcount--;
126
127     /* move another lock into the empty space */
128     if (i < ad->adf_lockcount) {
129         memcpy(lock, lock + ad->adf_lockcount - i, sizeof(adf_lock_t));
130     }
131
132     /* free extra cruft if we go past a boundary. we always want to
133      * keep at least some stuff around for allocations. this wastes
134      * a bit of space to save time on reallocations. */
135     if ((ad->adf_lockmax > ARRAY_FREE_DELTA) &&
136         (ad->adf_lockcount + ARRAY_FREE_DELTA < ad->adf_lockmax)) {
137             struct adf_lock_t *tmp;
138
139             tmp = (struct adf_lock_t *) 
140                     realloc(ad->adf_lock, sizeof(adf_lock_t)*
141                             (ad->adf_lockcount + ARRAY_FREE_DELTA));
142             if (tmp) {
143                 ad->adf_lock = tmp;
144                 ad->adf_lockmax = ad->adf_lockcount + ARRAY_FREE_DELTA;
145             }
146     }
147 }
148
149
150 /* this needs to deal with the following cases:
151  * 1) free all UNIX byterange lock from any fork
152  * 2) free all locks of the requested fork
153  *
154  * i converted to using arrays of locks. everytime a lock
155  * gets removed, we shift all of the locks down.
156  */
157 static void adf_unlock(struct adouble *ad, struct ad_fd *adf, const int fork, int unlckbrl)
158 {
159     adf_lock_t *lock = adf->adf_lock;
160     int i;
161
162     for (i = 0; i < adf->adf_lockcount; i++) {
163         if ((unlckbrl && lock[i].lock.l_start < AD_FILELOCK_BASE)
164             || lock[i].user == fork) {
165             /* we're really going to delete this lock. note: read locks
166                are the only ones that allow refcounts > 1 */
167             adf_freelock(adf, i);
168             /* we shifted things down, so we need to backtrack */
169             i--; 
170             /* unlikely but realloc may have change adf_lock */
171             lock = adf->adf_lock;       
172         }
173     }
174 }
175
176 /* relock any byte lock that overlaps off/len. unlock everything
177  * else. */
178 static void adf_relockrange(struct ad_fd *ad, int fd, off_t off, off_t len)
179 {
180     adf_lock_t *lock = ad->adf_lock;
181     int i;
182     
183     for (i = 0; i < ad->adf_lockcount; i++) {
184         if (OVERLAP(off, len, lock[i].lock.l_start, lock[i].lock.l_len)) 
185             set_lock(fd, F_SETLK, &lock[i].lock);
186     }
187 }
188
189
190 /* find a byte lock that overlaps off/len for a particular open fork */
191 static int adf_findlock(struct ad_fd *ad,
192                         const int fork, const int type,
193                         const off_t off,
194                         const off_t len)
195 {
196   adf_lock_t *lock = ad->adf_lock;
197   int i;
198   
199   for (i = 0; i < ad->adf_lockcount; i++) {
200     if ((((type & ADLOCK_RD) && (lock[i].lock.l_type == F_RDLCK)) ||
201         ((type & ADLOCK_WR) && (lock[i].lock.l_type == F_WRLCK))) &&
202         (lock[i].user == fork) && 
203         OVERLAP(off, len, lock[i].lock.l_start, lock[i].lock.l_len)) {
204       return i;
205     }
206   }
207   return -1;
208 }
209
210
211 /* search other fork lock lists */
212 static int adf_findxlock(struct ad_fd *ad, 
213                          const int fork, const int type,
214                          const off_t off,
215                          const off_t len)
216 {
217     adf_lock_t *lock = ad->adf_lock;
218     int i;
219   
220     for (i = 0; i < ad->adf_lockcount; i++) {
221         if ((((type & ADLOCK_RD) && (lock[i].lock.l_type == F_RDLCK))
222              ||
223              ((type & ADLOCK_WR) && (lock[i].lock.l_type == F_WRLCK)))
224             &&
225             (lock[i].user != fork)
226             && 
227             OVERLAP(off, len, lock[i].lock.l_start, lock[i].lock.l_len)) 
228             return i;
229     } 
230     return -1;
231 }
232
233 /* okay, this needs to do the following:
234  * 1) check current list of locks. error on conflict.
235  * 2) apply the lock. error on conflict with another process.
236  * 3) update the list of locks this file has.
237  * 
238  * NOTE: this treats synchronization locks a little differently. we
239  *       do the following things for those:
240  *       1) if the header file exists, all the locks go in the beginning
241  *          of that.
242  *       2) if the header file doesn't exist, we stick the locks
243  *          in the locations specified by AD_FILELOCK_RD/WR.
244  */
245
246 /* -------------- 
247         translate a resource fork lock to an offset
248 */
249 static off_t rf2off(off_t off)
250 {
251     off_t start = off;
252         if (off == AD_FILELOCK_OPEN_WR)
253                 start = AD_FILELOCK_RSRC_OPEN_WR;
254         else if (off == AD_FILELOCK_OPEN_RD)
255                 start = AD_FILELOCK_RSRC_OPEN_RD;
256     else if (off == AD_FILELOCK_DENY_RD)
257                 start = AD_FILELOCK_RSRC_DENY_RD;
258         else if (off == AD_FILELOCK_DENY_WR)
259                 start = AD_FILELOCK_RSRC_DENY_WR;
260         else if (off == AD_FILELOCK_OPEN_NONE)
261                 start = AD_FILELOCK_RSRC_OPEN_NONE;
262         return start;
263 }
264
265 /*!
266  * Test a lock
267  *
268  * (1) Test against our own locks array
269  * (2) Test fcntl lock, locks from other processes
270  *
271  * @param adf     (r) handle
272  * @param off     (r) offset
273  * @param len     (r) lenght
274  *
275  * @returns           1 if there's an existing lock, 0 if there's no lock,
276  *                    -1 in case any error occured
277  */
278 static int testlock(const struct ad_fd *adf, off_t off, off_t len)
279 {
280     struct flock lock;
281     adf_lock_t *plock;
282     int i;
283
284     lock.l_start = off;
285
286     plock = adf->adf_lock;
287     lock.l_whence = SEEK_SET;
288     lock.l_len = len;
289
290     /* (1) Do we have a lock ? */
291     for (i = 0; i < adf->adf_lockcount; i++) {
292         if (OVERLAP(lock.l_start, 1, plock[i].lock.l_start, plock[i].lock.l_len)) 
293             return 1;
294     }
295
296     /* (2) Does another process have a lock? */
297     lock.l_type = (adf->adf_flags & O_RDWR) ? F_WRLCK : F_RDLCK;
298
299     if (set_lock(adf->adf_fd, F_GETLK, &lock) < 0) {
300         /* is that kind of error possible ?*/
301         return (errno == EACCES || errno == EAGAIN) ? 1 : -1;
302     }
303   
304     if (lock.l_type == F_UNLCK) {
305         return 0;
306     }
307     return 1;
308 }
309
310 #define LTYPE2STRBUFSIZ 128
311 static const char *locktypetostr(int type)
312 {
313     int first = 1;
314     static char buf[LTYPE2STRBUFSIZ];
315
316     buf[0] = 0;
317
318     if (type == 0) {
319         strlcat(buf, "CLR", LTYPE2STRBUFSIZ);
320         first = 0;
321         return buf;
322     }
323     if (type & ADLOCK_RD) {
324         if (!first)
325             strlcat(buf, "|", LTYPE2STRBUFSIZ);
326         strlcat(buf, "RD", LTYPE2STRBUFSIZ);
327         first = 0;
328     }
329     if (type & ADLOCK_WR) {
330         if (!first)
331             strlcat(buf, "|", LTYPE2STRBUFSIZ);
332         strlcat(buf, "WR", LTYPE2STRBUFSIZ);
333         first = 0;
334     }
335     if (type & ADLOCK_UPGRADE) {
336         if (!first)
337             strlcat(buf, "|", LTYPE2STRBUFSIZ);
338         strlcat(buf, "UPG", LTYPE2STRBUFSIZ);
339         first = 0;
340     }
341     if (type & ADLOCK_FILELOCK) {
342         if (!first)
343             strlcat(buf, "|", LTYPE2STRBUFSIZ);
344         strlcat(buf, "FILELOCK", LTYPE2STRBUFSIZ);
345         first = 0;
346     }
347
348     return buf;
349 }
350
351 /******************************************************************************
352  * Public functions
353  ******************************************************************************/
354
355 int ad_lock(struct adouble *ad, uint32_t eid, int locktype, off_t off, off_t len, int fork)
356 {
357     struct flock lock;
358     struct ad_fd *adf;
359     adf_lock_t *adflock;
360     int oldlock;
361     int i;
362     int type;  
363     int ret = 0, fcntl_lock_err = 0;
364
365     LOG(log_debug, logtype_default, "ad_lock(\"%s\", %s, %s, off: %jd (%s), len: %jd): BEGIN",
366         ad->ad_m_name ? ad->ad_m_name : "???",
367         eid == ADEID_DFORK ? "data" : "reso",
368         locktypetostr(locktype),
369         (intmax_t)off,
370         shmdstrfromoff(off),
371         (intmax_t)len);
372
373     if ((locktype & ADLOCK_FILELOCK) && (len != 1))
374         AFP_PANIC("lock API error");
375
376     type = locktype;
377
378     if (eid == ADEID_DFORK) {
379         adf = &ad->ad_data_fork;
380         lock.l_start = off;
381     } else { /* rfork */
382         if (type & ADLOCK_FILELOCK) {
383             adf = &ad->ad_data_fork;
384             lock.l_start = rf2off(off);
385         } else {
386             adf = ad->ad_rfp;
387             lock.l_start = off + ad_getentryoff(ad, ADEID_RFORK);
388         }
389     }
390
391     /* NOTE: we can't write lock a read-only file. on those, we just
392      * make sure that we have a read lock set. that way, we at least prevent
393      * someone else from really setting a deny read/write on the file. 
394      */
395     if (!(adf->adf_flags & O_RDWR) && (type & ADLOCK_WR)) {
396         type = (type & ~ADLOCK_WR) | ADLOCK_RD;
397     }
398   
399     lock.l_type = XLATE_FCNTL_LOCK(type & ADLOCK_MASK);
400     lock.l_whence = SEEK_SET;
401     lock.l_len = len;
402
403     /* byte_lock(len=-1) lock whole file */
404     if (len == BYTELOCK_MAX) {
405         lock.l_len -= lock.l_start; /* otherwise  EOVERFLOW error */
406     }
407
408     /* see if it's locked by another fork. 
409      * NOTE: this guarantees that any existing locks must be at most
410      * read locks. we use ADLOCK_WR/RD because F_RD/WRLCK aren't
411      * guaranteed to be ORable. */
412     if (adf_findxlock(adf, fork, ADLOCK_WR | 
413                       ((type & ADLOCK_WR) ? ADLOCK_RD : 0), 
414                       lock.l_start, lock.l_len) > -1) {
415         errno = EACCES;
416         ret = -1;
417         goto exit;
418     }
419   
420     /* look for any existing lock that we may have */
421     i = adf_findlock(adf, fork, ADLOCK_RD | ADLOCK_WR, lock.l_start, lock.l_len);
422     adflock = (i < 0) ? NULL : adf->adf_lock + i;
423
424     /* here's what we check for:
425        1) we're trying to re-lock a lock, but we didn't specify an update.
426        2) we're trying to free only part of a lock. 
427        3) we're trying to free a non-existent lock. */
428     if ( (!adflock && (lock.l_type == F_UNLCK))
429          ||
430          (adflock
431           && !(type & ADLOCK_UPGRADE)
432           && ((lock.l_type != F_UNLCK)
433               || (adflock->lock.l_start != lock.l_start)
434               || (adflock->lock.l_len != lock.l_len) ))
435         ) {
436         errno = EINVAL;
437         ret = -1;
438         goto exit;
439     }
440
441
442     /* now, update our list of locks */
443     /* clear the lock */
444     if (lock.l_type == F_UNLCK) { 
445         adf_freelock(adf, i);
446         goto exit;
447     }
448
449     /* attempt to lock the file. */
450     if (set_lock(adf->adf_fd, F_SETLK, &lock) < 0) {
451         ret = -1;
452         goto exit;
453     }
454
455     /* we upgraded this lock. */
456     if (adflock && (type & ADLOCK_UPGRADE)) {
457         memcpy(&adflock->lock, &lock, sizeof(lock));
458         goto exit;
459     } 
460
461     /* it wasn't an upgrade */
462     oldlock = -1;
463     if (lock.l_type == F_RDLCK) {
464         oldlock = adf_findxlock(adf, fork, ADLOCK_RD, lock.l_start, lock.l_len);
465     } 
466     
467     /* no more space. this will also happen if lockmax == lockcount == 0 */
468     if (adf->adf_lockmax == adf->adf_lockcount) { 
469         adf_lock_t *tmp = (adf_lock_t *) 
470             realloc(adf->adf_lock, sizeof(adf_lock_t)*
471                     (adf->adf_lockmax + ARRAY_BLOCK_SIZE));
472         if (!tmp) {
473             ret = fcntl_lock_err = -1;
474             goto exit;
475         }
476         adf->adf_lock = tmp;
477         adf->adf_lockmax += ARRAY_BLOCK_SIZE;
478     } 
479     adflock = adf->adf_lock + adf->adf_lockcount;
480
481     /* fill in fields */
482     memcpy(&adflock->lock, &lock, sizeof(lock));
483     adflock->user = fork;
484     if (oldlock > -1) {
485         adflock->refcount = (adf->adf_lock + oldlock)->refcount;
486     } else if ((adflock->refcount = calloc(1, sizeof(int))) == NULL) {
487         ret = fcntl_lock_err = 1;
488         goto exit;
489     }
490   
491     (*adflock->refcount)++;
492     adf->adf_lockcount++;
493
494 exit:
495     if (ret != 0) {
496         if (fcntl_lock_err != 0) {
497             lock.l_type = F_UNLCK;
498             set_lock(adf->adf_fd, F_SETLK, &lock);
499         }
500     }
501     LOG(log_debug, logtype_default, "ad_lock: END: %d", ret);
502     return ret;
503 }
504
505 int ad_tmplock(struct adouble *ad, uint32_t eid, int locktype, off_t off, off_t len, int fork)
506 {
507     struct flock lock;
508     struct ad_fd *adf;
509     int err;
510     int type;  
511
512     LOG(log_debug, logtype_default, "ad_tmplock(\"%s\", %s, %s, off: %jd (%s), len: %jd): BEGIN",
513         ad->ad_m_name ? ad->ad_m_name : "???",
514         eid == ADEID_DFORK ? "data" : "reso",
515         locktypetostr(locktype),
516         (intmax_t)off,
517         shmdstrfromoff(off),
518         (intmax_t)len);
519
520     lock.l_start = off;
521     type = locktype;
522
523     if (eid == ADEID_DFORK) {
524         adf = &ad->ad_data_fork;
525     } else {
526         adf = &ad->ad_resource_fork;
527         if (adf->adf_fd == -1) {
528             /* there's no resource fork. return success */
529             err = 0;
530             goto exit;
531         }
532         /* if ADLOCK_FILELOCK we want a lock from offset 0
533          * it's used when deleting a file:
534          * in open we put read locks on meta datas
535          * in delete a write locks on the whole file
536          * so if the file is open by somebody else it fails
537          */
538         if (!(type & ADLOCK_FILELOCK))
539             lock.l_start += ad_getentryoff(ad, eid);
540     }
541
542     if (!(adf->adf_flags & O_RDWR) && (type & ADLOCK_WR)) {
543         type = (type & ~ADLOCK_WR) | ADLOCK_RD;
544     }
545   
546     lock.l_type = XLATE_FCNTL_LOCK(type & ADLOCK_MASK);
547     lock.l_whence = SEEK_SET;
548     lock.l_len = len;
549
550     /* see if it's locked by another fork. */
551     if (fork && adf_findxlock(adf, fork,
552                               ADLOCK_WR | ((type & ADLOCK_WR) ? ADLOCK_RD : 0), 
553                               lock.l_start, lock.l_len) > -1) {
554         errno = EACCES;
555         err = -1;
556         goto exit;
557     }
558
559     /* okay, we might have ranges byte-locked. we need to make sure that
560      * we restore the appropriate ranges once we're done. so, we check
561      * for overlap on an unlock and relock. 
562      * XXX: in the future, all the byte locks will be sorted and contiguous.
563      *      we just want to upgrade all the locks and then downgrade them
564      *      here. */
565     err = set_lock(adf->adf_fd, F_SETLK, &lock);
566     if (!err && (lock.l_type == F_UNLCK))
567         adf_relockrange(adf, adf->adf_fd, lock.l_start, len);
568
569 exit:
570     LOG(log_debug, logtype_default, "ad_tmplock: END: %d", err);
571     return err;
572 }
573
574 /* --------------------- */
575 void ad_unlock(struct adouble *ad, const int fork, int unlckbrl)
576 {
577     LOG(log_debug, logtype_default, "ad_unlock(\"%s\", unlckbrl: %d): BEGIN",
578         ad->ad_m_name ? ad->ad_m_name : "???", unlckbrl);
579
580     if (ad_data_fileno(ad) != -1) {
581         adf_unlock(ad, &ad->ad_data_fork, fork, unlckbrl);
582     }
583     if (ad_reso_fileno(ad) != -1) {
584         adf_unlock(ad, &ad->ad_resource_fork, fork, unlckbrl);
585     }
586
587     LOG(log_debug, logtype_default, "ad_unlock(\"%s\"): END", ad->ad_m_name ? ad->ad_m_name : "???");
588 }
589
590 /*!
591  * Test for a share mode lock
592  *
593  * @param ad      (rw) handle
594  * @param eid     (r)  datafork or ressource fork
595  * @param off     (r)  sharemode lock to test
596  *
597  * @returns           1 if there's an existing lock, 0 if there's no lock,
598  *                    -1 in case any error occured
599  */
600 int ad_testlock(struct adouble *ad, int eid, const off_t off)
601 {
602     int ret = 0;
603     off_t lock_offset;
604
605     LOG(log_debug, logtype_default, "ad_testlock(\"%s\", %s, off: %jd (%s): BEGIN",
606         ad->ad_m_name ? ad->ad_m_name : "???",
607         eid == ADEID_DFORK ? "data" : "reso",
608         (intmax_t)off,
609         shmdstrfromoff(off));
610
611     if (eid == ADEID_DFORK) {
612         lock_offset = off;
613     } else { /* rfork */
614         lock_offset = rf2off(off);
615     }
616
617     ret = testlock(&ad->ad_data_fork, lock_offset, 1);
618
619     LOG(log_debug, logtype_default, "ad_testlock: END: %d", ret);
620     return ret;
621 }
622
623 /*!
624  * Return if a file is open by another process.
625  *
626  * Optimized for the common case:
627  * - there's no locks held by another process (clients)
628  * - or we already know the answer and don't need to test (attrbits)
629  *
630  * @param ad          (rw) handle
631  * @param attrbits    (r)  forks opened by us
632  * @returns                bitflags ATTRBIT_DOPEN | ATTRBIT_ROPEN if
633  *                         other process has fork of file opened 
634  */
635 uint16_t ad_openforks(struct adouble *ad, uint16_t attrbits)
636 {
637     uint16_t ret = 0;
638     struct ad_fd *adf;
639     off_t off;
640     off_t len;
641
642     if (ad_data_fileno(ad) == -1)
643         return 0;
644
645     if (!(attrbits & (ATTRBIT_DOPEN | ATTRBIT_ROPEN))) {
646         /* Test all 4 locks at once */
647         off = AD_FILELOCK_OPEN_WR;
648         len = 4;
649         if (testlock(&ad->ad_data_fork, off, len) == 0)
650             return 0;
651     }
652
653     /* either there's a lock or we already know one fork is open */
654
655     if (!(attrbits & ATTRBIT_DOPEN)) {
656         off = AD_FILELOCK_OPEN_WR;
657         ret = testlock(&ad->ad_data_fork, off, 2) > 0 ? ATTRBIT_DOPEN : 0;
658     }
659
660     if (!(attrbits & ATTRBIT_ROPEN)) {
661         off = AD_FILELOCK_RSRC_OPEN_WR;
662         ret |= testlock(&ad->ad_data_fork, off, 2) > 0? ATTRBIT_ROPEN : 0;
663     }
664
665     return ret;
666 }