]> infiniteadaptability.org Git - seeder/commitdiff
...
authoralex <[email protected]>
Fri, 31 Dec 2021 08:37:20 +0000 (00:37 -0800)
committeralex <[email protected]>
Fri, 31 Dec 2021 08:37:20 +0000 (00:37 -0800)
16 files changed:
Makefile.am
inc/feed.h
inc/fs.h
inc/torrent.h
src/feed.c [deleted file]
src/feed/entries.c [new file with mode: 0644]
src/feed/gen.c [new file with mode: 0644]
src/feed/info.c [new file with mode: 0644]
src/feed/path.c [new file with mode: 0644]
src/fs/dir.c
src/main.c
src/torrent/file.c
src/torrent/path.c
test/unit/Makefile.am
test/unit/test_utils.c
test/unit/torrent.tests.c

index 1442513dd3554e4db15f2d92f359c1869f6dc947..b790ce60fdb618555e7f7da3c337f4755166a7a8 100644 (file)
@@ -17,7 +17,10 @@ seederd_SOURCES = \
        src/bencode/encode.c \
        src/block.c \
        src/default.c \
-       src/feed.c \
+       src/feed/entries.c \
+       src/feed/gen.c \
+       src/feed/info.c \
+       src/feed/path.c \
        src/file.c \
        src/fs/concat.c \
        src/fs/dir.c \
index 09082892701154f214aa97cd22a2bfd28b1d46db..f62e8b5994c3462a537bcebced8e0aad64b8d3ba 100644 (file)
@@ -1,11 +1,15 @@
 #ifndef __FEED_H_
 #define __FEED_H_
 
+#include<meta.h>
 #include<rss.h>
 #include<torrent.h>
 
 #define FEED_DIRECTORY PREFIX "/feeds"
 
-int feed();
+int feeds();
+int feed_entries(FILE*,struct torrent*);
+int feed_info(struct torrent*,struct rss_channel_info*);
+char *feed_path(const char*);
 
 #endif
index e1a83127b288f2bac84fa38398fd6eda1ef5c8da..adc39f9157930b7e94b3a74a82a7fa3d7bdc20a7 100644 (file)
--- a/inc/fs.h
+++ b/inc/fs.h
@@ -8,6 +8,7 @@
 #include<sys/stat.h>
 
 char *concat(const char*,const char*);
+int directory_create(const char*);
 int file_filter_all(const char*);
 int file_filter_ignore_dotfiles(const char*);
 int is_directory(const char*);
index a65a443746525024231864ab146ac7d00b17729a..5eb2f3fc22cb0f63cb5c690b77356e460d5094ba 100644 (file)
@@ -29,13 +29,13 @@ struct torrent {
        int watch_fd;
 };
 
-#define TORRENT_FILE_DIRECTORY "/torrents"
+#define TORRENT_FILE_DIRECTORY PREFIX "/torrents"
 #define TORRENT_FILE_EXTENSION ".torrent"
 
 int torrent_add(struct torrent*,struct file*);
-int torrent_file(const char*,struct torrent*);
+int torrent_file(struct torrent*);
 ssize_t torrent_file_info(struct torrent*,uint8_t**);
-char *torrent_file_path(const char*,unsigned char*,size_t);
+char *torrent_file_path(unsigned char*,size_t);
 int torrent_file_piece_layers(FILE*,struct torrent*);
 void torrent_free(struct torrent*);
 int torrent_init(struct torrent**,const char*,const char*,unsigned long long);
