Annotation of libaitio/src/aitio.c, revision 1.10.6.6

1.1       misho       1: /*************************************************************************
1.5       misho       2: * (C) 2010 AITNET ltd - Sofia/Bulgaria - <misho@aitnet.org>
                      3: *  by Michael Pounov <misho@elwix.org>
1.1       misho       4: *
                      5: * $Author: misho $
1.10.6.6! misho       6: * $Id: aitio.c,v 1.10.6.5 2012/05/23 14:06:08 misho Exp $
1.1       misho       7: *
1.5       misho       8: **************************************************************************
                      9: The ELWIX and AITNET software is distributed under the following
                     10: terms:
                     11: 
                     12: All of the documentation and software included in the ELWIX and AITNET
                     13: Releases is copyrighted by ELWIX - Sofia/Bulgaria <info@elwix.org>
                     14: 
1.9       misho      15: Copyright 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
1.5       misho      16:        by Michael Pounov <misho@elwix.org>.  All rights reserved.
                     17: 
                     18: Redistribution and use in source and binary forms, with or without
                     19: modification, are permitted provided that the following conditions
                     20: are met:
                     21: 1. Redistributions of source code must retain the above copyright
                     22:    notice, this list of conditions and the following disclaimer.
                     23: 2. Redistributions in binary form must reproduce the above copyright
                     24:    notice, this list of conditions and the following disclaimer in the
                     25:    documentation and/or other materials provided with the distribution.
                     26: 3. All advertising materials mentioning features or use of this software
                     27:    must display the following acknowledgement:
                     28: This product includes software developed by Michael Pounov <misho@elwix.org>
                     29: ELWIX - Embedded LightWeight unIX and its contributors.
                     30: 4. Neither the name of AITNET nor the names of its contributors
                     31:    may be used to endorse or promote products derived from this software
                     32:    without specific prior written permission.
                     33: 
                     34: THIS SOFTWARE IS PROVIDED BY AITNET AND CONTRIBUTORS ``AS IS'' AND
                     35: ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     36: IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     37: ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     38: FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     39: DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     40: OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     41: HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     42: LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     43: OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     44: SUCH DAMAGE.
                     45: */
1.1       misho      46: #include "global.h"
                     47: 
                     48: 
1.2       misho      49: int io_Debug;
1.10.6.4  misho      50: mpool_t *io_mpool;
                     51: 
                     52: 
                     53: /* Memory management */
                     54: 
                     55: void *(*io_malloc)(size_t) = malloc;
                     56: void *(*io_calloc)(size_t, size_t) = calloc;
                     57: void *(*io_realloc)(void*, size_t) = realloc;
                     58: char *(*io_strdup)(const char*) = strdup;
                     59: void (*io_free)(void*) = free;
1.2       misho      60: 
                     61: 
1.1       misho      62: #pragma GCC visibility push(hidden)
                     63: 
1.10.6.4  misho      64: int use_mm;
1.10.6.2  misho      65: 
1.1       misho      66: int io_Errno;
                     67: char io_Error[STRSIZ];
                     68: 
                     69: #pragma GCC visibility pop
                     70: 
                     71: 
                     72: // io_GetErrno() Get error code of last operation
1.6       misho      73: inline int
                     74: io_GetErrno()
1.1       misho      75: {
                     76:        return io_Errno;
                     77: }
                     78: 
                     79: // io_GetError() Get error text of last operation
1.6       misho      80: inline const char *
                     81: io_GetError()
1.1       misho      82: {
                     83:        return io_Error;
                     84: }
                     85: 
                     86: // io_SetErr() Set error to variables for internal use!!!
1.6       misho      87: inline void
                     88: io_SetErr(int eno, char *estr, ...)
1.1       misho      89: {
                     90:        va_list lst;
                     91: 
                     92:        io_Errno = eno;
                     93:        memset(io_Error, 0, STRSIZ);
                     94:        va_start(lst, estr);
                     95:        vsnprintf(io_Error, STRSIZ, estr, lst);
                     96:        va_end(lst);
                     97: }
                     98: 
