optimize apk_pathbuilder_pop to get the old length

avoids memrchr
cute-signatures
Timo Teräs 2021-11-09 21:28:55 +02:00
parent 7c8f872572
commit c60b7424a0
5 changed files with 21 additions and 21 deletions

View File

@ -18,7 +18,7 @@ struct apk_pathbuilder {
}; };
int apk_pathbuilder_pushb(struct apk_pathbuilder *pb, apk_blob_t b); int apk_pathbuilder_pushb(struct apk_pathbuilder *pb, apk_blob_t b);
void apk_pathbuilder_pop(struct apk_pathbuilder *pb); void apk_pathbuilder_pop(struct apk_pathbuilder *pb, int);
static inline int apk_pathbuilder_setb(struct apk_pathbuilder *pb, apk_blob_t b) static inline int apk_pathbuilder_setb(struct apk_pathbuilder *pb, apk_blob_t b)

View File

@ -96,7 +96,7 @@ static int process_v3_meta(struct apk_extract_ctx *ectx, struct adb_obj *pkg)
struct apk_pathbuilder pb; struct apk_pathbuilder pb;
char buf[APK_DIGEST_MAX_LENGTH*2+1]; char buf[APK_DIGEST_MAX_LENGTH*2+1];
apk_blob_t hex; apk_blob_t hex;
int i, j; int i, j, n;
adb_ro_obj(pkg, ADBI_PKG_PATHS, &paths); adb_ro_obj(pkg, ADBI_PKG_PATHS, &paths);
@ -107,7 +107,7 @@ static int process_v3_meta(struct apk_extract_ctx *ectx, struct adb_obj *pkg)
for (j = ADBI_FIRST; j <= adb_ra_num(&files); j++) { for (j = ADBI_FIRST; j <= adb_ra_num(&files); j++) {
adb_ro_obj(&files, j, &file); adb_ro_obj(&files, j, &file);
apk_pathbuilder_pushb(&pb, adb_ro_blob(&file, ADBI_FI_NAME)); n = apk_pathbuilder_pushb(&pb, adb_ro_blob(&file, ADBI_FI_NAME));
apk_digest_from_blob(&digest, adb_ro_blob(&file, ADBI_FI_HASHES)); apk_digest_from_blob(&digest, adb_ro_blob(&file, ADBI_FI_HASHES));
hex = APK_BLOB_BUF(buf); hex = APK_BLOB_BUF(buf);
@ -118,7 +118,7 @@ static int process_v3_meta(struct apk_extract_ctx *ectx, struct adb_obj *pkg)
mctx->prefix1, mctx->prefix2, mctx->prefix1, mctx->prefix2,
apk_digest_alg_str(digest.alg), buf, apk_digest_alg_str(digest.alg), buf,
apk_pathbuilder_cstr(&pb)); apk_pathbuilder_cstr(&pb));
apk_pathbuilder_pop(&pb); apk_pathbuilder_pop(&pb, n);
} }
} }

View File

@ -154,7 +154,7 @@ static int mkpkg_process_dirent(void *pctx, int dirfd, const char *entry)
char target[1022]; char target[1022];
} symlink; } symlink;
} ft; } ft;
int r; int r, n;
r = apk_fileinfo_get(dirfd, entry, APK_FI_NOFOLLOW | APK_FI_DIGEST(APK_DIGEST_SHA256), &fi, NULL); r = apk_fileinfo_get(dirfd, entry, APK_FI_NOFOLLOW | APK_FI_DIGEST(APK_DIGEST_SHA256), &fi, NULL);
if (r) return r; if (r) return r;
@ -178,14 +178,14 @@ static int mkpkg_process_dirent(void *pctx, int dirfd, const char *entry)
r = 0; r = 0;
break; break;
case S_IFDIR: case S_IFDIR:
apk_pathbuilder_push(&ctx->pb, entry); n = apk_pathbuilder_push(&ctx->pb, entry);
r = mkpkg_process_directory(ctx, openat(dirfd, entry, O_RDONLY), &fi); r = mkpkg_process_directory(ctx, openat(dirfd, entry, O_RDONLY), &fi);
apk_pathbuilder_pop(&ctx->pb); apk_pathbuilder_pop(&ctx->pb, n);
return r; return r;
default: default:
apk_pathbuilder_push(&ctx->pb, entry); n = apk_pathbuilder_push(&ctx->pb, entry);
apk_out(out, "%s: special file ignored", apk_pathbuilder_cstr(&ctx->pb)); apk_out(out, "%s: special file ignored", apk_pathbuilder_cstr(&ctx->pb));
apk_pathbuilder_pop(&ctx->pb); apk_pathbuilder_pop(&ctx->pb, n);
return 0; return 0;
} }
@ -330,13 +330,13 @@ static int mkpkg_main(void *pctx, struct apk_ctx *ac, struct apk_string_array *a
.path_idx = i, .path_idx = i,
.file_idx = j, .file_idx = j,
}; };
apk_pathbuilder_pushb(&ctx->pb, filename); int n = apk_pathbuilder_pushb(&ctx->pb, filename);
adb_c_block_data( adb_c_block_data(
os, APK_BLOB_STRUCT(hdr), sz, os, APK_BLOB_STRUCT(hdr), sz,
apk_istream_from_fd(openat(files_fd, apk_istream_from_fd(openat(files_fd,
apk_pathbuilder_cstr(&ctx->pb), apk_pathbuilder_cstr(&ctx->pb),
O_RDONLY))); O_RDONLY)));
apk_pathbuilder_pop(&ctx->pb); apk_pathbuilder_pop(&ctx->pb, n);
} }
} }
close(files_fd); close(files_fd);

