1 /* ----------------------------------------------------------------------- *
3 * Copyright 2004-2008 H. Peter Anvin - All Rights Reserved
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, Inc., 53 Temple Place Ste 330,
8 * Boston MA 02111-1307, USA; either version 2 of the License, or
9 * (at your option) any later version; incorporated herein by reference.
11 * ----------------------------------------------------------------------- */
16 * Search a FAT directory for a particular pre-mangled filename.
17 * Copies the directory entry into direntry and returns the starting cluster
18 * if found; returns -2 on not found, -1 on error, 0 on empty file.
22 #include "libfatint.h"
24 int32_t libfat_searchdir(struct libfat_filesystem
*fs
, int32_t dirclust
,
25 const void *name
, struct libfat_direntry
*direntry
)
27 struct fat_dirent
*dep
;
29 libfat_sector_t s
= libfat_clustertosector(fs
, dirclust
);
33 return -2; /* Not found */
34 else if (s
== (libfat_sector_t
) - 1)
35 return -1; /* Error */
37 dep
= libfat_get_sector(fs
, s
);
39 return -1; /* Read error */
41 for (nent
= 0; nent
< LIBFAT_SECTOR_SIZE
;
42 nent
+= sizeof(struct fat_dirent
)) {
43 if (!memcmp(dep
->name
, name
, 11)) {
45 memcpy(direntry
->entry
, dep
, sizeof(*dep
));
47 direntry
->offset
= nent
;
49 if (read32(&dep
->size
) == 0)
50 return 0; /* An empty file has no clusters */
52 return read16(&dep
->clustlo
) +
53 (read16(&dep
->clusthi
) << 16);
56 if (dep
->name
[0] == 0)
57 return -2; /* Hit high water mark */
62 s
= libfat_nextsector(fs
, s
);