1.10.6.4  misho      99: // io_mm_inuse() Check for memory management model
1.10.6.2  misho     100: inline int
1.10.6.4  misho     101: io_mm_inuse()
1.10.6.2  misho     102: {
1.10.6.4  misho     103:        return use_mm & IO_MPOOL;
1.10.6.2  misho     104: }
                    105: 
1.1       misho     106: 
1.10.6.3  misho     107: // init libaitio routine
                    108: void
                    109: _init()
                    110: {
1.10.6.4  misho     111:        ioLibInit(IO_MPOOL, 0);
1.10.6.3  misho     112: }
                    113: 
                    114: // fini libaitio routine
                    115: void
                    116: _fini()
                    117: {
1.10.6.4  misho     118:        ioLibFini();
                    119: }
                    120: 
                    121: /*
                    122:  * ioLibInit() - Init libaitio library memory management
                    123:  *
                    124:  * @mm = memory management (IO_SYSM or IO_MPOOL)
                    125:  * @maxmem = memory limit
                    126:  * return: -1 error or !=-1 used memory management model
                    127:  */
                    128: inline int
                    129: ioLibInit(int mm, u_long maxmem)
                    130: {
                    131:        switch (mm) {
                    132:                case IO_MPOOL:  /* mpool */
                    133:                        io_mpool = mpool_init(maxmem);
                    134:                        if (io_mpool) {
                    135:                                io_malloc = mpool_xmalloc;
                    136:                                io_calloc = mpool_xcalloc;
                    137:                                io_realloc = mpool_xrealloc;
                    138:                                io_strdup = mpool_xstrdup;
                    139:                                io_free = mpool_xfree;
                    140:                                use_mm = mm;
                    141:                                break;
                    142:                        } else {
                    143:                                #undef USE_MPOOL
                    144:                        }
                    145:                case IO_SYSM:   /* system */
                    146:                        io_malloc = malloc;
                    147:                        io_calloc = calloc;
                    148:                        io_realloc = realloc;
                    149:                        io_strdup = strdup;
                    150:                        io_free = free;
                    151:                        use_mm = mm;
                    152:                        break;
                    153:                default:        /* not supported */
                    154:                        io_SetErr(EINVAL, "Not supported memory management");
                    155:                        return -1;
                    156:        }
                    157: 
                    158:        return use_mm;
                    159: }
                    160: 
                    161: /*
                    162:  * ioLibFini() - Finish libaitio library memory management
                    163:  *
                    164:  * return: none
                    165:  */
                    166: inline void
                    167: ioLibFini()
                    168: {
                    169:        switch (use_mm) {
1.10.6.5  misho     170:                case IO_MPOOL:
1.10.6.4  misho     171:                        mpool_destroy(&io_mpool);
                    172: 
                    173:                        io_malloc = malloc;
                    174:                        io_calloc = calloc;
                    175:                        io_realloc = realloc;
                    176:                        io_strdup = strdup;
                    177:                        io_free = free;
                    178:                        use_mm = IO_SYSM;
                    179:                        break;
                    180:        }
1.10.6.3  misho     181: }
                    182: 
                    183: 
1.1       misho     184: /*
1.10      misho     185:  * ioPromptRead() - Read data from input h[0] with prompt to output h[1]
1.9       misho     186:  *
1.1       misho     187:  * @h = file handles h[0] = input, h[1] = output, if NULL use stdin, stdout
                    188:  * @csPrompt = Prompt before input, may be NULL
                    189:  * @psData = Readed data
                    190:  * @dataLen = Length of data
                    191:  * return: 0 EOF; -1 error:: can`t read; >0 count of readed chars
                    192: */
1.6       misho     193: int
                    194: ioPromptRead(int *h, const char *csPrompt, char * __restrict psData, int dataLen)