diff --git a/src/feed.c b/src/feed.c
deleted file mode 100644 (file)
index 502ac5d..0000000
+++ /dev/null
@@ -1,70 +0,0 @@
-#include<feed.h>
-
-static int feed_entries(FILE*,struct torrent*);
-static int feed_generate(struct torrent*);
-static int feed_info(struct torrent*,struct rss_channel_info*);
-static char *feed_path(const char*,const char*);
-
-int feed() {
-       for(size_t i=0;i<session.torrent_count;i++) {
-               if(feed_generate(session.torrents[i])<0) { return -1; }
-       }
-
-       return 1;
-}
-
-static int feed_generate(struct torrent *torrent) {
-       FILE *fp;
-       struct rss_channel_info info;
-       char *path;
-
-       path = feed_path(PREFIX FEED_DIRECTORY,torrent->name);
-       if(NULL==path) { return -1; }
-
-       fp = fopen(path,"w");
-       if(NULL==fp) {
-               free(path);
-               return -1;
-       }
-
-       if(torrent_file(PREFIX TORRENT_FILE_DIRECTORY,torrent)<0) { goto clean; }
-
-       if(rss_header(fp)<0) { goto clean; }
-       if(feed_info(torrent,&info)<0) { goto clean; }
-       if(rss_info(fp,&info)<0) { return -1; }
-
-       if(feed_entries(fp,torrent)<0) { goto clean; }
-
-       if(rss_footer(fp)<0) { goto clean; }
-
-       fclose(fp);
-       free(path);
-
-       return 1;
-clean:
-       fclose(fp);
-       remove(path);
-       free(path);
-       return -1;
-}
-
-static int feed_entries(FILE *fp, struct torrent *torrent) {
-       return -1;
-}
-
-static int feed_info(struct torrent *torrent_p, struct rss_channel_info *info) {
-       return -1;
-}
-
-static char *feed_path(const char *directory, const char *name) {
-       return NULL;
-}
-
-/*static int feed_now(char *buf,size_t buf_len) {
-       struct tm now;
-
-       now = *localtime(&(time_t){time(NULL)});
-       if(!(strftime((*timebuf),buf_len, "%a, %d %b %Y %H:%M:%S %z", &now))) { return -1; }
-
-       return 1;
-} */
diff --git a/src/feed/entries.c b/src/feed/entries.c
new file mode 100644 (file)
index 0000000..5f26f16
--- /dev/null
@@ -0,0 +1,61 @@
+#include<feed.h>
+
+static int entries_sort(const void*,const void*);
+
+int feed_entries(FILE *fp, struct torrent *torrent) {
+       struct file *p;
+       struct rss_entry **entries, *entry;
+       size_t count, index;
+
+       count = 0;
+       for(size_t i=0;i<torrent->files.roots->size;i++) {
+               if(torrent->files.roots->map[i]!=NULL) { count++; }
+       }
+
+       entries = malloc(sizeof(struct rss_entry*)*count);
+       if(NULL==entries) { return -1; }
+
+       for(size_t i=0;i<count;i++) {
+               entries[i] = NULL;
+       }
+
+       index = 0;
+       for(size_t i=0;i<torrent->files.roots->size;i++) {
+               p = torrent->files.roots->map[i];
+               if(p!=NULL) {
+                       if(rss_entry_init(&entry)<0) { goto clean; }
+                       if(meta_entry(p->path,entry)<0) {
+                               rss_entry_free(entry);
+                               goto clean;
+                       }
+                       entries[index] = entry;
+                       index++;
+               }
+       }
+       
+       qsort(entries,count,sizeof(struct rss_entry*),&entries_sort);
+
+       return 1;
+clean:
+       for(size_t i=0;i<count;i++) {
+               if(entries[i]!=NULL) {
+                       rss_entry_free(entries[i]);
+               }
+       }
+       free(entries);
+       return -1;
+}
+
+static int entries_sort(const void *p1,const void *p2) {
+       struct rss_entry *entry1, *entry2;
+       time_t a,b;
+       if(NULL==p1) { return 1; }
+       if(NULL==p2) { return -1; }
+
+       entry1 = (struct rss_entry*)p1;
+       entry2 = (struct rss_entry*)p2;
+       a = mktime(&(entry1->pub_date));
+       b = mktime(&(entry2->pub_date));
+
+       return difftime(a,b);
+}
diff --git a/src/feed/gen.c b/src/feed/gen.c
new file mode 100644 (file)
index 0000000..8d4ac45
--- /dev/null
@@ -0,0 +1,50 @@
+#include<feed.h>
+
+static int feed_generate(struct torrent*);
+
+int feeds() {
+       for(size_t i=0;i<session.torrent_count;i++) {
+               if(feed_generate(session.torrents[i])<0) { return -1; }
+       }
+
+       return 1;
+}
+
+static int feed_generate(struct torrent *torrent) {
+       FILE *fp;
+       struct rss_channel_info info;
+       char *path;
+
+       if(is_directory(FEED_DIRECTORY)<0) {
+               if(directory_create(FEED_DIRECTORY)<0) { return -1; }
+       }
+
+       path = feed_path(torrent->name);
+       if(NULL==path) { return -1; }
+
+       fp = fopen(path,"w");
+       if(NULL==fp) {
+               free(path);
+               return -1;
+       }
+
+       if(torrent_file(torrent)<0) { goto clean; }
+
+       if(rss_header(fp)<0) { goto clean; }
+       if(feed_info(torrent,&info)<0) { goto clean; }
+       if(rss_info(fp,&info)<0) { return -1; }
+
+       if(feed_entries(fp,torrent)<0) { goto clean; }
+
+       if(rss_footer(fp)<0) { goto clean; }
+
+       fclose(fp);
+       free(path);
+
+       return 1;
+clean:
+       fclose(fp);
+       remove(path);
+       free(path);
+       return -1;
+}
diff --git a/src/feed/info.c b/src/feed/info.c
new file mode 100644 (file)
index 0000000..5b15ef0
--- /dev/null
@@ -0,0 +1,5 @@
+#include<feed.h>
+
+int feed_info(struct torrent *torrent_p, struct rss_channel_info *info) {
+       return -1;
+}
diff --git a/src/feed/path.c b/src/feed/path.c
new file mode 100644 (file)
index 0000000..09fcec9
--- /dev/null
@@ -0,0 +1,5 @@
+#include<feed.h>
+
+char *feed_path(const char *name) {
+       return NULL;
+}
index 632f9c43feae9cccb4bb940547f973790f919a57..af8fa55240a31b3c115dbbcce877df1f8687554c 100644 (file)
@@ -8,8 +8,13 @@ int is_directory(const char *path) {
                return -1;
        }
 
