void thread(void *arg) { char * ptr=(char *)arg; sleep(1); FILE *datafp = NULL;; datafp=fopen("student.txt", "w+"); fwrite(ptr,1,20,datafp); fclose(datafp); DBHANDLE db; if ((db = db_open("db4", O_RDWR | O_CREAT | O_TRUNC, FILE_MODE)) == NULL) err_sys("db_open error"); if (db_store(db, "Alpha", "data1", DB_INSERT) != 0) err_quit("db_store error for alpha"); if (db_store(db, "beta", "Data for beta", DB_INSERT) != 0) err_quit("db_store error for beta"); if (db_store(db, "gamma", "record3", DB_INSERT) != 0) err_quit("db_store error for gamma"); db_close(db); exit(0); //printf("hello world! %s\n",ptr); }
int main(int argc,char **argv) { static GDBM_FILE gdbm_stotest = NULL; datum key,data; int i; for(i = 1;i<argc;i++) { key.dptr = "store test1!"; key.dsize = strlen("store test1")+1; data = key; gdbm_stotest = db_open(argv[i]); printf("\n--------open dbm id:%d-----------\n",gdbm_stotest); if(db_store(gdbm_stotest,key,data) < 0) { printf("\n---------store err-----------\n"); db_close(gdbm_stotest); break; } else { printf("\n---------store successfully!-----------\n"); db_close(gdbm_stotest); } printf("\n--------close dbm id:%d-----------\n",gdbm_stotest); key.dptr = "store test2!"; key.dsize = strlen("store test1")+1; data = key; gdbm_stotest = db_open(argv[i]); printf("\n--------open dbm id:%d-----------\n",gdbm_stotest); if(db_store(gdbm_stotest,key,data) < 0) { printf("\n---------store err-----------\n"); db_close(gdbm_stotest); break; } else { printf("\n---------store successfully!-----------\n"); db_close(gdbm_stotest); } printf("\n--------close dbm id:%d-----------\n",gdbm_stotest); gdbm_stotest = db_open(argv[i]); key.dptr = "store test1!"; key.dsize = strlen("store test1!")+1; data = key; if(gdbm_exists(gdbm_stotest,key) != 0){ printf("-------really exist here!-----"); } else { printf("-------not exist here!-------"); } } }
void dhblock_keyhash_srv::real_store (chordID key, str od, str nd, u_int32_t exp, cb_dhstat cb) { u_int32_t v1 = dhblock_keyhash::version (nd.cstr (), nd.len ()); if (od.len ()) { u_int32_t v0 = dhblock_keyhash::version (od.cstr (), od.len ()); if (v0 > v1) { chordID p = node->my_pred ()->id (); chordID m = node->my_ID (); if (betweenrightincl (p, m, key)) cb (DHASH_STALE); else cb (DHASH_RETRY); } else { info << "db delete: " << key << "\n"; db->remove (key, v0, wrap (this, &dhblock_keyhash_srv::delete_cb, key, nd, v1, exp, cb)); } } else { info << "db write: " << node->my_ID () << " N " << key << " " << nd.len () << "\n"; db_store (key, nd, v1, exp, cb); } }
int main(void) { DBHANDLE db; if ((db = db_open("db4", O_RDWR | O_CREAT | O_TRUNC, FILE_MODE)) == NULL) err_sys("db_open error"); if (db_store(db, "Alpha", "data1", DB_INSERT) != 0) err_quit("db_store error for alpha"); if (db_store(db, "beta", "Data for beta", DB_INSERT) != 0) err_quit("db_store error for beta"); if (db_store(db, "gamma", "record3", DB_INSERT) != 0) err_quit("db_store error for gamma"); db_close(db); exit(0); }
void dhblock_keyhash_srv::delete_cb (chordID k, str d, u_int32_t v, u_int32_t exp, cb_dhstat cb, adb_status stat) { assert (stat == ADB_OK); info << "db write: " << node->my_ID () << " U " << k << " " << d.len () << "\n"; db_store (k, d, v, exp, cb); }
int main() { DBHANDLE db; if ((db = db_open("db4", O_RDWR | O_CREAT | O_TRUNC, FILE_MODE)) == NULL) err_sys("db_open error"); if (db_store(db, "wbf", "data1", DB_INSERT) != 0) err_quit("db_store for wbf"); if (db_store(db, "wbf2", "data for wbf2", DB_INSERT) != 0) err_quit("db_store for wbf2"); if (db_store(db, "wbf3", "data for wbf3", DB_INSERT) != 0) err_quit("db_store for wbf3"); db_close(db); return 0; }
/*! \brief Report error message \param s error message (can be NULL) */ void db_error(const char *s) { if (s == NULL) s = _("<NULL error message>"); if (err_msg) db_free(err_msg); err_msg = db_store(s); err_flag = 1; if (auto_print_errors) db_print_error(); err_code = DB_FAILED; }
int main(int argc, char* argv[]){ DBHANDLE db; char *tmp_val; if ((db = db_open("data/db1", O_RDWR | O_CREAT | O_TRUNC, FILE_MODE)) == NULL) err_sys("db_open error"); //printf("%p\n", db); if (db_store(db, "key_1", "value_1", DB_INSERT) != 0) err_quit("db_store error for key_1"); if (db_store(db, "key_2", "value_2", DB_INSERT) != 0) err_quit("db_store error for key_2"); if (db_store(db, "key_3", "value_3", DB_INSERT) != 0) err_quit("db_store error for key_3"); tmp_val = db_fetch(db,"key_1"); printf("%s\n",tmp_val); if (db_delete(db, "key_1") != 0){ err_quit("db_delete error"); } if (db_store(db, "key_2", "value_2_2", DB_REPLACE) != 0) err_quit("db_store error for key_1"); tmp_val = db_fetch(db,"key_2"); printf("%s\n",tmp_val); db_close(db); exit(0); }
int main(void) { int dbfd=open("test.per", O_RDWR | O_SYNC, S_IRWXU | S_IRWXG); if(dbfd<0) { if(errno==ENOENT) { dbfd=open("test.per", O_RDWR | O_SYNC | O_CREAT, S_IRWXU | S_IRWXG); if(dbfd<0) { perror("persist: open"); return(1); } else { if(db_init(dbfd)) { fprintf(stderr, "persist: failed to initialise db\n"); return(1); } } } else { perror("persist: open"); return(1); } } if(flock(dbfd, LOCK_EX)) { perror("persist: flock"); close(dbfd); return(1); } uint8_t ans=42; if(!db_store(dbfd, "Answer", T_U8, &ans)) { fprintf(stderr, "persist: db_store failed\n"); return(1); } flock(dbfd, LOCK_UN); close(dbfd); return(0); }
bool DB::Store(const string& filename) { return db_store(this, filename); }
int main(int argc, char **argv) { DBHANDLE db; int rc,c, flags, len; char *ptr_command, *ptr_key, *find; char *command, *key, *value; command = key = value = NULL; char *help_cmd = "command list:\n [add | update] key value \n [del | find] key \n [open | close] dbname\n [help count iterate exit]"; flags = O_RDWR; while ((c = getopt(argc, argv, "nt?")) != -1) { switch (c) { case 'n': flags |= O_CREAT; break; case 't': flags |= O_TRUNC; break; case '?': err_quit("usage: [-n] create new file [-t] truncate db <filename>"); break; } } if (optind != argc - 1) { err_quit("usage: [-n] create new file [-t] truncate db <filename>"); } if ((db = db_open(argv[optind], flags, FILE_MODE)) == NULL) { err_sys("db_open %s error", argv[optind]); } for (;;) { char buf[MAX_LEN]; printf("APUEDB> "); if (fgets(buf, MAX_LEN, stdin) == NULL) { printf("Bye~\n"); exit(1); } to_lowercase(buf); len = strlen(buf); if ((ptr_command = strchr(buf,COMMAND_SEP)) == NULL) { buf[len-1] = '\0'; if (strcmp(buf,"help") == 0) { printf("%s\n", help_cmd); continue; } else if (strcmp(buf, "count") == 0) { printf("%d\n", db_count(db)); } else if (strcmp(buf, "iterate") == 0) { char *ptr; db_rewind(db); while ((ptr = db_nextrec(db, NULL)) != NULL) { printf("%s\n", ptr); } continue; } else if (strcmp(buf, "exit") == 0) { printf("Bye~\n"); exit(1); } else { printf("invalid command, use command help for help\n"); continue; } } command = strndup(buf, ptr_command - buf); if (strcmp(command, "add") == 0) { if ((ptr_key = strchr(ptr_command+1, COMMAND_SEP)) == NULL) { printf("invalid add command, usage: add key value\n"); } else { key = strndup(ptr_command+1, ptr_key - ptr_command - 1); value = strndup(ptr_key+1, buf + len - 1 - ptr_key - 1); rc = db_store(db, key, value, DB_INSERT); if (rc == 0) { printf("add +OK\n"); } else { printf("key %s already exist\n", key); } } } else if (strcmp(command, "update") == 0) { if ((ptr_key = strchr(ptr_command+1, COMMAND_SEP)) == NULL) { printf("invalid add command, usage: add key value\n"); } else { key = strndup(ptr_command+1, ptr_key - ptr_command - 1); value = strndup(ptr_key+1, buf + len - 1 - ptr_key - 1); rc = db_store(db, key, value, DB_REPLACE); if (rc == 0) { printf("update +OK\n"); } else { printf("key %s not found\n", key); } } } else if (strcmp(command, "del") == 0) { if ((ptr_key = strchr(ptr_command+1, COMMAND_SEP)) != NULL) { printf("invalid del command, usage: del key\n"); continue; } key = strndup(ptr_command+1, buf + len - 1 - ptr_command -1); rc = db_delete(db, key); if (rc == 0) { printf("del +OK\n"); } else { printf("key %s not found\n", key); } } else if (strcmp(command, "find") == 0) { if ((ptr_key = strchr(ptr_command+1, COMMAND_SEP)) != NULL) { printf("invalid find command, usage: find key\n"); continue; } key = strndup(ptr_command+1, buf + len - 1 - ptr_command -1); find = db_fetch(db, key); if (find != NULL) { printf("%d,%s\n", strlen(find), find); } else { printf("key %s not found\n", key); } } if (key != NULL) { free(key); key = NULL; } if (command != NULL) { free(command); command = NULL; } if (value != NULL) { free(value); value = NULL; } } }
int http_analyze(int cfd, threadpool_t *tpool) { char method[20],version[20], url[MAXURL], query[MAXQUERY], line[MAXLINE], *pos; int ret=0, iscgi=0; if ((ret = http_getline(cfd, line, MAXLINE))==0){ struct sockaddr_in saddr; socklen_t len; getsockname(cfd, (struct sockaddr*)&saddr, &len); char buf[100]; sprintf (buf, "%s ip:%s create an error connection", __FUNCTION__, inet_ntoa(saddr.sin_addr)); errlog(buf); return -1; } sscanf(line, "%s %s %s", method, url, version); if (strncasecmp(method, "GET", 3) !=0 && strncasecmp(method, "POST", 4) !=0){ http_not_support(cfd); return 0; } pos = url; while(*pos != '?' && *pos != '\0') pos++; if (*pos == '?'){ iscgi=1; *pos='\0'; pos++; strncpy(query, pos, strlen(pos)+1); } struct sockaddr_in addr; socklen_t socklen; getsockname(cfd, (struct sockaddr*)&addr, &socklen); time_t t = time(NULL); char* tmpbuf = (char*)malloc(50); sprintf (tmpbuf, "%s----", ctime(&t)); sprintf (tmpbuf, "%sIP:%s", tmpbuf, inet_ntoa(addr.sin_addr)); db_store(walker_db, (const char*)tmpbuf, (const char*)url, DB_INSERT); free(tmpbuf); if (strncasecmp(method, "GET", 3)==0){ char *filepath; filepath = (char *)malloc(strlen(walkerconf[ROOT].value)+strlen(url)+1); strcpy(filepath, walkerconf[ROOT].value); strcat(filepath, url); while((ret = http_getline(cfd, line, MAXLINE))>0); struct stat st; if (stat(filepath, &st) == -1){ http_not_found(cfd); return 0; } if (access(filepath, R_OK)){ http_error(cfd); return 0; } if ((S_ISDIR(st.st_mode) || S_ISREG(st.st_mode)) && !iscgi){ struct http_job* job = http_make_job(cfd, filepath); //threadpool_add(tpool, http_thread_work, (void*)job, 0); http_thread_work((void*)job); return 0; }else if (S_ISREG(st.st_mode) && iscgi){ }else{ http_error(cfd); return 0; } }else if (strncasecmp(method, "POST", 4) == 0){ int pi[2], pid, content_length; char *p; char *filepath; filepath = (char *)malloc(strlen(walkerconf[ROOT].value)+strlen(url)+1); strcpy(filepath, walkerconf[ROOT].value); strcat(filepath, url); content_length=-1; while((ret=http_getline(cfd, line, MAXLINE)) !=0){ if (strncasecmp(line, "Content-Length:", 15) == 0){ p = &line[15]; p += strspn(p, " \t"); content_length = atoi(p); } } if (content_length != -1){ recv(cfd, postdata, content_length, 0); } socketpair(AF_UNIX, SOCK_STREAM, 0, pi); pid = fork(); if (pid==0){ dup2(pi[0], 0); dup2(pi[1], 1); close(pi[0]); close(pi[1]); char meth_env[255]; char length_env[255]; sprintf (meth_env, "REQUEST_METHOD=%s", method); putenv(meth_env); sprintf (length_env, "CONTENT_LENGTH=%d", content_length); putenv(length_env); execl("/usr/bin/php-cgi", "php-cgi", filepath, NULL); exit(0); }else{ char recvdata[MAXLINE], packet[MAXLINE]; write(pi[0], postdata, content_length); if ((ret = read(pi[0], recvdata, MAXLINE))>0){ recvdata[ret]='\0'; //sprintf(packet, "%sContent-Type: text/html\r\n", packet); sprintf(packet, "HTTP/1.1 200 OK\r\n"); sprintf(packet, "%sContent-Length: %d\r\n\r\n", packet, ret); sprintf(packet, "%s%s", packet, recvdata); send(cfd, packet, MAXLINE, 0); //send(cfd, recvdata, MAXLINE, 0); } close(pi[0]); close(pi[1]); free(filepath); waitpid(pid, NULL, 0); } } return 0; }
/*! \brief Set 'who' for error messages \param me my name */ void db_set_error_who(const char *me) { if (who) db_free(who); who = db_store(me); }