Annotation of embedaddon/strongswan/src/libstrongswan/plugins/files/files_fetcher.c, revision 1.1

1.1     ! misho       1: /*
        !             2:  * Copyright (C) 2015 Tobias Brunner
        !             3:  * HSR Hochschule fuer Technik Rapperswil
        !             4:  *
        !             5:  * This program is free software; you can redistribute it and/or modify it
        !             6:  * under the terms of the GNU General Public License as published by the
        !             7:  * Free Software Foundation; either version 2 of the License, or (at your
        !             8:  * option) any later version.  See <http://www.fsf.org/copyleft/gpl.txt>.
        !             9:  *
        !            10:  * This program is distributed in the hope that it will be useful, but
        !            11:  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
        !            12:  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
        !            13:  * for more details.
        !            14:  */
        !            15: 
        !            16: #include <errno.h>
        !            17: 
        !            18: #include <library.h>
        !            19: #include <utils/debug.h>
        !            20: 
        !            21: #include "files_fetcher.h"
        !            22: 
        !            23: typedef struct private_files_fetcher_t private_files_fetcher_t;
        !            24: 
        !            25: /**
        !            26:  * private data of a files_fetcher_t object.
        !            27:  */
        !            28: struct private_files_fetcher_t {
        !            29: 
        !            30:        /**
        !            31:         * Public data
        !            32:         */
        !            33:        files_fetcher_t public;
        !            34: 
        !            35:        /**
        !            36:         * Callback function
        !            37:         */
        !            38:        fetcher_callback_t cb;
        !            39: };
        !            40: 
        !            41: METHOD(fetcher_t, fetch, status_t,
        !            42:        private_files_fetcher_t *this, char *uri, void *userdata)
        !            43: {
        !            44:        chunk_t *data;
        !            45:        status_t status = FAILED;
        !            46: 
        !            47:        if (this->cb == fetcher_default_callback)
        !            48:        {
        !            49:                *(chunk_t*)userdata = chunk_empty;
        !            50:        }
        !            51:        if (!strpfx(uri, "file://"))
        !            52:        {
        !            53:                return NOT_SUPPORTED;
        !            54:        }
        !            55:        uri = uri + strlen("file://");
        !            56:        data = chunk_map(uri, FALSE);
        !            57:        if (!data)
        !            58:        {
        !            59:                DBG1(DBG_LIB, "  opening '%s' failed: %s", uri, strerror(errno));
        !            60:                return FAILED;
        !            61:        }
        !            62:        if (this->cb(userdata, *data))
        !            63:        {
        !            64:                status = SUCCESS;
        !            65:        }
        !            66:        chunk_unmap(data);
        !            67:        return status;
        !            68: }
        !            69: 
        !            70: METHOD(fetcher_t, set_option, bool,
        !            71:        private_files_fetcher_t *this, fetcher_option_t option, ...)
        !            72: {
        !            73:        bool supported = TRUE;
        !            74:        va_list args;
        !            75: 
        !            76:        va_start(args, option);
        !            77:        switch (option)
        !            78:        {
        !            79:                case FETCH_CALLBACK:
        !            80:                {
        !            81:                        this->cb = va_arg(args, fetcher_callback_t);
        !            82:                        break;
        !            83:                }
        !            84:                default:
        !            85:                        supported = FALSE;
        !            86:                        break;
        !            87:        }
        !            88:        va_end(args);
        !            89:        return supported;
        !            90: }
        !            91: 
        !            92: METHOD(fetcher_t, destroy, void,
        !            93:        private_files_fetcher_t *this)
        !            94: {
        !            95:        free(this);
        !            96: }
        !            97: 
        !            98: /*
        !            99:  * Described in header.
        !           100:  */
        !           101: files_fetcher_t *files_fetcher_create()
        !           102: {
        !           103:        private_files_fetcher_t *this;
        !           104: 
        !           105:        INIT(this,
        !           106:                .public = {
        !           107:                        .interface = {
        !           108:                                .fetch = _fetch,
        !           109:                                .set_option = _set_option,
        !           110:                                .destroy = _destroy,
        !           111:                        },
        !           112:                },
        !           113:                .cb = fetcher_default_callback,
        !           114:        );
        !           115: 
        !           116:        return &this->public;
        !           117: }

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