1.1       misho     195: {
                    196:        int ok = 0;
                    197:        FILE *inp, *out;
                    198:        char szLine[BUFSIZ], *pos;
                    199: 
                    200:        if (!psData || !dataLen)
                    201:                return -1;
                    202: 
                    203:        inp = fdopen(!h ? 0 : h[0], "r");
                    204:        if (!inp) {
                    205:                LOGERR;
                    206:                return -1;
                    207:        }
                    208:        out = fdopen(!h ? 1 : h[1], "w");
                    209:        if (!out) {
                    210:                LOGERR;
                    211:                return -1;
                    212:        }
                    213: 
                    214:        while (!ok) {
                    215:                if (csPrompt) {
                    216:                        fprintf(out, "%s", csPrompt);
                    217:                        fflush(out);
                    218:                }
                    219: 
                    220:                memset(szLine, 0, BUFSIZ);
                    221:                if (!fgets(szLine, BUFSIZ, inp)) {
                    222:                        clearerr(inp);
                    223:                        fpurge(out);
                    224:                        fflush(out);
                    225:                        return 0;
                    226:                }
                    227: 
                    228:                if ((pos = strchr(szLine, '\n')))
                    229:                        *pos = 0;
                    230: 
                    231:                strlcpy(psData, szLine, dataLen);
                    232:                ok = 1;
                    233:        }
                    234: 
                    235:        return pos - szLine;
                    236: }
                    237: 
                    238: /*
1.10      misho     239:  * ioPromptPassword() - Read password from input h[0] with prompt to output h[1]
1.9       misho     240:  *
1.1       misho     241:  * @h = file handles h[0] = input, h[1] = output, if NULL use stdin, stdout
                    242:  * @csPrompt = Prompt before input, may be NULL
                    243:  * @psPass = Readed password
                    244:  * @passLen = Length of password
                    245:  * @confirm = Confirm password, 0 - get password, !=0 Ask for confirmation
                    246:  * return: 0 EOF; -1 error:: can`t read; >0 count of readed chars
                    247: */
1.6       misho     248: int
                    249: ioPromptPassword(int *h, const char *csPrompt, char * __restrict psPass, int passLen, int confirm)
1.1       misho     250: {
                    251:        int ret, ok = 0;
                    252:        FILE *inp, *out;
                    253:        char szLine[2][STRSIZ];
                    254:        struct sgttyb tty_state;
                    255: 
                    256:        if (!psPass || !passLen)
                    257:                return -1;
                    258: 
                    259:        inp = fdopen(!h ? 0 : h[0], "r");
                    260:        if (!inp) {
                    261:                LOGERR;
                    262:                return -1;
                    263:        }
                    264:        out = fdopen(!h ? 1 : h[1], "w");
                    265:        if (!out) {
                    266:                LOGERR;
                    267:                return -1;
                    268:        }
                    269: 
                    270:        if (ioctl(fileno(inp), TIOCGETP, &tty_state) == -1) {
                    271:                LOGERR;
                    272:                return -1;
                    273:        } else {
                    274:                tty_state.sg_flags &= ~ECHO;
                    275:                if (ioctl(fileno(inp), TIOCSETP, &tty_state) == -1) {
                    276:                        LOGERR;
                    277:                        return -1;
                    278:                }
                    279:        }
                    280: 
                    281:        while (!ok) {
                    282:                switch ((ret = ioPromptRead(h, (!csPrompt || !*csPrompt) ? "Password:" : csPrompt, 
                    283:                                                szLine[0], STRSIZ))) {
                    284:                        case -1:
                    285:                                LOGERR;
                    286:                                ok = -1;
                    287:                        case 0:
                    288:                                goto next;
                    289:                }
                    290:                if (confirm) {
                    291:                        fprintf(out, "\n");
                    292:                        fflush(out);
                    293: 
                    294:                        switch (ioPromptRead(h, "Password confirm:", szLine[1], STRSIZ)) {
                    295:                                case -1:
                    296:                                        LOGERR;
                    297:                                        ok = -1;
                    298:                                        goto next;
                    299:                                case 0:
                    300:                                default:
                    301:                                        if (strcmp(szLine[0], szLine[1])) {
                    302:                                                fprintf(out, "\n\07\07Mismatch - Try again!\n");
                    303:                                                fflush(out);
                    304:                                                continue;
                    305:                                        }
                    306:                        }
                    307:                }
                    308: 
                    309:                strlcpy(psPass, szLine[0], passLen);
                    310:                ok = ret;
                    311:                fprintf(out, "\n");
                    312:                fflush(out);
                    313:        }
                    314: 
                    315: next:
                    316:        tty_state.sg_flags |= ECHO;
                    317:        if (ioctl(fileno(inp), TIOCSETP, &tty_state) == -1) {
                    318:                LOGERR;
                    319:                return -1;
                    320:        }
                    321: 
                    322:        return ok;
                    323: }
                    324: 
                    325: /*
1.10      misho     326:  * ioRegexVerify() - Function for verify data match in regex expression
1.9       misho     327:  *
1.1       misho     328:  * @csRegex = Regulare expression pattern
                    329:  * @csData = Data for check and verify
                    330:  * @startPos = Return start positions
                    331:  * @endPos = Return end positions
                    332:  * return: NULL not match or error; !=NULL begin of matched data
                    333: */
