void do_open(char *fname, int handle, int size) { int fd, i; int flags = O_RDWR|O_CREAT; struct stat st; static int count; strupper(fname); if (size == 0) flags |= O_TRUNC; fd = open(fname, flags, 0600); if (fd == -1) { printf("(%d) open %s failed for handle %d (%s)\n", line_count, fname, handle, strerror(errno)); return; } fstat(fd, &st); if (size > st.st_size) { #if DEBUG printf("(%d) expanding %s to %d from %d\n", line_count, fname, size, (int)st.st_size); #endif expand_file(fd, size - st.st_size); } else if (size < st.st_size) { printf("truncating %s to %d from %d\n", fname, size, (int)st.st_size); ftruncate(fd, size); } for (i=0;i<MAX_FILES;i++) { if (ftable[i].handle == 0) break; } if (i == MAX_FILES) { printf("file table full for %s\n", fname); exit(1); } ftable[i].handle = handle; ftable[i].fd = fd; if (count++ % 100 == 0) { printf("."); } }
static int prepare_data_room(board_t* board, uint32_t length) { struct stat st; assert(NULL != board); ERRORP(-1 == fstat(board->data.fd, &st), "Can't fstat data file."); board->data.st_size = st.st_size; if (board->data.st_size - board->data.used_size < length) { ERRORP(-1 == expand_file(&board->data), "Error happened when expand data file."); } return 0; L_error: return -1; }
/* * Adjust size of base to meet a cylinder boundary. */ int adjust_base(int f, struct sun_disklabel *slp) { struct stat st; off_t sz; if (lseek(f, 0, SEEK_END) == -1) err(1, "lseek"); if (fstat(f, &st) == -1) err(1, "fstat"); sz = ((off_t)slp->sl_nsectors) * ((off_t)slp->sl_ntracks) * ((off_t)512); if ((st.st_size % sz) != 0) { if (expand_file(f, sz - (st.st_size % sz))) err(1, "expand_file"); } return (0); }
static int prepare_index_room(board_t* board) { struct stat st; assert(NULL != board); ERRORP(-1 == fstat(board->index.fd, &st), "Can't fstat index file."); board->index.st_size = st.st_size; if (board->index.st_size - board->index.used_size < 10 * sizeof(article_t)) { ERRORP(-1 == expand_file(&board->index), "Error happened when expand index file."); ERRORP(-1 == write_stat_info(board->index.fd, &board->stat), "Error happend when write stat info."); } return 0; L_error: return -1; }
int cfg_read(Cfg *cfg) { CfgLoc *loc; CfgLoc *loc_inc; List *lines; List *expand; List *stack; Octstr *name; Octstr *value; Octstr *filename; CfgGroup *grp; long equals; long lineno; long error_lineno; loc = loc_inc = NULL; /* * expand initial main config file and add it to the recursion * stack to protect against cycling */ if ((lines = expand_file(cfg->filename, 1)) == NULL) { panic(0, "Failed to load main configuration file `%s'. Aborting!", octstr_get_cstr(cfg->filename)); } stack = gwlist_create(); gwlist_insert(stack, 0, octstr_duplicate(cfg->filename)); grp = NULL; lineno = 0; error_lineno = 0; while (error_lineno == 0 && (loc = gwlist_extract_first(lines)) != NULL) { octstr_strip_blanks(loc->line); if (octstr_len(loc->line) == 0) { if (grp != NULL && add_group(cfg, grp) == -1) { error_lineno = loc->line_no; destroy_group(grp); } grp = NULL; } else if (octstr_get_char(loc->line, 0) != '#') { equals = octstr_search_char(loc->line, '=', 0); if (equals == -1) { error(0, "An equals sign ('=') is missing on line %ld of file %s.", loc->line_no, octstr_get_cstr(loc->filename)); error_lineno = loc->line_no; } else /* * check for special config directives, like include or conditional * directives here */ if (octstr_search(loc->line, octstr_imm("include"), 0) != -1) { filename = octstr_copy(loc->line, equals + 1, octstr_len(loc->line)); parse_value(filename); /* check if we are cycling */ if (gwlist_search(stack, filename, octstr_item_match) != NULL) { panic(0, "Recursive include for config file `%s' detected " "(on line %ld of file %s).", octstr_get_cstr(filename), loc->line_no, octstr_get_cstr(loc->filename)); } else { List *files = gwlist_create(); Octstr *file; struct stat filestat; /* check if included file is a directory */ if (lstat(octstr_get_cstr(filename), &filestat) != 0) { error(errno, "lstat failed: couldn't stat `%s'", octstr_get_cstr(filename)); panic(0, "Failed to include `%s' " "(on line %ld of file %s). Aborting!", octstr_get_cstr(filename), loc->line_no, octstr_get_cstr(loc->filename)); } /* * is a directory, create a list with files of * this directory and load all as part of the * whole configuration. */ if (S_ISDIR(filestat.st_mode)) { DIR *dh; struct dirent *diritem; debug("gwlib.cfg", 0, "Loading include dir `%s' " "(on line %ld of file %s).", octstr_get_cstr(filename), loc->line_no, octstr_get_cstr(loc->filename)); dh = opendir(octstr_get_cstr(filename)); while ((diritem = readdir(dh))) { Octstr *fileitem; fileitem = octstr_duplicate(filename); octstr_append_cstr(fileitem, "/"); octstr_append_cstr(fileitem, diritem->d_name); lstat(octstr_get_cstr(fileitem), &filestat); if (!S_ISDIR(filestat.st_mode)) { gwlist_insert(files, 0, fileitem); } else { octstr_destroy(fileitem); } } closedir(dh); } /* is a file, create a list with it */ else { gwlist_insert(files, 0, octstr_duplicate(filename)); } /* include files */ while ((file = gwlist_extract_first(files)) != NULL) { gwlist_insert(stack, 0, octstr_duplicate(file)); debug("gwlib.cfg", 0, "Loading include file `%s' (on line %ld of file %s).", octstr_get_cstr(file), loc->line_no, octstr_get_cstr(loc->filename)); /* * expand the given include file and add it to the current * processed main while loop */ if ((expand = expand_file(file, 0)) != NULL) { while ((loc_inc = gwlist_extract_first(expand)) != NULL) gwlist_insert(lines, 0, loc_inc); } else { panic(0, "Failed to load whole configuration. Aborting!"); } gwlist_destroy(expand, NULL); cfgloc_destroy(loc_inc); octstr_destroy(file); } gwlist_destroy(files, octstr_destroy_item); } octstr_destroy(filename); } /* * this is a "normal" line, so process it accodingly */ else { name = octstr_copy(loc->line, 0, equals); octstr_strip_blanks(name); value = octstr_copy(loc->line, equals + 1, octstr_len(loc->line)); parse_value(value); if (grp == NULL) grp = create_group(); if (grp->configfile != NULL) { octstr_destroy(grp->configfile); grp->configfile = NULL; } grp->configfile = octstr_duplicate(cfg->filename); cfg_set(grp, name, value); octstr_destroy(name); octstr_destroy(value); } } cfgloc_destroy(loc); } if (grp != NULL && add_group(cfg, grp) == -1) { error_lineno = 1; destroy_group(grp); } gwlist_destroy(lines, NULL); gwlist_destroy(stack, octstr_destroy_item); if (error_lineno != 0) { error(0, "Error found on line %ld of file `%s'.", error_lineno, octstr_get_cstr(cfg->filename)); return -1; } return 0; }