X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?p=elogind.git;a=blobdiff_plain;f=src%2Freadahead%2Freadahead-analyze.c;h=e24b2a32985d041439afc709ffa16e1c3166f828;hp=9c328a5f58f12a5ac21979a8ffbc95902e1ca4d6;hb=268d944e44bea60d5c498cb24a8629f012aac055;hpb=a0bbec1ab0493963bacb5364304fa57afbd14277 diff --git a/src/readahead/readahead-analyze.c b/src/readahead/readahead-analyze.c index 9c328a5f5..e24b2a329 100644 --- a/src/readahead/readahead-analyze.c +++ b/src/readahead/readahead-analyze.c @@ -32,48 +32,46 @@ #include "readahead-common.h" -int main_analyze(const char *pack_path) -{ - char line[1024]; - char path[PATH_MAX]; - FILE *pack; +int main_analyze(const char *pack_path) { + char line[LINE_MAX]; + FILE _cleanup_fclose_ *pack = NULL; int a; int missing = 0; - off_t size; - long tsize = 0; - uint64_t inode; - uint32_t b; - uint32_t c; - struct stat st; + size_t tsize = 0; if (!pack_path) pack_path = "/.readahead"; - pack = fopen(pack_path, "r"); + pack = fopen(pack_path, "re"); if (!pack) { - fprintf(stderr, "Pack file missing\n"); - return EXIT_FAILURE; + log_error("Pack file missing."); + goto fail; } - if (!(fgets(line, sizeof(line), pack))) { - fprintf(stderr, "Pack file corrupt\n"); - return EXIT_FAILURE; + if (!fgets(line, sizeof(line), pack)) { + log_error("Pack file corrupt."); + goto fail; } - if (!strstr(line, READAHEAD_PACK_FILE_VERSION)) { - fprintf(stderr, "Pack file version incompatible with this parser\n"); - return EXIT_FAILURE; + char_array_0(line); + + if (!endswith(line, READAHEAD_PACK_FILE_VERSION)) { + log_error("Pack file version incompatible with this parser."); + goto fail; } if ((a = getc(pack)) == EOF) { - fprintf(stderr, "Pack file corrupt\n"); - return EXIT_FAILURE; + log_error("Pack file corrupt."); + goto fail; } - fprintf(stdout, " pct sections size: path\n"); - fprintf(stdout, " === ======== ====: ====\n"); + fputs(" pct sections size: path\n" + " === ======== ====: ====\n", stdout); - while(true) { + for (;;) { + char path[PATH_MAX]; + struct stat st; + uint64_t inode; int pages = 0; int sections = 0; @@ -83,21 +81,23 @@ int main_analyze(const char *pack_path) path[strlen(path)-1] = 0; if (fread(&inode, sizeof(inode), 1, pack) != 1) { - fprintf(stderr, "Pack file corrupt\n"); - return EXIT_FAILURE; + log_error("Pack file corrupt."); + goto fail; } - while (true) { + for (;;) { + uint32_t b, c; + if (fread(&b, sizeof(b), 1, pack) != 1 || fread(&c, sizeof(c), 1, pack) != 1) { - fprintf(stderr, "Pack file corrupt\n"); - return EXIT_FAILURE; + log_error("Pack file corrupt."); + goto fail; } if ((b == 0) && (c == 0)) break; /* Uncomment this to get all the chunks separately - fprintf(stdout, " %d: %d %d\n", sections, b, c); + printf(" %d: %d %d\n", sections, b, c); */ pages += (c - b); @@ -105,6 +105,8 @@ int main_analyze(const char *pack_path) } if (stat(path, &st) == 0) { + size_t size; + if (sections == 0) size = st.st_size; else @@ -112,13 +114,13 @@ int main_analyze(const char *pack_path) tsize += size; - fprintf(stdout, " %4d%% (%2d) %12ld: %s\n", - sections ? (int)(size / st.st_size * 100.0) : 100, + printf(" %4zd%% (%2d) %12zd: %s\n", + sections && st.st_size ? size * 100 / st.st_size : 100, sections ? sections : 1, - (unsigned long)size, + size, path); } else { - fprintf(stdout, " %4dp (%2d) %12s: %s (MISSING)\n", + printf(" %4dp (%2d) %12s: %s (MISSING)\n", sections ? pages : -1, sections ? sections : 1, "???", @@ -128,10 +130,17 @@ int main_analyze(const char *pack_path) } - fprintf(stdout, "\nHOST: %s", line); - fprintf(stdout, "TYPE: %c\n", a); - fprintf(stdout, "MISSING: %d\n", missing); - fprintf(stdout, "TOTAL: %ld\n", tsize); + printf("\nHOST: %s" + "TYPE: %c\n" + "MISSING: %d\n" + "TOTAL: %zu\n", + line, + a, + missing, + tsize); return EXIT_SUCCESS; + +fail: + return EXIT_FAILURE; }