/* read in the summary file from dir */ static int scr_summary_read_v6(const scr_path* dir, scr_hash* summary_hash) { /* check that we got a pointer to a hash */ if (summary_hash == NULL) { return SCR_FAILURE; } /* assume that we'll fail */ int rc = SCR_FAILURE; /* build the summary filename */ scr_path* summary_path = scr_path_dup(dir); scr_path_append_str(summary_path, ".scr"); scr_path_append_str(summary_path, "summary.scr"); char* summary_file = scr_path_strdup(summary_path); /* check whether we can read the file before we actually try, * we take this step to avoid printing an error in scr_hash_read */ if (scr_file_is_readable(summary_file) != SCR_SUCCESS) { goto cleanup; } /* read in the summary hash file */ if (scr_hash_read(summary_file, summary_hash) != SCR_SUCCESS) { scr_err("Reading summary file %s @ %s:%d", summary_file, __FILE__, __LINE__ ); goto cleanup; } /* read the version from the summary hash */ int version; if (scr_hash_util_get_int(summary_hash, SCR_SUMMARY_KEY_VERSION, &version) != SCR_SUCCESS) { scr_err("Failed to read version from summary file %s @ %s:%d", summary_file, __FILE__, __LINE__ ); goto cleanup; } /* check that the version number matches */ if (version != SCR_SUMMARY_FILE_VERSION_6) { scr_err("Summary file %s is version %d instead of version %d @ %s:%d", summary_file, version, SCR_SUMMARY_FILE_VERSION_6, __FILE__, __LINE__ ); goto cleanup; } /* if we made it here, we successfully read the summary file as a hash */ rc = SCR_SUCCESS; cleanup: /* free the summary file string */ scr_free(&summary_file); scr_path_delete(&summary_path); return rc; }
int main (int argc, char *argv[]) { /* process command line arguments */ struct arglist args; if (!process_args(argc, argv, &args)) { return 1; } /* determine the number of bytes we need to hold the full name of the nodes file */ int filelen = snprintf(NULL, 0, "%s/nodes.scr", args.dir); filelen++; /* add one for the terminating NUL char */ /* allocate space to store the filename */ char* file = NULL; if (filelen > 0) { file = (char*) malloc(filelen); } if (file == NULL) { scr_err("%s: Failed to allocate storage to store nodes file name @ %s:%d", PROG, __FILE__, __LINE__ ); return 1; } /* build the full file name */ int n = snprintf(file, filelen, "%s/nodes.scr", args.dir); if (n >= filelen) { scr_err("%s: Flush file name is too long (need %d bytes, %d byte buffer) @ %s:%d", PROG, n, filelen, __FILE__, __LINE__ ); free(file); return 1; } /* assume we'll fail */ int rc = 1; /* create a new hash to hold the file data */ scr_hash* hash = scr_hash_new(); /* read in our nodes file */ if (scr_hash_read(file, hash) != SCR_SUCCESS) { /* failed to read the nodes file */ goto cleanup; } /* lookup the value associated with the NODES key */ char* nodes_str = scr_hash_elem_get_first_val(hash, SCR_NODES_KEY_NODES); if (nodes_str != NULL) { printf("%s\n", nodes_str); rc = 0; } else { printf("0\n"); } cleanup: /* delete the hash holding the nodes file data */ scr_hash_delete(hash); /* free off our file name storage */ if (file != NULL) { free(file); file = NULL; } /* return appropriate exit code */ return rc; }