int extract_message_and_get_prev(char *curr_id) { //fprintf(stdout,"In extract message\n"); char directory_navigator[BEARGIT_LENGTH + COMMIT_ID_SIZE + 6]; sprintf(directory_navigator, ".beargit/%s/.msg", curr_id); if (access(directory_navigator, F_OK) == -1){ // fprintf(stdout, "in if\n"); return 1; } fprintf(stdout, "commit %s\n", curr_id); char message[MSG_SIZE]; read_string_from_file(directory_navigator, message, sizeof(message)); fprintf(stdout," %s", message); fprintf(stdout,"\n\n"); sprintf(directory_navigator, ".beargit/%s/.prev", curr_id); //fprintf(stdout, "*********BEFORE-SWITCH ************\n%s\n", curr_id); read_string_from_file(directory_navigator,curr_id, COMMIT_ID_SIZE); //fprintf(stdout,"*********AFTER-SWITCH ************\n%s\n", curr_id); return 0; }
static int ubi_volume_init(struct volume *v) { struct ubi_volume *p = container_of(v, struct ubi_volume, v); char voldir[BUFLEN], voldev[BUFLEN], *volname; unsigned int volsize; snprintf(voldir, sizeof(voldir), "%s/ubi%u/ubi%u_%u", ubi_dir_name, p->ubi_num, p->ubi_num, p->ubi_volid); snprintf(voldev, sizeof(voldev), "/dev/ubi%u_%u", p->ubi_num, p->ubi_volid); volname = read_string_from_file(voldir, "name"); if (!volname) return -1; if (read_uint_from_file(voldir, "data_bytes", &volsize)) return -1; v->name = volname; v->type = UBIVOLUME; v->size = volsize; v->blk = strdup(voldev); return 0; }
/* Returns 0 if passwords match. Returns -1 if password does not match stored hash Returns 1 if password file cannot be opened */ int verify_password_from_file(char * password){ FILE * password_file = fopen(PASSWORD_FILE, "r"); if (NULL != password_file){ // Grab the base64 encoded hash from the passwords file char stored_hash[BCRYPT_HASHSIZE]; read_string_from_file(stored_hash, BCRYPT_HASHSIZE, password_file, NULL); fclose(password_file); return bcrypt_checkpw(password, stored_hash); // // // Convert base 64 encoded PW hash into binary string // int stored_hash_num_bytes = Base64decode_len(stored_hash); // char * stored_hash_bytes = (char *) calloc(stored_hash_num_bytes, sizeof(char)); // Base64decode(stored_hash_bytes, stored_hash); // // // Hash the user's submitted password // unsigned char hash[crypto_hash_BYTES]; // crypto_hash(hash, (unsigned char *) password, strlen(password)); // // // Compare hashes using constant time function to prevent timing attacks // return crypto_verify_32((unsigned char *) stored_hash_bytes, hash); } return 1; }
int beargit_log(int limit) { /* COMPLETE THE REST */ char commit_id[COMMIT_ID_SIZE]; read_string_from_file(".beargit/.prev", commit_id, COMMIT_ID_SIZE); if (strcmp(init_id, commit_id) == 0) { // if there has been no commits // throw error fprintf(stderr, "ERROR: There are no commits!\n"); return 1; } else { // if there has been commits if (limit > 0) { // if limit is greater than 0 // print limit amount char prev_commit[COMMIT_ID_SIZE]; // read the prev_commit from .prev read_string_from_file(".beargit/.prev", prev_commit, COMMIT_ID_SIZE); // while limit != 0, and prev_commit != 40s 0 // //printf("\n"); while ((limit != 0) && (strcmp(init_id, prev_commit) != 0)) { limit--; // limit down by 1 printf("\ncommit %s\n", prev_commit); char prev_msg_dir[FILENAME_SIZE]; strcpy(prev_msg_dir, ".beargit/"); strcat(prev_msg_dir, prev_commit); // .beargit/<prev_id> strcat(prev_msg_dir, "/.msg"); // .beargit/<prev_id>/.msg char prev_msg[MSG_SIZE]; read_string_from_file(prev_msg_dir, prev_msg, MSG_SIZE); printf(" %s", prev_msg); char temp_prev_commit[FILENAME_SIZE]; strcpy(temp_prev_commit, ".beargit/"); // .beargit/ strcat(temp_prev_commit, prev_commit); // .beargit/<prev_id> strcat(temp_prev_commit, "/.prev"); // .beargit/<previ_id>/.prev // overwrite prev_commit with .prev from its .prev read_string_from_file(temp_prev_commit, prev_commit, COMMIT_ID_SIZE); printf("\n"); } printf("\n"); } } return 0; }
int is_HEAD() { char HEAD_STRING[BRANCHNAME_SIZE]; read_string_from_file(".beargit/.current_branch", HEAD_STRING, BRANCHNAME_SIZE); int i = strlen(HEAD_STRING); if (i != 0) { return 1; } return 0; }
void Config::read(const std::string& config_dir, const std::string& app_name, bool ignoreFileExists) { config_file = get_config_file_full_name(config_dir, app_name); fileExists = file_exists(config_file); if(ignoreFileExists && !fileExists) return; auto json = read_string_from_file(config_file); if(json=="") return; setting = parse_json_settings(json); Config::httpConf.host = setting["kodiHostIp"]; Config::httpConf.port = setting["port"]; Config::httpConf.timeout = (uint_fast32_t) std::stoi(Config::setting["httpTimeOutMs"]); }
/* * Brings memory block online/offline using the sysfs memory-hotplug interface * https://www.kernel.org/doc/Documentation/memory-hotplug.txt */ static int change_memblock_state(uint32_t mem_block_id, mem_state_t new_state) { int status = 0; const char *cmd; mem_state_t cur_state; char buf[BUF_SIZE]; char numa_file_path[BUF_SIZE]; sprintf(numa_file_path, MEMBLK_STATE_PATH_FMT, mem_block_id); status = read_string_from_file(numa_file_path, buf, sizeof(buf)); if (status < 0) goto done; cur_state = !!strstr(buf, STATE_ONLINE) ? NV_IOCTL_NUMA_STATUS_ONLINE : NV_IOCTL_NUMA_STATUS_OFFLINE; if (cur_state == new_state) goto done; switch (new_state) { case NV_IOCTL_NUMA_STATUS_ONLINE: cmd = BRING_ONLINE_CMD; break; case NV_IOCTL_NUMA_STATUS_OFFLINE: cmd = BRING_OFFLINE_CMD; break; default: return -EINVAL; } status = write_string_to_file(numa_file_path, cmd, strlen(cmd)); done: if (status == 0) { SYSLOG_VERBOSE(LOG_DEBUG, "NUMA: Successfully changed memblock state of %s to %s\n", numa_file_path, mem_state_to_string(new_state)); } else { SYSLOG_VERBOSE(LOG_DEBUG, "NUMA: Failed to change state of %s to %s: %s\n", numa_file_path, mem_state_to_string(new_state), strerror(-status)); } return status; }
/** * Reads keys in keyfile if group GN_CLIENT is in that keyfile and fills * options_t *opt structure accordingly. * @param[in,out] opt : options_t * structure to store options read from the * configuration file "filename". * @param keyfile is the GKeyFile structure that is used by glib to read * groups and keys from. * @param filename : the filename of the configuration file to read from */ static void read_from_group_client(options_t *opt, GKeyFile *keyfile, gchar *filename) { gchar *dircache = NULL; gint cmptype = 0; if (keyfile != NULL && filename != NULL && g_key_file_has_group(keyfile, GN_CLIENT) == TRUE) { /* Reading the directory list */ opt->dirname_list = read_list_from_file(keyfile, filename, GN_CLIENT, KN_DIR_LIST, _("Could not load directory list from file")); opt->exclude_list = read_list_from_file(keyfile, filename, GN_CLIENT, KN_EXC_LIST, _("Could not load exclude file list from file")); /* Reading blocksize */ opt->blocksize = read_int64_from_file(keyfile, filename, GN_CLIENT, KN_BLOCK_SIZE, _("Could not load blocksize from file"), CLIENT_BLOCK_SIZE); /* Reading the cache directory if any */ dircache = read_string_from_file(keyfile, filename, GN_CLIENT, KN_CACHE_DIR, _("Could not load directory name")); opt->dircache = normalize_directory(dircache); free_variable(dircache); /* Reading filename of the database if any */ opt->dbname = read_string_from_file(keyfile, filename, GN_CLIENT, KN_DB_NAME, _("Could not load cache database name")); /* Adaptative mode for blocksize ? */ opt->adaptive = read_boolean_from_file(keyfile, filename, GN_CLIENT, KN_ADAPTIVE, _("Could not load adaptive configuration from file.")); /* Scanning option */ opt->noscan = read_boolean_from_file(keyfile, filename, GN_CLIENT, KN_NOSCAN, _("Could not load scan configuration from file.")); /* Buffer size to be used to send data to server */ opt->buffersize = read_int_from_file(keyfile, filename, GN_CLIENT, KN_BUFFER_SIZE, _("Could not load buffersize from file"), CLIENT_MIN_BUFFER); /* Compression type if any */ cmptype = read_int_from_file(keyfile, filename, GN_CLIENT, KN_COMPRESSION_TYPE, _("Compression type not defined in configuration file"), opt->cmptype); set_compression_type(opt, cmptype); } }
void next_commit_id(char* commit_id) { /* COMPLETE THE REST */ char cur_branch[BRANCHNAME_SIZE]; read_string_from_file(".beargit/.current_branch", cur_branch, BRANCHNAME_SIZE); if(!strlen(cur_branch)) { *commit_id = '\0'; } else { char combined[BRANCHNAME_SIZE + COMMIT_ID_SIZE]; sprintf(combined, "%s%s", cur_branch, commit_id); //fprintf(stdout, "Crypto %s \n", commit_id); // fprintf(stdout, "%s\n", cur_branch); cryptohash(combined, commit_id); // fprintf(stdout, "Crypto-After %s \n", commit_id); } }
void next_commit_id(char* commit_id) { char current_branch[BRANCHNAME_SIZE]; read_string_from_file(".beargit/.current_branch", current_branch, BRANCHNAME_SIZE); // The first COMMIT_ID_BRANCH_BYTES=10 characters of the commit ID will // be used to encode the current branch number. This is necessary to avoid // duplicate IDs in different branches, as they can have the same pre- // decessor (so next_commit_id has to depend on something else). int n = get_branch_number(current_branch); for (int i = 0; i < COMMIT_ID_BRANCH_BYTES; i++) { commit_id[i] = digits[n%3]; n /= 3; } next_commit_id_part1(commit_id + COMMIT_ID_BRANCH_BYTES); }
/** * Reads keys in keyfile if groupname is in that keyfile and fills * options_t *opt structure accordingly. * @param[in,out] opt : options_t * structure to store options read from the * configuration file "filename". * @param keyfile is the GKeyFile structure that is used by glib to read * groups and keys from. * @param filename : the filename of the configuration file to read from */ static void read_from_group_server(options_t *opt, GKeyFile *keyfile, gchar *filename) { gint port = 0; if (opt != NULL && keyfile != NULL && filename != NULL && g_key_file_has_group(keyfile, GN_SERVER) == TRUE) { /* Reading the port number if any */ port = read_int_from_file(keyfile, filename, GN_SERVER, KN_SERVER_PORT, _("Could not load server port number from file."), SERVER_PORT); if (port > 1024 && port < 65535) { opt->port = port; } /* Reading IP address of server's host if any */ opt->ip = read_string_from_file(keyfile, filename, GN_SERVER, KN_SERVER_IP, _("Could not load cache database name")); } }
int beargit_log(int limit) { /* COMPLETE THE REST */ int counter = 0; if (!initialCheck()) { fprintf(stderr, "ERROR: There are no commits.\n"); return 1; } else { char prev_commit_id[COMMIT_ID_SIZE]; //fprintf(stdout, "Before while\n"); read_string_from_file(".beargit/.prev",prev_commit_id, sizeof(prev_commit_id)); while ((counter < limit) && !extract_message_and_get_prev(prev_commit_id)) { //fprintf(stdout,"In While\n"); ++counter; } } return 0; }
int beargit_branch() { /* COMPLETE THE REST */ FILE *branch_file = fopen(".beargit/.branches", "r"); char branch[BRANCHNAME_SIZE]; char curr_branch[BRANCHNAME_SIZE]; read_string_from_file(".beargit/.current_branch", curr_branch, sizeof(curr_branch)); //fprintf(stdout,"%s\n", curr_branch); while (fgets(branch, sizeof(branch), branch_file)) { strtok(branch, "\n"); //fprintf(stdout, "%s\n", branch); if ( !strcmp(branch,curr_branch)) { fprintf(stdout, "* %s\n", branch); } else { fprintf(stdout, " %s\n", branch); } } fclose(branch_file); return 0; }
int beargit_merge(const char* arg) { // Get the commit_id or throw an error char commit_id[COMMIT_ID_SIZE]; if (!is_it_a_commit_id(arg)) { if (get_branch_number(arg) == -1) { fprintf(stderr, "ERROR: No branch or commit %s exists.\n", arg); return 1; } char branch_file[FILENAME_SIZE]; snprintf(branch_file, FILENAME_SIZE, ".beargit/.branch_%s", arg); read_string_from_file(branch_file, commit_id, COMMIT_ID_SIZE); } else { snprintf(commit_id, COMMIT_ID_SIZE, "%s", arg); } // Iterate through each line of the commit_id index and determine how you // should copy the index file over /* COMPLETE THE REST */ char commit_index[BEARGIT_LENGTH + COMMIT_ID_SIZE + 10]; sprintf(commit_index,".beargit/%s/.index", commit_id); FILE *index = fopen(commit_index, "r"); char file[FILENAME_SIZE]; while(fgets(file, sizeof(file), index)) { strtok(file, "\n"); if(file_in_index(file)){ char confilcted_file[FILENAME_SIZE +COMMIT_ID_SIZE+10]; char file_name_in_commit[BEARGIT_LENGTH + COMMIT_ID_SIZE + FILENAME_SIZE+10]; sprintf(file_name_in_commit, ".beargit/%s/%s", commit_id, file); sprintf(confilcted_file, "%s.%s",file,commit_id); fs_cp(file_name_in_commit, confilcted_file); fprintf(stdout, "%s conflicted copy created\n", file); } else { char file_name_in_commit[BEARGIT_LENGTH + COMMIT_ID_SIZE + FILENAME_SIZE+10]; sprintf(file_name_in_commit, ".beargit/%s/%s", commit_id, file); fs_cp(file_name_in_commit, file); fprintf(stdout, "%s added\n", file); } } fclose(index); return 0; }
int beargit_branch() { /* COMPLETE THE REST */ FILE* findex = fopen(".beargit/.branches", "r"); char line[BRANCHNAME_SIZE]; char curren_branch_name [BRANCHNAME_SIZE]; read_string_from_file(".beargit/.current_branch", curren_branch_name, BRANCHNAME_SIZE); while(fgets(line, sizeof(line), findex)) { strtok(line, "\n"); /* Split string to files */ if (!strcmp(line, curren_branch_name)) { printf("* %s\n", line); } else { printf(" %s\n", line); } } fclose(findex); return 0; }
static int ubi_volume_match(struct volume *v, char *name, int ubi_num, int volid) { char voldir[BUFLEN], volblkdev[BUFLEN], *volname; struct ubi_priv *p; snprintf(voldir, sizeof(voldir), "%s/ubi%u/ubi%u_%u", ubi_dir_name, ubi_num, ubi_num, volid); snprintf(volblkdev, sizeof(volblkdev), "/dev/ubiblock%u_%u", ubi_num, volid); /* skip if ubiblock device exists */ if (test_open(volblkdev)) return -1; /* todo: skip existing gluebi device for legacy support */ volname = read_string_from_file(voldir, "name"); if (!volname) { ULOG_ERR("Couldn't read %s/name\n", voldir); return -1; } if (strncmp(name, volname, strlen(volname) + 1)) return -1; p = calloc(1, sizeof(struct ubi_priv)); if (!p) return -1; v->priv = p; v->drv = &ubi_driver; p->ubi_num = ubi_num; p->ubi_volid = volid; return ubi_volume_init(v); }
int beargit_branch() { /* COMPLETE THE REST */ FILE* fbranches = fopen(".beargit/.branches", "r"); //FILE* current_branch = fopen("./beargit/.current_branch", "r"); char line[FILENAME_SIZE]; char current[BRANCHNAME_SIZE]; printf("%s",line); read_string_from_file(".beargit/.current_branch", current, BRANCHNAME_SIZE); while(fgets(line, sizeof(line), fbranches)){ ///////////////////// strtok(line, "\n"); if(strcmp(line, current) == 0){ printf("* %s\n", current); } else{ printf(" %s\n", line); } } fclose(fbranches); //fclose(current_branch); return 0; }
int beargit_commit(const char* msg) { if (!is_commit_msg_ok(msg)) { fprintf(stderr, "ERROR: Message must contain \"%s\"\n", go_bears); return 1; } char commit_id[COMMIT_ID_SIZE]; read_string_from_file(".beargit/.prev", commit_id, COMMIT_ID_SIZE); next_commit_id(commit_id); //fprintf(stdout, "Before Kostya's mistake"); /* COMPLETE THE REST */ if(*commit_id == '\0'){ fprintf(stderr, "ERROR: Need to be on HEAD of a branch to commit.\n"); return 1; } char bear_directory[BEARGIT_LENGTH + COMMIT_ID_SIZE]; char fileNames[FILENAME_SIZE]; sprintf(bear_directory, ".beargit/%s", commit_id); fs_mkdir(bear_directory); copy_files(".beargit/.index", bear_directory, 1); copy_files(".beargit/.prev", bear_directory, 1); FILE *tracked_files = fopen(".beargit/.index", "r"); while(fgets(fileNames,sizeof(fileNames), tracked_files)) { strtok(fileNames, "\n"); copy_files(fileNames, bear_directory, 0); } sprintf(fileNames,"%s/.msg", bear_directory); write_string_to_file(fileNames,msg); write_string_to_file(".beargit/.prev", commit_id); fclose(tracked_files); return 0; }
//This suite tests merging multiple files. It first tests for error cases, then //it tests for non-conflicting files then test for conflicted files. void merge_test(void) { struct commit* commit_list = NULL; int retval; retval = beargit_init(); CU_ASSERT(0==retval); write_string_to_file("wug.txt", "Dis a wug"); write_string_to_file("wug1.txt", "Dis a wug 1.5"); write_string_to_file("wug2.txt", "Der two wugz"); write_string_to_file("wug3.txt", "Dis not a wug"); retval = beargit_add("wug.txt"); CU_ASSERT(0==retval); retval = beargit_add("wug1.txt"); CU_ASSERT(0==retval); run_commit(&commit_list, "THIS IS BEAR TERRITORY!1"); //save commit1's ID char commit1_id[512] = ""; read_string_from_file(".beargit/.prev", commit1_id, 512); retval = beargit_add("wug2.txt"); CU_ASSERT(0==retval); run_commit(&commit_list, "THIS IS BEAR TERRITORY!2"); //remove wug, wug1 and wug2 fs_rm("wug.txt"); retval = beargit_rm("wug.txt"); CU_ASSERT(0==retval); fs_rm("wug1.txt"); retval = beargit_rm("wug1.txt"); CU_ASSERT(0==retval); fs_rm("wug2.txt"); retval = beargit_rm("wug2.txt"); CU_ASSERT(0==retval); //add wug3 & commit retval = beargit_add("wug3.txt"); CU_ASSERT(0==retval); run_commit(&commit_list, "THIS IS BEAR TERRITORY!3"); //Check for err msg when merging non existent commit ID / brench retval = beargit_merge("alskdfjlaskfj"); CU_ASSERT(1==retval); //first line of stderr: "ERROR: No branch or commit alskdfjlaskfj exists." //test merging non-conflicted files retval = beargit_merge(commit1_id); CU_ASSERT(0==retval); //first line of stdout: "wug.txt added\nwug1.txt added\n" //check files actually moved FILE* fstdout_wug = fopen("wug.txt", "r"); CU_ASSERT_PTR_NOT_NULL(fstdout_wug); FILE* fstdout_wug1 = fopen("wug1.txt", "r"); CU_ASSERT_PTR_NOT_NULL(fstdout_wug1); fclose(fstdout_wug); fclose(fstdout_wug1); //test merging conflicted files retval = beargit_merge(commit1_id); CU_ASSERT(0==retval); //first line of stdout: "wug.txt conflicted copy created \nwug1.txt conflicted copy created\n" //check conflicted files created char wugc[512] = "wug.txt."; strcat(wugc, commit1_id); char wugc1[512] = "wug1.txt."; strcat(wugc1, commit1_id); FILE* fstdout_wugc = fopen(wugc, "r"); CU_ASSERT_PTR_NOT_NULL(fstdout_wugc); FILE* fstdout_wugc1 = fopen(wugc1, "r"); CU_ASSERT_PTR_NOT_NULL(fstdout_wugc1); fclose(fstdout_wugc); fclose(fstdout_wugc1); //Read stderr file char PostToBe4Err[512] = "ERROR: No branch or commit alskdfjlaskfj exists.\n"; char ActualErr[512] =""; read_string_from_file("TEST_STDERR", ActualErr, 512); CU_ASSERT(strcmp(PostToBe4Err, ActualErr) == 0); // read stdout char PostToBe[512] = "wug.txt added\nwug1.txt added\nwug.txt conflicted copy created\nwug1.txt conflicted copy created\n"; char Actual[512] =""; read_string_from_file("TEST_STDOUT", Actual, 512); CU_ASSERT(strcmp(PostToBe, Actual) == 0); free_commit_list(&commit_list); }
static void start_element( GMarkupParseContext *ctx, const gchar *tag, const gchar **attr_names, const gchar **attr_values, gpointer ud, GError **error) { parse_data_t *pd = (parse_data_t*)ud; union { gint id; gpointer pid; } id; gint ii; // Check to see if the first element found has been closed // If so, ignore any junk following it. if (pd->closed_top) return; for (ii = 0; ii < TAG_MAP_SZ; ii++) { if (strcmp(tag, tag_map[ii].tag) == 0) { id.id = tag_map[ii].id; break; } } if (ii == TAG_MAP_SZ) { g_warning("Unrecognized start tag (%s)", tag); return; } g_queue_push_head(pd->tag_stack, id.pid); GType gtype = 0; GValue *gval = NULL; GValue *current = g_queue_peek_head(pd->stack); switch (id.id) { case R_SECTION: { const gchar *name; name = lookup_attr_value("name", attr_names, attr_values); if (name && strcmp(name, "icons") == 0) { gval = ghb_dict_value_new(); if (pd->key) g_free(pd->key); pd->key = g_strdup(name); g_queue_push_head(pd->stack, gval); } } break; case R_ICON: { gchar *filename; const gchar *name; name = lookup_attr_value("file", attr_names, attr_values); filename = find_file(inc_list, name); name = lookup_attr_value("name", attr_names, attr_values); if (filename && name) { ghb_rawdata_t *rd; GdkPixbuf *pb; GError *err = NULL; pb = gdk_pixbuf_new_from_file(filename, &err); if (pb == NULL) { g_warning("Failed to open icon file %s: %s", filename, err->message); break; } gval = ghb_dict_value_new(); int colorspace = gdk_pixbuf_get_colorspace(pb); gboolean alpha = gdk_pixbuf_get_has_alpha(pb); int width = gdk_pixbuf_get_width(pb); int height = gdk_pixbuf_get_height(pb); int bps = gdk_pixbuf_get_bits_per_sample(pb); int rowstride = gdk_pixbuf_get_rowstride(pb); ghb_dict_insert(gval, g_strdup("colorspace"), ghb_int_value_new(colorspace)); ghb_dict_insert(gval, g_strdup("alpha"), ghb_boolean_value_new(alpha)); ghb_dict_insert(gval, g_strdup("width"), ghb_int_value_new(width)); ghb_dict_insert(gval, g_strdup("height"), ghb_int_value_new(height)); ghb_dict_insert(gval, g_strdup("bps"), ghb_int_value_new(bps)); ghb_dict_insert(gval, g_strdup("rowstride"), ghb_int_value_new(rowstride)); rd = g_malloc(sizeof(ghb_rawdata_t)); rd->data = gdk_pixbuf_get_pixels(pb); rd->size = height * rowstride * bps / 8; GValue *data = ghb_rawdata_value_new(rd); ghb_dict_insert(gval, g_strdup("data"), data); if (pd->key) g_free(pd->key); pd->key = g_strdup(name); g_free(filename); } else { g_warning("%s:missing a requried attribute", name); exit(EXIT_FAILURE); } } break; case R_PLIST: { gchar *filename; const gchar *name; name = lookup_attr_value("file", attr_names, attr_values); filename = find_file(inc_list, name); name = lookup_attr_value("name", attr_names, attr_values); if (filename && name) { gval = ghb_plist_parse_file(filename); if (pd->key) g_free(pd->key); pd->key = g_strdup(name); g_free(filename); } else { g_warning("%s:missing a requried attribute", name); exit(EXIT_FAILURE); } } break; case R_STRING: { gchar *filename; const gchar *name; name = lookup_attr_value("file", attr_names, attr_values); filename = find_file(inc_list, name); name = lookup_attr_value("name", attr_names, attr_values); if (filename && name) { gval = read_string_from_file(filename); if (pd->key) g_free(pd->key); pd->key = g_strdup(name); g_free(filename); } else { g_warning("%s:missing a requried attribute", name); exit(EXIT_FAILURE); } } break; } // Add the element to the current container if (gval) { // There's an element to add if (current == NULL) { pd->plist = gval; return; } gtype = G_VALUE_TYPE(current); if (gtype == ghb_array_get_type()) { ghb_array_append(current, gval); } else if (gtype == ghb_dict_get_type()) { if (pd->key == NULL) { g_warning("No key for dictionary item"); ghb_value_free(gval); } else { ghb_dict_insert(current, g_strdup(pd->key), gval); } } else { g_error("Invalid container type. This shouldn't happen"); } } }
//this test tests reset. It checks for the two erroring cases //It checks if reset replaces the version of the file in the //working directory with the specified file for reset. //it also tests if index is update and file is sucessfully //reset if the file is not in the working directory. void reset_test(void) { struct commit* commit_list = NULL; int retval; retval = beargit_init(); CU_ASSERT(0==retval); write_string_to_file("wug.txt", "Dis a wug"); write_string_to_file("wug2.txt", "Der two wugz"); write_string_to_file("wug3.txt", "Dis not a wug"); retval = beargit_add("wug.txt"); CU_ASSERT(0==retval); run_commit(&commit_list, "THIS IS BEAR TERRITORY!1"); retval = beargit_add("wug2.txt"); CU_ASSERT(0==retval); run_commit(&commit_list, "THIS IS BEAR TERRITORY!2"); retval = beargit_add("wug3.txt"); CU_ASSERT(0==retval); run_commit(&commit_list, "THIS IS BEAR TERRITORY!3"); char commit_id[512] = ""; read_string_from_file(".beargit/.prev", commit_id, 512); //Check for error if commit doens't exit retval = beargit_reset("alskdfjlaskfj", "wug.txt"); CU_ASSERT(1==retval); //first line of stderr: "ERROR: Commit alskdfjlaskfj does not exist.\n" //Check for error if file not in commit retval = beargit_reset(commit_id, "wug6.txt"); CU_ASSERT(1==retval); //second line of stderr: "ERROR: wug6.txt is not in the index of commit [insert commit_id].\n" //modifies wug.txt in working directory write_string_to_file("wug.txt", "JK dis not a wug"); //Resets wug.txt from commit 3 retval = beargit_reset(commit_id, "wug.txt"); CU_ASSERT(0==retval); char stuffinwug[512] = ""; read_string_from_file("wug.txt", stuffinwug, 512); CU_ASSERT(strcmp(stuffinwug, "Dis a wug") == 0); //remove wug3.txt fs_rm("wug3.txt"); retval = beargit_rm("wug3.txt"); CU_ASSERT(retval == 0); FILE* fstdout_wug = fopen("wug3.txt", "r"); CU_ASSERT_PTR_NULL(fstdout_wug); //try to reset wug3.txt retval = beargit_reset(commit_id, "wug3.txt"); FILE* check_wug_exist = fopen("wug3.txt", "r"); CU_ASSERT_PTR_NOT_NULL(check_wug_exist); char check_index_4wug[512] = ""; read_string_from_file(".beargit/.index", check_index_4wug, 512); char PostToBe[512] = "wug2.txt\nwug.txt\nwug3.txt\n"; CU_ASSERT(strcmp(check_index_4wug, PostToBe) == 0); fclose(check_wug_exist); //Read stderr file char PostToBe4Err[512] = ""; strcat(PostToBe4Err, "ERROR: Commit alskdfjlaskfj does not exist.\nERROR: wug6.txt is not in the index of commit "); strcat(PostToBe4Err, commit_id); strcat(PostToBe4Err, ".\n"); char ActualErr[512] =""; read_string_from_file("TEST_STDERR", ActualErr, 512); CU_ASSERT(strcmp(PostToBe4Err, ActualErr) == 0); free_commit_list(&commit_list); }
int beargit_commit(const char* msg) { if (!is_commit_msg_ok(msg)) { fprintf(stderr, "ERROR: Message must contain \"%s\"\n", go_bears); return 1; } //////////////////////// FIX char curr[BRANCHNAME_SIZE]; read_string_from_file(".beargit/.current_branch", curr, BRANCHNAME_SIZE); // if (strlen(curr) == 0) { // fprintf(stderr,"ERROR: Need to be on HEAD of a branch to commit\n"); // return 1; // } int check = 1; FILE* fbranches = fopen(".beargit/.branches", "r"); char line[FILENAME_SIZE]; while(fgets(line, sizeof(line), fbranches)) { strtok(line, "\n"); if (strcmp(line, curr) == 0) { check = 0; break; } } fclose(fbranches); if (strlen(curr) == 0 || check == 1) { fprintf(stderr,"ERROR: Need to be on HEAD of a branch to commit\n"); return 1; } char commit_id[COMMIT_ID_SIZE]; read_string_from_file(".beargit/.prev", commit_id, COMMIT_ID_SIZE); next_commit_id(commit_id); /* COMPLETE THE REST */ // beargitID char beargitID[FILENAME_SIZE]; strcpy(beargitID, ".beargit/"); strcat(beargitID, commit_id); // .beargit/commit_id char temp[FILENAME_SIZE]; strcpy(temp, beargitID); strcat(temp, "/"); // .beargit/commit_id/ fs_mkdir(beargitID); // FILE* yohoho = fopen(beargitID, "w"); char bgIndex[FILENAME_SIZE]; char bgCommitMSG[FILENAME_SIZE]; strcpy(bgIndex, beargitID); strcpy(bgCommitMSG, beargitID); strcat(bgIndex, "/.index"); // .beargit/commit_id/.prev strcat(beargitID, "/.prev"); // .beargit/commit_id/.index strcat(bgCommitMSG, "/.msg"); // .beargit/commit_id/.msg // copy from .beargit/.prev to .beargit/commit_id/.prev fs_cp(".beargit/.prev", beargitID); // copy from .beargit/.prev to .beargit/commit_id/.prev fs_cp(".beargit/.index", bgIndex); copyTo(".beargit/.index", temp); write_string_to_file(bgCommitMSG, msg); // printf(">>> msg saved as %s\n", bgCommitMSG); write_string_to_file(".beargit/.prev", commit_id); // printf(">>> id saved as %s\n", commit_id); return 0; }
static int check_memory_auto_online(uint32_t node_id, NvCfgBool *auto_online_success) { DIR *dir_ptr; int status = 0; struct dirent *dir_entry; char read_buf[BUF_SIZE]; char numa_file_path[BUF_SIZE]; char memory_file_path[BUF_SIZE]; int num_memory_node_in_dir = 0; int num_memory_online_movable = 0; *auto_online_success = NVCFG_FALSE; sprintf(numa_file_path, NID_PATH_FMT, node_id); dir_ptr = opendir(numa_file_path); if (!dir_ptr) { syslog(LOG_ERR, "NUMA: Failed to open directory %s: %s\n", numa_file_path, strerror(errno)); return -errno; } /* Iterate through the node directory */ while ((dir_entry = readdir(dir_ptr)) != NULL) { uint32_t block_id; /* Skip entries that are not a memory node */ if (get_memblock_id_from_dirname(dir_entry->d_name, &block_id) < 0) { continue; } num_memory_node_in_dir++; sprintf(memory_file_path, MEMBLK_STATE_PATH_FMT, block_id); status = read_string_from_file(memory_file_path, read_buf, sizeof(read_buf)); if (status < 0) { syslog(LOG_ERR, "NUMA: Failed to read %s state\n", dir_entry->d_name); goto cleanup; } /* Check if state has already been auto onlined */ if (strstr(read_buf, STATE_ONLINE)) { SYSLOG_VERBOSE(LOG_NOTICE, "NUMA: Device NUMA memory is already online\n"); sprintf(memory_file_path, MEMBLK_VALID_ZONES_PATH_FMT, block_id); status = read_string_from_file(memory_file_path, read_buf, sizeof(read_buf)); if (status < 0) { syslog(LOG_ERR, "NUMA: Failed to read %s valid_zones\n", dir_entry->d_name); goto cleanup; } /* If memory was auto-onlined, check if valid_zones is Movable */ if (strstr(read_buf, VALID_MOVABLE_STATE) != read_buf) { syslog(LOG_NOTICE, MEMORY_AUTO_ONLINE_WARNING_FMT, dir_entry->d_name, read_buf); status = -ENOTSUP; break; } else { num_memory_online_movable++; } } } /* Check if any memory nodes exist */ if (num_memory_node_in_dir == 0) { syslog(LOG_ERR, "NUMA: No memory nodes in node%d directory!\n", node_id); status = -ENOENT; goto cleanup; } /* Check if all the memory are set to online movable */ if (num_memory_online_movable == num_memory_node_in_dir) { *auto_online_success = NVCFG_TRUE; } cleanup: closedir(dir_ptr); return status; }
int beargit_log(int limit) { if (limit == 0) { return 0; } int logged = 0; /* COMPLETE THE REST */ // printf(">>default limit num of log: %d\n", limit); // 0000000000000000000000000000000000000000 char cm_id[COMMIT_ID_SIZE]; char cmMsg[MSG_SIZE]; read_string_from_file(".beargit/.prev", cm_id, COMMIT_ID_SIZE); // printf("%s\n%s",d, cm_id); if (strcmp(cm_id, d) == 0) { fprintf(stderr, "ERROR: There are no commits!\n"); return 1; } char MSG_index[FILENAME_SIZE]; strcpy(MSG_index, ".beargit/"); strcat(MSG_index, cm_id); strcat(MSG_index, "/.msg"); read_string_from_file(MSG_index, cmMsg, MSG_SIZE); printf("\n"); printf("commit %s\n %s\n", cm_id, cmMsg); logged++; // int i = 3; while (logged < limit) { // printf("%d < %d ? \n", logged, limit); // init char new_dir[FILENAME_SIZE]; char msgLama[FILENAME_SIZE]; // dir for commit id strcpy(new_dir, ".beargit/"); strcat(new_dir, cm_id); strcat(new_dir, "/.prev"); // printf(">> .beargit/id/.prev \n>>%s\n", new_dir); // free cm_id free_char_p(cm_id); read_string_from_file(new_dir, cm_id, COMMIT_ID_SIZE); free_char_p(new_dir); /* .beargit/commit_id/.msg */ strcpy(msgLama, ".beargit/"); strcat(msgLama, cm_id); strcat(msgLama, "/.msg"); if (strcmp(cm_id, d) == 0) { break; } read_string_from_file(msgLama, cmMsg, MSG_SIZE); printf("\ncommit %s\n %s\n", cm_id, cmMsg); logged++; } printf("\n"); return 0; }
int beargit_commit(const char* msg) { if (!is_commit_msg_ok(msg)) { // checking if true for go_bears // if not ok, error is thrown fprintf(stderr, "ERROR: Message must contain \"%s\"\n", go_bears); return 1; } if (!is_HEAD()) { fprintf(stderr, "ERROR: Need to be on HEAD of a branch to commit\n"); return 1; } // Read the ID of the previous last commit from .beargit/.prev char commit_id[COMMIT_ID_SIZE]; read_string_from_file(".beargit/.prev", commit_id, COMMIT_ID_SIZE); next_commit_id(commit_id); // getting a new id /* COMPLETE THE REST */ // Generate a new directory .beargit/<newid> and // copy .beargit/.index, .beargit/.prev and all // tracked files into the directory. // Generate a new directory .beargit/<newid> and copy // .beargit/.index, .beargit/.prev and all tracked files // into the directory. char newid_dir[FILENAME_SIZE]; strcpy(newid_dir, ".beargit/"); strcat(newid_dir, commit_id); // newid_dir = .beargit/<newID> fs_mkdir(newid_dir); // making .beargit/<newid> char prev_dir[FILENAME_SIZE]; strcpy(prev_dir, newid_dir); strcat(prev_dir, "/.prev"); // prev_dir = .beargit/<newID>/.prev fs_cp(".beargit/.prev", prev_dir); // copying .beargit/.prev to .beargit/<newID>/.prev char index_dir[FILENAME_SIZE]; strcpy(index_dir, newid_dir); strcat(index_dir, "/.index"); // index_dir = .beargit/<newID>/.index fs_cp(".beargit/.index", index_dir); // copying .beargit/.index to .beargit/<newID>/.index // copy the tracked files into .beargit/<newID> FILE* findex = fopen(".beargit/.index", "r"); char line[FILENAME_SIZE]; while(fgets(line, sizeof(line), findex)) { strtok(line, "\n"); // read one line each run. line = <tracked_file> char track_dir[FILENAME_SIZE]; strcpy(track_dir, newid_dir); // track_dir = .beargit/<newID> strcat(track_dir, "/"); // track_dir = .beargit/<newID>/ strcat(track_dir, line); // track_dir = .beargit/<newID>/line // track_dir is dst char ori_dir[FILENAME_SIZE]; strcpy(ori_dir, line); // ori_dir = line // ori_dir is src fs_cp(ori_dir, track_dir); } fclose(findex); // Store the commit message (<msg>) into .beargit/<newID>/.msg strcat(newid_dir, "/.msg"); // newid_dir = .beargit/<newID>/.msg write_string_to_file(newid_dir, msg); // Write the new ID into .beargit/.prev. write_string_to_file(".beargit/.prev", commit_id); return 0; }
int beargit_checkout(const char* arg, int new_branch) { // Get the current branch char current_branch[BRANCHNAME_SIZE]; read_string_from_file(".beargit/.current_branch", current_branch, BRANCHNAME_SIZE); //MISTAKE 1: the second arguments should be current_branch rather than "current_branch" // If not detached, update the current branch by storing the current HEAD into that branch's file... // Even if we cancel later, this is still ok. if (strlen(current_branch)) { char current_branch_file[BRANCHNAME_SIZE+50]; sprintf(current_branch_file, ".beargit/.branch_%s", current_branch); fs_cp(".beargit/.prev", current_branch_file); } // Check whether the argument is a commit ID. If yes, we just stay in detached mode // without actually having to change into any other branch. if (is_it_a_commit_id(arg)) { char commit_dir[FILENAME_SIZE] = ".beargit/"; strcat(commit_dir, arg); if (!fs_check_dir_exists(commit_dir)) { fprintf(stderr, "ERROR: Commit %s does not exist\n", arg); return 1; } // Set the current branch to none (i.e., detached). write_string_to_file(".beargit/.current_branch", ""); return checkout_commit(arg); } // Just a better name, since we now know the argument is a branch name. const char* branch_name = arg; // Read branches file (giving us the HEAD commit id for that branch). // branch_exists=1: exits // branch_exists=0: not exist int branch_exists = (get_branch_number(branch_name) >= 0); // Check for errors. if (!(!branch_exists || !new_branch)) { fprintf(stderr, "ERROR: A branch named %s already exists\n", branch_name); return 1; //MISTAKE 2: no branch exist error should only been print if new_branch evaluate to false and branch_exist ealuate to false. } else if (!branch_exists && !new_branch) { fprintf(stderr, "ERROR: No branch %s exists\n", branch_name); return 1; } // File for the branch we are changing into. //MISTAKES 3: if use char* branch_file, it is only for reading, can apply strcat //should change to char branch_file [] = " " char branch_file[] = ".beargit/.branch_"; strcat(branch_file, branch_name); // Update the branch file if new branch is created (now it can't go wrong anymore) if (new_branch) { FILE* fbranches = fopen(".beargit/.branches", "a"); fprintf(fbranches, "%s\n", branch_name); fclose(fbranches); fs_cp(".beargit/.prev", branch_file); } write_string_to_file(".beargit/.current_branch", branch_name); // Read the head commit ID of this branch. char branch_head_commit_id[COMMIT_ID_SIZE]; read_string_from_file(branch_file, branch_head_commit_id, COMMIT_ID_SIZE); // Check out the actual commit. return checkout_commit(branch_head_commit_id); }
int beargit_checkout(const char* arg, int new_branch) { // Get the current branch char current_branch[BRANCHNAME_SIZE]; read_string_from_file(".beargit/.current_branch", current_branch, BRANCHNAME_SIZE); // If not detached, leave the current branch by storing the current HEAD into that branch's file... if (strlen(current_branch)) { char current_branch_file[BRANCHNAME_SIZE+50]; sprintf(current_branch_file, ".beargit/.branch_%s", current_branch); fs_cp(".beargit/.prev", current_branch_file); } // Check whether the argument is a commit ID. If yes, we just change to detached mode // without actually having to change into any other branch. if (is_it_a_commit_id(arg)) { char commit_dir[FILENAME_SIZE] = ".beargit/"; strcat(commit_dir, arg); // ...and setting the current branch to none (i.e., detached). write_string_to_file(".beargit/.current_branch", ""); return checkout_commit(arg); } // Read branches file (giving us the HEAD commit id for that branch). int branch_exists = (get_branch_number(arg) >= 0); // Check for errors. if (!(!branch_exists || !new_branch)) { fprintf(stderr, "ERROR: A branch named %s already exists.\n", arg); return 1; } else if (!branch_exists && !new_branch) { fprintf(stderr, "ERROR: No branch or commit %s exists.\n", arg); return 1; } // Just a better name, since we now know the argument is a branch name. const char* branch_name = arg; // File for the branch we are changing into. char branch_file[BRANCHNAME_SIZE + 50] = ".beargit/.branch_"; strcat(branch_file, branch_name); // Update the branch file if new branch is created (now it can't go wrong anymore) if (new_branch) { FILE* fbranches = fopen(".beargit/.branches", "a"); fprintf(fbranches, "%s\n", branch_name); fclose(fbranches); fs_cp(".beargit/.prev", branch_file); } write_string_to_file(".beargit/.current_branch", branch_name); // Read the head commit ID of this branch. char branch_head_commit_id[COMMIT_ID_SIZE]; read_string_from_file(branch_file, branch_head_commit_id, COMMIT_ID_SIZE); // Check out the actual commit. return checkout_commit(branch_head_commit_id); }
int initialCheck() { char prev_id[COMMIT_ID_SIZE]; read_string_from_file(".beargit/.prev",prev_id, sizeof(prev_id)); return strcmp(prev_id, "0000000000000000000000000000000000000000"); }