From b6c76b08babea35096f5f7f8127019cdd602753d Mon Sep 17 00:00:00 2001 From: alex Date: Fri, 31 Dec 2021 00:37:20 -0800 Subject: [PATCH] ... --- Makefile.am | 5 ++- inc/feed.h | 6 +++- inc/fs.h | 1 + inc/torrent.h | 6 ++-- src/feed.c | 70 --------------------------------------- src/feed/entries.c | 61 ++++++++++++++++++++++++++++++++++ src/feed/gen.c | 50 ++++++++++++++++++++++++++++ src/feed/info.c | 5 +++ src/feed/path.c | 5 +++ src/fs/dir.c | 7 +++- src/main.c | 2 +- src/torrent/file.c | 9 ++--- src/torrent/path.c | 8 ++--- test/unit/Makefile.am | 4 ++- test/unit/test_utils.c | 3 -- test/unit/torrent.tests.c | 14 ++++---- 16 files changed, 158 insertions(+), 98 deletions(-) delete mode 100644 src/feed.c create mode 100644 src/feed/entries.c create mode 100644 src/feed/gen.c create mode 100644 src/feed/info.c create mode 100644 src/feed/path.c diff --git a/Makefile.am b/Makefile.am index 1442513..b790ce6 100644 --- a/Makefile.am +++ b/Makefile.am @@ -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 \ diff --git a/inc/feed.h b/inc/feed.h index 0908289..f62e8b5 100644 --- a/inc/feed.h +++ b/inc/feed.h @@ -1,11 +1,15 @@ #ifndef __FEED_H_ #define __FEED_H_ +#include #include #include #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 diff --git a/inc/fs.h b/inc/fs.h index e1a8312..adc39f9 100644 --- a/inc/fs.h +++ b/inc/fs.h @@ -8,6 +8,7 @@ #include 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*); diff --git a/inc/torrent.h b/inc/torrent.h index a65a443..5eb2f3f 100644 --- a/inc/torrent.h +++ b/inc/torrent.h @@ -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 index 502ac5d..0000000 --- a/src/feed.c +++ /dev/null @@ -1,70 +0,0 @@ -#include - -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;iname); - 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 index 0000000..5f26f16 --- /dev/null +++ b/src/feed/entries.c @@ -0,0 +1,61 @@ +#include + +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;ifiles.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;ifiles.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;ipub_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 index 0000000..8d4ac45 --- /dev/null +++ b/src/feed/gen.c @@ -0,0 +1,50 @@ +#include + +static int feed_generate(struct torrent*); + +int feeds() { + for(size_t i=0;iname); + 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 index 0000000..5b15ef0 --- /dev/null +++ b/src/feed/info.c @@ -0,0 +1,5 @@ +#include + +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 index 0000000..09fcec9 --- /dev/null +++ b/src/feed/path.c @@ -0,0 +1,5 @@ +#include + +char *feed_path(const char *name) { + return NULL; +} diff --git a/src/fs/dir.c b/src/fs/dir.c index 632f9c4..af8fa55 100644 --- a/src/fs/dir.c +++ b/src/fs/dir.c @@ -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; +} diff --git a/src/main.c b/src/main.c index d160967..d5e4df7 100644 --- a/src/main.c +++ b/src/main.c @@ -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) { } diff --git a/src/torrent/file.c b/src/torrent/file.c index 8f43684..7441463 100644 --- a/src/torrent/file.c +++ b/src/torrent/file.c @@ -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"); diff --git a/src/torrent/path.c b/src/torrent/path.c index 44ff9f8..628f98d 100644 --- a/src/torrent/path.c +++ b/src/torrent/path.c @@ -1,6 +1,6 @@ #include -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); diff --git a/test/unit/Makefile.am b/test/unit/Makefile.am index dd4f862..0f27ab3 100644 --- a/test/unit/Makefile.am +++ b/test/unit/Makefile.am @@ -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 \ diff --git a/test/unit/test_utils.c b/test/unit/test_utils.c index 9017305..d91a323 100644 --- a/test/unit/test_utils.c +++ b/test/unit/test_utils.c @@ -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); diff --git a/test/unit/torrent.tests.c b/test/unit/torrent.tests.c index bf26afe..da79425 100644 --- a/test/unit/torrent.tests.c +++ b/test/unit/torrent.tests.c @@ -1,6 +1,6 @@ #include -#include +#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); -- 2.39.5