/* * util_str2recno -- * Convert a string to a record number. */ int util_str2recno(WT_SESSION *session, const char *p, uint64_t *recnop) { uint64_t recno; char *endptr; /* * strtouq takes lots of things like hex values, signs and so on and so * forth -- none of them are OK with us. Check the string starts with * digit, that turns off the special processing. */ if (!isdigit(p[0])) goto format; errno = 0; recno = __wt_strtouq(p, &endptr, 0); if (recno == ULLONG_MAX && errno == ERANGE) return ( util_err(session, ERANGE, "%s: invalid record number", p)); if (endptr[0] != '\0') format: return ( util_err(session, EINVAL, "%s: invalid record number", p)); *recnop = recno; return (0); }
/* * dump_record -- * Dump a single record, advance cursor to next/prev, along * with JSON formatting if needed. */ static int dump_record(WT_CURSOR *cursor, const char *name, int reverse, int json) { WT_DECL_RET; const char *infix, *key, *prefix, *suffix, *value; int once; once = 0; if (json) { prefix = "\n{\n"; infix = ",\n"; suffix = "\n}"; } else { prefix = ""; infix = "\n"; suffix = "\n"; } while ((ret = (reverse ? cursor->prev(cursor) : cursor->next(cursor))) == 0) { if ((ret = cursor->get_key(cursor, &key)) != 0) return (util_cerr(name, "get_key", ret)); if ((ret = cursor->get_value(cursor, &value)) != 0) return (util_cerr(name, "get_value", ret)); if (printf("%s%s%s%s%s%s", (json && once) ? "," : "", prefix, key, infix, value, suffix) < 0) return (util_err(EIO, NULL)); once = 1; } if (json && once && printf("\n") < 0) return (util_err(EIO, NULL)); return (ret == WT_NOTFOUND ? 0 : util_cerr(name, (reverse ? "prev" : "next"), ret)); }
int util_downgrade(WT_SESSION *session, WT_CONNECTION *conn, int argc, char *argv[]) { WT_DECL_RET; int ch; char config_str[128], *release; release = NULL; while ((ch = __wt_getopt(progname, argc, argv, "V:")) != EOF) switch (ch) { case 'V': release = __wt_optarg; break; case '?': default: return (usage()); } argc -= __wt_optind; /* * The release argument is required. * There should not be any more arguments. */ if (argc != 0 || release == NULL) return (usage()); if ((ret = __wt_snprintf(config_str, sizeof(config_str), "compatibility=(release=%s)", release)) != 0) return (util_err(session, ret, NULL)); if ((ret = conn->reconfigure(conn, config_str)) != 0) return (util_err(session, ret, "conn.downgrade")); return (0); }
int util_load(WT_SESSION *session, int argc, char *argv[]) { int ch; const char *filename; uint32_t flags; flags = 0; filename = "<stdin>"; while ((ch = __wt_getopt(progname, argc, argv, "af:jnr:")) != EOF) switch (ch) { case 'a': /* append (ignore record number keys) */ append = 1; break; case 'f': /* input file */ if (freopen(__wt_optarg, "r", stdin) == NULL) return ( util_err(errno, "%s: reopen", __wt_optarg)); else filename = __wt_optarg; break; case 'j': /* input is JSON */ json = 1; break; case 'n': /* don't overwrite existing data */ no_overwrite = 1; break; case 'r': /* rename */ cmdname = __wt_optarg; break; case '?': default: return (usage()); } argc -= __wt_optind; argv += __wt_optind; /* -a and -o are mutually exclusive. */ if (append == 1 && no_overwrite == 1) return (util_err(EINVAL, "the -a (append) and -n (no-overwrite) flags are mutually " "exclusive")); /* The remaining arguments are configuration uri/string pairs. */ if (argc != 0) { if (argc % 2 != 0) return (usage()); cmdconfig = argv; } if (json) { if (append) flags |= LOAD_JSON_APPEND; if (no_overwrite) flags |= LOAD_JSON_NO_OVERWRITE; return (util_load_json(session, filename, flags)); } else return (load_dump(session)); }
/* * list_get_allocsize -- * Get the allocation size for this file from the metadata. */ static int list_get_allocsize(WT_SESSION *session, const char *key, size_t *allocsize) { WT_CONFIG_ITEM szvalue; WT_CONFIG_PARSER *parser; WT_DECL_RET; WT_EXTENSION_API *wt_api; int tret; char *config; wt_api = session->connection->get_extension_api(session->connection); if ((ret = wt_api->metadata_search(wt_api, session, key, &config)) != 0) return (util_err( session, ret, "%s: WT_EXTENSION_API.metadata_search", key)); if ((ret = wt_api->config_parser_open(wt_api, session, config, strlen(config), &parser)) != 0) return (util_err( session, ret, "WT_EXTENSION_API.config_parser_open")); if ((ret = parser->get(parser, "allocation_size", &szvalue)) != 0) { if (ret == WT_NOTFOUND) { *allocsize = 0; ret = 0; } else ret = util_err(session, ret, "WT_CONFIG_PARSER.get"); if ((tret = parser->close(parser)) != 0) (void)util_err(session, tret, "WT_CONFIG_PARSER.close"); return (ret); } if ((ret = parser->close(parser)) != 0) return (util_err(session, ret, "WT_CONFIG_PARSER.close")); *allocsize = (size_t)szvalue.val; return (0); }
/* * print_config -- * Output a key/value URI pair by combining v1 and v2. */ static int print_config(WT_SESSION *session, const char *key, const char *v1, const char *v2) { WT_DECL_RET; char *value_ret; const char *cfg[] = { v1, v2, NULL }; /* * The underlying call will stop if the first string is NULL -- check * here and swap in that case. */ if (cfg[0] == NULL) { cfg[0] = cfg[1]; cfg[1] = NULL; } if ((ret = __wt_config_collapse( (WT_SESSION_IMPL *)session, cfg, &value_ret)) != 0) return (util_err(session, ret, NULL)); ret = printf("%s\n%s\n", key, value_ret); free((char *)value_ret); if (ret < 0) return (util_err(session, EIO, NULL)); return (0); }
/* * util_read_line -- * Read a line from stdin into a ULINE. */ int util_read_line(WT_SESSION *session, ULINE *l, bool eof_expected, bool *eofp) { static uint64_t line = 0; size_t len; int ch; ++line; *eofp = false; if (l->memsize == 0) { if ((l->mem = realloc(l->mem, l->memsize + 1024)) == NULL) return (util_err(session, errno, NULL)); l->memsize = 1024; } for (len = 0;; ++len) { if ((ch = getchar()) == EOF) { if (len == 0) { if (eof_expected) { *eofp = true; return (0); } return (util_err(session, 0, "line %" PRIu64 ": unexpected end-of-file", line)); } return (util_err(session, 0, "line %" PRIu64 ": no newline terminator", line)); } if (ch == '\n') break; /* * We nul-terminate the string so it's easier to convert the * line into a record number, that means we always need one * extra byte at the end. */ if (len >= l->memsize - 1) { if ((l->mem = realloc(l->mem, l->memsize + 1024)) == NULL) return (util_err(session, errno, NULL)); l->memsize += 1024; } ((uint8_t *)l->mem)[len] = (uint8_t)ch; } ((uint8_t *)l->mem)[len] = '\0'; /* nul-terminate */ return (0); }
/* * dump_suffix -- * Output the dump file header suffix. */ static int dump_suffix(WT_SESSION *session, bool json) { if (json) { if (printf( " },\n" " {\n" " \"data\" : [") < 0) return (util_err(session, EIO, NULL)); } else { if (printf("Data\n") < 0) return (util_err(session, EIO, NULL)); } return (0); }
/* * dump_suffix -- * Output the dump file header suffix. */ static int dump_suffix(void) { if (printf("Data\n") < 0) return (util_err(EIO, NULL)); return (0); }
/* * dump_json_table_end -- * Output the JSON syntax that ends a table. */ static int dump_json_table_end(void) { if (printf(" ]\n }\n ]") < 0) return (util_err(EIO, NULL)); return (0); }
/* * dump_json_begin -- * Output the dump file header prefix. */ static int dump_json_begin(void) { if (printf("{\n") < 0) return (util_err(EIO, NULL)); return (0); }
/* * config_rename -- * Update the URI name. */ static int config_rename(char **urip, const char *name) { size_t len; char *buf, *p; /* Allocate room. */ len = strlen(*urip) + strlen(name) + 10; if ((buf = malloc(len)) == NULL) return (util_err(errno, NULL)); /* * Find the separating colon characters, but not the trailing one may * not be there. */ if ((p = strchr(*urip, ':')) == NULL) { free(buf); return (format()); } *p = '\0'; p = strchr(p + 1, ':'); snprintf(buf, len, "%s:%s%s", *urip, name, p == NULL ? "" : p); *urip = buf; return (0); }
int util_alter(WT_SESSION *session, int argc, char *argv[]) { WT_DECL_RET; int ch; char **configp; while ((ch = __wt_getopt(progname, argc, argv, "")) != EOF) switch (ch) { case '?': default: return (usage()); } argc -= __wt_optind; argv += __wt_optind; /* The remaining arguments are uri/string pairs. */ if (argc % 2 != 0) return (usage()); for (configp = argv; *configp != NULL; configp += 2) if ((ret = session->alter( session, configp[0], configp[1])) != 0) { (void)util_err(session, ret, "session.alter: %s, %s", configp[0], configp[1]); return (1); } return (0); }
/* * append_target -- * Build a list of comma-separated targets. */ static int append_target(WT_SESSION *session, const char *target, char **bufp) { static bool first = true; static size_t len = 0, remain = 0; static char *buf = NULL; /* 20 bytes of slop */ if (buf == NULL || remain < strlen(target) + 20) { len += strlen(target) + 512; remain += strlen(target) + 512; if ((buf = realloc(buf, len)) == NULL) return (util_err(session, errno, NULL)); *bufp = buf; } if (first) { first = false; strcpy(buf, "target=("); } else buf[strlen(buf) - 1] = ','; /* overwrite previous ")" */ strcat(buf, "\""); strcat(buf, target); strcat(buf, "\")"); remain -= strlen(target) + 1; return (0); }
/* * dump_json_table_end -- * Output the JSON syntax that ends a table. */ static int dump_json_table_end(WT_SESSION *session) { if (printf(" ]\n }\n ]") < 0) return (util_err(session, EIO, NULL)); return (0); }
/* * dump_json_begin -- * Output the dump file header prefix. */ static int dump_json_separator(WT_SESSION *session) { if (printf(",\n") < 0) return (util_err(session, EIO, NULL)); return (0); }
void handle_client_response(int sockfd){ char *headers = "HTTP/1.1 200 OK\nContent-Type: text/html\n"; struct FileBuffer *f = file_read("index.html"); if( f->data == NULL){ util_err("(handle_client_response) reading file"); } char *length = malloc(32); bzero(length,32); snprintf(length,32,"%d",f->size); char *ContentLength = "Content-Length: "; char *end = "\n\n"; // Writes the first two headers write(sockfd,headers,strlen(headers)); // Writes 'Content Length: ' write(sockfd,ContentLength,strlen(ContentLength)); // Writes the 'value' of Content Length write(sockfd,length,strlen(length)); // Two endline (aka CRLF, newline, EOL) chars to seperate the HEADERS from the BODY of the response. write(sockfd,end,strlen(end)); // The actual content. (html file) write(sockfd,f->data,f->size); }
/* * dump_json_begin -- * Output the dump file header prefix. */ static int dump_json_separator(void) { if (printf(",\n") < 0) return (util_err(EIO, NULL)); return (0); }
/* * dump_suffix -- * Output the dump file header suffix. */ static int dump_suffix(WT_SESSION *session) { if (printf("Data\n") < 0) return (util_err(session, EIO, NULL)); return (0); }
/* * dump_json_begin -- * Output the dump file header prefix. */ static int dump_json_begin(WT_SESSION *session) { if (printf("{\n") < 0) return (util_err(session, EIO, NULL)); return (0); }
int util_create(WT_SESSION *session, int argc, char *argv[]) { WT_DECL_RET; int ch; const char *config, *uri; config = NULL; while ((ch = __wt_getopt(progname, argc, argv, "c:")) != EOF) switch (ch) { case 'c': /* command-line configuration */ config = __wt_optarg; break; case '?': default: return (usage()); } argc -= __wt_optind; argv += __wt_optind; /* The remaining argument is the uri. */ if (argc != 1) return (usage()); if ((uri = util_name(*argv, "table")) == NULL) return (1); if ((ret = session->create(session, uri, config)) != 0) return (util_err(ret, "%s: session.create", uri)); return (0); }
/* * dump_json_table_begin -- * Output the JSON syntax that starts a table, along with its config. */ static int dump_json_table_begin( WT_SESSION *session, WT_CURSOR *cursor, const char *uri, const char *config) { WT_DECL_RET; const char *name; char *jsonconfig, *stripped; jsonconfig = NULL; /* Get the table name. */ if ((name = strchr(uri, ':')) == NULL) { fprintf(stderr, "%s: %s: corrupted uri\n", progname, uri); return (1); } ++name; if ((ret = __wt_session_create_strip(session, config, NULL, &stripped)) != 0) return (util_err(ret, NULL)); ret = dup_json_string(stripped, &jsonconfig); free(stripped); if (ret != 0) return (util_cerr(uri, "config dup", ret)); if (printf(" \"%s\" : [\n {\n", uri) < 0) goto eio; if (printf(" \"config\" : \"%s\",\n", jsonconfig) < 0) goto eio; if ((ret = dump_json_table_cg( session, cursor, uri, name, "colgroup:", "colgroups")) == 0) { if (printf(",\n") < 0) goto eio; ret = dump_json_table_cg( session, cursor, uri, name, "index:", "indices"); } if (printf("\n },\n {\n \"data\" : [") < 0) goto eio; if (0) { eio: ret = util_err(EIO, NULL); } free(jsonconfig); return (ret); }
/* * dump_json_table_config -- * Dump the config for the uri. */ static int dump_json_table_config(WT_SESSION *session, const char *uri) { WT_CURSOR *cursor; WT_DECL_RET; int tret; char *value; /* Dump the config. */ /* Open a metadata cursor. */ if ((ret = session->open_cursor( session, "metadata:create", NULL, NULL, &cursor)) != 0) { fprintf(stderr, "%s: %s: session.open_cursor: %s\n", progname, "metadata:create", session->strerror(session, ret)); return (1); } /* * Search for the object itself, to make sure it * exists, and get its config string. This where we * find out a table object doesn't exist, use a simple * error message. */ cursor->set_key(cursor, uri); if ((ret = cursor->search(cursor)) == 0) { if ((ret = cursor->get_value(cursor, &value)) != 0) ret = util_cerr(cursor, "get_value", ret); else if (dump_json_table_begin( session, cursor, uri, value) != 0) ret = 1; } else if (ret == WT_NOTFOUND) ret = util_err( session, 0, "%s: No such object exists", uri); else ret = util_err(session, ret, "%s", uri); if ((tret = cursor->close(cursor)) != 0) { tret = util_cerr(cursor, "close", tret); if (ret == 0) ret = tret; } return (ret); }
/* * config_exec -- * Create the tables/indices/colgroups implied by the list. */ int config_exec(WT_SESSION *session, char **list) { WT_DECL_RET; for (; *list != NULL; list += 2) if ((ret = session->create(session, list[0], list[1])) != 0) return (util_err(ret, "%s: session.create", list[0])); return (0); }
void run_server(int port){ // Socket Identifier int server_sock, client_sock; // Socket Address struct sockaddr *server_addr, *client_addr; // * Avoid Undefined Behavior server_addr = NULL; client_addr = NULL; // Attempt To Bind Server server_sock = sl_tcp_server(sl_sockaddr_server(port)); // Allocate a sockaddr for the client client_addr = sl_sockaddr(); signed char running = 1; if(server_sock < 0){ util_err("(sl_tcp_server) binding socket"); running = 0; } if(OPTION_VERBOSE==1) util_startup(port); while(running){ client_sock = sl_accept(server_sock,client_addr); if(client_sock < 0){ util_err("(sl_accept) accepting connection."); running = 0; }else{ util_say("Accepted Connection."); } handle_client(client_sock); close(client_sock); util_say("Closed Connection."); } // We check != NULL here incase our addr's failed to be allocated - // which happens for server_addr if sl_tcp_server fails to bind. if(server_addr!=NULL) free(server_addr); if(client_addr!=NULL) free(client_addr); }
int util_load(WT_SESSION *session, int argc, char *argv[]) { int ch; while ((ch = util_getopt(argc, argv, "af:nr:")) != EOF) switch (ch) { case 'a': /* append (ignore record number keys) */ append = 1; break; case 'f': /* input file */ if (freopen(util_optarg, "r", stdin) == NULL) return ( util_err(errno, "%s: reopen", util_optarg)); break; case 'n': /* don't overwrite existing data */ no_overwrite = 1; break; case 'r': /* rename */ cmdname = util_optarg; break; case '?': default: return (usage()); } argc -= util_optind; argv += util_optind; /* -a and -o are mutually exclusive. */ if (append == 1 && no_overwrite == 1) return (util_err(EINVAL, "the -a (append) and -n (no-overwrite) flags are mutually " "exclusive")); /* The remaining arguments are configuration uri/string pairs. */ if (argc != 0) { if (argc % 2 != 0) return (usage()); cmdconfig = argv; } return (load_dump(session)); }
/* * util_flush -- * Flush the file successfully, or drop it. */ int util_flush(WT_SESSION *session, const char *uri) { WT_DECL_RET; size_t len; char *buf; len = strlen(uri) + 100; if ((buf = malloc(len)) == NULL) return (util_err(session, errno, NULL)); (void)snprintf(buf, len, "target=(\"%s\")", uri); if ((ret = session->checkpoint(session, buf)) != 0) { ret = util_err(session, ret, "%s: session.checkpoint", uri); (void)session->drop(session, uri, NULL); } free(buf); return (ret); }
/* * dump_prefix -- * Output the dump file header prefix. */ static int dump_prefix(WT_SESSION *session, bool hex, bool json) { int vmajor, vminor, vpatch; (void)wiredtiger_version(&vmajor, &vminor, &vpatch); if (!json && (printf( "WiredTiger Dump (WiredTiger Version %d.%d.%d)\n", vmajor, vminor, vpatch) < 0 || printf("Format=%s\n", hex ? "hex" : "print") < 0 || printf("Header\n") < 0)) return (util_err(session, EIO, NULL)); else if (json && printf( " \"%s\" : \"%d (%d.%d.%d)\",\n", DUMP_JSON_VERSION_MARKER, DUMP_JSON_CURRENT_VERSION, vmajor, vminor, vpatch) < 0) return (util_err(session, EIO, NULL)); return (0); }
/* * dump_config -- * Dump the config for the uri. */ static int dump_config(WT_SESSION *session, const char *uri, WT_CURSOR *cursor, bool hex, bool json) { WT_CURSOR *mcursor; WT_DECL_RET; int tret; /* Open a metadata cursor. */ if ((ret = session->open_cursor( session, "metadata:create", NULL, NULL, &mcursor)) != 0) { fprintf(stderr, "%s: %s: session.open_cursor: %s\n", progname, "metadata:create", session->strerror(session, ret)); return (1); } /* * Search for the object itself, just to make sure it exists, we don't * want to output a header if the user entered the wrong name. This is * where we find out a table doesn't exist, use a simple error message. */ mcursor->set_key(mcursor, uri); if ((ret = mcursor->search(mcursor)) == 0) { if ((!json && dump_prefix(session, hex, json) != 0) || dump_table_config(session, mcursor, cursor, uri, json) != 0 || dump_suffix(session, json) != 0) ret = 1; } else if (ret == WT_NOTFOUND) ret = util_err(session, 0, "%s: No such object exists", uri); else ret = util_err(session, ret, "%s", uri); if ((tret = mcursor->close(mcursor)) != 0) { tret = util_cerr(mcursor, "close", tret); if (ret == 0) ret = tret; } return (ret); }
/* * util_name -- * Build a name. */ char * util_name(const char *s, const char *type, u_int flags) { size_t len; int copy; char *name; copy = 0; if (WT_PREFIX_MATCH(s, "backup:")) { goto type_err; } else if (WT_PREFIX_MATCH(s, "colgroup:")) { if (!(flags & UTIL_COLGROUP_OK)) goto type_err; copy = 1; } else if (WT_PREFIX_MATCH(s, "config:")) { goto type_err; } else if (WT_PREFIX_MATCH(s, "file:")) { if (!(flags & UTIL_FILE_OK)) goto type_err; copy = 1; } else if (WT_PREFIX_MATCH(s, "index:")) { if (!(flags & UTIL_INDEX_OK)) goto type_err; copy = 1; } else if (WT_PREFIX_MATCH(s, "lsm:")) { if (!(flags & UTIL_LSM_OK)) goto type_err; copy = 1; } else if (WT_PREFIX_MATCH(s, "statistics:")) { goto type_err; } else if (WT_PREFIX_MATCH(s, "table:")) { if (!(flags & UTIL_TABLE_OK)) { type_err: fprintf(stderr, "%s: %s: unsupported object type: %s\n", progname, command, s); return (NULL); } copy = 1; } len = strlen(type) + strlen(s) + 2; if ((name = calloc(len, 1)) == NULL) { (void)util_err(errno, NULL); return (NULL); } if (copy) strcpy(name, s); else snprintf(name, len, "%s:%s", type, s); return (name); }