summary refs log tree commit diff
diff options
context:
space:
mode:
authorRichard Nyberg <rnyberg@murmeldjur.se>2006-10-14 19:53:56 +0000
committerRichard Nyberg <rnyberg@murmeldjur.se>2006-10-14 19:53:56 +0000
commitb0b50e29b6a04621e11f5af1f248dd43ce3718f7 (patch)
tree9cf03607f3ce699d0f190d93333584ab244b480b
parent0b9d19de09ddc3708f5925f1728a78ae1c2de969 (diff)
downloadbtpd-b0b50e29b6a04621e11f5af1f248dd43ce3718f7.tar.gz
btpd-b0b50e29b6a04621e11f5af1f248dd43ce3718f7.zip
Create all files of a torrent at startup.
-rw-r--r--btpd/content.c29
1 files changed, 11 insertions, 18 deletions
diff --git a/btpd/content.c b/btpd/content.c
index 4829cff..bfe6779 100644
--- a/btpd/content.c
+++ b/btpd/content.c
@@ -106,7 +106,7 @@ static int
 fd_cb_wr(const char *path, int *fd, void *arg)
 {
     struct torrent *tp = arg;
-    return vopen(fd, O_RDWR|O_CREAT, "%s/%s", tp->tl->dir, path);
+    return vopen(fd, O_RDWR, "%s/%s", tp->tl->dir, path);
 }
 
 static void
@@ -576,6 +576,7 @@ struct rstat {
 int
 stat_and_adjust(struct torrent *tp, struct rstat ret[])
 {
+    int fd;
     char path[PATH_MAX];
     struct stat sb;
     for (int i = 0; i < tp->nfiles; i++) {
@@ -583,24 +584,19 @@ stat_and_adjust(struct torrent *tp, struct rstat ret[])
 again:
         if (stat(path, &sb) == -1) {
             if (errno == ENOENT) {
-                ret[i].mtime = -1;
-                ret[i].size = -1;
+                errno = vopen(&fd, O_CREAT|O_RDWR, "%s", path);
+                if (errno != 0 || close(fd) != 0)
+                    return errno;
+                goto again;
             } else
                 return errno;
-        } else {
-            ret[i].mtime = sb.st_mtime;
-            ret[i].size = sb.st_size;
-        }
-        if (ret[i].size > tp->files[i].length) {
+        } else if (sb.st_size > tp->files[i].length) {
             if (truncate(path, tp->files[i].length) != 0)
                 return errno;
             goto again;
-        } else if (ret[i].size == -1 && tp->files[i].length == 0) {
-            int fd;
-            errno = vopen(&fd, O_CREAT|O_RDWR, "%s", path);
-            if (errno != 0 || close(fd) != 0)
-                return errno;
-            goto again;
+        } else {
+            ret[i].mtime = sb.st_mtime;
+            ret[i].size = sb.st_size;
         }
     }
     return 0;
@@ -688,10 +684,7 @@ cm_td_start(struct cm_op *op)
                 uint32_t start, end;
                 end = (off + tp->files[i].length - 1)
                     / tp->piece_length;
-                if (sbs[i].size == -1)
-                    start = off / tp->piece_length;
-                else
-                    start = (off + sbs[i].size) / tp->piece_length;
+                start = (off + sbs[i].size) / tp->piece_length;
                 while (start <= end) {
                     clear_bit(cm->pos_field, start);
                     clear_bit(cm->piece_field, start);