diff options
author | David Robillard <d@drobilla.net> | 2012-03-08 17:10:55 +0000 |
---|---|---|
committer | David Robillard <d@drobilla.net> | 2012-03-08 17:10:55 +0000 |
commit | b1e37ba0fb6fe83e823140a57cbbe79fd485d86e (patch) | |
tree | 2b60da93ad152383a932031e57aef2da303254e7 | |
parent | 17a7b5714a26d137579c78699aa112431f3a31d2 (diff) | |
download | lilv-b1e37ba0fb6fe83e823140a57cbbe79fd485d86e.tar.gz lilv-b1e37ba0fb6fe83e823140a57cbbe79fd485d86e.tar.bz2 lilv-b1e37ba0fb6fe83e823140a57cbbe79fd485d86e.zip |
Fix memory leaks.
git-svn-id: http://svn.drobilla.net/lad/trunk/lilv@4028 a436a847-0d15-0410-975c-d299462d15a1
-rw-r--r-- | src/util.c | 70 |
1 files changed, 37 insertions, 33 deletions
@@ -539,6 +539,17 @@ lilv_mkdir_p(const char* dir_path) return 0; } +static off_t +lilv_file_size(const char* path) +{ + struct stat buf; + if (stat(path, &buf)) { + LILV_ERRORF("stat(%s) (%s)\n", path, strerror(errno)); + return 0; + } + return buf.st_size; +} + bool lilv_file_equals(const char* a_path, const char* b_path) { @@ -546,46 +557,39 @@ lilv_file_equals(const char* a_path, const char* b_path) return true; // Paths match } + bool match = false; + FILE* a_file = NULL; + FILE* b_file = NULL; char* const a_real = lilv_realpath(a_path); char* const b_real = lilv_realpath(b_path); if (!a_real || !b_real) { - return false; // Missing file matches nothing - } - - if (!strcmp(a_real, b_real)) { - return true; // Real paths match - } - - off_t a_size; - off_t b_size; - lilv_size_mtime(a_path, &a_size, NULL); - lilv_size_mtime(b_path, &b_size, NULL); - if (a_size != b_size) { - return false; // Sizes differ - } - - FILE* a_file = fopen(a_real, "rb"); - if (!a_file) { - return false; // Missing file matches nothing + match = false; // Missing file matches nothing + } else if (!strcmp(a_real, b_real)) { + match = true; // Real paths match + } else if (lilv_file_size(a_path) != lilv_file_size(b_path)) { + match = false; // Sizes differ + } else if (!(a_file = fopen(a_real, "rb"))) { + match = false; // Missing file matches nothing + } else if (!(b_file = fopen(b_real, "rb"))) { + match = false; // Missing file matches nothing + } else { + match = true; + // TODO: Improve performance by reading chunks + while (!feof(a_file) && !feof(b_file)) { + if (fgetc(a_file) != fgetc(b_file)) { + match = false; + break; + } + } } - FILE* b_file = fopen(b_real, "rb"); - if (!b_file) { + if (a_file) { fclose(a_file); - return false; // Missing file matches nothing } - - bool match = true; - - // TODO: Improve performance by reading chunks - while (!feof(a_file) && !feof(b_file)) { - if (fgetc(a_file) != fgetc(b_file)) { - match = false; - break; - } + if (b_file) { + fclose(b_file); } - - fclose(a_file); - fclose(b_file); + free(a_real); + free(b_real); return match; } |