diff options
Diffstat (limited to 'misc')
| -rw-r--r-- | misc/benc.c | 210 | ||||
| -rw-r--r-- | misc/benc.h | 2 | ||||
| -rw-r--r-- | misc/metainfo.c | 200 | ||||
| -rw-r--r-- | misc/stream.c | 160 | ||||
| -rw-r--r-- | misc/stream.h | 2 | ||||
| -rw-r--r-- | misc/subr.c | 70 |
6 files changed, 322 insertions, 322 deletions
diff --git a/misc/benc.c b/misc/benc.c index 632b0fd..68f7f95 100644 --- a/misc/benc.c +++ b/misc/benc.c @@ -17,7 +17,7 @@ benc_validate(const char *p, size_t len) const char *end = p + len - 1; if (len <= 0) - return EINVAL; + return EINVAL; return benc_validate_aux(p, end) == end ? 0 : EINVAL; } @@ -28,51 +28,51 @@ benc_validate_aux(const char *p, const char *end) size_t d = 0; switch (*p) { case 'd': - d = 1; + d = 1; case 'l': - for (p++; p <= end && *p != 'e'; p++) { - if (d != 0) { - if (d % 2 == 1 && !isdigit(*p)) - return NULL; - else - d++; - } - if ((p = benc_validate_aux(p, end)) == NULL) - return NULL; - } - if (p > end || (d != 0 && d % 2 != 1)) - return NULL; - break; + for (p++; p <= end && *p != 'e'; p++) { + if (d != 0) { + if (d % 2 == 1 && !isdigit(*p)) + return NULL; + else + d++; + } + if ((p = benc_validate_aux(p, end)) == NULL) + return NULL; + } + if (p > end || (d != 0 && d % 2 != 1)) + return NULL; + break; case 'i': - p++; - if (p > end) - return NULL; - if (*p == '-') - p++; - if (p > end || !isdigit(*p)) - return NULL; - p++; - while (p <= end && isdigit(*p)) - p++; - if (p > end || *p != 'e') - return NULL; - break; + p++; + if (p > end) + return NULL; + if (*p == '-') + p++; + if (p > end || !isdigit(*p)) + return NULL; + p++; + while (p <= end && isdigit(*p)) + p++; + if (p > end || *p != 'e') + return NULL; + break; default: - if (isdigit(*p)) { - size_t len = 0; - while (p <= end && isdigit(*p)) { - len *= 10; - len += *p - '0'; - p++; - } - if (p <= end && *p == ':' && p + len <= end) - p += len; - else - return NULL; - } - else - return NULL; - break; + if (isdigit(*p)) { + size_t len = 0; + while (p <= end && isdigit(*p)) { + len *= 10; + len += *p - '0'; + p++; + } + if (p <= end && *p == ':' && p + len <= end) + p += len; + else + return NULL; + } + else + return NULL; + break; } return p; } @@ -86,21 +86,21 @@ benc_length(const char *p) switch (*p) { case 'd': case 'l': - blen = 2; // [l|d]...e - next = benc_first(p); - while (*next != 'e') { - size_t len = benc_length(next); - blen += len; - next += len; - } - return blen; + blen = 2; // [l|d]...e + next = benc_first(p); + while (*next != 'e') { + size_t len = benc_length(next); + blen += len; + next += len; + } + return blen; case 'i': - for (next = p + 1; *next != 'e'; next++) - ; - return next - p + 1; + for (next = p + 1; *next != 'e'; next++) + ; + return next - p + 1; default: - assert(benc_str(p, &next, &blen, NULL) == 0); - return next - p + blen; + assert(benc_str(p, &next, &blen, NULL) == 0); + return next - p + blen; } } @@ -109,7 +109,7 @@ benc_nelems(const char *p) { size_t nelems = 0; for (p = benc_first(p); p != NULL; p = benc_next(p)) - nelems++; + nelems++; return nelems; } @@ -135,9 +135,9 @@ benc_str(const char *p, const char **out, size_t *len, const char**next) blen = *p - '0'; p++; while (isdigit(*p)) { - blen *= 10; - blen += *p - '0'; - p++; + blen *= 10; + blen += *p - '0'; + p++; } assert(*p == ':'); benc_safeset(len, blen); @@ -154,12 +154,12 @@ benc_strz(const char *p, char **out, size_t *len, const char **next) const char *bstr; if ((err = benc_str(p, &bstr, &blen, next)) == 0) { - if ((*out = malloc(blen + 1)) != NULL) { - memcpy(*out, bstr, blen); - (*out)[blen] = '\0'; - benc_safeset(len, blen); - } else - err = ENOMEM; + if ((*out = malloc(blen + 1)) != NULL) { + memcpy(*out, bstr, blen); + (*out)[blen] = '\0'; + benc_safeset(len, blen); + } else + err = ENOMEM; } return err; } @@ -172,11 +172,11 @@ benc_stra(const char *p, char **out, size_t *len, const char **next) const char *bstr; if ((err = benc_str(p, &bstr, &blen, next)) == 0) { - if ((*out = malloc(blen)) != NULL) { - memcpy(*out, bstr, blen); - benc_safeset(len, blen); - } else - err = ENOMEM; + if ((*out = malloc(blen)) != NULL) { + memcpy(*out, bstr, blen); + benc_safeset(len, blen); + } else + err = ENOMEM; } return err; } @@ -190,16 +190,16 @@ benc_int64(const char *p, int64_t *out, const char **next) assert(*p == 'i'); p++; if (*p == '-') { - sign = -1; - p++; + sign = -1; + p++; } assert(isdigit(*p)); res += sign * (*p - '0'); p++; while (isdigit(*p)) { - res *= sign * 10; - res += sign * (*p - '0'); - p++; + res *= sign * 10; + res += sign * (*p - '0'); + p++; } assert(*p == 'e'); benc_safeset(out, res); @@ -214,10 +214,10 @@ benc_uint32(const char *p, uint32_t *out, const char **next) int err; int64_t res; if ((err = benc_int64(p, &res, next)) == 0) { - if (res >= 0 && res <= 0xffffffffUL) - *out = (uint32_t)res; - else - err = EINVAL; + if (res >= 0 && res <= 0xffffffffUL) + *out = (uint32_t)res; + else + err = EINVAL; } return err; } @@ -235,17 +235,17 @@ benc_dget_any(const char *p, const char *key, const char **val) p = benc_first(p); while (p != NULL) { - if ((res = benc_str(p, &bstr, &blen, &p)) != 0) - return res; - - res = strncmp(bstr, key, blen); - if (res == 0 && len == blen) { - *val = p; - return 0; - } else if (res <= 0) { - p = benc_next(p); - } else - return ENOENT; + if ((res = benc_str(p, &bstr, &blen, &p)) != 0) + return res; + + res = strncmp(bstr, key, blen); + if (res == 0 && len == blen) { + *val = p; + return 0; + } else if (res <= 0) { + p = benc_next(p); + } else + return ENOENT; } return ENOENT; } @@ -255,8 +255,8 @@ benc_dget_lst(const char *p, const char *key, const char **val) { int err; if ((err = benc_dget_any(p, key, val)) == 0) - if (!benc_islst(*val)) - err = EINVAL; + if (!benc_islst(*val)) + err = EINVAL; return err; } @@ -265,8 +265,8 @@ benc_dget_dct(const char *p, const char *key, const char **val) { int err; if ((err = benc_dget_any(p, key, val)) == 0) - if (!benc_isdct(*val)) - err = EINVAL; + if (!benc_isdct(*val)) + err = EINVAL; return err; } @@ -276,8 +276,8 @@ benc_dget_str(const char *p, const char *key, const char **val, size_t *len) int err; const char *sp; if ((err = benc_dget_any(p, key, &sp)) == 0) - err = benc_isstr(sp) ? benc_str(sp, val, len, NULL) : EINVAL; - return err; + err = benc_isstr(sp) ? benc_str(sp, val, len, NULL) : EINVAL; + return err; } int @@ -286,8 +286,8 @@ benc_dget_stra(const char *p, const char *key, char **val, size_t *len) int err; const char *sp; if ((err = benc_dget_any(p, key, &sp)) == 0) - err = benc_isstr(sp) ? benc_stra(sp, val, len, NULL) : EINVAL; - return err; + err = benc_isstr(sp) ? benc_stra(sp, val, len, NULL) : EINVAL; + return err; } int @@ -296,8 +296,8 @@ benc_dget_strz(const char *p, const char *key, char **val, size_t *len) int err; const char *sp; if ((err = benc_dget_any(p, key, &sp)) == 0) - err = benc_isstr(sp) ? benc_strz(sp, val, len, NULL) : EINVAL; - return err; + err = benc_isstr(sp) ? benc_strz(sp, val, len, NULL) : EINVAL; + return err; } int @@ -306,9 +306,9 @@ benc_dget_int64(const char *p, const char *key, int64_t *val) int err; const char *ip; if ((err = benc_dget_any(p, key, &ip)) == 0) - err = benc_isint(ip) ? benc_int64(ip, val, NULL) : EINVAL; + err = benc_isint(ip) ? benc_int64(ip, val, NULL) : EINVAL; return err; -} +} int benc_dget_uint32(const char *p, const char *key, uint32_t *val) @@ -316,9 +316,9 @@ benc_dget_uint32(const char *p, const char *key, uint32_t *val) int err; const char *ip; if ((err = benc_dget_any(p, key, &ip)) == 0) - err = benc_isint(ip) ? benc_uint32(ip, val, NULL) : EINVAL; + err = benc_isint(ip) ? benc_uint32(ip, val, NULL) : EINVAL; return err; -} +} int benc_islst(const char *p) diff --git a/misc/benc.h b/misc/benc.h index 0cc3873..8088190 100644 --- a/misc/benc.h +++ b/misc/benc.h @@ -21,7 +21,7 @@ int benc_dget_any(const char *p, const char *key, const char **val); int benc_dget_lst(const char *p, const char *key, const char **val); int benc_dget_dct(const char *p, const char *key, const char **val); int benc_dget_str(const char *p, const char *key, - const char **val, size_t *len); + const char **val, size_t *len); int benc_dget_stra(const char *p, const char *key, char **val, size_t *len); int benc_dget_strz(const char *p, const char *key, char **val, size_t *len); int benc_dget_int64(const char *p, const char *key, int64_t *val); diff --git a/misc/metainfo.c b/misc/metainfo.c index 61da727..261f60a 100644 --- a/misc/metainfo.c +++ b/misc/metainfo.c @@ -44,7 +44,7 @@ print_metainfo(struct metainfo *tp) printf("Info hash: "); for (i = 0; i < 20; i++) - printf("%.2x", tp->info_hash[i]); + printf("%.2x", tp->info_hash[i]); printf("\n"); printf("Tracker URL: %s\n", tp->announce); printf("Piece length: %" PRId64 "\n", (int64_t)tp->piece_length); @@ -53,8 +53,8 @@ print_metainfo(struct metainfo *tp) printf("Advisory name: %s\n", tp->name); printf("Files:\n"); for (i = 0; i < tp->nfiles; i++) { - printf("%s (%" PRId64 ")\n", - tp->files[i].path, (int64_t)tp->files[i].length); + printf("%s (%" PRId64 ")\n", + tp->files[i].path, (int64_t)tp->files[i].length); } printf("Total length: %" PRId64 "\n\n", (int64_t)tp->total_length); } @@ -63,13 +63,13 @@ static int check_path(const char *path, size_t len) { if (len == 0) - return 0; + return 0; else if (len == 1 && path[0] == '.') - return 0; + return 0; else if (len == 2 && path[0] == '.' && path[1] == '.') - return 0; + return 0; else if (memchr(path, '/', len) != NULL) - return 0; + return 0; return 1; } @@ -81,27 +81,27 @@ fill_fileinfo(const char *fdct, struct fileinfo *tfp) const char *plst, *iter, *str; if ((err = benc_dget_off(fdct, "length", &tfp->length)) != 0) - return err; + return err; if ((err = benc_dget_lst(fdct, "path", &plst)) != 0) - return err; + return err; npath = plen = 0; iter = benc_first(plst); while (iter != NULL) { - if (!benc_isstr(iter)) - return EINVAL; - benc_str(iter, &str, &len, &iter); - if (!check_path(str, len)) - return EINVAL; - npath++; - plen += len; + if (!benc_isstr(iter)) + return EINVAL; + benc_str(iter, &str, &len, &iter); + if (!check_path(str, len)) + return EINVAL; + npath++; + plen += len; } if (npath == 0) - return EINVAL; + return EINVAL; if ((tfp->path = malloc(plen + (npath - 1) + 1)) == NULL) - return ENOMEM; + return ENOMEM; iter = benc_first(plst); benc_str(iter, &str, &len, &iter); @@ -109,11 +109,11 @@ fill_fileinfo(const char *fdct, struct fileinfo *tfp) plen = len; npath--; while (npath > 0) { - tfp->path[plen++] = '/'; - benc_str(iter, &str, &len, &iter); - memcpy(tfp->path + plen, str, len); - plen += len; - npath--; + tfp->path[plen++] = '/'; + benc_str(iter, &str, &len, &iter); + memcpy(tfp->path + plen, str, len); + plen += len; + npath--; } tfp->path[plen] = '\0'; return 0; @@ -124,18 +124,18 @@ clear_metainfo(struct metainfo *mip) { int i; if (mip->piece_hash != NULL) - free(mip->piece_hash); + free(mip->piece_hash); if (mip->announce != NULL) - free(mip->announce); + free(mip->announce); if (mip->files != NULL) { - for (i = 0; i < mip->nfiles; i++) { - if (mip->files[i].path != NULL) - free(mip->files[i].path); - } - free(mip->files); + for (i = 0; i < mip->nfiles; i++) { + if (mip->files[i].path != NULL) + free(mip->files[i].path); + } + free(mip->files); } if (mip->name != NULL) - free(mip->name); + free(mip->name); } int @@ -147,91 +147,91 @@ fill_metainfo(const char *bep, struct metainfo *tp, int mem_hashes) const char *hash_addr; if (!benc_isdct(bep)) - return EINVAL; + return EINVAL; if ((err = benc_dget_strz(bep, "announce", &tp->announce, NULL)) != 0) - goto out; + goto out; if ((err = benc_dget_dct(bep, "info", &bep)) != 0) - goto out; + goto out; SHA1(bep, benc_length(bep), tp->info_hash); if ((err = benc_dget_off(bep, "piece length", &tp->piece_length)) != 0) - goto out; + goto out; if ((err = benc_dget_str(bep, "pieces", &hash_addr, &len)) != 0) - goto out; + goto out; if (len % 20 != 0) { - err = EINVAL; - goto out; + err = EINVAL; + goto out; } tp->npieces = len / 20; tp->pieces_off = hash_addr - base_addr; - + if (mem_hashes) { - if ((tp->piece_hash = malloc(len)) == NULL) { - err = ENOMEM; - goto out; - } - bcopy(hash_addr, tp->piece_hash, len); + if ((tp->piece_hash = malloc(len)) == NULL) { + err = ENOMEM; + goto out; + } + bcopy(hash_addr, tp->piece_hash, len); } if ((err = benc_dget_strz(bep, "name", &tp->name, NULL)) != 0) - goto out; + goto out; err = benc_dget_off(bep, "length", &tp->total_length); if (err == 0) { - tp->nfiles = 1; - tp->files = calloc(1, sizeof(struct fileinfo)); - if (tp->files != NULL) { - tp->files[0].length = tp->total_length; - tp->files[0].path = strdup(tp->name); - if (tp->files[0].path == NULL) { - err = ENOMEM; - goto out; - } - } else { - err = ENOMEM; - goto out; - } + tp->nfiles = 1; + tp->files = calloc(1, sizeof(struct fileinfo)); + if (tp->files != NULL) { + tp->files[0].length = tp->total_length; + tp->files[0].path = strdup(tp->name); + if (tp->files[0].path == NULL) { + err = ENOMEM; + goto out; + } + } else { + err = ENOMEM; + goto out; + } } else if (err == ENOENT) { - int i; - const char *flst, *fdct; - - if ((err = benc_dget_lst(bep, "files", &flst)) != 0) - goto out; - - tp->nfiles = benc_nelems(flst); - if (tp->nfiles < 1) { - err = EINVAL; - goto out; - } - tp->files = calloc(tp->nfiles, sizeof(struct fileinfo)); - - tp->total_length = 0; - i = 0; - for (fdct = benc_first(flst); fdct != NULL; fdct = benc_next(fdct)) { - if (!benc_isdct(fdct)) { - err = EINVAL; - goto out; - } - - if ((err = fill_fileinfo(fdct, &tp->files[i])) != 0) - goto out; - - tp->total_length += tp->files[i].length; - i++; - } + int i; + const char *flst, *fdct; + + if ((err = benc_dget_lst(bep, "files", &flst)) != 0) + goto out; + + tp->nfiles = benc_nelems(flst); + if (tp->nfiles < 1) { + err = EINVAL; + goto out; + } + tp->files = calloc(tp->nfiles, sizeof(struct fileinfo)); + + tp->total_length = 0; + i = 0; + for (fdct = benc_first(flst); fdct != NULL; fdct = benc_next(fdct)) { + if (!benc_isdct(fdct)) { + err = EINVAL; + goto out; + } + + if ((err = fill_fileinfo(fdct, &tp->files[i])) != 0) + goto out; + + tp->total_length += tp->files[i].length; + i++; + } } else - goto out; + goto out; out: if (err != 0) - clear_metainfo(tp); + clear_metainfo(tp); return err; } @@ -244,31 +244,31 @@ load_metainfo(const char *path, off_t size, int mem_hashes, int fd, err = 0; if ((fd = open(path, O_RDONLY)) == -1) - return errno; + return errno; if (size <= 0) { - struct stat sb; - if (fstat(fd, &sb) == -1) { - close(fd); - return errno; - } else - size = sb.st_size; + struct stat sb; + if (fstat(fd, &sb) == -1) { + close(fd); + return errno; + } else + size = sb.st_size; } if ((buf = mmap(0, size, PROT_READ, MAP_PRIVATE, fd, 0)) == MAP_FAILED) - err = errno; + err = errno; close(fd); if (err == 0) - err = benc_validate(buf, size); + err = benc_validate(buf, size); if (err == 0) - if ((*res = calloc(1, sizeof(**res))) == NULL) - err = ENOMEM; + if ((*res = calloc(1, sizeof(**res))) == NULL) + err = ENOMEM; if (err == 0) - if ((err = fill_metainfo(buf, *res, mem_hashes)) != 0) - free(*res); + if ((err = fill_metainfo(buf, *res, mem_hashes)) != 0) + free(*res); munmap(buf, size); return err; diff --git a/misc/stream.c b/misc/stream.c index 4d94342..b1f6fc0 100644 --- a/misc/stream.c +++ b/misc/stream.c @@ -16,7 +16,7 @@ bts_open_ro(struct metainfo *meta, off_t off, F_fdcb fd_cb, void *fd_arg) { struct bt_stream_ro *bts = malloc(sizeof(*bts)); if (bts == NULL) - return NULL; + return NULL; bts->meta = meta; bts->fd_cb = fd_cb; @@ -37,16 +37,16 @@ bts_seek_ro(struct bt_stream_ro *bts, off_t off) assert(off >= 0 && off <= bts->meta->total_length); if (bts->fd != -1) { - close(bts->fd); - bts->fd = -1; + close(bts->fd); + bts->fd = -1; } bts->t_off = off; bts->index = 0; while (off >= files[bts->index].length) { - off -= files[bts->index].length; - bts->index++; + off -= files[bts->index].length; + bts->index++; } bts->f_off = off; @@ -63,31 +63,31 @@ bts_read_ro(struct bt_stream_ro *bts, char *buf, size_t len) boff = 0; while (boff < len) { - if (bts->fd == -1) { - int err = - bts->fd_cb(files[bts->index].path, &bts->fd, bts->fd_arg); - if (err != 0) - return err; - if (bts->f_off != 0) - lseek(bts->fd, bts->f_off, SEEK_SET); - } - - wantread = min(len - boff, files[bts->index].length - bts->f_off); - didread = read(bts->fd, buf + boff, wantread); - if (didread == -1) - return errno; - - boff += didread; - bts->f_off += didread; - bts->t_off += didread; - if (bts->f_off == files[bts->index].length) { - close(bts->fd); - bts->fd = -1; - bts->f_off = 0; - bts->index++; - } - if (didread != wantread) - return ENOENT; + if (bts->fd == -1) { + int err = + bts->fd_cb(files[bts->index].path, &bts->fd, bts->fd_arg); + if (err != 0) + return err; + if (bts->f_off != 0) + lseek(bts->fd, bts->f_off, SEEK_SET); + } + + wantread = min(len - boff, files[bts->index].length - bts->f_off); + didread = read(bts->fd, buf + boff, wantread); + if (didread == -1) + return errno; + + boff += didread; + bts->f_off += didread; + bts->t_off += didread; + if (bts->f_off == files[bts->index].length) { + close(bts->fd); + bts->fd = -1; + bts->f_off = 0; + bts->index++; + } + if (didread != wantread) + return ENOENT; } return 0; } @@ -96,7 +96,7 @@ void bts_close_ro(struct bt_stream_ro *bts) { if (bts->fd != -1) - close(bts->fd); + close(bts->fd); free(bts); } @@ -112,11 +112,11 @@ bts_sha(struct bt_stream_ro *bts, off_t length, uint8_t *hash) SHA1_Init(&ctx); while (length > 0) { - wantread = min(length, SHAFILEBUF); - if ((err = bts_read_ro(bts, buf, wantread)) != 0) - break; - length -= wantread; - SHA1_Update(&ctx, buf, wantread); + wantread = min(length, SHAFILEBUF); + if ((err = bts_read_ro(bts, buf, wantread)) != 0) + break; + length -= wantread; + SHA1_Update(&ctx, buf, wantread); } SHA1_Final(hash, &ctx); return err; @@ -136,23 +136,23 @@ bts_hashes(struct metainfo *meta, off_t llen = meta->total_length % plen; if ((bts = bts_open_ro(meta, 0, fd_cb, arg)) == NULL) - return ENOMEM; - - for (piece = 0; piece < meta->npieces; piece++) { + return ENOMEM; + + for (piece = 0; piece < meta->npieces; piece++) { if (piece < meta->npieces - 1) - err = bts_sha(bts, plen, hash); - else - err = bts_sha(bts, llen, hash); - - if (err == 0) - cb(piece, hash, arg); - else if (err == ENOENT) { - cb(piece, NULL, arg); - if (piece < meta->npieces - 1) - bts_seek_ro(bts, (piece + 1) * plen); - err = 0; - } else - break; + err = bts_sha(bts, plen, hash); + else + err = bts_sha(bts, llen, hash); + + if (err == 0) + cb(piece, hash, arg); + else if (err == ENOENT) { + cb(piece, NULL, arg); + if (piece < meta->npieces - 1) + bts_seek_ro(bts, (piece + 1) * plen); + err = 0; + } else + break; } bts_close_ro(bts); return err; @@ -163,7 +163,7 @@ bts_open_wo(struct metainfo *meta, off_t off, F_fdcb fd_cb, void *fd_arg) { struct bt_stream_wo *bts = malloc(sizeof(*bts)); if (bts == NULL) - return NULL; + return NULL; bts->meta = meta; bts->fd_cb = fd_cb; @@ -187,24 +187,24 @@ bts_write_wo(struct bt_stream_wo *bts, const char *buf, size_t len) boff = 0; while (boff < len) { - if (bts->fd == -1) { - int err = - bts->fd_cb(files[bts->index].path, &bts->fd, bts->fd_arg); - if (err != 0) - return err; - if (bts->f_off != 0) - lseek(bts->fd, bts->f_off, SEEK_SET); - } - - wantwrite = min(len - boff, files[bts->index].length - bts->f_off); - didwrite = write(bts->fd, buf + boff, wantwrite); - if (didwrite == -1) - return errno; - - boff += didwrite; - bts->f_off += didwrite; - bts->t_off += didwrite; - if (bts->f_off == files[bts->index].length) { + if (bts->fd == -1) { + int err = + bts->fd_cb(files[bts->index].path, &bts->fd, bts->fd_arg); + if (err != 0) + return err; + if (bts->f_off != 0) + lseek(bts->fd, bts->f_off, SEEK_SET); + } + + wantwrite = min(len - boff, files[bts->index].length - bts->f_off); + didwrite = write(bts->fd, buf + boff, wantwrite); + if (didwrite == -1) + return errno; + + boff += didwrite; + bts->f_off += didwrite; + bts->t_off += didwrite; + if (bts->f_off == files[bts->index].length) { if (fsync(bts->fd) == -1) { int err = errno; close(bts->fd); @@ -212,10 +212,10 @@ bts_write_wo(struct bt_stream_wo *bts, const char *buf, size_t len) } if (close(bts->fd) == -1) return errno; - bts->fd = -1; - bts->f_off = 0; - bts->index++; - } + bts->fd = -1; + bts->f_off = 0; + bts->index++; + } } return 0; } @@ -225,11 +225,11 @@ bts_close_wo(struct bt_stream_wo *bts) { int err = 0; if (bts->fd != -1) { - if (fsync(bts->fd) == -1) { - err = errno; - close(bts->fd); - } else if (close(bts->fd) == -1) - err = errno; + if (fsync(bts->fd) == -1) { + err = errno; + close(bts->fd); + } else if (close(bts->fd) == -1) + err = errno; } free(bts); return err; diff --git a/misc/stream.h b/misc/stream.h index f71e5c9..6ed91d5 100644 --- a/misc/stream.h +++ b/misc/stream.h @@ -31,6 +31,6 @@ int bts_close_wo(struct bt_stream_wo *bts); int bts_sha(struct bt_stream_ro *bts, off_t length, uint8_t *hash); int bts_hashes(struct metainfo *, F_fdcb fd_cb, - void (*cb)(uint32_t, uint8_t *, void *), void *arg); + void (*cb)(uint32_t, uint8_t *, void *), void *arg); #endif diff --git a/misc/subr.c b/misc/subr.c index abbf4e9..f894812 100644 --- a/misc/subr.c +++ b/misc/subr.c @@ -34,9 +34,9 @@ set_nonblocking(int fd) { int oflags; if ((oflags = fcntl(fd, F_GETFL, 0)) == -1) - return errno; + return errno; if (fcntl(fd, F_SETFL, oflags | O_NONBLOCK) == -1) - return errno; + return errno; return 0; } @@ -45,9 +45,9 @@ set_blocking(int fd) { int oflags; if ((oflags = fcntl(fd, F_GETFL, 0)) == -1) - return errno; + return errno; if (fcntl(fd, F_SETFL, oflags & ~O_NONBLOCK) == -1) - return errno; + return errno; return 0; } @@ -58,16 +58,16 @@ mkdirs(char *path) char *spos = strchr(path + 1, '/'); // Must ignore the root while (spos != NULL) { - *spos = '\0'; - err = mkdir(path, 0777); - *spos = '/'; - - if (err != 0 && errno != EEXIST) { - err = errno; - break; - } - - spos = strchr(spos + 1, '/'); + *spos = '\0'; + err = mkdir(path, 0777); + *spos = '/'; + + if (err != 0 && errno != EEXIST) { + err = errno; + break; + } + + spos = strchr(spos + 1, '/'); } return err; } @@ -81,8 +81,8 @@ vopen(int *res, int flags, const char *fmt, ...) va_start(ap, fmt); if (vsnprintf(path, PATH_MAX, fmt, ap) >= PATH_MAX) { - va_end(ap); - return ENAMETOOLONG; + va_end(ap); + return ENAMETOOLONG; } va_end(ap); @@ -90,18 +90,18 @@ vopen(int *res, int flags, const char *fmt, ...) again: fd = open(path, flags, 0666); if (fd < 0 && errno == ENOENT && (flags & O_CREAT) != 0 && !didmkdirs) { - if (mkdirs(path) == 0) { - didmkdirs = 1; - goto again; - } else - return errno; + if (mkdirs(path) == 0) { + didmkdirs = 1; + goto again; + } else + return errno; } if (fd >= 0) { - *res = fd; - return 0; + *res = fd; + return 0; } else - return errno; + return errno; } int @@ -110,22 +110,22 @@ canon_path(const char *path, char **res) char rp[PATH_MAX]; if (realpath(path, rp) == NULL) - return errno; + return errno; #if 0 // This could be necessary on solaris. if (rp[0] != '/') { - char wd[MAXPATHLEN]; - if (getcwd(wd, MAXPATHLEN) == NULL) - return errno; - if (strlcat(wd, "/", MAXPATHLEN) >= MAXPATHLEN) - return ENAMETOOLONG; - if (strlcat(wd, rp, MAXPATHLEN) >= MAXPATHLEN) - return ENAMETOOLONG; - strcpy(rp, wd); + char wd[MAXPATHLEN]; + if (getcwd(wd, MAXPATHLEN) == NULL) + return errno; + if (strlcat(wd, "/", MAXPATHLEN) >= MAXPATHLEN) + return ENAMETOOLONG; + if (strlcat(wd, rp, MAXPATHLEN) >= MAXPATHLEN) + return ENAMETOOLONG; + strcpy(rp, wd); } #endif if ((*res = strdup(rp)) == NULL) - return ENOMEM; + return ENOMEM; return 0; } @@ -136,6 +136,6 @@ round_to_page(size_t size) size_t psize = getpagesize(); size_t rem = size % psize; if (rem != 0) - size += psize - rem; + size += psize - rem; return size; } |