1.6       misho     334: const char *
                    335: ioRegexVerify(const char *csRegex, const char *csData, int *startPos, int *endPos)
1.1       misho     336: {
                    337:        regex_t re;
                    338:        regmatch_t match;
                    339:        char szErr[STRSIZ];
                    340:        int ret, flg;
                    341:        const char *pos;
                    342: 
                    343:        if (!csRegex || !csData)
                    344:                return NULL;
                    345: 
                    346:        if ((ret = regcomp(&re, csRegex, REG_EXTENDED))) {
                    347:                regerror(ret, &re, szErr, STRSIZ);
1.10      misho     348:                io_SetErr(ret, "%s", szErr);
1.1       misho     349:                regfree(&re);
                    350:                return NULL;
                    351:        }
                    352: 
                    353:        for (ret = flg = 0, pos = csData; !(ret = regexec(&re, pos, 1, &match, flg)); 
                    354:                        pos += match.rm_eo, flg = REG_NOTBOL) {
                    355:                if (startPos)
                    356:                        *startPos = match.rm_so;
                    357:                if (endPos)
                    358:                        *endPos = match.rm_eo;
                    359: 
                    360:                pos += match.rm_so;
                    361:                break;
                    362:        }
                    363: 
                    364:        if (ret) {
                    365:                regerror(ret, &re, szErr, STRSIZ);
1.10      misho     366:                io_SetErr(ret, "%s", szErr);
1.1       misho     367:                pos = NULL;
                    368:        }
                    369: 
                    370:        regfree(&re);
                    371:        return pos;
                    372: }
                    373: 
                    374: /*
1.10      misho     375:  * ioRegexGet() - Function for get data match in regex expression
1.9       misho     376:  *
1.1       misho     377:  * @csRegex = Regulare expression pattern
                    378:  * @csData = Data from get
                    379:  * @psString = Returned string if match
                    380:  * @strLen = Length of string
                    381:  * return: 0 not match; >0 count of returned chars
                    382: */
1.6       misho     383: int
                    384: ioRegexGet(const char *csRegex, const char *csData, char * __restrict psString, int strLen)
1.1       misho     385: {
                    386:        int sp, ep, len;
                    387:        const char *str;
                    388: 
                    389:        if (!csRegex || !csData)
                    390:                return -1;
                    391: 
                    392:        str = ioRegexVerify(csRegex, csData, &sp, &ep);
                    393:        if (!str)
                    394:                return 0;
                    395: 
                    396:        len = ep - sp;
                    397:        if (psString && strLen) {
                    398:                memset(psString, 0, strLen);
                    399:                strncpy(psString, str, strLen <= len ? strLen - 1 : len);
                    400:        }
                    401: 
                    402:        return len;
                    403: }
                    404: 
                    405: /*
1.10      misho     406:  * ioRegexReplace() - Function for replace data match in regex expression with newdata
1.9       misho     407:  *
1.1       misho     408:  * @csRegex = Regulare expression pattern
                    409:  * @csData = Source data
                    410:  * @csNew = Data for replace
1.10.6.5  misho     411:  * return: NULL not match or error; !=NULL allocated new string, must be io_free after use!
1.1       misho     412: */
1.6       misho     413: char *
                    414: ioRegexReplace(const char *csRegex, const char *csData, const char *csNew)
