From patchwork Fri Apr 3 14:04:50 2026 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Simon Glass X-Patchwork-Id: 2111 Return-Path: X-Original-To: u-boot-concept@u-boot.org Delivered-To: u-boot-concept@u-boot.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1775225205; bh=JdUXrncmNlbxjMaDPaaWU39FmWj/oll7KDQF6DLYkJY=; h=From:To:Date:In-Reply-To:References:CC:Subject:List-Id: List-Archive:List-Help:List-Owner:List-Post:List-Subscribe: List-Unsubscribe:From; b=hpHgjkvhwKyykparm/nLTinsGDS6NJXtnH3AFlwBE++YSoy+WTleYZdzpdbIYswey QhVTcSKd9poBvpdgjENzoEvBXfBWtvOoBv568Jr3dO9fbvW/ri5LS1joWso4SzZfpZ vQBJR4OrcWxjchYf9ya2aaUbD1aQ7RQCo+kL2/uwwflD/WhCObb1hgI/kHnMa0cQc6 R2QTmy+cCFl2rRFN0QsTc++g0UPYiFBiwvYeGGeJC0GOq16blvBc9EgQNetw5aCSXQ k0J05xe44p4Y7N4cknORutX68c0olnAhFS38xYIMdtypfT+FARJnhQuaPKWIwbIKx/ +xHVHWys+zCzw== Received: from localhost (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id 479D36A375 for ; Fri, 3 Apr 2026 08:06:45 -0600 (MDT) X-Virus-Scanned: Debian amavis at Received: from mail.u-boot.org ([127.0.0.1]) by localhost (mail.u-boot.org [127.0.0.1]) (amavis, port 10024) with ESMTP id l83OWjzLB_y6 for ; Fri, 3 Apr 2026 08:06:45 -0600 (MDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1775225204; bh=JdUXrncmNlbxjMaDPaaWU39FmWj/oll7KDQF6DLYkJY=; h=From:To:Date:In-Reply-To:References:CC:Subject:List-Id: List-Archive:List-Help:List-Owner:List-Post:List-Subscribe: List-Unsubscribe:From; b=R8cQMZ2aP9urEzAEm19RedzlFZB8Xb2ZV38ySyd4NjF1P9XV5nw9un+7sGp4DkAWQ lAVc5qD/5ljsD/aRy7aznDAl/4K7cK8s2pdDhcjBSwKFISc0AglwEQVJlFBcX53NsJ 7YMAi7IsdR25NyJhK1UZaZVRmcr8QiGb3HoYow/SrTeNKRbMCpdJcagm11K1NQjFhr nfpijRrXwpWBMD+8hWjWTwcBDJslLQgEXq+I2LQj4QYLIJyaGQrJRNjY03oSMvUQvu mjzzmhYZAScdNFRA9Yyqp6U2wFgc0bm/KI8Iinzoaw+byntnQcfma7h5iz+AzeQ+RZ dL3R93nD8NKow== Received: from mail.u-boot.org (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id 93D5F6A366 for ; Fri, 3 Apr 2026 08:06:44 -0600 (MDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1775225200; bh=O+S3hFfNmfIZ2bUlrfbxl35NvsX+rdXTVc1Dy7aFpF4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=U1+iQ2dhx84lkLIa0IEWj/B7Ivhh7bbOU+819mdnp4ZXh0a1wvfgNRAqw+Nbfd4w1 KpRuZLC/cI6o5dnIW/Y0YVBOxAAVHtpO91Du7pB8qPdnIy9fv0NZCjs26gC+AZshRz DjkrWNviv3fcK3H/GVu8HKeInUtQgxxFRqWqYXkKC80hIxfyVYUJ0sfgJXKWEtoU5j NkTRjSWWzg4y/idf2vBsvxffCYfWUsLYOYBR9jvXfTX/BvOwB7B9iBn2Dh/tFTVQok uto9I2LFEiC6PNGIyB7sBIByC5K7QNSApGCQVCFId2DwbxEmP35326UdhqzQluXz79 D9aT9G63LOv1A== Received: from localhost (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id 9C86267EE6; Fri, 3 Apr 2026 08:06:40 -0600 (MDT) X-Virus-Scanned: Debian amavis at Received: from mail.u-boot.org ([127.0.0.1]) by localhost (mail.u-boot.org [127.0.0.1]) (amavis, port 10026) with ESMTP id xccWoIYRAIff; Fri, 3 Apr 2026 08:06:40 -0600 (MDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1775225198; bh=M/4BJS5JnXZasyzfAF9Gj5WHyVcK3bpnszGjA6bnM/k=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=QZY15SmNhL0CZXkjl/xJXhirNvQObLFEsiV4Yr6rwTUXN5b4aOyftTRdFlmmbMewE t+D3PIuFwiN+l3t33tYfhlVcg/EPHd8aBDRG81A3oIsxm49C59u3Z3G4ASa0Xjs0Bv 81sH67ZkhICFe8YhD80RvDaHrEOrIy48/x2pD3xGHonOb9mt168TopNU3RKoV13MYj CPp1wqfyHx01UJhFp612E2I0qCDl1g0zCNh54yZCW+EcK63re68wddra0BJmeOsTCY x7VwNuZagzerxqCzw5Xd0yMHXLVQav0h6A5dZY/FsHa0Ak8MFuAjP2+vopZhf+NF92 JlUtg13gsPMKA== Received: from u-boot.org (unknown [73.34.74.121]) by mail.u-boot.org (Postfix) with ESMTPSA id DE8E56A34E; Fri, 3 Apr 2026 08:06:37 -0600 (MDT) From: Simon Glass To: U-Boot Concept Date: Fri, 3 Apr 2026 08:04:50 -0600 Message-ID: <20260403140523.1998228-29-sjg@u-boot.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20260403140523.1998228-1-sjg@u-boot.org> References: <20260403140523.1998228-1-sjg@u-boot.org> MIME-Version: 1.0 Message-ID-Hash: VYDC42ZOKK2WEMCAXCKHYR5PEAOL7TR6 X-Message-ID-Hash: VYDC42ZOKK2WEMCAXCKHYR5PEAOL7TR6 X-MailFrom: sjg@u-boot.org X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; loop; banned-address; emergency; member-moderation; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header CC: Simon Glass X-Mailman-Version: 3.3.10 Precedence: list Subject: [Concept] [PATCH 28/34] vfs: Sort directory listing output alphabetically List-Id: Discussion and patches related to U-Boot Concept Archived-At: List-Archive: List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: From: Simon Glass Collect directory entries into an array and sort them by name before printing, so that vfs_ls() produces deterministic output regardless of the on-disk directory-entry order. Signed-off-by: Simon Glass --- fs/vfs.c | 49 ++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 42 insertions(+), 7 deletions(-) diff --git a/fs/vfs.c b/fs/vfs.c index 2e781e00d7e..c22b8fa37dc 100644 --- a/fs/vfs.c +++ b/fs/vfs.c @@ -22,6 +22,7 @@ #include #include #include +#include #include #include "vfs_internal.h" #include @@ -802,15 +803,25 @@ int vfs_stat(const char *path, struct fs_dirent *dent) return log_msg_ret("vsn", -ENOENT); } +static int dirent_cmp(const void *a, const void *b) +{ + const struct fs_dirent *da = a; + const struct fs_dirent *db = b; + + return strcmp(da->name, db->name); +} + int vfs_ls(const char *path) { char resolved[FILE_MAX_PATH_LEN]; struct udevice *mnt, *dir = NULL; struct fs_dir_stream *strm; + struct fs_dirent *entries; struct fs_dirent dent; const char *subpath; - bool empty = true; - int ret; + int count = 0; + int max = 64; + int ret, i; ret = vfs_resolve_mount(path, resolved, sizeof(resolved), &mnt, &subpath); @@ -830,17 +841,41 @@ int vfs_ls(const char *path) if (ret) return ret; + entries = malloc(max * sizeof(*entries)); + if (!entries) + return -ENOMEM; + ret = dir_open(dir, &strm); - if (ret) + if (ret) { + free(entries); return ret; + } while (!dir_read(dir, strm, &dent)) { - if (dent.type == FS_DT_DIR) - printf("DIR %10u %s\n", 0, dent.name); + if (count == max) { + struct fs_dirent *tmp; + + max *= 2; + tmp = realloc(entries, max * sizeof(*entries)); + if (!tmp) { + free(entries); + return -ENOMEM; + } + entries = tmp; + } + entries[count++] = dent; + } + + qsort(entries, count, sizeof(*entries), dirent_cmp); + + for (i = 0; i < count; i++) { + if (entries[i].type == FS_DT_DIR) + printf("DIR %10u %s\n", 0, entries[i].name); else - printf(" %10llu %s\n", dent.size, dent.name); - empty = false; + printf(" %10llu %s\n", entries[i].size, + entries[i].name); } + free(entries); dir_close(dir, strm);