]> git.proxmox.com Git - mirror_edk2.git/blob - FatPkg/EnhancedFatDxe/DirectoryCache.c
BaseTools: Library hashing fix and optimization for --hash feature
[mirror_edk2.git] / FatPkg / EnhancedFatDxe / DirectoryCache.c
1 /** @file
2 Functions for directory cache operation.
3
4 Copyright (c) 2005, Intel Corporation. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
6
7
8 **/
9
10 #include "Fat.h"
11
12 /**
13
14 Free the directory structure and release the memory.
15
16 @param ODir - The directory to be freed.
17
18 **/
19 STATIC
20 VOID
21 FatFreeODir (
22 IN FAT_ODIR *ODir
23 )
24 {
25 FAT_DIRENT *DirEnt;
26
27 //
28 // Release Directory Entry Nodes
29 //
30 while (!IsListEmpty (&ODir->ChildList)) {
31 DirEnt = DIRENT_FROM_LINK (ODir->ChildList.ForwardLink);
32 RemoveEntryList (&DirEnt->Link);
33 //
34 // Make sure the OFile has been closed
35 //
36 ASSERT (DirEnt->OFile == NULL);
37 FatFreeDirEnt (DirEnt);
38 }
39
40 FreePool (ODir);
41 }
42
43 /**
44
45 Allocate the directory structure.
46
47 @param OFile - The corresponding OFile.
48
49 **/
50 STATIC
51 FAT_ODIR *
52 FatAllocateODir (
53 IN FAT_OFILE *OFile
54 )
55 {
56 FAT_ODIR *ODir;
57
58 ODir = AllocateZeroPool (sizeof (FAT_ODIR));
59 if (ODir != NULL) {
60 //
61 // Initialize the directory entry list
62 //
63 ODir->Signature = FAT_ODIR_SIGNATURE;
64 InitializeListHead (&ODir->ChildList);
65 ODir->CurrentCursor = &ODir->ChildList;
66 }
67
68 return ODir;
69 }
70
71 /**
72
73 Discard the directory structure when an OFile will be freed.
74 Volume will cache this directory if the OFile does not represent a deleted file.
75
76 @param OFile - The OFile whose directory structure is to be discarded.
77
78 **/
79 VOID
80 FatDiscardODir (
81 IN FAT_OFILE *OFile
82 )
83 {
84 FAT_ODIR *ODir;
85 FAT_VOLUME *Volume;
86
87 Volume = OFile->Volume;
88 ODir = OFile->ODir;
89 if (!OFile->DirEnt->Invalid) {
90 //
91 // If OFile does not represent a deleted file, then we will cache the directory
92 // We use OFile's first cluster as the directory's tag
93 //
94 ODir->DirCacheTag = OFile->FileCluster;
95 InsertHeadList (&Volume->DirCacheList, &ODir->DirCacheLink);
96 if (Volume->DirCacheCount == FAT_MAX_DIR_CACHE_COUNT) {
97 //
98 // Replace the least recent used directory
99 //
100 ODir = ODIR_FROM_DIRCACHELINK (Volume->DirCacheList.BackLink);
101 RemoveEntryList (&ODir->DirCacheLink);
102 } else {
103 //
104 // No need to find a replace
105 //
106 Volume->DirCacheCount++;
107 ODir = NULL;
108 }
109 }
110 //
111 // Release ODir Structure
112 //
113 if (ODir != NULL) {
114 FatFreeODir (ODir);
115 }
116 }
117
118 /**
119
120
121 Request the directory structure when an OFile is newly generated.
122 If the directory structure is cached by volume, then just return this directory;
123 Otherwise, allocate a new one for OFile.
124
125 @param OFile - The OFile which requests directory structure.
126
127 **/
128 VOID
129 FatRequestODir (
130 IN FAT_OFILE *OFile
131 )
132 {
133 UINTN DirCacheTag;
134 FAT_VOLUME *Volume;
135 FAT_ODIR *ODir;
136 FAT_ODIR *CurrentODir;
137 LIST_ENTRY *CurrentODirLink;
138
139 Volume = OFile->Volume;
140 ODir = NULL;
141 DirCacheTag = OFile->FileCluster;
142 for (CurrentODirLink = Volume->DirCacheList.ForwardLink;
143 CurrentODirLink != &Volume->DirCacheList;
144 CurrentODirLink = CurrentODirLink->ForwardLink
145 ) {
146 CurrentODir = ODIR_FROM_DIRCACHELINK (CurrentODirLink);
147 if (CurrentODir->DirCacheTag == DirCacheTag) {
148 RemoveEntryList (&CurrentODir->DirCacheLink);
149 Volume->DirCacheCount--;
150 ODir = CurrentODir;
151 break;
152 }
153 }
154
155 if (ODir == NULL) {
156 //
157 // This directory is not cached, then allocate a new one
158 //
159 ODir = FatAllocateODir (OFile);
160 }
161
162 OFile->ODir = ODir;
163 }
164
165 /**
166
167 Clean up all the cached directory structures when the volume is going to be abandoned.
168
169 @param Volume - FAT file system volume.
170
171 **/
172 VOID
173 FatCleanupODirCache (
174 IN FAT_VOLUME *Volume
175 )
176 {
177 FAT_ODIR *ODir;
178 while (Volume->DirCacheCount > 0) {
179 ODir = ODIR_FROM_DIRCACHELINK (Volume->DirCacheList.BackLink);
180 RemoveEntryList (&ODir->DirCacheLink);
181 FatFreeODir (ODir);
182 Volume->DirCacheCount--;
183 }
184 }