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 \
#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
#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*);
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);
+++ /dev/null
-#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;
-} */
--- /dev/null
+#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);
+}
--- /dev/null
+#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;
+}
--- /dev/null
+#include<feed.h>
+
+int feed_info(struct torrent *torrent_p, struct rss_channel_info *info) {
+ return -1;
+}
--- /dev/null
+#include<feed.h>
+
+char *feed_path(const char *name) {
+ return NULL;
+}
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;
+}
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) { }
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];
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");
#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];
// 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);
$(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 \
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);
#include<test_utils.h>
-#include<torrent.h>
+#include INCLUDE(TORRENT_INFO_SRC_FILE)
int main();
static void torrent_add_basic_test();
torrent_setup_example(&torrent);
- assert(1==torrent_file(TEST_DIRECTORY,torrent));
+ assert(1==torrent_file(torrent));
torrent_free(torrent);
}
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);
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);