-
        if(!S_ISDIR(st.st_mode)) { return -1; }
 
        return 1;
 }
+
+int directory_create(const char *path) {
+       if(mkdir(path,0700)!=0) { return -1; }
+
+       return 1;
+}
index d160967c00985b5458332db0ab0b2ae8130488d0..d5e4df71a4d2352fea6cfd92bc78e1c2ea7dbc68 100644 (file)
@@ -6,7 +6,7 @@ int main(int argc, char **argv) {
        if(setup_logging()<0) { return EXIT_FAILURE; }
 
        if(add()<0) { return EXIT_FAILURE; }
-       if(feed()<0) { return EXIT_FAILURE; }
+       if(feeds()<0) { return EXIT_FAILURE; }
        if(watch()<0) { return EXIT_FAILURE; }
 
        while(1) { }
index 8f436843ff00fed074cd82f8d8b48f9effc66d40..74414638ed48e6e9fb41c539daa3d65b41498bda 100644 (file)
@@ -6,7 +6,7 @@ static int torrent_file_write_info(FILE*,uint8_t*,size_t);
 static int torrent_file_write_piece_layers(FILE*,struct torrent*);
 static int torrent_file_write_start(FILE*);
 
-int torrent_file(const char *datadir, struct torrent *torrent_p) {
+int torrent_file(struct torrent *torrent_p) {
        uint8_t *info;
        FILE *fp;
        unsigned char infohash[crypto_hash_sha256_BYTES];
@@ -17,14 +17,15 @@ int torrent_file(const char *datadir, struct torrent *torrent_p) {
        fp = NULL;
        path = NULL;
 
-       if(NULL==datadir) { goto clean; }
-       if(is_directory(datadir)<0) { goto clean; }
+       if(is_directory(TORRENT_FILE_DIRECTORY)<0) {
+               if(directory_create(TORRENT_FILE_DIRECTORY)<0) {goto clean; }
+       }
 
        if((i = torrent_file_info(torrent_p,&info))<0) { goto clean; }
 
        if(hash(info,i,infohash,crypto_hash_sha256_BYTES)<0) { goto clean; }
        
-       path = torrent_file_path(datadir,infohash,crypto_hash_sha256_BYTES);
+       path = torrent_file_path(infohash,crypto_hash_sha256_BYTES);
        if(NULL==path) { goto clean; }
        
        fp = fopen(path,"w");
index 44ff9f8f0009d780cb3a9c771eb6a6a8eb116e8c..628f98d02c7f43f85f7ce92937eb2cb206fc1903 100644 (file)
@@ -1,6 +1,6 @@
 #include<torrent.h>
 
-char *torrent_file_path(const char *prefix, unsigned char *infohash, size_t len) {
+char *torrent_file_path(unsigned char *infohash, size_t len) {
        char *path, *p;
        char hex[(crypto_hash_sha256_BYTES<<1)+1];
 
@@ -9,11 +9,7 @@ char *torrent_file_path(const char *prefix, unsigned char *infohash, size_t len)
        // this always returns hex, unnecessary to return
        sodium_bin2hex(hex,(crypto_hash_sha256_BYTES<<1)+1,infohash,crypto_hash_sha256_BYTES);
 
-       path = concat(prefix,TORRENT_FILE_DIRECTORY);
-       if(NULL==path) { return NULL; }
-
-       p = concat(path,hex);
-       free(path);
+       p = concat(TORRENT_FILE_DIRECTORY,hex);
        if(NULL==p) { return NULL; }
 
        path = malloc(strlen(p)+strlen(".torrent")+1);
index dd4f862cae4f16c3857af6882b014d631e62f721..0f27ab3a2c3c6c2a52ace726558a01a02583c17a 100644 (file)
@@ -94,13 +94,15 @@ torrent_tests_SOURCES = \
        $(top_srcdir)/src/torrent/add.c \
        $(top_srcdir)/src/torrent/file.c \
        $(top_srcdir)/src/torrent/free.c \
-       $(top_srcdir)/src/torrent/info.c \
        $(top_srcdir)/src/torrent/init.c \
        $(top_srcdir)/src/torrent/magnet.c \
        $(top_srcdir)/src/torrent/path.c \
        $(top_srcdir)/src/torrent/piece.c \
        $(top_srcdir)/src/tree.c
 
+torrent_tests_CPPFLAGS = $(AM_CPPFLAGS) \
+       -DTORRENT_INFO_SRC_FILE="$(top_srcdir)/src/torrent/info.c"
+
 tree_tests_SOURCES = \
        $(common_SOURCES) \
        tree.tests.c \
index 901730576b9f3b713ea3254b3c32d7653feeaf24..d91a323ac19bc4a18231973f42e82dbee1b7c69f 100644 (file)
@@ -18,9 +18,6 @@ void setup_env() {
        srand(time(NULL));
 
        create_test_directory(TEST_DIRECTORY);
-       create_test_directory(TEST_DIRECTORY "/data");
-       create_test_directory(TEST_DIRECTORY "/feeds");
-       create_test_directory(TEST_DIRECTORY "/torrents");
        create_test_file(TEST_FILE_1,TEST_FILE_1_CONTENTS);
        create_test_file(TEST_FILE_2,TEST_FILE_2_CONTENTS);
        create_test_file(TEST_FILE_3,TEST_FILE_3_CONTENTS);
index bf26afe02d5cd0918a7b971eda46e84e381ce931..da7942557ee0ecad45d1a53f157bdc98d1c03d89 100644 (file)
@@ -1,6 +1,6 @@
 #include<test_utils.h>
 
-#include<torrent.h>
+#include INCLUDE(TORRENT_INFO_SRC_FILE)
 
 int main();
 static void torrent_add_basic_test();
@@ -86,7 +86,7 @@ static void torrent_file_basic_test() {
        
        torrent_setup_example(&torrent);
 
-       assert(1==torrent_file(TEST_DIRECTORY,torrent));
+       assert(1==torrent_file(torrent));
 
        torrent_free(torrent);
 }
@@ -150,7 +150,7 @@ static void torrent_file_info_conformance_test() {
 
        assert(memcmp(out,expected,crypto_hash_sha256_BYTES)==0);
 
-       assert(torrent_file(TEST_DIRECTORY,torrent_p)==1);
+       assert(torrent_file(torrent_p)==1);
 
        assert(sodium_hex2bin(expected,crypto_hash_sha256_BYTES,torrent_file_hash_expected,strlen(torrent_file_hash_expected),NULL,&i,NULL)==0);
        assert(i==32);
@@ -169,16 +169,16 @@ static void torrent_file_path_basic_test() {
 
        memset(infohash,0,crypto_hash_sha256_BYTES);
                
-       p = torrent_file_path(NULL,NULL,0);
+       p = torrent_file_path(NULL,0);
        assert(NULL==p);
 
-       p = torrent_file_path(TEST_DIRECTORY,infohash,0);
+       p = torrent_file_path(infohash,0);
        assert(NULL==p);
 
-       p = torrent_file_path(TEST_DIRECTORY,infohash,23);
+       p = torrent_file_path(infohash,23);
        assert(NULL==p);
 
-       p = torrent_file_path(TEST_DIRECTORY,infohash,crypto_hash_sha256_BYTES);
+       p = torrent_file_path(infohash,crypto_hash_sha256_BYTES);
        assert(strcmp(p,TEST_DIRECTORY "/torrents/0000000000000000000000000000000000000000000000000000000000000000.torrent")==0);
 
        free(p);