aboutsummaryrefslogtreecommitdiff
path: root/src/hotload.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/hotload.c')
-rw-r--r--src/hotload.c59
1 files changed, 53 insertions, 6 deletions
diff --git a/src/hotload.c b/src/hotload.c
index 8677cc2..4bb0aa0 100644
--- a/src/hotload.c
+++ b/src/hotload.c
@@ -1,4 +1,5 @@
#include "hotload.h"
+#include <sys/stat.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@@ -40,6 +41,40 @@ file_name(const char *file)
return name;
}
+internal char *
+resolve_symlink(char *file, bool *is_symlink, bool *success)
+{
+ struct stat buffer;
+ if (lstat(file, &buffer) != 0) {
+ goto fail;
+ }
+
+ if (S_ISLNK(buffer.st_mode)) {
+ ssize_t size = buffer.st_size + 1;
+ char *result = malloc(size);
+ ssize_t read = readlink(file, result, size);
+
+ if (read == -1) {
+ free(result);
+ goto fail;
+ }
+
+ result[read] = '\0';
+ *is_symlink = true;
+ *success = true;
+ return result;
+ } else {
+ *is_symlink = false;
+ *success = true;
+ return file;
+ }
+
+fail:
+ *success = false;
+ return NULL;
+}
+
+
internal struct watched_file *
hotloader_watched_file(struct hotloader *hotloader, char *absolutepath)
{
@@ -78,14 +113,26 @@ internal FSEVENT_CALLBACK(hotloader_handler)
}
}
-void hotloader_add_file(struct hotloader *hotloader, const char *file)
+void hotloader_add_file(struct hotloader *hotloader, char *file)
{
if (!hotloader->enabled) {
- struct watched_file watch_info;
- watch_info.directory = file_directory(file);
- watch_info.filename = file_name(file);
- hotloader->watch_list[hotloader->watch_count++] = watch_info;
- printf("hotload: watching file '%s' in directory '%s'\n", watch_info.filename, watch_info.directory);
+ bool is_symlink, success;
+ char *real_path = resolve_symlink(file, &is_symlink, &success);
+
+ if (success) {
+ struct watched_file watch_info;
+ watch_info.directory = file_directory(real_path);
+ watch_info.filename = file_name(real_path);
+
+ if (is_symlink) {
+ free(real_path);
+ }
+
+ hotloader->watch_list[hotloader->watch_count++] = watch_info;
+ printf("hotload: watching file '%s' in directory '%s'\n", watch_info.filename, watch_info.directory);
+ } else {
+ fprintf(stderr, "hotload: could not watch file '%s'\n", file);
+ }
}
}