From patchwork Sun Nov 29 02:02:06 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Heinrich Schuchardt X-Patchwork-Id: 1407782 X-Patchwork-Delegate: trini@ti.com Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.denx.de (client-ip=2a01:238:438b:c500:173d:9f52:ddab:ee01; helo=phobos.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=gmx.de Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; secure) header.d=gmx.net header.i=@gmx.net header.a=rsa-sha256 header.s=badeba3b8450 header.b=aa8ODtgW; dkim-atps=neutral Received: from phobos.denx.de (phobos.denx.de [IPv6:2a01:238:438b:c500:173d:9f52:ddab:ee01]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4CkBXd49zMz9s0b for ; Sun, 29 Nov 2020 13:05:37 +1100 (AEDT) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 06FCA82777; Sun, 29 Nov 2020 03:03:53 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=none (p=none dis=none) header.from=gmx.de Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (1024-bit key; secure) header.d=gmx.net header.i=@gmx.net header.b="aa8ODtgW"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id EB0D28274A; Sun, 29 Nov 2020 03:03:05 +0100 (CET) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=-2.6 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,FREEMAIL_FROM,RCVD_IN_DNSWL_LOW,RCVD_IN_MSPIKE_H2, SPF_HELO_NONE autolearn=ham autolearn_force=no version=3.4.2 Received: from mout.gmx.net (mout.gmx.net [212.227.15.19]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id 7A1BF8275D for ; Sun, 29 Nov 2020 03:02:45 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=none (p=none dis=none) header.from=gmx.de Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=xypron.glpk@gmx.de DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gmx.net; s=badeba3b8450; t=1606615352; bh=Vw2QIncgvWVmgKYZVyAU270NrPvsrnAonT4ZfFNEfOo=; h=X-UI-Sender-Class:From:To:Cc:Subject:Date:In-Reply-To:References; b=aa8ODtgWUDdimgj5eVOZKy64zpg+a00aEAtWeiXWzivkP5PHtY4iXrDKfvlXE6XJl rOmeMPW1O4lLwf7n+l658PNIOLGrqOxyQDrfD3BB01z5AMxxDCB5AWxAn5wt1AweXI tld+IdozSgWxBXdRQ1QNdfdNhk2KkXQkWdpnfs2k= X-UI-Sender-Class: 01bb95c1-4bf8-414a-932a-4f6e2808ef9c Received: from mcbin.fritz.box ([62.143.246.89]) by mail.gmx.com (mrgmx005 [212.227.17.184]) with ESMTPSA (Nemesis) id 1Mw9UE-1jthxE2VQC-00s29Z; Sun, 29 Nov 2020 03:02:32 +0100 From: Heinrich Schuchardt To: Tom Rini Cc: Simon Glass , Christian Gmeiner , Jason Wessel , AKASHI Takahiro , Marek Szyprowski , Bin Meng , Reuben Dowle , Marcin Juszkiewicz , Thomas Hebb , Samer El-Haj-Mahmoud , u-boot@lists.denx.de, Heinrich Schuchardt Subject: [PATCH 08/18] fs: fat: call set_name() only once Date: Sun, 29 Nov 2020 03:02:06 +0100 Message-Id: <20201129020216.4865-9-xypron.glpk@gmx.de> X-Mailer: git-send-email 2.29.2 In-Reply-To: <20201129020216.4865-1-xypron.glpk@gmx.de> References: <20201129020216.4865-1-xypron.glpk@gmx.de> MIME-Version: 1.0 X-Provags-ID: V03:K1:f6yBLQId+9u9/4mpimJa5zvSOv/wISR3tkMXphtmhXEbFkqNPzj D3AfCgyCSrTFRkd0dSk78lFPBGearLpUDCPq0bNqSM5P7OTAAOKAv7q5M0mW2PN5pGFUWRX u23XoRK1mbFQ2ol4V4mohJjqSqt/L6nVFOGJHaQhBviyrlqoWR07rZ7deU8fLKGRiQ9fMOD j4vQyY3BDCpfwb2Qql63g== X-UI-Out-Filterresults: notjunk:1;V03:K0:D0m3TQ50qPk=:65LbPS0UyWTASM+eI+4hNY GupDZ9JlxLyFC9pEsCjW/VuIUVdpeKie8QwwMYOTrJe2g8VYdVbbpqgxUBWvC+0ezTr9Id10q q7LypmH0Dz0YesuQ+4bvZehkg7d+/6TTEZnJ/DUZKYCPnP2nrI4heyypfZjT8CkBPh7ZbHEde jgvj14ohC9Q4q4qfGs5iWBuoiB0GE29vWXf04lHUObEG7N90ojUvij9JtOCxKrRAF2Homyg6C cE6Qr7u01vbger2rImViR4N88cpgM8o6t/bCJa0PHzhlXmllPzN9TZGC3W4hMxmf8yMRZu5j6 6UFPyA2ZfdLYPoA0Sq9gVbcnM4WUCeTAL2IstTan6vKYwBCIYL0QLPIHt6UXdwweJl6qW/+tC vMi1g9Xo4XRJTnAImUPtIpfadVPoA3ylUDtOXrH2TnHCKm9xlOq4esAV9OqLqZ1Gz5jU8pr8q Iqhyjy8h9HcmPzJLrayfTU+v5Paf9UvBsvpzLJI7mc0xeIsVJ2a4xCl3t2UYIXe2yHD8Zc9fe TapV100s4g/1EsYYE04+MrAU+4wxrvSuIxJzPUzubeOouF5bHYjl7xaScpSw5smhrmwoPH+zA ZIi4FsDYSYJ4ul3FICDDCEL2M2VDajjIQE6Hzf6B2Cbo+AB6GP+nl2rlKPrgcRiw2fPvw7cSN 3ZF0QSz0wZMrDlXvYQ8yQMxsagWzmWtroBl1Nq1t46ZvWis1GnpAEkS91W2F5MMifbLtRKmXz 2GIcg6L7Y6iZc2ZS12hJAOGMfwIbXa7e4Psclas5iCDHXh6eW+8gLhZm8m0ga45ahJQS1pbN2 5wvWBc3ys+9bhtepP5qPaDXpF+Gj0JWaF0OHYa1/jgN7e9G/dvOxveI6mxz6SnDuj5VQxtc66 wn3MrHsaztYveWpL84cw== X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.34 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.102.3 at phobos.denx.de X-Virus-Status: Clean In set_name() we select the short name. Once this is correctly implemented this will be a performance intensive operation because we need to check that the name does not exist yet. So set_name should only be called once. Signed-off-by: Heinrich Schuchardt --- fs/fat/fat_write.c | 87 ++++++++++++++++++++++++++++------------------ 1 file changed, 54 insertions(+), 33 deletions(-) -- 2.29.2 diff --git a/fs/fat/fat_write.c b/fs/fat/fat_write.c index 7e8886791c..716b6a6627 100644 --- a/fs/fat/fat_write.c +++ b/fs/fat/fat_write.c @@ -23,6 +23,9 @@ /* Characters that may only be used in long file names */ static const char LONG_ONLY_CHARS[] = "+,;=[]"; +/* Combined size of the name and ext fields in the directory entry */ +#define SHORT_NAME_SIZE 11 + /** * str2fat() - convert string to valid FAT name characters * @@ -68,24 +71,28 @@ static int str2fat(char *dest, char *src, int length) * * If a long name is needed, a short name is constructed. * - * @dirent: directory entry * @filename: long file name + * @shortname: buffer of 11 bytes to receive chosen short name and extension * Return: number of directory entries needed, negative on error */ -static int set_name(dir_entry *dirent, const char *filename) +static int set_name(const char *filename, char *shortname) { char *period; char *pos; int period_location; char buf[13]; int i; + int ret; + struct { + char name[8]; + char ext[3]; + } dirent; if (!filename) return -EIO; - /* Initialize buffers */ - memset(dirent->name, ' ', sizeof(dirent->name)); - memset(dirent->ext, ' ', sizeof(dirent->ext)); + /* Initialize buffer */ + memset(&dirent, ' ', sizeof(dirent)); /* Convert filename to upper case short name */ period = strrchr(filename, '.'); @@ -95,20 +102,22 @@ static int set_name(dir_entry *dirent, const char *filename) period = 0; } if (period) - str2fat(dirent->ext, period + 1, sizeof(dirent->ext)); - period_location = str2fat(dirent->name, pos, sizeof(dirent->name)); + str2fat(dirent.ext, period + 1, sizeof(dirent.ext)); + period_location = str2fat(dirent.name, pos, sizeof(dirent.name)); if (period_location < 0) return period_location; - if (*dirent->name == ' ') - *dirent->name = '_'; + if (*dirent.name == ' ') + *dirent.name = '_'; /* 0xe5 signals a deleted directory entry. Replace it by 0x05. */ - if (*dirent->name == 0xe5) - *dirent->name = 0x05; + if (*dirent.name == 0xe5) + *dirent.name = 0x05; /* If filename and short name are the same, quit. */ - sprintf(buf, "%.*s.%.3s", period_location, dirent->name, dirent->ext); - if (!strcmp(buf, filename)) - return 1; + sprintf(buf, "%.*s.%.3s", period_location, dirent.name, dirent.ext); + if (!strcmp(buf, filename)) { + ret = 1; + goto out; + } /* Construct an indexed short name */ for (i = 1; i < 0x200000; ++i) { @@ -128,20 +137,24 @@ static int set_name(dir_entry *dirent, const char *filename) suffix_start = 8 - suffix_len; if (suffix_start > period_location) suffix_start = period_location; - memcpy(dirent->name + suffix_start, buf, suffix_len); - if (*dirent->ext != ' ') + memcpy(dirent.name + suffix_start, buf, suffix_len); + if (*dirent.ext != ' ') sprintf(buf, "%.*s.%.3s", suffix_start + suffix_len, - dirent->name, dirent->ext); + dirent.name, dirent.ext); else sprintf(buf, "%.*s", suffix_start + suffix_len, - dirent->name); + dirent.name); debug("short name: %s\n", buf); /* TODO: Check that the short name does not exist yet. */ /* Each long name directory entry takes 13 characters. */ - return (strlen(filename) + 25) / 13; + ret = (strlen(filename) + 25) / 13; + goto out; } return -EIO; +out: + memcpy(shortname, dirent.name, SHORT_NAME_SIZE); + return ret; } static int total_sector; @@ -1019,18 +1032,27 @@ getit: return 0; } -/* - * Fill dir_entry +/** + * fill_dentry() - fill directory entry with shortname + * + * @mydata: private filesystem parameters + * @dentptr: directory entry + * @shortname: chosen short name + * @start_cluster: first cluster of file + * @size: file size + * @attr: file attributes */ static void fill_dentry(fsdata *mydata, dir_entry *dentptr, - const char *filename, __u32 start_cluster, __u32 size, __u8 attr) + const char *shortname, __u32 start_cluster, __u32 size, __u8 attr) { + memset(dentptr, 0, sizeof(*dentptr)); + set_start_cluster(mydata, dentptr, start_cluster); dentptr->size = cpu_to_le32(size); dentptr->attr = attr; - set_name(dentptr, filename); + memcpy(dentptr->name, shortname, SHORT_NAME_SIZE); } /* @@ -1215,6 +1237,7 @@ int file_fat_write_at(const char *filename, loff_t pos, void *buffer, retdent->size = cpu_to_le32(pos + size); } else { /* Create a new file */ + char shortname[SHORT_NAME_SIZE]; if (itr->is_root) { /* root dir cannot have "." or ".." */ @@ -1237,21 +1260,19 @@ int file_fat_write_at(const char *filename, loff_t pos, void *buffer, goto exit; } - memset(itr->dent, 0, sizeof(*itr->dent)); - /* Check if long name is needed */ - ret = set_name(itr->dent, filename); + ret = set_name(filename, shortname); if (ret < 0) goto exit; if (ret > 1) { /* Set long name entries */ - ret = fill_dir_slot(itr, filename, itr->dent->name); + ret = fill_dir_slot(itr, filename, shortname); if (ret) goto exit; } /* Set short name entry */ - fill_dentry(itr->fsdata, itr->dent, filename, 0, size, + fill_dentry(itr->fsdata, itr->dent, shortname, 0, size, ATTR_ARCH); retdent = itr->dent; @@ -1484,6 +1505,8 @@ int fat_mkdir(const char *new_dirname) ret = -EEXIST; goto exit; } else { + char shortname[SHORT_NAME_SIZE]; + if (itr->is_root) { /* root dir cannot have "." or ".." */ if (!strcmp(l_dirname, ".") || @@ -1499,21 +1522,19 @@ int fat_mkdir(const char *new_dirname) goto exit; } - memset(itr->dent, 0, sizeof(*itr->dent)); - /* Check if long name is needed */ - ret = set_name(itr->dent, dirname); + ret = set_name(dirname, shortname); if (ret < 0) goto exit; if (ret > 1) { /* Set long name entries */ - ret = fill_dir_slot(itr, dirname, itr->dent->name); + ret = fill_dir_slot(itr, dirname, shortname); if (ret) goto exit; } /* Set attribute as archive for regular file */ - fill_dentry(itr->fsdata, itr->dent, dirname, 0, 0, + fill_dentry(itr->fsdata, itr->dent, shortname, 0, 0, ATTR_DIR | ATTR_ARCH); retdent = itr->dent;