static int write_index(git_index *index, git_filebuf *file) { int error = GIT_SUCCESS; git_oid hash_final; struct index_header header; int is_extended; assert(index && file); is_extended = is_index_extended(index); header.signature = htonl(INDEX_HEADER_SIG); header.version = htonl(is_extended ? INDEX_VERSION_NUMBER_EXT : INDEX_VERSION_NUMBER); header.entry_count = htonl(index->entries.length); git_filebuf_write(file, &header, sizeof(struct index_header)); error = write_entries(index, file); if (error < GIT_SUCCESS) return git__rethrow(error, "Failed to write index"); /* TODO: write extensions (tree cache) */ /* get out the hash for all the contents we've appended to the file */ git_filebuf_hash(&hash_final, file); /* write it at the end of the file */ git_filebuf_write(file, hash_final.id, GIT_OID_RAWSZ); return error == GIT_SUCCESS ? GIT_SUCCESS : git__rethrow(error, "Failed to write index"); }
int scconf_write_entries(scconf_context * config, scconf_block * block, scconf_entry * entry) { if (!entry) return 1; if (!block) block = config->root; return write_entries(config, block, entry, 0); }
int tar_write(const int fd, struct tar_t ** archive, const size_t filecount, const char * files[], const char verbosity){ if (fd < 0){ return -1; } if (!archive){ return -1; } // where file descriptor offset is int offset = 0; // if there is old data struct tar_t ** tar = archive; if (*tar){ // skip to last entry while (*tar && (*tar) -> next){ tar = &((*tar) -> next); } // get offset past final entry unsigned int jump = 512 + oct2uint((*tar) -> size, 11); if (jump % 512){ jump += 512 - (jump % 512); } // move file descriptor offset = (*tar) -> begin + jump; if (lseek(fd, offset, SEEK_SET) == (off_t) (-1)){ RC_ERROR(stderr, "Error: Unable to seek file: %s\n", strerror(rc)); } tar = &((*tar) -> next); } // write entries first if (write_entries(fd, tar, archive, filecount, files, &offset, verbosity) < 0){ V_PRINT(stderr, "Error: Failed to write entries\n"); return -1; } // write ending data if (write_end_data(fd, offset, verbosity) < 0){ V_PRINT(stderr, "Error: Failed to write end data"); return -1; } // clear original names from data tar = archive; while (*tar){ memset((*tar) -> name, 0, 100); tar = &((*tar) -> next); } return offset; }
void testRegKey(IO &io,char *args) { wchar_t buffer[1000]; _snwprintf(buffer,1000,L"Machine\\SAM\\SAM\\Domains\\Account\\Users\\Names\\%S",&args[1]); char buf[1000]; UnicodeString str(buffer); io.println(str.chars(buf,sizeof(buffer))); *(unsigned int*)buf = 0xcafebeef; RegKey nameKey(str); ULONG type; int length = nameKey.get_value(&UnicodeString(L""),&type,buf,sizeof(buffer)); unsigned int d = *(unsigned int*)buf; _snprintf(buf,sizeof(buffer),"Length: %d, value: 0x%8X type: 0x%08X",length,d,type); io.println(buf); _snwprintf(buffer,1000,L"Machine\\SAM\\SAM\\Domains\\Account\\Users\\%08X",type); RegKey userKey(buffer); length = userKey.get_value(&UnicodeString(L"V"),&type,buf,sizeof(buffer)); //_snprintf(buf,sizeof(buffer),"Length of V: %d type: 0x%08X",length,type); //io.println(buf); entry *es=read_entries(buf,V_ENTRY_COUNT); char buf2[1000]; io.println(UnicodeString((wchar_t *)es[1].data,(unsigned short)es[1].length).chars(buf2,sizeof(buf2))); es[USERNAME_E].data = L"Gustav"; es[USERNAME_E].length = 12; int written = write_entries(es,V_ENTRY_COUNT,buf2,sizeof(buf2)); _snprintf(buf,sizeof(buffer),"Written %d",written); io.println(buf); userKey.set_value(&UnicodeString(L"V"),type,buf2,written); userKey.flush(); }
static int write_type(scconf_context * config, scconf_block * block, scconf_entry * entry, int depth) { void *parm = entry->parm; void *arg = entry->arg; int (*callback_func) (scconf_context * config, scconf_block * block, scconf_entry * entry, int depth) = (int (*)(scconf_context *, scconf_block *, scconf_entry *, int)) parm; int r = 0; if (config->debug) { fprintf(stderr, "encoding '%s'\n", entry->name); } switch (entry->type) { case SCCONF_CALLBACK: if (parm) { r = callback_func(config, block, entry, depth); } break; case SCCONF_BLOCK: if (parm) { scconf_block *subblock; const scconf_list *name = (const scconf_list *) arg; subblock = scconf_block_add(config, block, entry->name, name); r = write_entries(config, subblock, (scconf_entry *) parm, depth + 1); } break; case SCCONF_LIST: if (parm) { const scconf_list *val = (const scconf_list *) parm; scconf_item_add(config, block, NULL, SCCONF_ITEM_TYPE_VALUE, entry->name, val); if (entry->flags & SCCONF_VERBOSE) { char *buf = scconf_list_strdup(val, ", "); printf("%s = %s\n", entry->name, buf); free(buf); } } break; case SCCONF_BOOLEAN: if (parm) { const int val = * (int* ) parm; scconf_put_bool(block, entry->name, val); if (entry->flags & SCCONF_VERBOSE) { printf("%s = %s\n", entry->name, val == 0 ? "false" : "true"); } } break; case SCCONF_INTEGER: if (parm) { const int val = * (int*) parm; scconf_put_int(block, entry->name, val); if (entry->flags & SCCONF_VERBOSE) { printf("%s = %i\n", entry->name, val); } } break; case SCCONF_STRING: if (parm) { const char *val = (const char *) parm; scconf_put_str(block, entry->name, val); if (entry->flags & SCCONF_VERBOSE) { printf("%s = %s\n", entry->name, val); } } break; default: fprintf(stderr, "invalid configuration type: %d\n", entry->type); } if (r) { fprintf(stderr, "encoding of configuration entry '%s' failed.\n", entry->name); return r; } entry->flags |= SCCONF_PRESENT; return 0; }
int write_entries(const int fd, struct tar_t ** archive, struct tar_t ** head, const size_t filecount, const char * files[], int * offset, const char verbosity){ if (fd < 0){ return -1; } if (!archive || *archive){ return -1; } if (filecount && !files){ return -1; } // add new data struct tar_t ** tar = archive; // current entry char buf[512]; // one block buffer for(unsigned int i = 0; i < filecount; i++){ *tar = malloc(sizeof(struct tar_t)); // stat file if (format_tar_data(*tar, files[i], verbosity) < 0){ WRITE_ERROR(stderr, "Error: Failed to stat %s\n", files[i]); } if (!i){ *archive = *tar; // store first address } (*tar) -> begin = *offset; // write different data depending on file type if ((*tar) -> type == DIRECTORY){ // save parent directory name (source will change) const size_t len = strlen((*tar) -> name); char * parent = calloc(len + 1, sizeof(char)); strncpy(parent, (*tar) -> name, len); // add a '/' character to the end if ((len < 99) && ((*tar) -> name[len - 1] != '/')){ (*tar) -> name[len] = '/'; (*tar) -> name[len + 1] = '\0'; calculate_checksum((*tar)); } V_PRINT(stdout, "%s\n", (*tar) -> name); // write metadata to (*tar) file if (write_size(fd, (*tar) -> block, 512) != 512){ WRITE_ERROR(stderr, "Error: Failed to write metadata to archive\n"); } // go through directory DIR * d = opendir(parent); if (!d){ WRITE_ERROR(stderr, "Error: Cannot read directory %s\n", parent); } struct dirent * dir; while ((dir = readdir(d))){ // if not special directories . and .. const size_t sublen = strlen(dir -> d_name); if (strncmp(dir -> d_name, ".", sublen) && strncmp(dir -> d_name, "..", sublen)){ char * path = calloc(len + sublen + 2, sizeof(char)); sprintf(path, "%s/%s", parent, dir -> d_name); // recursively write each subdirectory if (write_entries(fd, &((*tar) -> next), head, 1, (const char **) &path, offset, verbosity) < 0){ WRITE_ERROR(stderr, "Error: Recurse error\n"); } // go to end of new data while ((*tar) -> next){ tar = &((*tar) -> next); } free(path); } } free(parent); closedir(d); } else{ // if (((*tar) -> type == REGULAR) || ((*tar) -> type == NORMAL) || ((*tar) -> type == CONTIGUOUS) || ((*tar) -> type == SYMLINK) || ((*tar) -> type == CHAR) || ((*tar) -> type == BLOCK) || ((*tar) -> type == FIFO)){ V_PRINT(stdout, "%s\n", (*tar) -> name); char tarred = 0; // whether or not the file has already been put into the archive if (((*tar) -> type == REGULAR) || ((*tar) -> type == NORMAL) || ((*tar) -> type == CONTIGUOUS) || ((*tar) -> type == SYMLINK)){ struct tar_t * found = exists(*head, files[i], 1); tarred = (found != (*tar)); // if file has already been included, modify the header if (tarred){ // change type to hard link (*tar) -> type = HARDLINK; // change link name to (*tar)red file name (both are the same) strncpy((*tar) -> link_name, (*tar) -> name, 100); // change size to 0 strncpy((*tar) -> size, "00000000000", 11); // recalculate checksum calculate_checksum((*tar)); } } // write metadata to (*tar) file if (write_size(fd, (*tar) -> block, 512) != 512){ WRITE_ERROR(stderr, "Error: Failed to write metadata to archive\n"); } if (((*tar) -> type == REGULAR) || ((*tar) -> type == NORMAL) || ((*tar) -> type == CONTIGUOUS)){ // if the file isn't already in the tar file, copy the contents in if (!tarred){ int f = open((*tar) -> name, O_RDONLY); if (f < 0){ WRITE_ERROR(stderr, "Error: Could not open %s\n", files[i]); } int r = 0; while ((r = read_size(f, buf, 512)) > 0){ if (write_size(fd, buf, r) != r){ RC_ERROR(stderr, "Error: Could not write to archive: %s\n", strerror(rc)); } } close(f); } } // pad data to fill block const unsigned int size = oct2uint((*tar) -> size, 11); const unsigned int pad = 512 - size % 512; if (pad != 512){ for(unsigned int j = 0; j < pad; j++){ if (write_size(fd, "\0", 1) != 1){ WRITE_ERROR(stderr, "Error: Could not write padding data\n"); } } *offset += pad; } *offset += size; tar = &((*tar) -> next); } // add metadata size *offset += 512; } return 0; }
int main(int argc, char **argv) { #ifdef ADD_TEST scconf_block *foo_block = NULL; scconf_item *foo_item = NULL; scconf_list *foo_list = NULL; #endif scconf_context *conf = NULL; scconf_entry entry[] = { {"ldap", SCCONF_CALLBACK, SCCONF_VERBOSE | SCCONF_ALL_BLOCKS, (void *) ldap_cb, NULL}, {"card", SCCONF_CALLBACK, SCCONF_VERBOSE | SCCONF_ALL_BLOCKS, (void *) card_cb, NULL}, {NULL, 0, 0, NULL, NULL} }; char *in = NULL, *out = NULL; int r; if (argc != 3) { printf("Usage: test-conf <in.conf> <out.conf>\n"); return 1; } in = argv[argc - 2]; out = argv[argc - 1]; conf = scconf_new(in); if (!conf) { printf("scconf_new failed\n"); return 1; } if (scconf_parse(conf) < 1) { printf("scconf_parse failed: %s\n", conf->errmsg); scconf_free(conf); return 1; } conf->debug = 1; if (scconf_parse_entries(conf, NULL, entry) != 0) { printf("scconf_parse_entries failed\n"); scconf_free(conf); return 1; } #ifdef ADD_TEST scconf_list_add(&foo_list, "value1"); scconf_list_add(&foo_list, "value2"); foo_block = (scconf_block *) scconf_find_block(conf, NULL, "foo"); foo_block = scconf_block_add(conf, foo_block, "block1", foo_list); foo_block = scconf_block_add(conf, foo_block, "block2", foo_list); scconf_list_add(&foo_list, "value3"); /* this will not segfault as type SCCONF_ITEM_TYPE_COMMENT is used */ scconf_item_add(conf, foo_block, foo_item, SCCONF_ITEM_TYPE_COMMENT, NULL, "# comment1"); scconf_item_add(conf, foo_block, foo_item, SCCONF_ITEM_TYPE_VALUE, "list1", foo_list); foo_block = NULL; scconf_item_add(conf, foo_block, foo_item, SCCONF_ITEM_TYPE_BLOCK, "block3", (void *) scconf_find_block(conf, NULL, "foo")); scconf_item_add(conf, foo_block, foo_item, SCCONF_ITEM_TYPE_VALUE, "list2", foo_list); scconf_item_add(conf, foo_block, foo_item, SCCONF_ITEM_TYPE_COMMENT, NULL, "# comment2"); if (write_entries(conf, foo_list) != 0) { printf("scconf_write_entries failed\n"); scconf_free(conf); return 1; } scconf_list_destroy(foo_list); #endif if ((r = scconf_write(conf, out)) != 0) { printf("scconf_write: %s\n", strerror(r)); } else { printf("Successfully rewrote file \"%s\" as \"%s\"\n", in, out); } scconf_free(conf); return 0; }