int commit_read (state_t *ur, struct commit *commit, const unsigned char sha1[20]) { int fd = -1; char * buf = NULL; int len; struct tm tm; char *cp; commit->msg = NULL; if (commit->alive) goto error; if ((fd = object_open (ur, sha1)) < -1) goto error; buf = readline (fd); if (buf == NULL) goto error; cp = strptime (buf, "%a %b %d %T %Y", &tm); commit->ctime = mktime (&tm); free (buf); buf = NULL; buf = readline (fd); if (buf == NULL) goto error; hex_to_sha1 (buf, commit->parent_sha1_1); free (buf); buf = NULL; buf = readline (fd); if (buf == NULL) goto error; hex_to_sha1 (buf, commit->parent_sha1_2); free (buf); buf = NULL; buf = readline (fd); if (buf == NULL) goto error; commit->object_type = atoi (buf); free (buf); buf = NULL; buf = readline (fd); if (buf == NULL) goto error; hex_to_sha1 (buf, commit->object_sha1); free (buf); buf = NULL; buf = readline (fd); if (buf == NULL) goto error; len = atoi (buf); free (buf); buf = NULL; commit->msg = (char *) malloc (len); readn (fd, commit->msg, len); close (fd); commit->alive = true; return 0; error: if (commit->msg != NULL) free (commit->msg); return -1; }
int ccnet_session_load_config (CcnetSession *session, const char *config_dir_r) { int ret = 0; char *config_file, *config_dir; char *id = 0, *name = 0, *port_str = 0, *lport_str, *user_name = 0; #ifdef CCNET_SERVER char *service_url; #endif int port, local_port = 0; unsigned char sha1[20]; GKeyFile *key_file; config_dir = ccnet_expand_path (config_dir_r); if (checkdir(config_dir) < 0) { ccnet_error ("Config dir %s does not exist or is not " "a directory.\n", config_dir); return -1; } config_file = g_build_filename (config_dir, SESSION_CONFIG_FILENAME, NULL); key_file = g_key_file_new (); g_key_file_set_list_separator (key_file, ','); if (!g_key_file_load_from_file (key_file, config_file, G_KEY_FILE_KEEP_COMMENTS, NULL)) { ccnet_warning ("Can't load config file %s.\n", config_file); return -1; } id = ccnet_key_file_get_string (key_file, "General", "ID"); user_name = ccnet_key_file_get_string (key_file, "General", "USER_NAME"); name = ccnet_key_file_get_string (key_file, "General", "NAME"); #ifdef CCNET_SERVER service_url = ccnet_key_file_get_string (key_file, "General", "SERVICE_URL"); #endif port_str = ccnet_key_file_get_string (key_file, "Network", "PORT"); lport_str = ccnet_key_file_get_string (key_file, "Client", "PORT"); if (port_str == NULL) port = DEFAULT_PORT; else port = atoi (port_str); if (lport_str != NULL) local_port = atoi (lport_str); if ( (id == NULL) || (strlen (id) != SESSION_ID_LENGTH) || (hex_to_sha1 (id, sha1) < 0) ) { ccnet_error ("Wrong ID\n"); ret = -1; goto onerror; } memcpy (session->base.id, id, 40); session->base.id[40] = '\0'; session->base.name = g_strdup(name); session->base.user_name = g_strdup(user_name); session->base.public_port = port; #ifdef CCNET_SERVER session->base.service_url = g_strdup(service_url); #endif session->config_file = config_file; session->config_dir = config_dir; session->local_port = local_port; session->keyf = key_file; load_rsakey(session); ret = 0; onerror: g_free (id); g_free (name); g_free (user_name); g_free (port_str); #ifdef CCNET_SERVER g_free (service_url); #endif return ret; }
int tree_read (state_t *ur, struct tree *tree, unsigned char sha1[20]) { int fd = -1, i; int blob_cnt = 0; int branch_cnt = 0; char * buf = NULL; char *name = NULL, *branch = NULL; unsigned char commit[20]; struct blob_tree_entry *blob_entry = NULL; struct branch_tree_entry *branch_entry = NULL; tree_init (tree); if ((fd = object_open (ur, sha1)) < -1) goto error; buf = readline (fd); if (buf == NULL) goto error; blob_cnt = atoi (buf); free (buf); buf = NULL; for (i = 0; i < blob_cnt; i ++) { buf = readline (fd); if (buf == NULL) goto error; name = buf; buf = NULL; buf = readline (fd); if (buf == NULL) goto error; hex_to_sha1 (buf, commit); free (buf); buf = NULL; blob_entry = (struct blob_tree_entry *) malloc (sizeof (struct blob_tree_entry)); if (blob_entry == NULL) goto error; blob_entry->name = name; memcpy (blob_entry->commit, commit, 20); list_push_back (&tree->blob_entries, &blob_entry->elem); name = NULL; blob_entry = NULL; } buf = readline (fd); if (buf == NULL) goto error; branch_cnt = atoi (buf); free (buf); buf = NULL; for (i = 0; i < branch_cnt; i ++) { buf = readline (fd); if (buf == NULL) goto error; name = buf; buf = NULL; buf = readline (fd); if (buf == NULL) goto error; branch = buf; buf = NULL; buf = readline (fd); if (buf == NULL) goto error; hex_to_sha1 (buf, commit); free (buf); buf = NULL; branch_entry = (struct branch_tree_entry *) malloc (sizeof (struct branch_tree_entry)); if (branch_entry == NULL) goto error; branch_entry->name = name; branch_entry->branch = branch; memcpy (branch_entry->commit, commit, 20); list_push_back (&tree->branch_entries, &branch_entry->elem); name = NULL; branch = NULL; branch_entry = NULL; } close (fd); return 0; error: if (name != NULL) free (name); if (branch != NULL) free (branch); if (blob_entry != NULL) free (blob_entry); if (branch_entry != NULL) free (branch_entry); tree_destroy (tree); return -1; }