1.1       misho     415: {
                    416:        int sp, ep, len;
                    417:        char *str = NULL;
                    418: 
                    419:        if (!csRegex || !csData)
                    420:                return NULL;
                    421: 
                    422:        if (!ioRegexVerify(csRegex, csData, &sp, &ep))
                    423:                return NULL;
                    424: 
                    425:        // ___ before match
                    426:        len = sp + 1;
1.10.6.5  misho     427:        str = io_malloc(len);
1.1       misho     428:        if (!str) {
                    429:                LOGERR;
                    430:                return NULL;
                    431:        } else
                    432:                strlcpy(str, csData, len);
                    433:        // * replace match *
                    434:        if (csNew) {
                    435:                len += strlen(csNew);
1.10.6.5  misho     436:                str = io_realloc(str, len);
1.1       misho     437:                if (!str) {
                    438:                        LOGERR;
                    439:                        return NULL;
                    440:                } else
                    441:                        strlcat(str, csNew, len);
                    442:        }
                    443:        // after match ___
                    444:        len += strlen(csData) - ep;
1.10.6.5  misho     445:        str = io_realloc(str, len);
1.1       misho     446:        if (!str) {
                    447:                LOGERR;
                    448:                return NULL;
                    449:        } else
                    450:                strlcat(str, csData + ep, len);
                    451: 
                    452:        return str;
                    453: }
1.2       misho     454: 
1.3       misho     455: /*
1.10      misho     456:  * ioStrAst() - Function for evaluate string like asterisk variable "{text[:[-]#[:#]]}"
1.9       misho     457:  *
1.3       misho     458:  * @csString = Input string
1.10.6.5  misho     459:  * return: NULL error, !=NULL Allocated new string evaluated from input string, must be io_free()
1.3       misho     460: */
                    461: char *
1.8       misho     462: ioStrAst(const char *csString)
1.3       misho     463: {
                    464:        char *ext, *str, *out = NULL;
                    465:        int e[2] = { 0 };
                    466: 
                    467:        if (!csString)
                    468:                return NULL;
                    469: 
                    470:        if (!strchr(csString, '{') || !strrchr(csString, '}')) {
                    471:                memset(io_Error, 0, STRSIZ);
1.10      misho     472:                snprintf(io_Error, STRSIZ, "Invalid input string format ... "
1.3       misho     473:                                "must be like {text[:[-]#[:#]]}");
                    474:                io_Errno = EINVAL;
                    475:                return NULL;
                    476:        } else {
                    477:                str = strdup(strchr(csString, '{') + 1);
                    478:                *strrchr(str, '}') = 0;
                    479:        }
                    480: 
                    481:        if ((ext = strchr(str, ':'))) {
                    482:                *ext++ = 0;
                    483:                e[0] = strtol(ext, NULL, 0);
                    484:                if ((ext = strchr(ext, ':')))
                    485:                        e[1] = strtol(++ext, NULL, 0);
                    486: 
                    487:                /* make cut prefix */
                    488:                if (e[0] >= 0)
                    489:                        ext = str + e[0];
                    490:                else
                    491:                        ext = str + strlen(str) + e[0];
                    492:                /* make cut suffix */
                    493:                if (e[1] > 0)
                    494:                        *(ext + e[1]) = 0;
                    495:        } else
                    496:                /* ok, clear show */
                    497:                ext = str;
                    498: 
                    499:        out = strdup(ext);
1.10.6.5  misho     500:        io_free(str);
1.3       misho     501: 
                    502:        return out;
                    503: }
                    504: 
1.2       misho     505: 
                    506: /*
1.10      misho     507:  * ioMkDir() - Function for racursive directory creation and validation
1.9       misho     508:  *
1.2       misho     509:  * @csDir = Full directory path
                    510:  * @mode = Mode for directory creation if missing dir
                    511:  * return: -1 error, 0 directory path exist, >0 created missing dirs
                    512: */
                    513: int
                    514: ioMkDir(const char *csDir, int mode)
                    515: {
                    516:        char *str, *s, *pbrk, szOld[MAXPATHLEN] = { 0 };
                    517:        register int cx = -1;
                    518: 
                    519:        if (!csDir)
                    520:                return cx;
                    521: 
                    522:        str = strdup(csDir);
                    523:        if (!str) {
                    524:                LOGERR;
                    525:                return cx;
                    526:        }
                    527: 
                    528:        getcwd(szOld, MAXPATHLEN);
                    529:        if (*str == '/')
                    530:                chdir("/");
                    531: 
                    532:        for (cx = 0, s = strtok_r(str, "/", &pbrk); s; s = strtok_r(NULL, "/", &pbrk)) {
                    533:                if (mkdir(s, mode) == -1) {
                    534:                        if (errno != EEXIST) {
                    535:                                LOGERR;
                    536:                                cx = -1;
                    537:                                goto end;
                    538:                        }
                    539:                } else
                    540:                        cx++;
                    541: 
                    542:                if (chdir(s) == -1) {
                    543:                        LOGERR;
                    544:                        cx = -1;
                    545:                        goto end;
                    546:                }
                    547:        }
                    548: end:
                    549:        chdir(szOld);
1.10.6.5  misho     550:        io_free(str);
1.2       misho     551:        return cx;
                    552: }
                    553: 
