Ejemplo n.º 1
0
/* Tokenize a given sds, setting a term to zero-length sds if it's
 * a stopword. A total number of tokens and total number of nonstopwords
 * will be returned */
static sds *sds_tokenize(sds s, int *len, int *nonstopwords) {
    int i, l, k;
    sds *terms;
    struct stemmer *stmer;

    *nonstopwords = 0;
    terms = sdssplitlen(s, sdslen(s), " ", 1, len);
    if (!terms) return NULL;
    stmer = create_stemmer();
    for (i = 0; i < *len; i++) {
        sds stemmed = NULL, term = terms[i];
        term = sdstrim(term, puncs);
        l = sdslen(term);
        sdstolower(term);
        if (l == 0 || rr_stopwords_check(term)) {
            sdsclear(term);
            continue;
        }
        *nonstopwords += 1;
        /* note that the third argument is a zero-based index */
        k = stem(stmer, term, l-1);
        if (k < l-1) {
            stemmed = sdsnewlen(term, k+1);
            sdsfree(term);
            terms[i] = stemmed;
        }
    }

    free_stemmer(stmer);
    return terms;
}
Ejemplo n.º 2
0
/**
 * Takes partial tags and replaces them with a parsed partial template.
 * 
 * @param sds tag  The tag 
 *
 * @retval sds buff  Partial template buffer.
 */
static sds tag_partial(sds tag) 
{
	puts("tag_partial");
	printf("tag found: '%s'\n",tag);
	sds re = sdsempty();
	sds f = sdsempty();
	sds buff = sdsempty();

	re = sdscatprintf(re, "%s>([^}]+)%s", ldelim, rdelim);
	struct slre_cap caps[1];

	if (slre_match(re, tag, strlen(tag), caps, 1) > 0) {
		f = sdscatprintf(f,"%.*s", caps[0].len, caps[0].ptr);
		sdstrim(f," ");
		sds fname = sdscatprintf(sdsempty(),"%s%s", tpldir, f);
		fname = sdscat(fname,tplext);
		//puts(fname);

		FILE * fp;
		if ((fp = fopen(fname, "r")) != NULL) {
			fclose(fp);
			buff = render_template(f);
		}
		puts("free fname");
		sdsfree(fname);
		
	} 
	puts("free f");
	sdsfree(f);
//	//sdsfree(buff);
	puts("free re");
	sdsfree(re);
	return buff;
}
Ejemplo n.º 3
0
/* Given the filename, return the absolute path as an SDS string, or NULL
 * if it fails for some reason. Note that "filename" may be an absolute path
 * already, this will be detected and handled correctly.
 *
 * The function does not try to normalize everything, but only the obvious
 * case of one or more "../" appearning at the start of "filename"
 * relative path. */
