From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-wm1-f51.google.com (mail-wm1-f51.google.com [209.85.128.51]) by mx.groups.io with SMTP id smtpd.web09.11632.1629557300939028022 for ; Sat, 21 Aug 2021 07:48:21 -0700 Authentication-Results: mx.groups.io; dkim=pass header.i=@gmail.com header.s=20161025 header.b=nquvC1M9; spf=pass (domain: gmail.com, ip: 209.85.128.51, mailfrom: pedro.falcato@gmail.com) Received: by mail-wm1-f51.google.com with SMTP id u15so7632738wmj.1 for ; Sat, 21 Aug 2021 07:48:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=MYSUv2gSFvhd4i/E59+0DRKNLLPyfhK9MD/12+VdzdE=; b=nquvC1M99Q/Mu1V1R4pbLh5Xv6/mv194kLwgaKZNCBFX3g4TWRnA8wg8882JNuxM1R zlBxd2AmfsMMmGvpVI5Kc5CDFJp/nx2EudJI6YhTmZ+qErEjhuFz0PPGOH+6bO/WZ4Ut 5Q/ldqAdFioIt2arSfa3QYPq0HII/AF0CV30j1jYHb9eTfARaR8ZYfl6zrDdIly8trwn Nfjuy18bBdwuL2l99nKa3xjdQX22x/rGTYHKiJ3TmxPiGjfCxD4yMNc30Fz1KvfWdboM 7fo4ca0ZOn2CGopFuRb3yiPpeDrA0Or/zbGJRjzurxkCEuBvvpHX0N0nvni8DzxjpMVh NvBg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=MYSUv2gSFvhd4i/E59+0DRKNLLPyfhK9MD/12+VdzdE=; b=m+k/1XzispuYGpFmb4nAzyANJyiyK6KqsaSiVgiLophKBS9olPIuc1oLTXyUo53J1y TKrNWIRYXk4qM0BXkxP7Qomzj8YlMy7laJbYYzcEkRwaKE73UtrwMhH+GlaPoR/F1O9V 7I+Y1Vzs/UCKQ+HpWn5kn5kNwmMNUQuHUOaimasr90XZv8NhoL5l8W53AEt59WcvwIa3 M72Wy78r62JuOlrRMGbI1p/ADlGkSLmzDlSNC20snquE8k96dUq+r3s/YFxoEFLN3CEM 2T4ikYFwF4pONstDDGIbWKNSDUKsWmxvqtlgze8PCvz4cTbMx4yZhsTjf1unW836bXCf mV9w== X-Gm-Message-State: AOAM532jNbLswJ8fOzqOgTYWElzt6wdH7Qa0H87VOg3v5LMAA8TQAMtI DeyK1uyOlM20pt7V4sUNf7z7ZZ92mVBEp2gj X-Google-Smtp-Source: ABdhPJzVObs6KcQLJr87wXKqkoRGejvnurhDU7eEIRbn+pBYHSce2YKZPjKW2ML44Uttyeu9L5PgUQ== X-Received: by 2002:a05:600c:154f:: with SMTP id f15mr7349712wmg.124.1629557299294; Sat, 21 Aug 2021 07:48:19 -0700 (PDT) Return-Path: Received: from PC-PEDRO.lan (bl8-253-151.dsl.telepac.pt. [85.241.253.151]) by smtp.gmail.com with ESMTPSA id j17sm9118036wrt.69.2021.08.21.07.48.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 21 Aug 2021 07:48:19 -0700 (PDT) From: "Pedro Falcato" To: devel@edk2.groups.io Cc: Pedro Falcato , Leif Lindholm , Michael D Kinney , Bret Barkelew Subject: [edk2-platforms PATCH v2 2/5] Ext4Pkg: Hide "." and ".." entries from Read() callers. Date: Sat, 21 Aug 2021 15:47:07 +0100 Message-Id: <20210821144711.39546-3-pedro.falcato@gmail.com> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20210821144711.39546-1-pedro.falcato@gmail.com> References: <20210821144711.39546-1-pedro.falcato@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable This makes it so callers that may expect FAT32 filesystems (most do) have more normal looking ReadDir() results. This commit also presents a better filename for files opened through Open("."). Cc: Leif Lindholm Cc: Michael D Kinney Cc: Bret Barkelew Signed-off-by: Pedro Falcato --- Features/Ext4Pkg/Ext4Dxe/Directory.c | 45 +++++++++++++++++++++++----- Features/Ext4Pkg/Ext4Dxe/Ext4Dxe.h | 4 ++- Features/Ext4Pkg/Ext4Dxe/Inode.c | 2 +- 3 files changed, 41 insertions(+), 10 deletions(-) diff --git a/Features/Ext4Pkg/Ext4Dxe/Directory.c b/Features/Ext4Pkg/Ext4Dx= e/Directory.c index 081c6bf0f435..c85c4df6d5c5 100644 --- a/Features/Ext4Pkg/Ext4Dxe/Directory.c +++ b/Features/Ext4Pkg/Ext4Dxe/Directory.c @@ -180,6 +180,7 @@ Ext4RetrieveDirent ( @param[in] OpenMode Mode in which the file is supposed to be op= en.=0D @param[out] OutFile Pointer to the newly opened file.=0D @param[in] Entry Directory entry to be used.=0D + @param[in] Directory Pointer to the opened directory.=0D =0D @retval EFI_STATUS Result of the operation=0D **/=0D @@ -188,12 +189,18 @@ Ext4OpenDirent ( IN EXT4_PARTITION *Partition,=0D IN UINT64 OpenMode,=0D OUT EXT4_FILE **OutFile,=0D - IN EXT4_DIR_ENTRY *Entry=0D + IN EXT4_DIR_ENTRY *Entry,=0D + IN EXT4_FILE *Directory=0D )=0D {=0D EFI_STATUS Status;=0D - CHAR16 FileName[EXT4_NAME_MAX + 1];=0D + CHAR16 FileNameBuf[EXT4_NAME_MAX + 1];=0D EXT4_FILE *File;=0D + CHAR16 *FileName;=0D + UINTN DestMax;=0D +=0D + FileName =3D FileNameBuf;=0D + DestMax =3D ARRAY_SIZE (FileNameBuf);=0D =0D File =3D AllocateZeroPool (sizeof (EXT4_FILE));=0D =0D @@ -202,12 +209,18 @@ Ext4OpenDirent ( goto Error;=0D }=0D =0D - Status =3D Ext4GetUcs2DirentName (Entry, FileName);=0D + Status =3D Ext4GetUcs2DirentName (Entry, FileNameBuf);=0D =0D if (EFI_ERROR (Status)) {=0D goto Error;=0D }=0D =0D + if (StrCmp (FileNameBuf, L".") =3D=3D 0) {=0D + // We're using the parent directory's name=0D + FileName =3D Directory->FileName;=0D + DestMax =3D StrLen (FileName) + 1;=0D + }=0D +=0D File->FileName =3D AllocateZeroPool (StrSize (FileName));=0D =0D if (!File->FileName) {=0D @@ -222,7 +235,7 @@ Ext4OpenDirent ( }=0D =0D // This should not fail.=0D - StrCpyS (File->FileName, ARRAY_SIZE (FileName), FileName);=0D + StrCpyS (File->FileName, DestMax, FileName);=0D =0D File->InodeNum =3D Entry->inode;=0D =0D @@ -290,7 +303,7 @@ Ext4OpenFile ( return EFI_NOT_FOUND;=0D }=0D =0D - return Ext4OpenDirent (Partition, OpenMode, OutFile, &Entry);=0D + return Ext4OpenDirent (Partition, OpenMode, OutFile, &Entry, Directory);= =0D }=0D =0D /**=0D @@ -429,6 +442,8 @@ Ext4ReadDir ( UINT32 BlockRemainder;=0D EXT4_DIR_ENTRY Entry;=0D EXT4_FILE *TempFile;=0D + BOOLEAN ShouldSkip;=0D + BOOLEAN IsDotOrDotDot;=0D =0D DirIno =3D File->Inode;=0D Status =3D EFI_SUCCESS;=0D @@ -470,13 +485,27 @@ Ext4ReadDir ( goto Out;=0D }=0D =0D - if (Entry.inode =3D=3D 0) {=0D - // When inode =3D 0, it's unused=0D + // We don't care about passing . or .. entries to the caller of ReadDi= r(),=0D + // since they're generally useless entries *and* may break things if t= oo=0D + // many callers assume FAT32.=0D +=0D + // Entry.name_len may be 0 if it's a nameless entry, like an unused en= try=0D + // or a checksum at the end of the directory block.=0D + // memcmp (and CompareMem) return 0 when the passed length is 0.=0D +=0D + IsDotOrDotDot =3D Entry.name_len !=3D 0 &&=0D + (CompareMem (Entry.name, ".", Entry.name_len) =3D=3D 0= ||=0D + CompareMem (Entry.name, "..", Entry.name_len) =3D=3D = 0);=0D +=0D + // When inode =3D 0, it's unused.=0D + ShouldSkip =3D Entry.inode =3D=3D 0 || IsDotOrDotDot;=0D +=0D + if (ShouldSkip) {=0D Offset +=3D Entry.rec_len;=0D continue;=0D }=0D =0D - Status =3D Ext4OpenDirent (Partition, EFI_FILE_MODE_READ, &TempFile, &= Entry);=0D + Status =3D Ext4OpenDirent (Partition, EFI_FILE_MODE_READ, &TempFile, &= Entry, File);=0D =0D if (EFI_ERROR (Status)) {=0D goto Out;=0D diff --git a/Features/Ext4Pkg/Ext4Dxe/Ext4Dxe.h b/Features/Ext4Pkg/Ext4Dxe/= Ext4Dxe.h index 93f0a8a04add..1aafc60ab57d 100644 --- a/Features/Ext4Pkg/Ext4Dxe/Ext4Dxe.h +++ b/Features/Ext4Pkg/Ext4Dxe/Ext4Dxe.h @@ -353,6 +353,7 @@ Ext4OpenFile ( @param[in] OpenMode Mode in which the file is supposed to be op= en.=0D @param[out] OutFile Pointer to the newly opened file.=0D @param[in] Entry Directory entry to be used.=0D + @param[in] Directory Pointer to the opened directory.=0D =0D @retval EFI_STATUS Result of the operation=0D **/=0D @@ -361,7 +362,8 @@ Ext4OpenDirent ( IN EXT4_PARTITION *Partition,=0D IN UINT64 OpenMode,=0D OUT EXT4_FILE **OutFile,=0D - IN EXT4_DIR_ENTRY *Entry=0D + IN EXT4_DIR_ENTRY *Entry,=0D + IN EXT4_FILE *Directory=0D );=0D =0D /**=0D diff --git a/Features/Ext4Pkg/Ext4Dxe/Inode.c b/Features/Ext4Pkg/Ext4Dxe/In= ode.c index 1bbff9e69f4c..982b19c763d0 100644 --- a/Features/Ext4Pkg/Ext4Dxe/Inode.c +++ b/Features/Ext4Pkg/Ext4Dxe/Inode.c @@ -89,7 +89,7 @@ Ext4Read ( IN OUT UINTN *Length=0D )=0D {=0D - DEBUG ((DEBUG_FS, "[ext4] Ext4Read(Offset %lu, Length %lu)\n", Offset, *= Length));=0D + DEBUG ((DEBUG_FS, "[ext4] Ext4Read(%s, Offset %lu, Length %lu)\n", File-= >FileName, Offset, *Length));=0D EXT4_INODE *Inode;=0D UINT64 InodeSize;=0D UINT64 CurrentSeek;=0D --=20 2.33.0