Skip to content

Commit

Permalink
Delete the leaks.txt file when empty
Browse files Browse the repository at this point in the history
  • Loading branch information
TooTallNate committed Oct 7, 2023
1 parent d433882 commit 11288bf
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/fifty-rabbits-tell.md
@@ -0,0 +1,5 @@
---
'nxjs-runtime': patch
---

Delete the `leaks.txt` file when empty
38 changes: 37 additions & 1 deletion source/main.c
Expand Up @@ -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)
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}

0 comments on commit 11288bf

Please sign in to comment.