aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorkoekeishiya <aasvi93@hotmail.com>2017-11-26 20:03:00 +0100
committerkoekeishiya <aasvi93@hotmail.com>2017-11-26 20:03:00 +0100
commit4dbc5747881c12b8c31742d29e9128b7b5ea3b10 (patch)
treeeaa41b5a32c8a44467c7618244a7d14f2e6e234d
parent679ae51b23bd936485e1c5c1e868e4dec4995e97 (diff)
downloadskhd-4dbc5747881c12b8c31742d29e9128b7b5ea3b10.tar.gz
skhd-4dbc5747881c12b8c31742d29e9128b7b5ea3b10.zip
#19 hotloader should resolve symlinks
-rw-r--r--src/hotload.c59
-rw-r--r--src/hotload.h2
2 files changed, 54 insertions, 7 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);
+ }
}
}
diff --git a/src/hotload.h b/src/hotload.h
index 5948a21..ad782fb 100644
--- a/src/hotload.h
+++ b/src/hotload.h
@@ -27,6 +27,6 @@ struct hotloader
bool hotloader_begin(struct hotloader *hotloader, hotloader_callback *callback);
void hotloader_end(struct hotloader *hotloader);
-void hotloader_add_file(struct hotloader *hotloader, const char *file);
+void hotloader_add_file(struct hotloader *hotloader, char *file);
#endif