From 11288bfbc896042e9dc8dcc1a50024fdb6f8f513 Mon Sep 17 00:00:00 2001 From: Nathan Rajlich Date: Fri, 6 Oct 2023 23:05:03 -0300 Subject: [PATCH] Delete the `leaks.txt` file when empty --- .changeset/fifty-rabbits-tell.md | 5 +++++ source/main.c | 38 +++++++++++++++++++++++++++++++- 2 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 .changeset/fifty-rabbits-tell.md diff --git a/.changeset/fifty-rabbits-tell.md b/.changeset/fifty-rabbits-tell.md new file mode 100644 index 00000000..f75b8bde --- /dev/null +++ b/.changeset/fifty-rabbits-tell.md @@ -0,0 +1,5 @@ +--- +'nxjs-runtime': patch +--- + +Delete the `leaks.txt` file when empty diff --git a/source/main.c b/source/main.c index 8694df83..c6441608 100644 --- a/source/main.c +++ b/source/main.c @@ -121,6 +121,39 @@ uint8_t *read_file(const char *filename, size_t *out_size) return buffer; } +bool delete_if_empty(const char *filename) +{ + FILE *file = fopen(filename, "rb"); + if (!file) + { + return false; + } + + // Seek to the end of the file + fseek(file, 0, SEEK_END); + + // Get the file size + long size = ftell(file); + + // Close the file + fclose(file); + + // If the file is empty, delete it + if (size == 0) + { + if (remove(filename) == 0) + { + return true; + } + else + { + return false; + } + } + + return true; +} + static int is_running = 1; static JSValue js_exit(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv) @@ -638,7 +671,7 @@ int main(int argc, char *argv[]) } } - FILE* leaks_fd = freopen("leaks.txt", "w", stdout); + FILE *leaks_fd = freopen("leaks.txt", "w", stdout); JS_FreeValue(ctx, switch_dispatch_func); JS_FreeValue(ctx, native_obj); @@ -666,5 +699,8 @@ int main(int argc, char *argv[]) fflush(leaks_fd); fclose(leaks_fd); + /* If no leaks were detected then delete the log file */ + delete_if_empty("leaks.txt"); + return 0; }