View File

@ -223,10 +223,10 @@ static int fsys_file_control(struct apk_fsdir *d, apk_blob_t filename, int ctrl)
struct apk_ctx *ac = d->ac; struct apk_ctx *ac = d->ac;
char tmpname[TMPNAME_MAX], apknewname[TMPNAME_MAX]; char tmpname[TMPNAME_MAX], apknewname[TMPNAME_MAX];
const char *fn; const char *fn;
int rc = 0, atfd = apk_ctx_fd_dest(d->ac); int n, rc = 0, atfd = apk_ctx_fd_dest(d->ac);
apk_blob_t dirname = apk_pathbuilder_get(&d->pb); apk_blob_t dirname = apk_pathbuilder_get(&d->pb);
apk_pathbuilder_pushb(&d->pb, filename); n = apk_pathbuilder_pushb(&d->pb, filename);
fn = apk_pathbuilder_cstr(&d->pb); fn = apk_pathbuilder_cstr(&d->pb);
switch (ctrl) { switch (ctrl) {
@ -258,7 +258,7 @@ static int fsys_file_control(struct apk_fsdir *d, apk_blob_t filename, int ctrl)
break; break;
} }
apk_pathbuilder_pop(&d->pb); apk_pathbuilder_pop(&d->pb, n);
return rc; return rc;
} }
@ -267,10 +267,11 @@ static int fsys_file_digest(struct apk_fsdir *d, apk_blob_t filename, uint8_t al
struct apk_ctx *ac = d->ac; struct apk_ctx *ac = d->ac;
struct apk_istream *is; struct apk_istream *is;
apk_blob_t blob; apk_blob_t blob;
int n;
apk_pathbuilder_pushb(&d->pb, filename); n = apk_pathbuilder_pushb(&d->pb, filename);
is = apk_istream_from_file(apk_ctx_fd_dest(ac), apk_pathbuilder_cstr(&d->pb)); is = apk_istream_from_file(apk_ctx_fd_dest(ac), apk_pathbuilder_cstr(&d->pb));
apk_pathbuilder_pop(&d->pb); apk_pathbuilder_pop(&d->pb, n);
if (IS_ERR(is)) return PTR_ERR(is); if (IS_ERR(is)) return PTR_ERR(is);
apk_digest_ctx_reset(&ac->dctx, alg); apk_digest_ctx_reset(&ac->dctx, alg);

View File

@ -17,13 +17,12 @@ int apk_pathbuilder_pushb(struct apk_pathbuilder *pb, apk_blob_t b)
memcpy(&pb->name[i], b.ptr, b.len); memcpy(&pb->name[i], b.ptr, b.len);
pb->namelen = i + b.len; pb->namelen = i + b.len;
pb->name[pb->namelen] = 0; pb->name[pb->namelen] = 0;
return 0; return i;
} }
void apk_pathbuilder_pop(struct apk_pathbuilder *pb) void apk_pathbuilder_pop(struct apk_pathbuilder *pb, int pos)
{ {
char *slash = memrchr(pb->name, '/', pb->namelen); if (pos < 0) return;
if (slash) pb->namelen = slash - pb->name; pb->namelen = pos;
else pb->namelen = 0;
pb->name[pb->namelen] = 0; pb->name[pb->namelen] = 0;
} }