1.3       misho     554: /*
1.10      misho     555:  * ioWatchDirLoop() - Function for watching changes in directory and fire callback
1.9       misho     556:  *
1.3       misho     557:  * @csDir = Full directory path
                    558:  * @callback = Callback if raise event! nOp -1 delete, 0 change/move, 1 create
                    559:  * return: -1 error, !=-1 ok, number of total signaled events
                    560: */
                    561: int
                    562: ioWatchDirLoop(const char *csDir, int (*callback)(const char *csName, int nOp))
                    563: {
                    564:        glob_t g[2] = {{ 0 }, { 0 }};
                    565:        int d, kq, n = 0;
                    566:        register int j, i;
                    567:        struct kevent req, chg;
                    568:        char wrk[MAXPATHLEN * 2], str[MAXPATHLEN] = { 0 };
                    569: 
                    570:        if (!csDir || !callback)
                    571:                return 0;
                    572: 
                    573:        strlcpy(str, csDir, MAXPATHLEN);
                    574:        strlcat(str, "/*", MAXPATHLEN);
                    575: 
                    576:        kq = kqueue();
                    577:        if (kq == -1) {
                    578:                LOGERR;
                    579:                return -1;
                    580:        }
                    581:        d = open(csDir, O_RDONLY);
                    582:        if (d == -1) {
                    583:                LOGERR;
                    584:                close(kq);
                    585:                return -1;
                    586:        }
                    587: 
1.4       misho     588:        EV_SET(&req, d, EVFILT_VNODE, EV_ADD | EV_CLEAR, NOTE_WRITE, 0, 0);
1.3       misho     589: 
                    590:        if ((n = glob(str, GLOB_NOCHECK, NULL, &g[0]))) {
                    591:                LOGERR;
                    592:                close(d);
                    593:                close(kq);
                    594:                return -1;
                    595:        } /*else
                    596:                ioDEBUG(3, "Start files %d in %s\n", g[0].gl_matchc, str);*/
                    597: 
                    598:        while (kevent(kq, &req, 1, &chg, 1, NULL) > 0) {
                    599:                /*ioDEBUG(1, "Event:: req=0x%x -> chg=0x%x data=%x\n", req.fflags, chg.fflags, chg.data);*/
                    600: 
                    601:                if (!glob(str, GLOB_NOCHECK, NULL, &g[1])) {
                    602:                        /*ioDEBUG(3, "Diffs %d <> %d\n", g[0].gl_matchc, g[1].gl_matchc);*/
                    603: 
                    604:                        if (g[0].gl_matchc != g[1].gl_matchc) {
                    605:                                /* find new items */
                    606:                                for (j = 0; j < g[1].gl_matchc; j++) {
                    607:                                        for (i = 0; i < g[0].gl_matchc; i++)
                    608:                                                if (!strcmp(g[0].gl_pathv[i], g[1].gl_pathv[j]))
                    609:                                                        break;
                    610:                                        if (i == g[0].gl_matchc) {
                    611:                                                if (callback(g[1].gl_pathv[j], 1) < 0)
                    612:                                                        break;
                    613:                                                else
                    614:                                                        n++;
                    615:                                        }
                    616:                                }
                    617:                                /* find del items */
                    618:                                for (j = 0; j < g[0].gl_matchc; j++) {
                    619:                                        for (i = 0; i < g[1].gl_matchc; i++)
                    620:                                                if (!strcmp(g[1].gl_pathv[i], g[0].gl_pathv[j]))
                    621:                                                        break;
                    622:                                        if (i == g[1].gl_matchc) {
                    623:                                                if (callback(g[0].gl_pathv[j], -1) < 0)
                    624:                                                        break;
                    625:                                                else
                    626:                                                        n++;
                    627:                                        }
                    628:                                }
                    629:                        } else {
                    630:                                /* find chg from items */
                    631:                                for (j = 0; j < g[0].gl_matchc; j++) {
                    632:                                        for (i = 0; i < g[1].gl_matchc; i++)
                    633:                                                if (!strcmp(g[1].gl_pathv[i], g[0].gl_pathv[j]))
                    634:                                                        break;
                    635:                                        if (i == g[1].gl_matchc) {
                    636:                                                strlcpy(wrk, g[0].gl_pathv[j], sizeof wrk);
                    637:                                                strlcat(wrk, ":", sizeof wrk);
                    638:                                        }
                    639:                                }
                    640:                                /* find chg to items */
                    641:                                for (j = 0; j < g[1].gl_matchc; j++) {
                    642:                                        for (i = 0; i < g[0].gl_matchc; i++)
                    643:                                                if (!strcmp(g[0].gl_pathv[i], g[1].gl_pathv[j]))
                    644:                                                        break;
                    645:                                        if (i == g[0].gl_matchc) {
                    646:                                                strlcat(wrk, g[1].gl_pathv[j], sizeof wrk);
                    647:                                                if (callback(wrk, 0) < 0)
                    648:                                                        break;
                    649:                                                else
                    650:                                                        n++;
                    651:                                        }
                    652:                                }
                    653:                        }
                    654: 
                    655:                        globfree(&g[0]);
                    656:                        g[0] = g[1];
                    657:                }
                    658:        }
                    659: 
                    660:        globfree(&g[0]);
                    661:        close(d);
                    662:        close(kq);
                    663:        return n;
                    664: }
