From c60b7424a0e0b12540112bc14d42f3214f214cd6 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Timo=20Ter=C3=A4s?= <timo.teras@iki.fi>
Date: Tue, 9 Nov 2021 21:28:55 +0200
Subject: [PATCH] optimize apk_pathbuilder_pop to get the old length

avoids memrchr
---
 src/apk_pathbuilder.h |  2 +-
 src/app_manifest.c    |  6 +++---
 src/app_mkpkg.c       | 14 +++++++-------
 src/fs_fsys.c         | 11 ++++++-----
 src/pathbuilder.c     |  9 ++++-----
 5 files changed, 21 insertions(+), 21 deletions(-)

diff --git a/src/apk_pathbuilder.h b/src/apk_pathbuilder.h
index cabf51c1..88e79f03 100644
--- a/src/apk_pathbuilder.h
+++ b/src/apk_pathbuilder.h
@@ -18,7 +18,7 @@ struct apk_pathbuilder {
 };
 
 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)
diff --git a/src/app_manifest.c b/src/app_manifest.c
index 5c2e0855..a9d82d55 100644
--- a/src/app_manifest.c
+++ b/src/app_manifest.c
@@ -96,7 +96,7 @@ static int process_v3_meta(struct apk_extract_ctx *ectx, struct adb_obj *pkg)
 	struct apk_pathbuilder pb;
 	char buf[APK_DIGEST_MAX_LENGTH*2+1];
 	apk_blob_t hex;
-	int i, j;
+	int i, j, n;
 
 	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++) {
 			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));
 
 			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,
 				apk_digest_alg_str(digest.alg), buf,
 				apk_pathbuilder_cstr(&pb));
-			apk_pathbuilder_pop(&pb);
+			apk_pathbuilder_pop(&pb, n);
 		}
 	}
 
diff --git a/src/app_mkpkg.c b/src/app_mkpkg.c
index d8559997..04ef6971 100644
--- a/src/app_mkpkg.c
+++ b/src/app_mkpkg.c
@@ -154,7 +154,7 @@ static int mkpkg_process_dirent(void *pctx, int dirfd, const char *entry)
 			char target[1022];
 		} symlink;
 	} ft;
-	int r;
+	int r, n;
 
 	r = apk_fileinfo_get(dirfd, entry, APK_FI_NOFOLLOW | APK_FI_DIGEST(APK_DIGEST_SHA256), &fi, NULL);
 	if (r) return r;
@@ -178,14 +178,14 @@ static int mkpkg_process_dirent(void *pctx, int dirfd, const char *entry)
 		r = 0;
 		break;
 	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);
-		apk_pathbuilder_pop(&ctx->pb);
+		apk_pathbuilder_pop(&ctx->pb, n);
 		return r;
 	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_pathbuilder_pop(&ctx->pb);
+		apk_pathbuilder_pop(&ctx->pb, n);
 		return 0;
 	}
 
@@ -330,13 +330,13 @@ static int mkpkg_main(void *pctx, struct apk_ctx *ac, struct apk_string_array *a
 				.path_idx = i,
 				.file_idx = j,
 			};
-			apk_pathbuilder_pushb(&ctx->pb, filename);
+			int n = apk_pathbuilder_pushb(&ctx->pb, filename);
 			adb_c_block_data(
 				os, APK_BLOB_STRUCT(hdr), sz,
 				apk_istream_from_fd(openat(files_fd,
 					apk_pathbuilder_cstr(&ctx->pb),
 					O_RDONLY)));
-			apk_pathbuilder_pop(&ctx->pb);
+			apk_pathbuilder_pop(&ctx->pb, n);
 		}
 	}
 	close(files_fd);
diff --git a/src/fs_fsys.c b/src/fs_fsys.c
index 7615b791..09936071 100644
--- a/src/fs_fsys.c
+++ b/src/fs_fsys.c
@@ -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;
 	char tmpname[TMPNAME_MAX], apknewname[TMPNAME_MAX];
 	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_pathbuilder_pushb(&d->pb, filename);
+	n = apk_pathbuilder_pushb(&d->pb, filename);
 	fn = apk_pathbuilder_cstr(&d->pb);
 
 	switch (ctrl) {
@@ -258,7 +258,7 @@ static int fsys_file_control(struct apk_fsdir *d, apk_blob_t filename, int ctrl)
 		break;
 	}
 
-	apk_pathbuilder_pop(&d->pb);
+	apk_pathbuilder_pop(&d->pb, n);
 	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_istream *is;
 	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));
-	apk_pathbuilder_pop(&d->pb);
+	apk_pathbuilder_pop(&d->pb, n);
 	if (IS_ERR(is)) return PTR_ERR(is);
 
 	apk_digest_ctx_reset(&ac->dctx, alg);
diff --git a/src/pathbuilder.c b/src/pathbuilder.c
index 166aa225..277d0c2c 100644
--- a/src/pathbuilder.c
+++ b/src/pathbuilder.c
@@ -17,13 +17,12 @@ int apk_pathbuilder_pushb(struct apk_pathbuilder *pb, apk_blob_t b)
 	memcpy(&pb->name[i], b.ptr, b.len);
 	pb->namelen = i + b.len;
 	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 (slash) pb->namelen = slash - pb->name;
-	else pb->namelen = 0;
+	if (pos < 0) return;
+	pb->namelen = pos;
 	pb->name[pb->namelen] = 0;
 }
-- 
GitLab