sds getAbsolutePath ( char *filename )
{
    char cwd[1024];
    sds abspath;
    sds relpath = sdsnew (filename);

    relpath = sdstrim (relpath, " \r\n\t");
    if ( relpath[0] == '/' ) return relpath; /* Path is already absolute. */

    /* If path is relative, join cwd and relative path. */
    if ( getcwd (cwd, sizeof (cwd )) == NULL )
    {
        sdsfree (relpath);
        return NULL;
    }
    abspath = sdsnew (cwd);
    if ( sdslen (abspath) && abspath[sdslen (abspath) - 1] != '/' )
        abspath = sdscat (abspath, "/");

    /* At this point we have the current path always ending with "/", and
     * the trimmed relative path. Try to normalize the obvious case of
     * trailing ../ elements at the start of the path.
     *
     * For every "../" we find in the filename, we remove it and also remove
     * the last element of the cwd, unless the current cwd is "/". */
    while ( sdslen (relpath) >= 3 &&
            relpath[0] == '.' && relpath[1] == '.' && relpath[2] == '/' )
    {
        sdsrange (relpath, 3, - 1);
        if ( sdslen (abspath) > 1 )
        {
            char *p = abspath + sdslen (abspath) - 2;
            int trimlen = 1;

            while ( *p != '/' )
            {
                p --;
                trimlen ++;
            }
            sdsrange (abspath, 0, - ( trimlen + 1 ));
        }
    }

    /* Finally glue the two parts together. */
    abspath = sdscatsds (abspath, relpath);
    sdsfree (relpath);
    return abspath;
}
Ejemplo n.º 4
0
int main(){

    char hello[20]={"hello,world!"};
    printf("hello:%s\n",hello);
    
    printf("str new\n");
    sds str=sdsnew(hello);
    printf("str:%s\n",str);
    printf("str len:%d\n",sdslen(str));
    printf("str avail:%d\n",sdsavail(str));

    printf("str cat\n");
    sds newstr=sdscat(str,"this is strcat content");
    printf("str:%s\n",str);
    printf("str:%s\n",newstr);
    printf("str len:%d\n",sdslen(newstr));
    printf("str avail:%d\n",sdsavail(newstr));

    printf("str cpy\n");
    char* teststr="test a string";
    sdscpy(newstr,teststr);
    printf("str:%s\n",newstr);
    printf("str len:%d\n",sdslen(newstr));
    printf("str avail:%d\n",sdsavail(newstr));


    printf("trim char \n");
    sdstrim(newstr,"g");
    printf("str:%s\n",newstr);
    
    printf("sds range  \n");
    sdsrange(newstr,1,3);
    printf("str:%s\n",newstr);
    
    printf("sds toupper  \n");
    sdstoupper(newstr);
    printf("str:%s\n",newstr);
    
    printf("sds tolower  \n");
    sdstolower(newstr);
    printf("str:%s\n",newstr);
    
    printf("sds cmp  \n");
    sds cmpstr=sdsnew("est");
    printf("str:%d\n",sdscmp(newstr,cmpstr));
    return 0;
}
Ejemplo n.º 5
0
static sds cliReadLine(int fd) {
    sds line = sdsempty();

    while(1) {
        char c;

        if (read(fd,&c,1) == -1) {
            sdsfree(line);
            return NULL;
        } else if (c == '\n') {
            break;
        } else {
            line = sdscatlen(line,&c,1);
        }
    }
    return sdstrim(line,"\r\n");
}
Ejemplo n.º 6
0
static sds cliReadLine(int fd) {
    sds line = sdsempty();

    while(1) {
        char c;
        ssize_t ret;

        ret = read(fd,&c,1);
        if (ret <= 0) {
            sdsfree(line);
            return NULL;
        } else if ((ret == 0) || (c == '\n')) {
            break;
        } else {
            line = sdscatlen(line,&c,1);
        }
    }
    return sdstrim(line,"\r\n");
}
Ejemplo n.º 7
0
Archivo: util.c Proyecto: bugou/test
sds getAbsolutePath(char *filename) {
    char fullPath[MAX_PATH];
    DWORD gfpnResult;
    sds abspath;
    sds relpath = sdsnew(filename);

    relpath = sdstrim(relpath," \r\n\t");

    if (!PathIsRelative(relpath)) return relpath;

    gfpnResult = GetFullPathNameA(relpath, sizeof(fullPath), fullPath, NULL);
    sdsfree(relpath);

    if (gfpnResult == 0 || gfpnResult > sizeof(fullPath)) {
        return NULL;
    }
    abspath = sdsnew(fullPath);
    return abspath;
}
Ejemplo n.º 8
0
TEST(sds, sds)
{
    // struct sdshdr *sh;
    sds x = sdsnew("foo"), y;

    TEST_ASSERT_TRUE(
        sdslen(x) == 3 && memcmp(x,"foo\0",4) == 0)

    sdsfree(x);
    x = sdsnewlen("foo",2);
    TEST_ASSERT_TRUE(
        sdslen(x) == 2 && memcmp(x,"fo\0",3) == 0)

    x = sdscat(x,"bar");
    TEST_ASSERT_TRUE(
        sdslen(x) == 5 && memcmp(x,"fobar\0",6) == 0);

    x = sdscpy(x,"a");
    TEST_ASSERT_TRUE(
        sdslen(x) == 1 && memcmp(x,"a\0",2) == 0)

    x = sdscpy(x,"xyzxxxxxxxxxxyyyyyyyyyykkkkkkkkkk");
    TEST_ASSERT_TRUE(
        sdslen(x) == 33 &&
        memcmp(x,"xyzxxxxxxxxxxyyyyyyyyyykkkkkkkkkk\0",33) == 0)

    sdsfree(x);

    x = sdscatprintf(sdsempty(),"%d",123);
    TEST_ASSERT_TRUE(
        sdslen(x) == 3 && memcmp(x,"123\0",4) ==0)

    sdsfree(x);
    x = sdsnew("xxciaoyyy");
    sdstrim(x,"xy");
    TEST_ASSERT_TRUE(
        sdslen(x) == 4 && memcmp(x,"ciao\0",5) == 0)

    y = sdsdup(x);
    sdsrange(y,1,1);
    TEST_ASSERT_TRUE(
        sdslen(y) == 1 && memcmp(y,"i\0",2) == 0)

    sdsfree(y);
    y = sdsdup(x);
    sdsrange(y,1,-1);
    TEST_ASSERT_TRUE(
        sdslen(y) == 3 && memcmp(y,"iao\0",4) == 0)

    sdsfree(y);
    y = sdsdup(x);
    sdsrange(y,-2,-1);
    TEST_ASSERT_TRUE(
        sdslen(y) == 2 && memcmp(y,"ao\0",3) == 0)

    sdsfree(y);
    y = sdsdup(x);
    sdsrange(y,2,1);
    TEST_ASSERT_TRUE(
        sdslen(y) == 0 && memcmp(y,"\0",1) == 0)

    sdsfree(y);
    y = sdsdup(x);
    sdsrange(y,1,100);
    TEST_ASSERT_TRUE(
        sdslen(y) == 3 && memcmp(y,"iao\0",4) == 0)

    sdsfree(y);
    y = sdsdup(x);
    sdsrange(y,100,100);
    TEST_ASSERT_TRUE(
        sdslen(y) == 0 && memcmp(y,"\0",1) == 0)

    sdsfree(y);
    sdsfree(x);
    x = sdsnew("foo");
    y = sdsnew("foa");
    TEST_ASSERT_TRUE( sdscmp(x,y) > 0)

    sdsfree(y);
    sdsfree(x);
    x = sdsnew("bar");
    y = sdsnew("bar");
    TEST_ASSERT_TRUE( sdscmp(x,y) == 0)

    sdsfree(y);
    sdsfree(x);
    x = sdsnew("aar");
    y = sdsnew("bar");
    TEST_ASSERT_TRUE( sdscmp(x,y) < 0)

    sdsfree(y);
    sdsfree(x);
    // x = sdsnewlen("\a\n\0foo\r",7);
    // y = sdscatrepr(sdsempty(),x,sdslen(x));
    // TEST_ASSERT_TRUE(
    //     memcmp(y,"\"\\a\\n\\x00foo\\r\"",15) == 0)

#if 0
    {
        int oldfree;

        sdsfree(x);
        x = sdsnew("0");
        sh = (void*) (x-(sizeof(struct sdshdr)));
        TEST_ASSERT_TRUE( sh->len == 1 && sh->free == 0);
        x = sdsMakeRoomFor(x,1);
        sh = (void*) (x-(sizeof(struct sdshdr)));
        TEST_ASSERT_TRUE( sh->len == 1 && sh->free > 0);
        oldfree = sh->free;
        x[1] = '1';
        sdsIncrLen(x,1);
        TEST_ASSERT_TRUE( x[0] == '0' && x[1] == '1');
        TEST_ASSERT_TRUE( sh->len == 2);
        TEST_ASSERT_TRUE( sh->free == oldfree-1);
    }
#endif
}
Ejemplo n.º 9
0
int main(void) {
    {
        sds x = sdsnew("foo"), y;

        test_cond("Create a string and obtain the length",
            sdslen(x) == 3 && memcmp(x,"foo\0",4) == 0)

        sdsfree(x);
        x = sdsnewlen("foo",2);
        test_cond("Create a string with specified length",
            sdslen(x) == 2 && memcmp(x,"fo\0",3) == 0)

        x = sdscat(x,"bar");
        test_cond("Strings concatenation",
            sdslen(x) == 5 && memcmp(x,"fobar\0",6) == 0);

        x = sdscpy(x,"a");
        test_cond("sdscpy() against an originally longer string",
            sdslen(x) == 1 && memcmp(x,"a\0",2) == 0)

        x = sdscpy(x,"xyzxxxxxxxxxxyyyyyyyyyykkkkkkkkkk");
        test_cond("sdscpy() against an originally shorter string",
            sdslen(x) == 33 &&
            memcmp(x,"xyzxxxxxxxxxxyyyyyyyyyykkkkkkkkkk\0",33) == 0)

        sdsfree(x);
        x = sdscatprintf(sdsempty(),"%d",123);
        test_cond("sdscatprintf() seems working in the base case",
            sdslen(x) == 3 && memcmp(x,"123\0",4) ==0)

        sdsfree(x);
        x = sdstrim(sdsnew("xxciaoyyy"),"xy");
        test_cond("sdstrim() correctly trims characters",
            sdslen(x) == 4 && memcmp(x,"ciao\0",5) == 0)

        y = sdsrange(sdsdup(x),1,1);
        test_cond("sdsrange(...,1,1)",
            sdslen(y) == 1 && memcmp(y,"i\0",2) == 0)

        sdsfree(y);
        y = sdsrange(sdsdup(x),1,-1);
        test_cond("sdsrange(...,1,-1)",
            sdslen(y) == 3 && memcmp(y,"iao\0",4) == 0)

        sdsfree(y);
        y = sdsrange(sdsdup(x),-2,-1);
        test_cond("sdsrange(...,-2,-1)",
            sdslen(y) == 2 && memcmp(y,"ao\0",3) == 0)

        sdsfree(y);
        y = sdsrange(sdsdup(x),2,1);
        test_cond("sdsrange(...,2,1)",
            sdslen(y) == 0 && memcmp(y,"\0",1) == 0)

        sdsfree(y);
        y = sdsrange(sdsdup(x),1,100);
        test_cond("sdsrange(...,1,100)",
            sdslen(y) == 3 && memcmp(y,"iao\0",4) == 0)

        sdsfree(y);
        y = sdsrange(sdsdup(x),100,100);
        test_cond("sdsrange(...,100,100)",
            sdslen(y) == 0 && memcmp(y,"\0",1) == 0)

        sdsfree(y);
        sdsfree(x);
        x = sdsnew("foo");
        y = sdsnew("foa");
        test_cond("sdscmp(foo,foa)", sdscmp(x,y) > 0)

        sdsfree(y);
        sdsfree(x);
        x = sdsnew("bar");
        y = sdsnew("bar");
        test_cond("sdscmp(bar,bar)", sdscmp(x,y) == 0)

        sdsfree(y);
        sdsfree(x);
        x = sdsnew("aar");
        y = sdsnew("bar");
        test_cond("sdscmp(bar,bar)", sdscmp(x,y) < 0)
    }
    test_report()
}
Ejemplo n.º 10
0
void load_conf_from_string(char *config)
{
    char *err = NULL;
    int linenum = 0, tlines, i;
    sds *lines;

    lines = sdssplitlen(config, strlen(config), "\n", 1, &tlines);

    for(i = 0; i < tlines; i++)
    {
        sds *argv;
        int argc;

        linenum = i+1;

        log_debug(">> %0.3d %s", linenum,  lines[i]);

        lines[i] = sdstrim(lines[i], " \t\r\n");

        // skip comments and blank lines
        if(lines[i][0] == '#' || lines[i][0] == '\0')
            continue;

        argv = sdssplitargs(lines[i], &argc);
        if(argv == NULL)
        {
            err = "Unbalanced quotes in configuration line";
            goto loaderr;
        }

        if(argc == 0)
        {
            sdsfreesplitres(argv, argc);
            continue;
        }

        sdstolower(argv[0]);

        if(!strcasecmp(argv[0], "name") && argc == 2)
        {
            server.name = sdsdup(argv[1]);
        }else if(!strcasecmp(argv[0], "bind") && argc >= 2)
        {
            server.bind_addr_count = argc-1;

            if((argc-1) > ZR_MAX_BIND_ADDR)
            {
                err = "Too many bind address specified.";
                goto loaderr;
            }
            int j;

            for(j = 0; j < server.bind_addr_count; j++)
            {
                server.bind_arr[j].addr_str = sdsnew(argv[j+1]);
                server.bind_arr[j].addr.sin_family = AF_INET;
                server.bind_arr[j].addr.sin_addr.s_addr = inet_addr(argv[j+1]);
                if(server.bind_arr[j].addr.sin_addr.s_addr == INADDR_NONE)
                {
                    err = sdscatprintf(sdsempty(), "Wrong binding ip address: %s", argv[j+1]);
                    goto loaderr;
                }
            }
        }else if(!strcasecmp(argv[0], "port") && argc == 2)
        {
            server.port = atoi(argv[1]);

            if (server.port < 0 || server.port > 65535)
            {
                err = "Invalid port"; goto loaderr;
            }

        }else if(!strcasecmp(argv[0], "req-log-uds") && argc == 2)
        {
            server.req_log_uds = sdsnew(argv[1]);

        }else if(!strcasecmp(argv[0], "client-max") && argc == 2)
        {
            server.client_max = atoi(argv[1]);
            if(server.client_max < 0)
            {
                err = "Invalid server-connection-max value.";
                goto loaderr;
            }
        } else if (!strcasecmp(argv[0],"tcp-backlog") && argc == 2)
        {
            server.tcp_backlog = atoi(argv[1]);
            if (server.tcp_backlog < 0)
            {
                err = "Invalid backlog value";
                goto loaderr;
            }
/*
        } else if (!strcasecmp(argv[0],"rr-counter-buffer") && argc == 2)
        {
            server.tcp_backlog = atoi(argv[1]);
            if (server.tcp_backlog < 0)
            {
                err = "Invalid rr-counter-buffer value";
                goto loaderr;
            }
*/
        }else if(!strcasecmp(argv[0], "log-level") && argc == 2)
        {
            server.log_level = log_get_level_str(argv[1]);
        }else if(!strcasecmp(argv[0], "log-file") && argc == 2)
        {
            server.log_file = sdsdup(argv[1]);
            if(!strcasecmp(argv[1], "stdout"))
            {
                log_set_out(stdout);
            }else if(!strcasecmp(argv[1], "stderr"))
            {
                log_set_out(stderr);
            }else
            {
                FILE *fd;
                fd = fopen(argv[1], "a");
                if(fd == NULL)
                {
                    err = sdscatprintf(sdsempty(), "Can't open the log file: %s", strerror(errno));
                    goto loaderr;
                }
                
                log_set_out(fd);
            }
        }else if(!strcasecmp(argv[0], "zookeeper-health-uri") && argc == 2)
        {
            struct z_conn_info *zcinfo = z_parse_conn_string(argv[1]);
            if(zcinfo == NULL)
            {
                err = "Invalid zookeeper-health-url(Zookeeper URI) value.\n"
//                      "Zookeeper URI must be as below.\n"
//                      " - zoo://[USER[:PASS]@]HOST_STRING/NODE/PATH\n"
//                      "   ex) zoo://192.168.1.2:2181/test/node\n"
//                      "   ex) zoo://[email protected]:2181,192.168.1.3:2181/test/node\n"
//                      "   ex) zoo://foo:[email protected]:2181,192.168.1.3:2181/test/node\n"
                      "";
                goto loaderr;
            }

            server.z_health_conn = zcinfo;
        }else if(!strcasecmp(argv[0], "zookeeper-timeout") && argc == 2)
        {
            server.zoo_timeout = atoi(argv[1]);
            if(server.zoo_timeout < 0)
            {
                err = "Invalid zookeeper-timeout value";
                goto loaderr;
            }

        }else if(!strcasecmp(argv[0], "service") && argc == 3)
        {
            if(strlen(argv[1]) > ZR_MAX_SVC_NAME)
            {
                err = sdscatprintf(sdsempty(), "Service name is too long, first argument(service name) must be less then %d.", ZR_MAX_SVC_NAME);
                goto loaderr;
            }

            struct z_conn_info *cinfo = z_parse_conn_string(argv[2]);
            if(!cinfo)
            {
                err = "Invalid zookeeper-health-url(Zookeeper URI) value.\n"
//                      "Zookeeper URI must be as below.\n"
//                      " - zoo://[USER[:PASS]@]HOST_STRING/NODE/PATH\n"
//                      "   ex) zoo://192.168.1.2:2181/test/node\n"
//                      "   ex) zoo://[email protected]:2181,192.168.1.3:2181/test/node\n"
//                      "   ex) zoo://foo:[email protected]:2181,192.168.1.3:2181/test/node\n"
                      "";
                goto loaderr;
            }

            int j;
            for(j = 0; j < server.svc_count; j++)
            {
                struct svc *tsvc = server.svc_arr[j];
                if(!sdscmp(argv[1], tsvc->name))
                {
                    err = sdscatprintf(sdsempty(), "\"%s\" service name already exists.", argv[1]);
                    goto loaderr;
                }
            }

            struct svc *svc = svc_alloc(argv[1], strlen(argv[1]), server.svc_count);
            svc->z_conn = cinfo;
            server.svc_arr[server.svc_count] = svc;
            server.svc_count++;
        }else
        {
            err = "Bad config directive or wrong number of arguments";
            goto loaderr;
        }

        sdsfreesplitres(argv,argc);
    }

    sdsfreesplitres(lines,tlines);
    return;

loaderr:
    log_eerr("CONFIG FILE ERROR.");
    log_eerr("Reading configureation file, at line %d", linenum);
    log_eerr(">> '%s'", lines[i]);
    log_eerr("%s", err);
    exit(1);
}
Ejemplo n.º 11
0
void load_config_from_string(sds config) {
	char *err = NULL;
	int linenum = 0, totlines, i;
	sds *lines = sdssplitlen(config, strlen(config), "\n", 1, &totlines);

	for (i = 0; i < totlines; i++) {
		sds *argv;
		int argc;

		linenum = i + 1;
		lines[i] = sdstrim(lines[i], " \t\r\n");

		if (lines[i][0] == '#' || lines[i][0] == '\0')
			continue;

		argv = sdssplitargs(lines[i], &argc);
		if (argv == NULL) {
			err = "Unbalanced quotes in configuration line";
			goto loaderr;
		}

		if (argc == 0) {
			sdsfreesplitres(argv, argc);
			continue;
		}
		sdstolower(argv[0]);

		if (strcasecmp(argv[0], "working-directory") == 0 && argc == 2) {
			destor.working_directory = sdscpy(destor.working_directory,
					argv[1]);
		} else if (strcasecmp(argv[0], "simulation-level") == 0 && argc == 2) {
			if (strcasecmp(argv[1], "all") == 0) {
				destor.simulation_level = SIMULATION_ALL;
			} else if (strcasecmp(argv[1], "append") == 0) {
				destor.simulation_level = SIMULATION_APPEND;
			} else if (strcasecmp(argv[1], "restore") == 0) {
				destor.simulation_level = SIMULATION_RESTORE;
			} else if (strcasecmp(argv[1], "no") == 0) {
				destor.simulation_level = SIMULATION_NO;
			} else {
				err = "Invalid simulation level";
				goto loaderr;
			}
		} else if (strcasecmp(argv[0], "log-level") == 0 && argc == 2) {
			if (strcasecmp(argv[1], "debug") == 0) {
				destor.verbosity = DESTOR_DEBUG;
			} else if (strcasecmp(argv[1], "verbose") == 0) {
				destor.verbosity = DESTOR_VERBOSE;
			} else if (strcasecmp(argv[1], "notice") == 0) {
				destor.verbosity = DESTOR_NOTICE;
			} else if (strcasecmp(argv[1], "warning") == 0) {
				destor.verbosity = DESTOR_WARNING;
			} else {
				err = "Invalid log level";
				goto loaderr;
			}
		} else if (strcasecmp(argv[0], "chunk-algorithm") == 0 && argc == 2) {
			if (strcasecmp(argv[1], "fixed") == 0) {
				destor.chunk_algorithm = CHUNK_FIXED;
			} else if (strcasecmp(argv[1], "rabin") == 0) {
				destor.chunk_algorithm = CHUNK_RABIN;
			} else if (strcasecmp(argv[1], "normalized rabin") == 0) {
				destor.chunk_algorithm = CHUNK_NORMALIZED_RABIN;
			} else if (strcasecmp(argv[1], "file") == 0) {
				destor.chunk_algorithm = CHUNK_FILE;
			} else {
				err = "Invalid chunk algorithm";
				goto loaderr;
			}
		} else if (strcasecmp(argv[0], "chunk-avg-size") == 0 && argc == 2) {
			destor.chunk_avg_size = atoi(argv[1]);
		} else if (strcasecmp(argv[0], "chunk-max-size") == 0 && argc == 2) {
			destor.chunk_max_size = atoi(argv[1]);
		} else if (strcasecmp(argv[0], "chunk-min-size") == 0 && argc == 2) {
			destor.chunk_min_size = atoi(argv[1]);
		} else if (strcasecmp(argv[0], "fingerprint-index") == 0 && argc >= 3) {
			if (strcasecmp(argv[1], "exact") == 0) {
				destor.index_category[0] = INDEX_CATEGORY_EXACT;
			} else if (strcasecmp(argv[1], "near-exact") == 0) {
				destor.index_category[0] = INDEX_CATEGORY_NEAR_EXACT;
			} else {
				err = "Invalid index category";
				goto loaderr;
			}

			if (strcasecmp(argv[2], "physical") == 0) {
				destor.index_category[1] = INDEX_CATEGORY_PHYSICAL_LOCALITY;
			} else if (strcasecmp(argv[2], "logical") == 0) {
				destor.index_category[1] = INDEX_CATEGORY_LOGICAL_LOCALITY;
			} else {
				err = "Invalid index category";
				goto loaderr;
			}

			if (argc > 3) {
				if (strcasecmp(argv[3], "ddfs") == 0) {
					assert(destor.index_category[0] == INDEX_CATEGORY_EXACT 
                            && destor.index_category[1] == INDEX_CATEGORY_PHYSICAL_LOCALITY);
					destor.index_specific = INDEX_SPECIFIC_DDFS;
				} else if (strcasecmp(argv[3], "sampled index") == 0) {
					assert(destor.index_category[0] == INDEX_CATEGORY_NEAR_EXACT 
                            && destor.index_category[1] == INDEX_CATEGORY_PHYSICAL_LOCALITY);
					destor.index_specific = INDEX_SPECIFIC_SAMPLED;
				} else if (strcasecmp(argv[3], "block locality caching") == 0) {
					assert(destor.index_category[0] == INDEX_CATEGORY_EXACT 
                            && destor.index_category[1] == INDEX_CATEGORY_LOGICAL_LOCALITY);
					destor.index_specific =	INDEX_SPECIFIC_BLOCK_LOCALITY_CACHING;
				} else if (strcasecmp(argv[3], "extreme binning") == 0) {
					assert(destor.index_category[0] == INDEX_CATEGORY_NEAR_EXACT 
                            && destor.index_category[1] == INDEX_CATEGORY_LOGICAL_LOCALITY);
					destor.index_specific = INDEX_SPECIFIC_EXTREME_BINNING;
				} else if (strcasecmp(argv[3], "sparse index") == 0) {
					assert(destor.index_category[0] == INDEX_CATEGORY_NEAR_EXACT 
                            && destor.index_category[1] == INDEX_CATEGORY_LOGICAL_LOCALITY);
					destor.index_specific = INDEX_SPECIFIC_SPARSE;
				} else if (strcasecmp(argv[3], "silo") == 0) {
					assert(destor.index_category[0] == INDEX_CATEGORY_NEAR_EXACT 
                            && destor.index_category[1] == INDEX_CATEGORY_LOGICAL_LOCALITY);
					destor.index_specific = INDEX_SPECIFIC_SILO;
				} else {
					err = "Invalid index specific";
					goto loaderr;
				}
			}
		} else if (strcasecmp(argv[0], "fingerprint-index-cache-size")
				== 0 && argc == 2) {
			destor.index_cache_size = atoi(argv[1]);
		} else if (strcasecmp(argv[0], "fingerprint-index-key-value") == 0
				&& argc == 2) {
			if (strcasecmp(argv[1], "htable") == 0) {
				destor.index_key_value_store = INDEX_KEY_VALUE_HTABLE;
			} else {
				err = "Invalid key-value store";
				goto loaderr;
			}
		} else if (strcasecmp(argv[0], "fingerprint-index-key-size") == 0
				&& argc == 2) {
			destor.index_key_size = atoi(argv[1]);
		} else if (strcasecmp(argv[0], "fingerprint-index-value-length") == 0
				&& argc == 2) {
			destor.index_value_length = atoi(argv[1]);
		} else if (strcasecmp(argv[0], "fingerprint-index-bloom-filter") == 0
				&& argc == 2) {
			destor.index_bloom_filter_size = atoi(argv[1]);
		} else if (strcasecmp(argv[0], "fingerprint-index-sampling-method") == 0
				&& argc >= 2) {
			if (strcasecmp(argv[1], "optmin") == 0)
				destor.index_sampling_method[0] = INDEX_SAMPLING_OPTIMIZED_MIN;
			else if (strcasecmp(argv[1], "random") == 0)
				destor.index_sampling_method[0] = INDEX_SAMPLING_RANDOM;
			else if (strcasecmp(argv[1], "min") == 0)
				destor.index_sampling_method[0] = INDEX_SAMPLING_MIN;
			else if (strcasecmp(argv[1], "uniform") == 0)
				destor.index_sampling_method[0] = INDEX_SAMPLING_UNIFORM;
			else {
				err = "Invalid feature method!";
				goto loaderr;
			}

			if (argc > 2) {
				destor.index_sampling_method[1] = atoi(argv[2]);
			} else {
				destor.index_sampling_method[1] = 0;
			}
		} else if (strcasecmp(argv[0], "fingerprint-index-segment-algorithm")
				== 0 && argc >= 2) {
			if (strcasecmp(argv[1], "fixed") == 0)
				destor.index_segment_algorithm[0] = INDEX_SEGMENT_FIXED;
			else if (strcasecmp(argv[1], "file-defined") == 0)
				destor.index_segment_algorithm[0] = INDEX_SEGMENT_FILE_DEFINED;
			else if (strcasecmp(argv[1], "content-defined") == 0)
				destor.index_segment_algorithm[0] =	INDEX_SEGMENT_CONTENT_DEFINED;
			else {
				err = "Invalid segment algorithm";
				goto loaderr;
			}

			if (argc > 2) {
				assert(destor.index_segment_algorithm[0] != INDEX_SEGMENT_FILE_DEFINED);
				destor.index_segment_algorithm[1] = atoi(argv[2]);
			}
		} else if (strcasecmp(argv[0], "fingerprint-index-segment-boundary") == 0
				&& argc == 3) {
			destor.index_segment_min = atoi(argv[1]);
			destor.index_segment_max = atoi(argv[2]);
		} else if (strcasecmp(argv[0], "fingerprint-index-segment-selection")
				== 0 && argc >= 2) {
			destor.index_segment_selection_method[1] = 1;
			if (strcasecmp(argv[1], "base") == 0)
				destor.index_segment_selection_method[0] = INDEX_SEGMENT_SELECT_BASE;
			else if (strcasecmp(argv[1], "top") == 0) {
				destor.index_segment_selection_method[0] = INDEX_SEGMENT_SELECT_TOP;
				if (argc > 2)
					destor.index_segment_selection_method[1] = atoi(argv[2]);
			} else if (strcasecmp(argv[1], "mix") == 0)
				destor.index_segment_selection_method[0] = INDEX_SEGMENT_SELECT_MIX;
			else {
				err = "Invalid selection method!";
				goto loaderr;
			}
		} else if (strcasecmp(argv[0], "fingerprint-index-segment-prefetching")	== 0 && argc == 2) {
			destor.index_segment_prefech = atoi(argv[1]);
		} else if (strcasecmp(argv[0], "rewrite-algorithm") == 0 && argc >= 2) {
			if (strcasecmp(argv[1], "no") == 0)
				destor.rewrite_algorithm[0] = REWRITE_NO;
			else if (strcasecmp(argv[1], "cfl-based selective deduplication")
					== 0 || strcasecmp(argv[1], "cfl") == 0)
				destor.rewrite_algorithm[0] =
				REWRITE_CFL_SELECTIVE_DEDUPLICATION;
			else if (strcasecmp(argv[1], "context-based rewriting") == 0
					|| strcasecmp(argv[1], "cbr") == 0)
				destor.rewrite_algorithm[0] = REWRITE_CONTEXT_BASED;
			else if (strcasecmp(argv[1], "capping") == 0
					|| strcasecmp(argv[1], "cap") == 0)
				destor.rewrite_algorithm[0] = REWRITE_CAPPING;
			else {
				err = "Invalid rewriting algorithm";
				goto loaderr;
			}

			if (argc > 2) {
				assert(destor.rewrite_algorithm != REWRITE_NO);
				destor.rewrite_algorithm[1] = atoi(argv[2]);
			} else {
				destor.rewrite_algorithm[1] = 1024;
			}
		} else if (strcasecmp(argv[0], "rewrite-enable-cfl-switch") == 0
				&& argc == 2) {
			destor.rewrite_enable_cfl_switch = yesnotoi(argv[1]);
		} else if (strcasecmp(argv[0], "rewrite-cfl-require") == 0
				&& argc == 2) {
			destor.rewrite_cfl_require = atof(argv[1]);
		} else if (strcasecmp(argv[0], "rewrite-cfl-usage-threshold") == 0
				&& argc == 2) {
			destor.rewrite_cfl_usage_threshold = atof(argv[1]);
		} else if (strcasecmp(argv[0], "rewrite-cbr-limit") == 0 && argc == 2) {
			destor.rewrite_cbr_limit = atof(argv[1]);
		} else if (strcasecmp(argv[0], "rewrite-cbr-minimal-utility") == 0
				&& argc == 2) {
			destor.rewrite_cbr_minimal_utility = atof(argv[1]);
		} else if (strcasecmp(argv[0], "rewrite-capping-level") == 0
				&& argc == 2) {
			destor.rewrite_capping_level = atoi(argv[1]);
		} else if (strcasecmp(argv[0], "rewrite-enable-har") == 0
				&& argc == 2) {
			destor.rewrite_enable_har = yesnotoi(argv[1]);
		} else if (strcasecmp(argv[0], "rewrite-har-utilization-threshold") == 0
				&& argc == 2) {
			destor.rewrite_har_utilization_threshold = atof(argv[1]);
		} else if (strcasecmp(argv[0], "rewrite-har-rewrite-limit") == 0
				&& argc == 2) {
			destor.rewrite_har_rewrite_limit = atof(argv[1]);
		} else if (strcasecmp(argv[0], "rewrite-enable-cache-aware") == 0
				&& argc == 2) {
			destor.rewrite_enable_cache_aware = yesnotoi(argv[1]);
		} else if (strcasecmp(argv[0], "restore-cache") == 0 && argc == 3) {
			if (strcasecmp(argv[1], "lru") == 0)
				destor.restore_cache[0] = RESTORE_CACHE_LRU;
			else if (strcasecmp(argv[1], "optimal cache") == 0
					|| strcasecmp(argv[1], "opt") == 0)
				destor.restore_cache[0] = RESTORE_CACHE_OPT;
			else if (strcasecmp(argv[1], "forward assembly") == 0
					|| strcasecmp(argv[1], "asm") == 0)
				destor.restore_cache[0] = RESTORE_CACHE_ASM;
			else {
				err = "Invalid restore cache";
				goto loaderr;
			}

			destor.restore_cache[1] = atoi(argv[2]);
		} else if (strcasecmp(argv[0], "restore-opt-window-size") == 0
				&& argc == 2) {
			destor.restore_opt_window_size = atoi(argv[1]);
		} else if (strcasecmp(argv[0], "backup-retention-time") == 0
				&& argc == 2) {
			destor.backup_retention_time = atoi(argv[1]);
		} else {
			err = "Bad directive or wrong number of arguments";
			goto loaderr;
		}
		sdsfreesplitres(argv, argc);
	}
	sdsfreesplitres(lines, totlines);
	return;

	loaderr: fprintf(stderr, "\n*** FATAL CONFIG FILE ERROR in destor ***\n");
	fprintf(stderr, "Reading the configuration file, at line %d\n", linenum);
	fprintf(stderr, ">>> '%s'\n", lines[i]);
	fprintf(stderr, "%s\n", err);
	exit(1);
}
Ejemplo n.º 12
0
int main(void) {
    {
        sdshdr *sh;
        sds x = sdsnew("foo"), y;

        test_cond("Create a string and obtain the length",
            sdslen(x) == 3 && memcmp(x,"foo\0",4) == 0)

        sdsfree(x);
        x = sdsnewlen("foo",2);
        test_cond("Create a string with specified length",
            sdslen(x) == 2 && memcmp(x,"fo\0",3) == 0)

        x = sdscat(x,"bar");
        test_cond("Strings concatenation",
            sdslen(x) == 5 && memcmp(x,"fobar\0",6) == 0);

        x = sdscpy(x,"a");
        test_cond("sdscpy() against an originally longer string",
            sdslen(x) == 1 && memcmp(x,"a\0",2) == 0)

        x = sdscpy(x,"xyzxxxxxxxxxxyyyyyyyyyykkkkkkkkkk");
        test_cond("sdscpy() against an originally shorter string",
            sdslen(x) == 33 &&
            memcmp(x,"xyzxxxxxxxxxxyyyyyyyyyykkkkkkkkkk\0",33) == 0)

        sdsfree(x);
        x = sdscatprintf(sdsempty(),"%d",123);
        test_cond("sdscatprintf() seems working in the base case",
            sdslen(x) == 3 && memcmp(x,"123\0",4) ==0)

        sdsfree(x);
        x = sdsnew("xxciaoyyy");
        sdstrim(x,"xy");
        test_cond("sdstrim() correctly trims characters",
            sdslen(x) == 4 && memcmp(x,"ciao\0",5) == 0)

        y = sdsdup(x);
        sdsrange(y,1,1);
        test_cond("sdsrange(...,1,1)",
            sdslen(y) == 1 && memcmp(y,"i\0",2) == 0)

        sdsfree(y);
        y = sdsdup(x);
        sdsrange(y,1,-1);
        test_cond("sdsrange(...,1,-1)",
            sdslen(y) == 3 && memcmp(y,"iao\0",4) == 0)

        sdsfree(y);
        y = sdsdup(x);
        sdsrange(y,-2,-1);
        test_cond("sdsrange(...,-2,-1)",
            sdslen(y) == 2 && memcmp(y,"ao\0",3) == 0)

        sdsfree(y);
        y = sdsdup(x);
        sdsrange(y,2,1);
        test_cond("sdsrange(...,2,1)",
            sdslen(y) == 0 && memcmp(y,"\0",1) == 0)

        sdsfree(y);
        y = sdsdup(x);
        sdsrange(y,1,100);
        test_cond("sdsrange(...,1,100)",
            sdslen(y) == 3 && memcmp(y,"iao\0",4) == 0)

        sdsfree(y);
        y = sdsdup(x);
        sdsrange(y,100,100);
        test_cond("sdsrange(...,100,100)",
            sdslen(y) == 0 && memcmp(y,"\0",1) == 0)

        sdsfree(y);
        sdsfree(x);
        x = sdsnew("foo");
        y = sdsnew("foa");
        test_cond("sdscmp(foo,foa)", sdscmp(x,y) > 0)

        sdsfree(y);
        sdsfree(x);
        x = sdsnew("bar");
        y = sdsnew("bar");
        test_cond("sdscmp(bar,bar)", sdscmp(x,y) == 0)

        sdsfree(y);
        sdsfree(x);
        x = sdsnew("aar");
        y = sdsnew("bar");
        test_cond("sdscmp(bar,bar)", sdscmp(x,y) < 0)

        sdsfree(y);
        sdsfree(x);
        x = sdsnewlen("\a\n\0foo\r",7);
        y = sdscatrepr(sdsempty(),x,sdslen(x));
        test_cond("sdscatrepr(...data...)",
            memcmp(y,"\"\\a\\n\\x00foo\\r\"",15) == 0)
	sdsfree(y);

        {
            size_t oldfree;

            sdsfree(x);
            x = sdsnew("0");
            sh = sds_start(x);
            test_cond("sdsnew() free/len buffers", sh->len == 1 && sh->free == 0);
            x = sdsMakeRoomFor(x,1);
            sh = sds_start(x);
            test_cond("sdsMakeRoomFor()", sh->len == 1 && sh->free > 0);
            oldfree = sh->free;
            x[1] = '1';
            sdsIncrLen(x,1);
            test_cond("sdsIncrLen() -- content", x[0] == '0' && x[1] == '1');
            test_cond("sdsIncrLen() -- len", sh->len == 2);
            test_cond("sdsIncrLen() -- free", sh->free == oldfree-1);
	    sdsfree(x);
        }

        x = sdsnew("0FoO1bar\n");
        sdstolower(x);
        test_cond("sdstolower(...)",
            memcmp(x,"0foo1bar\n\0",10) == 0)

        sdsfree(x);

        x = sdsnew("0FoO1bar\n");
        sdstoupper(x);
        test_cond("sdstoupper(...)",
            memcmp(x,"0FOO1BAR\n\0",10) == 0)

        sdsfree(x);
    }
    test_report()
    return 0;
}
Ejemplo n.º 13
0
void load_config_from_string(sds config) {
	char *err = NULL;
	int linenum = 0, totlines, i;
	sds *lines = sdssplitlen(config, strlen(config), "\n", 1, &totlines);

	for (i = 0; i < totlines; i++) {
		sds *argv;
		int argc;

		linenum = i + 1;
		lines[i] = sdstrim(lines[i], " \t\r\n");

		if (lines[i][0] == '#' || lines[i][0] == '\0')
			continue;

		argv = sdssplitargs(lines[i], &argc);
		if (argv == NULL) {
			err = "Unbalanced quotes in configuration line";
			goto loaderr;
		}

		if (argc == 0) {
			sdsfreesplitres(argv, argc);
			continue;
		}
		sdstolower(argv[0]);

		if (strcasecmp(argv[0], "working-directory") == 0 && argc == 2) {
			destor.working_directory = sdscpy(destor.working_directory,
					argv[1]);
		} else if (strcasecmp(argv[0], "simulation-level") == 0 && argc == 2) {
			if (strcasecmp(argv[1], "all") == 0) {
				destor.simulation_level = SIMULATION_ALL;
			} else if (strcasecmp(argv[1], "append") == 0) {
				destor.simulation_level = SIMULATION_APPEND;
			} else if (strcasecmp(argv[1], "restore") == 0) {
				destor.simulation_level = SIMULATION_RESTORE;
			} else if (strcasecmp(argv[1], "no") == 0) {
				destor.simulation_level = SIMULATION_NO;
			} else {
				err = "Invalid simulation level";
				goto loaderr;
			}
		} else if (strcasecmp(argv[0], "log-level") == 0 && argc == 2) {
			if (strcasecmp(argv[1], "debug") == 0) {
				destor.verbosity = DESTOR_DEBUG;
			} else if (strcasecmp(argv[1], "verbose") == 0) {
				destor.verbosity = DESTOR_VERBOSE;
			} else if (strcasecmp(argv[1], "notice") == 0) {
				destor.verbosity = DESTOR_NOTICE;
			} else if (strcasecmp(argv[1], "warning") == 0) {
				destor.verbosity = DESTOR_WARNING;
			} else {
				err = "Invalid log level";
				goto loaderr;
			}
		} else if (strcasecmp(argv[0], "chunk-algorithm") == 0 && argc == 2) {
			if (strcasecmp(argv[1], "fixed") == 0) {
				destor.chunk_algorithm = CHUNK_FIXED;
			} else if (strcasecmp(argv[1], "rabin") == 0) {
				destor.chunk_algorithm = CHUNK_RABIN;
			} else if (strcasecmp(argv[1], "normalized rabin") == 0) {
				destor.chunk_algorithm = CHUNK_NORMALIZED_RABIN;
			} else {
				err = "Invalid chunk algorithm";
				goto loaderr;
			}
		} else if (strcasecmp(argv[0], "chunk-avg-size") == 0 && argc == 2) {
			destor.chunk_avg_size = atoi(argv[1]);
			int pwr;
			for (pwr = 0; destor.chunk_avg_size; pwr++) {
				destor.chunk_avg_size >>= 1;
			}
			destor.chunk_avg_size = 1 << (pwr - 1);
		} else if (strcasecmp(argv[0], "chunk-max-size") == 0 && argc == 2) {
Ejemplo n.º 14
0
/* I agree, this is a very rudimental way to load a configuration...
   will improve later if the config gets more complex */
void loadServerConfig(char *filename) {
		FILE *fp;
		char buf[REDIS_CONFIGLINE_MAX+1], *err = NULL;
		int linenum = 0;
		sds line = NULL;

		if (filename[0] == '-' && filename[1] == '\0')
				fp = stdin;
		else {
				if ((fp = fopen(filename,"r")) == NULL) {
						redisLog(REDIS_WARNING, "Fatal error, can't open config file '%s'", filename);
						exit(1);
				}
		}

		while(fgets(buf,REDIS_CONFIGLINE_MAX+1,fp) != NULL) {
				sds *argv;
				int argc, j;

				linenum++;
				line = sdsnew(buf);
				line = sdstrim(line," \t\r\n");

				/* Skip comments and blank lines*/
				if (line[0] == '#' || line[0] == '\0') {
						sdsfree(line);
						continue;
				}

				/* Split into arguments */
				argv = sdssplitargs(line,&argc);
				sdstolower(argv[0]);

				/* Execute config directives */
				if (!strcasecmp(argv[0],"timeout") && argc == 2) {
						server.maxidletime = atoi(argv[1]);
						if (server.maxidletime < 0) {
								err = "Invalid timeout value"; goto loaderr;
						}
				} else if (!strcasecmp(argv[0],"port") && argc == 2) {
						server.port = atoi(argv[1]);
						if (server.port < 0 || server.port > 65535) {
								err = "Invalid port"; goto loaderr;
						}
				} else if (!strcasecmp(argv[0],"bind") && argc == 2) {
						server.bindaddr = zstrdup(argv[1]);
				} else if (!strcasecmp(argv[0],"formula") && argc == 2) {
						void *val = loadfm(argv[1]);
						if(!val) goto loaderr;
						int retval = dictAdd(server.fms, sdsnew(argv[1]), val);
						if(retval != DICT_OK) goto loaderr;
				} else if (!strcasecmp(argv[0],"dir") && argc == 2) {
						if (chdir(argv[1]) == -1) {
								redisLog(REDIS_WARNING,"Can't chdir to '%s': %s",
												argv[1], strerror(errno));
								exit(1);
						}
				} else if (!strcasecmp(argv[0],"loglevel") && argc == 2) {
						if (!strcasecmp(argv[1],"debug")) server.verbosity = REDIS_DEBUG;
						else if (!strcasecmp(argv[1],"verbose")) server.verbosity = REDIS_VERBOSE;
						else if (!strcasecmp(argv[1],"notice")) server.verbosity = REDIS_NOTICE;
						else if (!strcasecmp(argv[1],"warning")) server.verbosity = REDIS_WARNING;
						else {
								err = "Invalid log level. Must be one of debug, notice, warning";
								goto loaderr;
						}
				} else if (!strcasecmp(argv[0],"logfile") && argc == 2) {
						FILE *logfp;

						server.logfile = zstrdup(argv[1]);
						if (!strcasecmp(server.logfile,"stdout")) {
								zfree(server.logfile);
								server.logfile = NULL;
						}
						if (server.logfile) {
								/* Test if we are able to open the file. The server will not
								 * be able to abort just for this problem later... */
								logfp = fopen(server.logfile,"a");
								if (logfp == NULL) {
										err = sdscatprintf(sdsempty(),
														"Can't open the log file: %s", strerror(errno));
										goto loaderr;
								}
								fclose(logfp);
						}
				} else if (!strcasecmp(argv[0],"databases") && argc == 2) {
						server.dbnum = atoi(argv[1]);
						if (server.dbnum < 1) {
								err = "Invalid number of databases"; goto loaderr;
						}
				} else if (!strcasecmp(argv[0],"maxclients") && argc == 2) {
						server.maxclients = atoi(argv[1]);
				} else if (!strcasecmp(argv[0],"daemonize") && argc == 2) {
						if ((server.daemonize = yesnotoi(argv[1])) == -1) {
								err = "argument must be 'yes' or 'no'"; goto loaderr;
						}
				} else if (!strcasecmp(argv[0],"pidfile") && argc == 2) {
						zfree(server.pidfile);
						server.pidfile = zstrdup(argv[1]);
				} else {
						err = "Bad directive or wrong number of arguments"; goto loaderr;
				}
				for (j = 0; j < argc; j++)
						sdsfree(argv[j]);
				zfree(argv);
				sdsfree(line);
		}
		if (fp != stdin) fclose(fp);
		return;

loaderr:
		fprintf(stderr, "\n*** FATAL CONFIG FILE ERROR ***\n");
		fprintf(stderr, "Reading the configuration file, at line %d\n", linenum);
		fprintf(stderr, ">>> '%s'\n", line);
		fprintf(stderr, "%s\n", err);
		exit(1);

}
Ejemplo n.º 15
0
static int
conf_pre_validate(rmt_conf *cf)
{
    int ret;
    FILE *fh;
    char line[256];
    dict *organization = NULL;
    sds str = NULL;
    sds org_name = NULL;
    sds *key_value = NULL;
    int key_value_count = 0;
    sds key = NULL;
    conf_value *cv = NULL, **cv_sub;

    if (cf == NULL) {
        return RMT_ERROR;
    }

    fh = cf->fh;
    if (fh == NULL) {
        return RMT_ERROR;
    }

    while (!feof(fh)) {
        if (fgets(line,256,fh) == NULL) {
            if(feof(fh)) break;
            
            log_error("ERROR: Read a line from conf file %s failed: %s", 
                cf->fname, strerror(errno));
            goto error;
        }

        if(str != NULL){
            sdsfree(str);
            str = NULL;
        }

        if(key_value!= NULL){
            sdsfreesplitres(key_value, key_value_count);
            key_value = NULL;
            key_value_count = 0;
        }
        
        ASSERT(str == NULL);
        str = sdsnew(line);
        if(str == NULL){
            log_error("ERROR: Out of memory");
            goto error;
        }

        sdstrim(str, " \n");
        if(strchr(str, '#') != NULL){
            if(str[0] == '#'){
                log_debug(LOG_VVERB, "This line is comment");
                sdsfree(str);
                str = NULL;
                continue;
            }

            sds *content_comment = NULL;
            int content_comment_count = 0;

            content_comment = sdssplitlenonce(str, sdslen(str), 
                "#", 1, &content_comment_count);
            if(content_comment == NULL || content_comment_count != 2){
                log_error("ERROR: Split content and comment error.");
                goto error;
            }

            sdsfree(str);
            str = content_comment[0];
            content_comment[0] = NULL;
            sdsfreesplitres(content_comment, content_comment_count);
        }
        
        log_debug(LOG_VVERB, "%s", str);

        if(sdslen(str) == 0){
            log_debug(LOG_VVERB, "This line is space");
            sdsfree(str);
            str = NULL;
            continue;
        }else if(*str == '['){
            if(sdslen(str) <= 2 || *(str+sdslen(str)-1) != ']') {
                log_error("ERROR: Organization name %s in conf file %s error",
                    str, cf->fname);
                goto error;
            }

            if(key != NULL || cv != NULL){
                ret = conf_key_value_insert(organization, key, cv);
                if(ret != RMT_OK){
                    log_error("ERROR: Key value insert into organization failed");
                    goto error;
                }
                organization = NULL;
                key = NULL;
                cv = NULL;
            }

            ASSERT(org_name == NULL);
            sdsrange(str,1,-2);
            org_name = sdstrim(str, " ");
            str = NULL;

            if(sdslen(org_name) == 0){
                log_error("ERROR: Organization name %s in conf file %s error",
                    str, cf->fname);
                goto error;
            }

            organization = dictCreate(&KeyValueDictType, NULL);
            if(organization == NULL){
                log_error("ERROR: Create dict organization %s failed", 
                    org_name);
                goto error;
            }

            ret = dictAdd(cf->organizations, org_name, organization);
            if(ret != DICT_OK){
                log_error("ERROR: Organization %s in conf file is duplicate", 
                    org_name);
                goto error;
            }
            org_name = NULL;
        }else if(*str == '-'){
            
            if(cv == NULL || cv->type != CONF_VALUE_ARRAY){
                log_error("ERROR: Array %s in conf file %s has wrong conf value",
                    str, cf->fname);
                goto error;
            }

            sdsrange(str,1,-1);
            sdstrim(str, " ");
            
            cv_sub = array_push(cv->value);
            *cv_sub = conf_value_create(CONF_VALUE_STRING);
            if(*cv_sub == NULL){
                log_error("ERROR: Conf value create failed");
                goto error;
            }
            
            (*cv_sub)->value = str;
            str = NULL;
        }else{        
            key_value = sdssplitlenonce(str,sdslen(str),":",1,&key_value_count);
            log_debug(LOG_VVERB, "key_value_count: %d", key_value_count);
            if(key_value == NULL || key_value_count == 0){
                log_error("ERROR: line %s split by : failed", str);
                goto error;
            }else if(key_value_count == 1){
                log_error("ERROR: Line %s in conf file %s has no :", 
                    str, cf->fname);
                goto error;
            }else if(key_value_count == 2){           

                if(key != NULL || cv != NULL){
                    ret = conf_key_value_insert(organization, key, cv);
                    if(ret != RMT_OK){
                        log_error("ERROR: Key value insert into organization failed");
                        goto error;
                    }
                    key = NULL;
                    cv = NULL;
                }

                sdstrim(key_value[0]," ");
                sdstrim(key_value[1]," ");
                
                if(sdslen(key_value[1]) == 0){
                    ASSERT(cv == NULL);
                    cv = conf_value_create(CONF_VALUE_ARRAY);
                    if(cv == NULL){
                        log_error("ERROR: Conf value create failed");
                        goto error;
                    }

                    ASSERT(key == NULL);
                    key = key_value[0];
                    key_value[0] = NULL;
                    
                    continue;
                }

                ASSERT(cv == NULL);
                cv = conf_value_create(CONF_VALUE_STRING);
                if(cv == NULL){
                    log_error("ERROR: Conf value create failed");
                    goto error;
                }

                cv->value = key_value[1];
                key_value[1] = NULL;

                if(organization == NULL){
                    log_error("ERROR: line %s in conf file %s has no organization", 
                        str, cf->fname);
                    goto error;
                }
                ret = dictAdd(organization,key_value[0],cv);
                if(ret != DICT_OK){
                    log_error("ERROR: Key %s in organization of conf file is duplicate", 
                        key_value[0]);
                    goto error;
                }
                key_value[0] = NULL;
                cv = NULL;     
            }else{
                NOT_REACHED();
            }
        }
        
    }

    if(key != NULL || cv != NULL){
        ret = conf_key_value_insert(organization, key, cv);
        if(ret != RMT_OK){
            log_error("ERROR: Key value insert into organization failed");
            goto error;
        }
        key = NULL;
        cv = NULL;
    }

    if(str != NULL){
        sdsfree(str);
        str = NULL;
    }

    if(key_value!= NULL){
        sdsfreesplitres(key_value, key_value_count);
        key_value = NULL;
        key_value_count = 0;
    }

    return RMT_OK;

error:

    if(str != NULL){
        sdsfree(str);
    }

    if(key != NULL){
        sdsfree(key);
    }

    if(org_name != NULL){
        sdsfree(org_name);
    }

    if(key_value != NULL){
        sdsfreesplitres(key_value, key_value_count);
    }

    if(cv != NULL){
        conf_value_destroy(cv);
    }

    return RMT_ERROR;
}
Ejemplo n.º 16
0
static int
initFromConfigBuffer (sds config, conf_t * conf)
{
  char *err = NULL;
  int linenum = 0, totlines, i;
  sds *lines;

  lines = sdssplitlen (config, sdslen (config), "\n", 1, &totlines);

  for (i = 0; i < totlines; i++)
    {
      sds *argv;
      int argc;

      linenum = i + 1;
      lines[i] = sdstrim (lines[i], " \t\r\n");

      /* Skip comments and blank lines */
      if (lines[i][0] == '#' || lines[i][0] == '\0')
	continue;

      /* Split into arguments */
      argv = sdssplitargs (lines[i], &argc);
      if (argv == NULL)
	{
	  err = "Unbalanced quotes in configuration line";
	  goto err;
	}

      /* Skip this line if the resulting command vector is empty. */
      if (argc == 0)
	{
	  sdsfreesplitres (argv, argc);
	  continue;
	}
      sdstolower (argv[0]);

      if (!strcasecmp (argv[0], "zookeeper") && argc == 2)
	{
	  conf->zk_addr = strdup (argv[1]);
	}
      else if (!strcasecmp (argv[0], "cluster_name") && argc == 2)
	{
	  conf->cluster_name = strdup (argv[1]);
	}
      else if (!strcasecmp (argv[0], "port") && argc == 2)
	{
	  conf->port = atoi (argv[1]);
	  if (conf->port < 0 || conf->port > 65535)
	    {
	      sdsfreesplitres (argv, argc);
	      err = "Invalid port";
	      goto err;
	    }
	}
      else if (!strcasecmp (argv[0], "daemonize") && argc == 2)
	{
	  if (!strcasecmp (argv[1], "yes"))
	    {
	      conf->daemonize = 1;
	    }
	  else if (!strcasecmp (argv[1], "no"))
	    {
	      conf->daemonize = 0;
	    }
	  else
	    {
	      sdsfreesplitres (argv, argc);
	      err = "argument must be 'yes' or 'no'";
	      goto err;
	    }
	}
      else if (!strcasecmp (argv[0], "num_conn_per_gw") && argc == 2)
	{
	  conf->capi_conf.num_conn_per_gw = atoi (argv[1]);
	  if (conf->capi_conf.num_conn_per_gw < 1
	      || conf->capi_conf.num_conn_per_gw > 32)
	    {
	      sdsfreesplitres (argv, argc);
	      err = "Invalid num_conn_per_gw value";
	      goto err;
	    }
	}
      else if (!strcasecmp (argv[0], "init_timeout_millis") && argc == 2)
	{
	  conf->capi_conf.init_timeout_millis = atoi (argv[1]);
	  if (conf->capi_conf.init_timeout_millis < 3000
	      || conf->capi_conf.init_timeout_millis > 600000)
	    {
	      sdsfreesplitres (argv, argc);
	      err = "Invalid init_timeout_millis value";
	      goto err;
	    }
	}
      else if (!strcasecmp (argv[0], "log_level") && argc == 2)
	{
	  if (!strcasecmp (argv[1], "NOLOG"))
	    {
	      conf->capi_conf.log_level = ARC_LOG_LEVEL_NOLOG;
	    }
	  else if (!strcasecmp (argv[1], "ERROR"))
	    {
	      conf->capi_conf.log_level = ARC_LOG_LEVEL_ERROR;
	    }
	  else if (!strcasecmp (argv[1], "WARN"))
	    {
	      conf->capi_conf.log_level = ARC_LOG_LEVEL_WARN;
	    }
	  else if (!strcasecmp (argv[1], "INFO"))
	    {
	      conf->capi_conf.log_level = ARC_LOG_LEVEL_INFO;
	    }
	  else if (!strcasecmp (argv[1], "DEBUG"))
	    {
	      conf->capi_conf.log_level = ARC_LOG_LEVEL_DEBUG;
	    }
	  else
	    {
	      sdsfreesplitres (argv, argc);
	      err = "Invalid log_level value";
	      goto err;
	    }
	}
      else if (!strcasecmp (argv[0], "log_file_prefix") && argc == 2)
	{
	  if (argv[1][0] != '\0')
	    {
	      conf->capi_conf.log_file_prefix = strdup (argv[1]);
	    }
	}
      else if (!strcasecmp (argv[0], "max_fd") && argc == 2)
	{
	  conf->capi_conf.max_fd = atoi (argv[1]);
	  if (conf->capi_conf.max_fd < 1024 || conf->capi_conf.max_fd > 16000)
	    {
	      sdsfreesplitres (argv, argc);
	      err = "Invalid max_fd value";
	      goto err;
	    }
	}
      else if (!strcasecmp (argv[0], "conn_reconnect_millis") && argc == 2)
	{
	  conf->capi_conf.conn_reconnect_millis = atoi (argv[1]);
	  if (conf->capi_conf.conn_reconnect_millis < 100
	      || conf->capi_conf.conn_reconnect_millis > 600000)
	    {
	      sdsfreesplitres (argv, argc);
	      err = "Invalid conn_reconnect_millis value";
	      goto err;
	    }
	}
      else if (!strcasecmp (argv[0], "zk_reconnect_millis") && argc == 2)
	{
	  conf->capi_conf.zk_reconnect_millis = atoi (argv[1]);
	  if (conf->capi_conf.zk_reconnect_millis < 100
	      || conf->capi_conf.zk_reconnect_millis > 600000)
	    {
	      sdsfreesplitres (argv, argc);
	      err = "Invalid zk_reconnect_millis value";
	      goto err;
	    }
	}
      else if (!strcasecmp (argv[0], "zk_session_timeout_millis")
	       && argc == 2)
	{
	  conf->capi_conf.zk_session_timeout_millis = atoi (argv[1]);
	  if (conf->capi_conf.zk_session_timeout_millis < 1000
	      || conf->capi_conf.zk_session_timeout_millis > 600000)
	    {
	      sdsfreesplitres (argv, argc);
	      err = "Invalid zk_session_timeout_millis value";
	      goto err;
	    }
	}
      else if (!strcasecmp (argv[0], "local_proxy_query_timeout_millis")
	       && argc == 2)
	{
	  conf->query_timeout_millis = atoi (argv[1]);
	  if (conf->query_timeout_millis < 1000
	      || conf->query_timeout_millis > 600000)
	    {
	      sdsfreesplitres (argv, argc);
	      err = "Invalid local_proxy_query_timeout_millis";
	      goto err;
	    }
	}
      else
	{
	  sdsfreesplitres (argv, argc);
	  err = "Bad directive or wrong number of arguments";
	  goto err;
	}
      sdsfreesplitres (argv, argc);
    }
  sdsfreesplitres (lines, totlines);
  return 0;

err:
  fprintf (stderr, "\n*** FATAL CONFIG FILE ERROR ***\n");
  fprintf (stderr, "Reading the configuration file, at line %d\n", linenum);
  fprintf (stderr, ">>> '%s'\n", lines[i]);
  fprintf (stderr, "%s\n", err);
  sdsfreesplitres (lines, totlines);
  return -1;
}