summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJon Bergli Heier <snakebite@jvnv.net>2010-08-16 01:12:28 +0200
committerJon Bergli Heier <snakebite@jvnv.net>2010-08-16 01:12:28 +0200
commita8d7b223e95bc40c65d6dd7838a1656a2203309b (patch)
tree62b1a30a8c7670142341a40e8591f02d4b5fc5a1
parentb4cbca161a1638e96d9e0a6fe12a29ed43173e43 (diff)
Detect files and directories when scanning and handle them accordingly.
-rw-r--r--music.c50
-rw-r--r--music.h2
2 files changed, 42 insertions, 10 deletions
diff --git a/music.c b/music.c
index 2e618bd..09ee7d8 100644
--- a/music.c
+++ b/music.c
@@ -8,6 +8,8 @@ gboolean music_init(const gchar *path) {
music_root = g_new0(struct directory, 1);
music_root->path = g_strdup(path);
+ g_debug("added music root %s", path);
+
return 1;
}
@@ -23,15 +25,45 @@ gboolean music_scan(struct directory *directory) {
const gchar *entry;
while((entry = g_dir_read_name(dir)) != NULL) {
- struct file *f = g_new0(struct file, 1);
- f->name = g_strdup(entry);
-
- if(directory->files == NULL) {
- directory->files = f;
- } else {
- struct file *last = directory->files;
- while(last->next) last = last->next;
- last->next = f;
+ struct stat st;
+ gchar *fullpath = g_build_filename(directory->path, entry, NULL);
+ if(fullpath == NULL) {
+ g_error("g_build_filename returned NULL");
+ }
+
+ if(g_stat(fullpath, &st) < 0) {
+ g_warning("g_stat failed on %s", fullpath);
+ }
+
+ if(S_ISREG(st.st_mode)) {
+ struct file *f = g_new0(struct file, 1);
+ f->name = g_strdup(entry);
+
+ if(directory->files == NULL) {
+ directory->files = f;
+ } else {
+ struct file *last = directory->files;
+ while(last->next) last = last->next;
+ last->next = f;
+ }
+
+ g_debug("added file %s to %s", entry, directory->path);
+ } else if(S_ISDIR(st.st_mode)) {
+ struct directory *d = g_new0(struct directory, 1);
+ d->path = g_build_filename(directory->path, entry, NULL);
+
+ if(directory->sub == NULL) {
+ directory->sub = d;
+ } else {
+ struct directory *last = directory->sub;
+ while(last->next) last = last->next;
+ last->next = d;
+ }
+
+ g_debug("added subdir %s to %s", entry, directory->path);
+
+ g_debug("recursing into %s", entry);
+ music_scan(d);
}
}
diff --git a/music.h b/music.h
index a51204e..e93a8c1 100644
--- a/music.h
+++ b/music.h
@@ -13,7 +13,7 @@ struct file {
};
struct directory {
- char *path;
+ gchar *path;
struct directory *sub, *next;
struct file *files;
};