Skip to content

Commit 6f9271f

Browse files
committed
btrfs-progs: list-chunks: Workarounds for replacing qsort_r when targeting Android
Android NDK does not support `qsort_r`, this workaround first add a list of `id` to struct `compare`, and then reverse the list, and finally do the sorting stuffs using `qsort`. This helps preserve the sorting order without `qsort_r` Signed-off-by: Shadichy <[email protected]>
1 parent 0ffc07e commit 6f9271f

File tree

3 files changed

+25
-1
lines changed

3 files changed

+25
-1
lines changed

cmds/inspect.c

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -878,9 +878,27 @@ static int print_list_chunks(struct list_chunks_ctx *ctx, const char *sortmode,
878878
}
879879

880880
/* Skip additional sort if nothing defined by user. */
881-
if (comp.count > 0)
881+
if (comp.count > 0) {
882+
#ifdef __ANDROID__
883+
for (i = comp.count - 1; i >= 0; i--) {
884+
if (comp.id[i] == CHUNK_SORT_PSTART) {
885+
qsort(ctx->stats, ctx->length, sizeof(ctx->stats[0]), (sort_cmp_t)cmp_cse_pstart);
886+
}
887+
else if (comp.id[i] == CHUNK_SORT_LSTART) {
888+
qsort(ctx->stats, ctx->length, sizeof(ctx->stats[0]), (sort_cmp_t)cmp_cse_lstart);
889+
}
890+
else if (comp.id[i] == CHUNK_SORT_USAGE) {
891+
qsort(ctx->stats, ctx->length, sizeof(ctx->stats[0]), (sort_cmp_t)cmp_cse_usage);
892+
}
893+
else if (comp.id[i] == CHUNK_SORT_LENGTH) {
894+
qsort(ctx->stats, ctx->length, sizeof(ctx->stats[0]), (sort_cmp_t)cmp_cse_length);
895+
}
896+
}
897+
#else
882898
qsort_r(ctx->stats, ctx->length, sizeof(ctx->stats[0]),
883899
(sort_r_cmp_t)compare_cmp_multi, &comp);
900+
#endif
901+
}
884902

885903
col_count = 9;
886904
/* Two rows for header and separator. */

common/sort-utils.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,9 @@ int compare_add_sort_id(struct compare *comp, int id)
7878
if (comp->sortdef[i].name == NULL)
7979
return -1;
8080
if (comp->sortdef[i].id == id) {
81+
#ifdef __ANDROID__
82+
comp->id[comp->count] = id;
83+
#endif
8184
comp->comp[comp->count] = comp->sortdef[i].comp;
8285
comp->count++;
8386
break;

common/sort-utils.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,9 @@ struct compare {
8989
unsigned long invert_map;
9090
int count;
9191
const struct sortdef *sortdef;
92+
#ifdef __ANDROID__
93+
int id[SORT_MAX_KEYS];
94+
#endif
9295
};
9396

9497
int compare_init(struct compare *comp, const struct sortdef *sortdef);

0 commit comments

Comments
 (0)