From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-wr1-f53.google.com (mail-wr1-f53.google.com [209.85.221.53]) by mx.groups.io with SMTP id smtpd.web11.75468.1629408563730652193 for ; Thu, 19 Aug 2021 14:29:24 -0700 Authentication-Results: mx.groups.io; dkim=pass header.i=@gmail.com header.s=20161025 header.b=jwiwVqZN; spf=pass (domain: gmail.com, ip: 209.85.221.53, mailfrom: pedro.falcato@gmail.com) Received: by mail-wr1-f53.google.com with SMTP id l11so11098014wrx.4 for ; Thu, 19 Aug 2021 14:29:23 -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=AfDnE6T55owny1+0+A9yjh8pkSXxN6deIxqXc2fHbY0=; b=jwiwVqZNJ1IdxthikI2K8IBaPQ34OVxqe5IzbWVXatPfafyS5A0uFBjcV469wyxFrm QF19ndmPkVbcEaTs0WsF1s92OK5pGUcIUg++sDJBTpu8zkdRjZInGJ2WUzIYnd6mtOe8 wcpCw8yV8FYVIOL2hlUsaOJJYLFpGPs13gmoPnguWC0/J3KnhLlGN53EQzYfC+HYIjQg +7ee/dOjvzcsDOq99OMYGnm03Wf2f9gSYMCqIzj3QDu8ejUvnuhJ3ZDs9kmFgMJNs0st twdT0EAh6NQdPgGMG7XzLLOS7e3M+riQQ7V5TkzKLhA9M9GYUtvn/SHJQRhW3L2aFO0e K2qA== 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=AfDnE6T55owny1+0+A9yjh8pkSXxN6deIxqXc2fHbY0=; b=f+9UlJamQCh/TM5HKF8OoWafQQw/id2ZZF2FQ3Phj6+glqvQGodeu7Y5KrN0n8dRgJ aPKw7UbfEqXBIG6Ntyq5R8m11Wiad13YWLsYvzRl+uce0AfNnsQRIf5Kle/BkVfyn79F YufHKb7DjByvdb9s0kAnpv696j2QbB1LtFsOmUSH9pul53qbR/zgkfPjIWUn9vGRJL91 T5rcJo7t4CWsSttYhSlB4QnlVaROQOn959byJtRjpW/T0JtOaKfeowfmQ6+Ju67hfcKj 8ZlxmEtnCfV/nUVGHOYuyPVoCycTKvlmHJwd2htgApEq0YtjuoQGZaGeERjm6nb93Okr iSCA== X-Gm-Message-State: AOAM531U+jT63MI1cVvPsuqPV3l4/YACkKsWZf9dEdGGycpCKul67w2j GXurdGzesAUlGhexK9v9jWS1V2rXo4hXPDqG X-Google-Smtp-Source: ABdhPJxp7BLnDi0pm6iEo4BeqdK5/KDcnn2cWEVhuzby/xPyvGo0HOwsFJe/+w9JJEv5cpjUZGRbDA== X-Received: by 2002:a5d:668f:: with SMTP id l15mr6318259wru.390.1629408562124; Thu, 19 Aug 2021 14:29:22 -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 p12sm3118387wmq.44.2021.08.19.14.29.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 19 Aug 2021 14:29:21 -0700 (PDT) From: "Pedro Falcato" To: devel@edk2.groups.io Cc: Pedro Falcato , Leif Lindholm , Michael D Kinney , Bret Barkelew Subject: [PATCH 2/3] Ext4Pkg: Hide "." and ".." entries from Read() callers. Date: Thu, 19 Aug 2021 22:28:42 +0100 Message-Id: <20210819212843.37454-3-pedro.falcato@gmail.com> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20210819212843.37454-1-pedro.falcato@gmail.com> References: <20210819212843.37454-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 081c6bf0f4..c85c4df6d5 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 93f0a8a04a..1aafc60ab5 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 1bbff9e69f..982b19c763 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