1.7       misho     665: 
                    666: /*
1.10      misho     667:  * ioCreatePIDFile() - Create PID file
1.9       misho     668:  *
1.7       misho     669:  * @csName = PID filename
                    670:  * @ifExists = !=0 if filename exists return error
                    671:  * return: -1 error or 0 ok
                    672:  */
                    673: inline int
                    674: ioCreatePIDFile(const char *csName, int ifExists)
                    675: {
                    676:        int fd;
                    677:        char str[STRSIZ] = { 0 };
                    678: 
                    679:        if (!csName)
                    680:                return -1;
                    681: 
                    682:        fd = open(csName, O_WRONLY | O_CREAT | (ifExists ? O_EXCL : 0), 0644);
                    683:        if (fd == -1) {
                    684:                LOGERR;
                    685:                return -1;
                    686:        }
                    687:        snprintf(str, sizeof str, "%d", getpid());
                    688:        write(fd, str, strlen(str));
                    689:        close(fd);
                    690:        return 0;
                    691: }
1.8       misho     692: 
                    693: 
                    694: /*
1.10      misho     695:  * ioSendFile() - AITNET sendfile() userland implementation, not dependant from OS
1.9       misho     696:  *
1.8       misho     697:  * @s = socket
                    698:  * @csFile = file for send
                    699:  * @sendLen = bytes to send, if 0 send all data
                    700:  * @offset = start file offset
                    701:  * @sndbuf = SO_SNDBUF value, if 0 use default
                    702:  * return: 0 error, >0 ok, sended bytes
                    703:  */
                    704: size_t
                    705: ioSendFile(int s, const char *csFile, size_t sendLen, off_t offset, int sndbuf)
                    706: {
                    707:        void *addr;
                    708:        int fd;
                    709:        size_t len = 0;
                    710:        register size_t off = 0;
                    711: 
                    712:        if (!csFile)
                    713:                return 0;
                    714: 
                    715:        if (sndbuf)
                    716:                if (setsockopt(s, SOL_SOCKET, SO_SNDBUF, &sndbuf, sizeof sndbuf) == -1) {
                    717:                        LOGERR;
                    718:                        return 0;
                    719:                }
                    720: 
                    721:        fd = open(csFile, O_RDONLY);
                    722:        if (fd == -1) {
                    723:                LOGERR;
                    724:                return 0;
                    725:        }
                    726:        if (!sendLen) {
                    727:                sendLen = lseek(fd, 0, SEEK_END);
                    728:                if (sendLen == -1) {
                    729:                        LOGERR;
                    730:                        close(fd);
                    731:                        return 0;
                    732:                }
                    733:        }
                    734:        addr = mmap(NULL, sendLen, PROT_READ, MAP_SHARED, fd, offset);
                    735:        if (addr == MAP_FAILED) {
                    736:                LOGERR;
                    737:                close(fd);
                    738:                return 0;
                    739:        } else
                    740:                close(fd);
                    741: 
                    742:        while (off < sendLen && (len = write(s, addr + off, sendLen - off)) != -1)
                    743:                off += len;
                    744:        if (len == -1) {
                    745:                LOGERR;
                    746:                munmap(addr, sendLen);
                    747:                return 0;
                    748:        } else
                    749:                len = off;
                    750: 
                    751:        if (len != sendLen) {
                    752:                io_SetErr(ECANCELED, "Different sizes - request %u bytes, actually sended %u bytes\n", 
                    753:                                sendLen, len);
                    754:                len ^= len;
                    755:        }
                    756: 
                    757:        munmap(addr, sendLen);
                    758:        return len;
                    759: }
                    760: 
                    761: /*
1.10      misho     762:  * ioRecvFile() - Receive file from socket, fastest (zero-copy) way
1.9       misho     763:  *
1.8       misho     764:  * @s = socket
                    765:  * @csFile = file for receive
                    766:  * @recvLen = receive bytes
                    767:  * @over = overwrite file if exists with mode like 0644
                    768:  * @rcvbuf = SO_RCVBUF value, if 0 use default
                    769:  * return: 0 error, >0 ok, received bytes
                    770:  */
                    771: size_t
                    772: ioRecvFile(int s, const char *csFile, size_t recvLen, int over, int rcvbuf)
                    773: {
                    774:        void *addr;
                    775:        int fd;
                    776:        size_t len = 0;
                    777:        register size_t off = 0;
                    778:        struct pollfd pfd = { s, POLLIN | POLLPRI, 0 };
                    779: 
                    780:        if (!csFile || !recvLen)
                    781:                return 0;
                    782:        if (!over && !access(csFile, F_OK))
                    783:                return 0;
                    784: 
                    785:        if (rcvbuf)
                    786:                if (setsockopt(s, SOL_SOCKET, SO_RCVBUF, &rcvbuf, sizeof rcvbuf) == -1) {
                    787:                        LOGERR;
                    788:                        return 0;
                    789:                }
                    790: 
                    791:        fd = open(csFile, O_WRONLY | O_CREAT | O_TRUNC, over);
                    792:        if (fd == -1) {
                    793:                LOGERR;
                    794:                unlink(csFile);
                    795:                return 0;
                    796:        }
1.10      misho     797:        if (ftruncate(fd, recvLen) == -1) {
1.8       misho     798:                LOGERR;
                    799:                close(fd);
                    800:                unlink(csFile);
                    801:                return 0;
                    802:        }
                    803:        addr = mmap(NULL, recvLen, PROT_WRITE, MAP_SHARED, fd, 0);
                    804:        if (addr == MAP_FAILED) {
                    805:                LOGERR;
                    806:                close(fd);
                    807:                unlink(csFile);
                    808:                return 0;
                    809:        } else
                    810:                close(fd);
                    811: 
                    812:        while (off < recvLen && poll(&pfd, 1, RECV_TIMEOUT) != -1)
                    813:                while (off < recvLen && (len = read(s, addr + off, recvLen - off)) != -1)
                    814:                        off += len;
                    815:        if (len == -1) {
                    816:                LOGERR;
                    817:                munmap(addr, recvLen);
                    818:                unlink(csFile);
                    819:                return 0;
                    820:        } else
                    821:                len = off;
                    822: 
                    823:        if (len != recvLen)
                    824:                io_SetErr(EAGAIN, "Different sizes - request %u bytes, actually received %u bytes\n", 
                    825:                                recvLen, len);
                    826: 
                    827:        munmap(addr, recvLen);
                    828:        return len;
                    829: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>