xref: /aosp_15_r20/external/mtools/dirCache.c (revision d5c9a868b113e0ec0db2f27bc2ce8a253e77c4b0)
1*d5c9a868SElliott Hughes /*  Copyright 1998,2001-2003,2007-2009 Alain Knaff.
2*d5c9a868SElliott Hughes  *  This file is part of mtools.
3*d5c9a868SElliott Hughes  *
4*d5c9a868SElliott Hughes  *  Mtools is free software: you can redistribute it and/or modify
5*d5c9a868SElliott Hughes  *  it under the terms of the GNU General Public License as published by
6*d5c9a868SElliott Hughes  *  the Free Software Foundation, either version 3 of the License, or
7*d5c9a868SElliott Hughes  *  (at your option) any later version.
8*d5c9a868SElliott Hughes  *
9*d5c9a868SElliott Hughes  *  Mtools is distributed in the hope that it will be useful,
10*d5c9a868SElliott Hughes  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11*d5c9a868SElliott Hughes  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12*d5c9a868SElliott Hughes  *  GNU General Public License for more details.
13*d5c9a868SElliott Hughes  *
14*d5c9a868SElliott Hughes  *  You should have received a copy of the GNU General Public License
15*d5c9a868SElliott Hughes  *  along with Mtools.  If not, see <http://www.gnu.org/licenses/>.
16*d5c9a868SElliott Hughes  */
17*d5c9a868SElliott Hughes #include "sysincludes.h"
18*d5c9a868SElliott Hughes #include "vfat.h"
19*d5c9a868SElliott Hughes #include "dirCache.h"
20*d5c9a868SElliott Hughes #include "dirCacheP.h"
21*d5c9a868SElliott Hughes #include <assert.h>
22*d5c9a868SElliott Hughes 
23*d5c9a868SElliott Hughes 
24*d5c9a868SElliott Hughes #define BITS_PER_INT (sizeof(unsigned int) * 8)
25*d5c9a868SElliott Hughes 
26*d5c9a868SElliott Hughes 
rol(uint32_t arg,int shift)27*d5c9a868SElliott Hughes static __inline__ uint32_t rol(uint32_t arg, int shift)
28*d5c9a868SElliott Hughes {
29*d5c9a868SElliott Hughes 	arg &= 0xffffffff; /* for 64 bit machines */
30*d5c9a868SElliott Hughes 	return (arg << shift) | (arg >> (32 - shift));
31*d5c9a868SElliott Hughes }
32*d5c9a868SElliott Hughes 
33*d5c9a868SElliott Hughes 
calcHash(wchar_t * name)34*d5c9a868SElliott Hughes static uint32_t calcHash(wchar_t *name)
35*d5c9a868SElliott Hughes {
36*d5c9a868SElliott Hughes 	uint32_t hash;
37*d5c9a868SElliott Hughes 	unsigned int i;
38*d5c9a868SElliott Hughes 	wint_t c;
39*d5c9a868SElliott Hughes 
40*d5c9a868SElliott Hughes 	hash = 0;
41*d5c9a868SElliott Hughes 	i = 0;
42*d5c9a868SElliott Hughes 	while(*name) {
43*d5c9a868SElliott Hughes 		/* rotate it */
44*d5c9a868SElliott Hughes 		hash = rol(hash,5); /* a shift of 5 makes sure we spread quickly
45*d5c9a868SElliott Hughes 				     * over the whole width, moreover, 5 is
46*d5c9a868SElliott Hughes 				     * prime with 32, which makes sure that
47*d5c9a868SElliott Hughes 				     * successive letters cannot cover each
48*d5c9a868SElliott Hughes 				     * other easily */
49*d5c9a868SElliott Hughes 		c = towupper((wint_t)*name);
50*d5c9a868SElliott Hughes 		hash ^=  (c * (c+2)) ^ (i * (i+2));
51*d5c9a868SElliott Hughes 		hash &= 0xffffffff;
52*d5c9a868SElliott Hughes 		i++;
53*d5c9a868SElliott Hughes 		name++;
54*d5c9a868SElliott Hughes 	}
55*d5c9a868SElliott Hughes 	hash = hash * (hash + 2);
56*d5c9a868SElliott Hughes 	/* the following two xors make sure all info is spread evenly over all
57*d5c9a868SElliott Hughes 	 * bytes. Important if we only keep the low order bits later on */
58*d5c9a868SElliott Hughes 	hash ^= (hash & 0xfff) << 12;
59*d5c9a868SElliott Hughes 	hash ^= (hash & 0xff000) << 24;
60*d5c9a868SElliott Hughes 	return hash;
61*d5c9a868SElliott Hughes }
62*d5c9a868SElliott Hughes 
addBit(unsigned int * bitmap,unsigned int hash,int checkOnly)63*d5c9a868SElliott Hughes static unsigned int addBit(unsigned int *bitmap,
64*d5c9a868SElliott Hughes 			   unsigned int hash, int checkOnly)
65*d5c9a868SElliott Hughes {
66*d5c9a868SElliott Hughes 	unsigned int bit;
67*d5c9a868SElliott Hughes 	int entry;
68*d5c9a868SElliott Hughes 
69*d5c9a868SElliott Hughes 	bit = 1u << (hash % BITS_PER_INT);
70*d5c9a868SElliott Hughes 	entry = (hash / BITS_PER_INT) % DC_BITMAP_SIZE;
71*d5c9a868SElliott Hughes 
72*d5c9a868SElliott Hughes 	if(checkOnly)
73*d5c9a868SElliott Hughes 		return bitmap[entry] & bit;
74*d5c9a868SElliott Hughes 	else {
75*d5c9a868SElliott Hughes 		bitmap[entry] |= bit;
76*d5c9a868SElliott Hughes 		return 1;
77*d5c9a868SElliott Hughes 	}
78*d5c9a868SElliott Hughes }
79*d5c9a868SElliott Hughes 
_addHash(dirCache_t * cache,unsigned int hash,int checkOnly)80*d5c9a868SElliott Hughes static int _addHash(dirCache_t *cache, unsigned int hash, int checkOnly)
81*d5c9a868SElliott Hughes {
82*d5c9a868SElliott Hughes 	return
83*d5c9a868SElliott Hughes 		addBit(cache->bm0, hash, checkOnly) &&
84*d5c9a868SElliott Hughes 		addBit(cache->bm1, rol(hash,12), checkOnly) &&
85*d5c9a868SElliott Hughes 		addBit(cache->bm2, rol(hash,24), checkOnly);
86*d5c9a868SElliott Hughes }
87*d5c9a868SElliott Hughes 
88*d5c9a868SElliott Hughes 
addNameToHash(dirCache_t * cache,wchar_t * name)89*d5c9a868SElliott Hughes static void addNameToHash(dirCache_t *cache, wchar_t *name)
90*d5c9a868SElliott Hughes {
91*d5c9a868SElliott Hughes 	_addHash(cache, calcHash(name), 0);
92*d5c9a868SElliott Hughes }
93*d5c9a868SElliott Hughes 
hashDce(dirCache_t * cache,dirCacheEntry_t * dce)94*d5c9a868SElliott Hughes static void hashDce(dirCache_t *cache, dirCacheEntry_t *dce)
95*d5c9a868SElliott Hughes {
96*d5c9a868SElliott Hughes 	if(dce->beginSlot != cache->nrHashed)
97*d5c9a868SElliott Hughes 		return;
98*d5c9a868SElliott Hughes 	cache->nrHashed = dce->endSlot;
99*d5c9a868SElliott Hughes 	if(dce->longName)
100*d5c9a868SElliott Hughes 		addNameToHash(cache, dce->longName);
101*d5c9a868SElliott Hughes 	addNameToHash(cache, dce->shortName);
102*d5c9a868SElliott Hughes }
103*d5c9a868SElliott Hughes 
isHashed(dirCache_t * cache,wchar_t * name)104*d5c9a868SElliott Hughes int isHashed(dirCache_t *cache, wchar_t *name)
105*d5c9a868SElliott Hughes {
106*d5c9a868SElliott Hughes 	int ret;
107*d5c9a868SElliott Hughes 
108*d5c9a868SElliott Hughes 	ret =  _addHash(cache, calcHash(name), 1);
109*d5c9a868SElliott Hughes 	return ret;
110*d5c9a868SElliott Hughes }
111*d5c9a868SElliott Hughes 
growDirCache(dirCache_t * cache,unsigned int slot)112*d5c9a868SElliott Hughes int growDirCache(dirCache_t *cache, unsigned int slot)
113*d5c9a868SElliott Hughes {
114*d5c9a868SElliott Hughes 	if((int) slot < 0) {
115*d5c9a868SElliott Hughes 		fprintf(stderr, "Bad slot %d\n", slot);
116*d5c9a868SElliott Hughes 		exit(1);
117*d5c9a868SElliott Hughes 	}
118*d5c9a868SElliott Hughes 
119*d5c9a868SElliott Hughes 	if( cache->nr_entries <= slot) {
120*d5c9a868SElliott Hughes 		unsigned int i;
121*d5c9a868SElliott Hughes 
122*d5c9a868SElliott Hughes 		cache->entries = realloc(cache->entries,
123*d5c9a868SElliott Hughes 					 (slot+1) * 2 *
124*d5c9a868SElliott Hughes 					 sizeof(dirCacheEntry_t *));
125*d5c9a868SElliott Hughes 		if(!cache->entries)
126*d5c9a868SElliott Hughes 			return -1;
127*d5c9a868SElliott Hughes 		for(i= cache->nr_entries; i < (slot+1) * 2; i++) {
128*d5c9a868SElliott Hughes 			cache->entries[i] = 0;
129*d5c9a868SElliott Hughes 		}
130*d5c9a868SElliott Hughes 		cache->nr_entries = (slot+1) * 2;
131*d5c9a868SElliott Hughes 	}
132*d5c9a868SElliott Hughes 	return 0;
133*d5c9a868SElliott Hughes }
134*d5c9a868SElliott Hughes 
allocDirCache(Stream_t * Stream,unsigned int slot)135*d5c9a868SElliott Hughes dirCache_t *allocDirCache(Stream_t *Stream, unsigned int slot)
136*d5c9a868SElliott Hughes {
137*d5c9a868SElliott Hughes 	dirCache_t **dcp;
138*d5c9a868SElliott Hughes 
139*d5c9a868SElliott Hughes 	if((int)slot < 0) {
140*d5c9a868SElliott Hughes 		fprintf(stderr, "Bad slot %d\n", slot);
141*d5c9a868SElliott Hughes 		exit(1);
142*d5c9a868SElliott Hughes 	}
143*d5c9a868SElliott Hughes 
144*d5c9a868SElliott Hughes 	dcp = getDirCacheP(Stream);
145*d5c9a868SElliott Hughes 	if(!*dcp) {
146*d5c9a868SElliott Hughes 		*dcp = New(dirCache_t);
147*d5c9a868SElliott Hughes 		if(!*dcp)
148*d5c9a868SElliott Hughes 			return 0;
149*d5c9a868SElliott Hughes 		(*dcp)->entries = NewArray((slot+1)*2+5, dirCacheEntry_t *);
150*d5c9a868SElliott Hughes 		if(!(*dcp)->entries) {
151*d5c9a868SElliott Hughes 			free(*dcp);
152*d5c9a868SElliott Hughes 			return 0;
153*d5c9a868SElliott Hughes 		}
154*d5c9a868SElliott Hughes 		(*dcp)->nr_entries = (slot+1) * 2;
155*d5c9a868SElliott Hughes 		memset( (*dcp)->bm0, 0, sizeof((*dcp)->bm0));
156*d5c9a868SElliott Hughes 		memset( (*dcp)->bm1, 0, sizeof((*dcp)->bm1));
157*d5c9a868SElliott Hughes 		memset( (*dcp)->bm2, 0, sizeof((*dcp)->bm1));
158*d5c9a868SElliott Hughes 		(*dcp)->nrHashed = 0;
159*d5c9a868SElliott Hughes 	} else
160*d5c9a868SElliott Hughes 		if(growDirCache(*dcp, slot) < 0)
161*d5c9a868SElliott Hughes 			return 0;
162*d5c9a868SElliott Hughes 	return *dcp;
163*d5c9a868SElliott Hughes }
164*d5c9a868SElliott Hughes 
165*d5c9a868SElliott Hughes /* Free a range of entries. The range being cleared is always aligned
166*d5c9a868SElliott Hughes  * on the begin of an entry. Entries which are cleared entirely are
167*d5c9a868SElliott Hughes  * free'd. Entries which are cleared half-way (can only happen at end)
168*d5c9a868SElliott Hughes  * are "shortened" by moving its beginSlot
169*d5c9a868SElliott Hughes  * If this was a range representing space after end-mark, return beginning
170*d5c9a868SElliott Hughes  * of range if it had been shortened in its lifetime (which means that end
171*d5c9a868SElliott Hughes  * mark must have moved => may need to be rewritten)
172*d5c9a868SElliott Hughes  */
freeDirCacheRange(dirCache_t * cache,unsigned int beginSlot,unsigned int endSlot)173*d5c9a868SElliott Hughes static int freeDirCacheRange(dirCache_t *cache,
174*d5c9a868SElliott Hughes 			     unsigned int beginSlot,
175*d5c9a868SElliott Hughes 			     unsigned int endSlot)
176*d5c9a868SElliott Hughes {
177*d5c9a868SElliott Hughes 	dirCacheEntry_t *entry;
178*d5c9a868SElliott Hughes 	unsigned int clearBegin;
179*d5c9a868SElliott Hughes 	unsigned int clearEnd;
180*d5c9a868SElliott Hughes 	unsigned int i;
181*d5c9a868SElliott Hughes 
182*d5c9a868SElliott Hughes 	if(endSlot < beginSlot) {
183*d5c9a868SElliott Hughes 		fprintf(stderr, "Bad slots %d %d in free range\n",
184*d5c9a868SElliott Hughes 			beginSlot, endSlot);
185*d5c9a868SElliott Hughes 		exit(1);
186*d5c9a868SElliott Hughes 	}
187*d5c9a868SElliott Hughes 
188*d5c9a868SElliott Hughes 	while(beginSlot < endSlot) {
189*d5c9a868SElliott Hughes 		entry = cache->entries[beginSlot];
190*d5c9a868SElliott Hughes 		if(!entry) {
191*d5c9a868SElliott Hughes 			beginSlot++;
192*d5c9a868SElliott Hughes 			continue;
193*d5c9a868SElliott Hughes 		}
194*d5c9a868SElliott Hughes 
195*d5c9a868SElliott Hughes 		/* Due to the way this is called, we _always_ de-allocate
196*d5c9a868SElliott Hughes 		 * starting from beginning... */
197*d5c9a868SElliott Hughes #ifdef HAVE_ASSERT_H
198*d5c9a868SElliott Hughes 		assert(entry->beginSlot == beginSlot);
199*d5c9a868SElliott Hughes #endif
200*d5c9a868SElliott Hughes 		clearEnd = entry->endSlot;
201*d5c9a868SElliott Hughes 		if(clearEnd > endSlot)
202*d5c9a868SElliott Hughes 			clearEnd = endSlot;
203*d5c9a868SElliott Hughes 		clearBegin = beginSlot;
204*d5c9a868SElliott Hughes 
205*d5c9a868SElliott Hughes 		for(i = clearBegin; i <clearEnd; i++)
206*d5c9a868SElliott Hughes 			cache->entries[i] = 0;
207*d5c9a868SElliott Hughes 
208*d5c9a868SElliott Hughes 		entry->beginSlot = clearEnd;
209*d5c9a868SElliott Hughes 
210*d5c9a868SElliott Hughes 		if(entry->beginSlot == entry->endSlot) {
211*d5c9a868SElliott Hughes 			int needWriteEnd = 0;
212*d5c9a868SElliott Hughes 			if(entry->endMarkPos != -1 &&
213*d5c9a868SElliott Hughes 			   entry->endMarkPos < (int) beginSlot)
214*d5c9a868SElliott Hughes 				needWriteEnd = 1;
215*d5c9a868SElliott Hughes 
216*d5c9a868SElliott Hughes 			if(entry->longName)
217*d5c9a868SElliott Hughes 				free(entry->longName);
218*d5c9a868SElliott Hughes 			if(entry->shortName)
219*d5c9a868SElliott Hughes 				free(entry->shortName);
220*d5c9a868SElliott Hughes 			free(entry);
221*d5c9a868SElliott Hughes 			if(needWriteEnd) {
222*d5c9a868SElliott Hughes 				return (int) beginSlot;
223*d5c9a868SElliott Hughes 			}
224*d5c9a868SElliott Hughes 		}
225*d5c9a868SElliott Hughes 
226*d5c9a868SElliott Hughes 		beginSlot = clearEnd;
227*d5c9a868SElliott Hughes 	}
228*d5c9a868SElliott Hughes 	return -1;
229*d5c9a868SElliott Hughes }
230*d5c9a868SElliott Hughes 
allocDirCacheEntry(dirCache_t * cache,unsigned int beginSlot,unsigned int endSlot,dirCacheEntryType_t type)231*d5c9a868SElliott Hughes static dirCacheEntry_t *allocDirCacheEntry(dirCache_t *cache,
232*d5c9a868SElliott Hughes 					   unsigned int beginSlot,
233*d5c9a868SElliott Hughes 					   unsigned int endSlot,
234*d5c9a868SElliott Hughes 					   dirCacheEntryType_t type)
235*d5c9a868SElliott Hughes {
236*d5c9a868SElliott Hughes 	dirCacheEntry_t *entry;
237*d5c9a868SElliott Hughes 	unsigned int i;
238*d5c9a868SElliott Hughes 
239*d5c9a868SElliott Hughes 	if(growDirCache(cache, endSlot) < 0)
240*d5c9a868SElliott Hughes 		return 0;
241*d5c9a868SElliott Hughes 
242*d5c9a868SElliott Hughes 	entry = New(dirCacheEntry_t);
243*d5c9a868SElliott Hughes 	if(!entry)
244*d5c9a868SElliott Hughes 		return 0;
245*d5c9a868SElliott Hughes 	entry->type = type;
246*d5c9a868SElliott Hughes 	entry->longName = 0;
247*d5c9a868SElliott Hughes 	entry->shortName = 0;
248*d5c9a868SElliott Hughes 	entry->beginSlot = beginSlot;
249*d5c9a868SElliott Hughes 	entry->endSlot = endSlot;
250*d5c9a868SElliott Hughes 	entry->endMarkPos = -1;
251*d5c9a868SElliott Hughes 
252*d5c9a868SElliott Hughes 	freeDirCacheRange(cache, beginSlot, endSlot);
253*d5c9a868SElliott Hughes 	for(i=beginSlot; i<endSlot; i++) {
254*d5c9a868SElliott Hughes 		cache->entries[i] = entry;
255*d5c9a868SElliott Hughes 	}
256*d5c9a868SElliott Hughes 	return entry;
257*d5c9a868SElliott Hughes }
258*d5c9a868SElliott Hughes 
addUsedEntry(dirCache_t * cache,unsigned int beginSlot,unsigned int endSlot,wchar_t * longName,wchar_t * shortName,struct directory * dir)259*d5c9a868SElliott Hughes dirCacheEntry_t *addUsedEntry(dirCache_t *cache,
260*d5c9a868SElliott Hughes 			      unsigned int beginSlot,
261*d5c9a868SElliott Hughes 			      unsigned int endSlot,
262*d5c9a868SElliott Hughes 			      wchar_t *longName, wchar_t *shortName,
263*d5c9a868SElliott Hughes 			      struct directory *dir)
264*d5c9a868SElliott Hughes {
265*d5c9a868SElliott Hughes 	dirCacheEntry_t *entry;
266*d5c9a868SElliott Hughes 
267*d5c9a868SElliott Hughes 	if(endSlot < beginSlot) {
268*d5c9a868SElliott Hughes 		fprintf(stderr,
269*d5c9a868SElliott Hughes 			"Bad slots %d %d in add used entry\n",
270*d5c9a868SElliott Hughes 			beginSlot, endSlot);
271*d5c9a868SElliott Hughes 		exit(1);
272*d5c9a868SElliott Hughes 	}
273*d5c9a868SElliott Hughes 
274*d5c9a868SElliott Hughes 
275*d5c9a868SElliott Hughes 	entry = allocDirCacheEntry(cache, beginSlot, endSlot, DCET_USED);
276*d5c9a868SElliott Hughes 	if(!entry)
277*d5c9a868SElliott Hughes 		return 0;
278*d5c9a868SElliott Hughes 
279*d5c9a868SElliott Hughes 	entry->beginSlot = beginSlot;
280*d5c9a868SElliott Hughes 	entry->endSlot = endSlot;
281*d5c9a868SElliott Hughes 	if(longName)
282*d5c9a868SElliott Hughes 		entry->longName = wcsdup(longName);
283*d5c9a868SElliott Hughes 	entry->shortName = wcsdup(shortName);
284*d5c9a868SElliott Hughes 	entry->dir = *dir;
285*d5c9a868SElliott Hughes 	hashDce(cache, entry);
286*d5c9a868SElliott Hughes 	return entry;
287*d5c9a868SElliott Hughes }
288*d5c9a868SElliott Hughes 
289*d5c9a868SElliott Hughes /* Try to merge free slot at position "slot" with slot at previous position
290*d5c9a868SElliott Hughes  * After successful merge, both will point to a same DCE (the one which used
291*d5c9a868SElliott Hughes  * to be previous)
292*d5c9a868SElliott Hughes  * Only does something if both of these slots are actually free
293*d5c9a868SElliott Hughes  */
mergeFreeSlots(dirCache_t * cache,unsigned int slot)294*d5c9a868SElliott Hughes static void mergeFreeSlots(dirCache_t *cache, unsigned int slot)
295*d5c9a868SElliott Hughes {
296*d5c9a868SElliott Hughes 	dirCacheEntry_t *previous, *next;
297*d5c9a868SElliott Hughes 	unsigned int i;
298*d5c9a868SElliott Hughes 
299*d5c9a868SElliott Hughes 	if(slot == 0)
300*d5c9a868SElliott Hughes 		return;
301*d5c9a868SElliott Hughes 	previous = cache->entries[slot-1];
302*d5c9a868SElliott Hughes 	next = cache->entries[slot];
303*d5c9a868SElliott Hughes 	if(next && next->type == DCET_FREE &&
304*d5c9a868SElliott Hughes 	   previous && previous->type == DCET_FREE) {
305*d5c9a868SElliott Hughes 		for(i=next->beginSlot; i < next->endSlot; i++)
306*d5c9a868SElliott Hughes 			cache->entries[i] = previous;
307*d5c9a868SElliott Hughes 		previous->endSlot = next->endSlot;
308*d5c9a868SElliott Hughes 		previous->endMarkPos = next->endMarkPos;
309*d5c9a868SElliott Hughes 		free(next);
310*d5c9a868SElliott Hughes 	}
311*d5c9a868SElliott Hughes }
312*d5c9a868SElliott Hughes 
313*d5c9a868SElliott Hughes /* Create a free range extending from beginSlot to endSlot, and try to merge
314*d5c9a868SElliott Hughes  * it with any neighboring free ranges */
addFreeEndEntry(dirCache_t * cache,unsigned int beginSlot,unsigned int endSlot,int isAtEnd)315*d5c9a868SElliott Hughes dirCacheEntry_t *addFreeEndEntry(dirCache_t *cache,
316*d5c9a868SElliott Hughes 				 unsigned int beginSlot,
317*d5c9a868SElliott Hughes 				 unsigned int endSlot,
318*d5c9a868SElliott Hughes 				 int isAtEnd)
319*d5c9a868SElliott Hughes {
320*d5c9a868SElliott Hughes 	dirCacheEntry_t *entry;
321*d5c9a868SElliott Hughes 
322*d5c9a868SElliott Hughes 	if(beginSlot < cache->nrHashed)
323*d5c9a868SElliott Hughes 		cache->nrHashed = beginSlot;
324*d5c9a868SElliott Hughes 
325*d5c9a868SElliott Hughes 	if(endSlot < beginSlot) {
326*d5c9a868SElliott Hughes 		fprintf(stderr, "Bad slots %d %d in add free entry\n",
327*d5c9a868SElliott Hughes 			beginSlot, endSlot);
328*d5c9a868SElliott Hughes 		exit(1);
329*d5c9a868SElliott Hughes 	}
330*d5c9a868SElliott Hughes 
331*d5c9a868SElliott Hughes 	if(endSlot == beginSlot)
332*d5c9a868SElliott Hughes 		return 0;
333*d5c9a868SElliott Hughes 	entry = allocDirCacheEntry(cache, beginSlot, endSlot, DCET_FREE);
334*d5c9a868SElliott Hughes 	if(isAtEnd)
335*d5c9a868SElliott Hughes 		entry->endMarkPos = (int) beginSlot;
336*d5c9a868SElliott Hughes 	mergeFreeSlots(cache, beginSlot);
337*d5c9a868SElliott Hughes 	mergeFreeSlots(cache, endSlot);
338*d5c9a868SElliott Hughes 	return cache->entries[beginSlot];
339*d5c9a868SElliott Hughes }
340*d5c9a868SElliott Hughes 
addFreeEntry(dirCache_t * cache,unsigned int beginSlot,unsigned int endSlot)341*d5c9a868SElliott Hughes dirCacheEntry_t *addFreeEntry(dirCache_t *cache,
342*d5c9a868SElliott Hughes 			      unsigned int beginSlot,
343*d5c9a868SElliott Hughes 			      unsigned int endSlot)
344*d5c9a868SElliott Hughes {
345*d5c9a868SElliott Hughes 	return addFreeEndEntry(cache, beginSlot, endSlot, 0);
346*d5c9a868SElliott Hughes }
347*d5c9a868SElliott Hughes 
addEndEntry(dirCache_t * cache,unsigned int pos)348*d5c9a868SElliott Hughes dirCacheEntry_t *addEndEntry(dirCache_t *cache, unsigned int pos)
349*d5c9a868SElliott Hughes {
350*d5c9a868SElliott Hughes 	return allocDirCacheEntry(cache, pos, pos+1u, DCET_END);
351*d5c9a868SElliott Hughes }
352*d5c9a868SElliott Hughes 
lookupInDircache(dirCache_t * cache,unsigned int pos)353*d5c9a868SElliott Hughes dirCacheEntry_t *lookupInDircache(dirCache_t *cache, unsigned int pos)
354*d5c9a868SElliott Hughes {
355*d5c9a868SElliott Hughes 	if(growDirCache(cache, pos+1) < 0)
356*d5c9a868SElliott Hughes 		return 0;
357*d5c9a868SElliott Hughes 	return cache->entries[pos];
358*d5c9a868SElliott Hughes }
359*d5c9a868SElliott Hughes 
freeDirCache(Stream_t * Stream)360*d5c9a868SElliott Hughes void freeDirCache(Stream_t *Stream)
361*d5c9a868SElliott Hughes {
362*d5c9a868SElliott Hughes 	dirCache_t *cache, **dcp;
363*d5c9a868SElliott Hughes 
364*d5c9a868SElliott Hughes 	dcp = getDirCacheP(Stream);
365*d5c9a868SElliott Hughes 	cache = *dcp;
366*d5c9a868SElliott Hughes 	if(cache) {
367*d5c9a868SElliott Hughes 		int n;
368*d5c9a868SElliott Hughes 		n=freeDirCacheRange(cache, 0, cache->nr_entries);
369*d5c9a868SElliott Hughes 		if(n >= 0)
370*d5c9a868SElliott Hughes 			low_level_dir_write_end(Stream, n);
371*d5c9a868SElliott Hughes 		free(cache);
372*d5c9a868SElliott Hughes 		*dcp = 0;
373*d5c9a868SElliott Hughes 	}
374*d5c9a868SElliott Hughes }
375