Skip to content

Commit

Permalink
Architecture adjustment for enumerating files with regards to whether
Browse files Browse the repository at this point in the history
symlinks are permitted.
  • Loading branch information
icculus committed Jul 16, 2001
1 parent b64284b commit c7fe9ab
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 11 deletions.
6 changes: 4 additions & 2 deletions archivers/dir.c
Expand Up @@ -140,13 +140,15 @@ static DirHandle *DIR_openArchive(const char *name, int forWriting)
} /* DIR_openArchive */


static LinkedStringList *DIR_enumerateFiles(DirHandle *h, const char *dname)
static LinkedStringList *DIR_enumerateFiles(DirHandle *h,
const char *dname,
int omitSymLinks)
{
char *d = __PHYSFS_platformCvtToDependent((char *)(h->opaque),dname,NULL);
LinkedStringList *retval;

BAIL_IF_MACRO(d == NULL, NULL, NULL);
retval = __PHYSFS_platformEnumerateFiles(d);
retval = __PHYSFS_platformEnumerateFiles(d, omitSymLinks);
free(d);
return(retval);
} /* DIR_enumerateFiles */
Expand Down
4 changes: 3 additions & 1 deletion archivers/grp.c
Expand Up @@ -197,7 +197,9 @@ static DirHandle *GRP_openArchive(const char *name, int forWriting)
} /* GRP_openArchive */


static LinkedStringList *GRP_enumerateFiles(DirHandle *h, const char *dirname)
static LinkedStringList *GRP_enumerateFiles(DirHandle *h,
const char *dirname,
int omitSymLinks)
{
char buf[16];
GRPinfo *g = (GRPinfo *) (h->opaque);
Expand Down
4 changes: 3 additions & 1 deletion archivers/zip.c
Expand Up @@ -123,7 +123,9 @@ static DirHandle *ZIP_openArchive(const char *name, int forWriting)
} /* ZIP_openArchive */


static LinkedStringList *ZIP_enumerateFiles(DirHandle *h, const char *dirname)
static LinkedStringList *ZIP_enumerateFiles(DirHandle *h,
const char *dirname,
int omitSymLinks)
{
ZIPinfo *zi = (ZIPinfo *) (h->opaque);
unzFile fh = zi->handle;
Expand Down
3 changes: 2 additions & 1 deletion physfs.c
Expand Up @@ -1022,6 +1022,7 @@ char **PHYSFS_enumerateFiles(const char *path)
char **retval = NULL;
LinkedStringList *rc;
LinkedStringList *finalList = NULL;
int omitSymLinks = !allowSymLinks;

while (*path == '/')
path++;
Expand All @@ -1031,7 +1032,7 @@ char **PHYSFS_enumerateFiles(const char *path)
DirHandle *h = i->dirHandle;
if (__PHYSFS_verifySecurity(h, path))
{
rc = h->funcs->enumerateFiles(h, path);
rc = h->funcs->enumerateFiles(h, path, omitSymLinks);
interpolateStringLists(&finalList, rc);
} /* if */
} /* for */
Expand Down
13 changes: 9 additions & 4 deletions physfs_internal.h
Expand Up @@ -142,11 +142,14 @@ typedef struct __PHYSFS_DIRFUNCTIONS__
* Returns a list of all files in dirname. Each element of this list
* (and its "str" field) will be deallocated with the system's free()
* function by the caller, so be sure to explicitly malloc() each
* chunk.
* chunk. Omit symlinks if (omitSymLinks) is non-zero.
* If you have a memory failure, return as much as you can.
* This dirname is in platform-independent notation.
*/
LinkedStringList *(*enumerateFiles)(DirHandle *r, const char *dirname);
LinkedStringList *(*enumerateFiles)(DirHandle *r,
const char *dirname,
int omitSymLinks);


/*
* Returns non-zero if filename can be opened for reading.
Expand Down Expand Up @@ -407,9 +410,11 @@ void __PHYSFS_platformTimeslice(void);
* DirFunctions->enumerateFiles() method (see above), except that the
* (dirName) that is passed to this function is converted to
* platform-DEPENDENT notation by the caller. The DirFunctions version
* uses platform-independent notation.
* uses platform-independent notation. Note that ".", "..", and other
* metaentries should always be ignored.
*/
LinkedStringList *__PHYSFS_platformEnumerateFiles(const char *dirname);
LinkedStringList *__PHYSFS_platformEnumerateFiles(const char *dirname,
int omitSymLinks);


/*
Expand Down
46 changes: 44 additions & 2 deletions platform/unix.c
Expand Up @@ -286,17 +286,40 @@ void __PHYSFS_platformTimeslice(void)
} /* __PHYSFS_platformTimeslice */


LinkedStringList *__PHYSFS_platformEnumerateFiles(const char *dirname)
LinkedStringList *__PHYSFS_platformEnumerateFiles(const char *dirname,
int omitSymLinks)
{
LinkedStringList *retval = NULL;
LinkedStringList *l = NULL;
LinkedStringList *prev = NULL;
DIR *dir;
struct dirent *ent;
int bufsize = 0;
char *buf = NULL;
int dlen = 0;

if (omitSymLinks)
{
dlen = strlen(dirname);
bufsize = dlen + 256;
buf = malloc(bufsize);
BAIL_IF_MACRO(buf == NULL, ERR_OUT_OF_MEMORY, NULL);
strcpy(buf, dirname);
if (buf[dlen - 1] != '/')
{
buf[dlen++] = '/';
buf[dlen] = '\0';
} /* if */
} /* if */

errno = 0;
dir = opendir(dirname);
BAIL_IF_MACRO(dir == NULL, strerror(errno), NULL);
if (dir == NULL)
{
if (buf != NULL)
free(buf);
BAIL_IF_MACRO(1, strerror(errno), NULL);
} /* if */

while (1)
{
Expand All @@ -310,6 +333,24 @@ LinkedStringList *__PHYSFS_platformEnumerateFiles(const char *dirname)
if (strcmp(ent->d_name, "..") == 0)
continue;

if (omitSymLinks)
{
char *p;
int len = strlen(ent->d_name) + dlen + 1;
if (len > bufsize)
{
p = realloc(buf, len);
if (p == NULL)
continue;
buf = p;
bufsize = len;
} /* if */

strcpy(buf + dlen, ent->d_name);
if (__PHYSFS_platformIsSymLink(buf))
continue;
} /* if */

l = (LinkedStringList *) malloc(sizeof (LinkedStringList));
if (l == NULL)
break;
Expand All @@ -333,6 +374,7 @@ LinkedStringList *__PHYSFS_platformEnumerateFiles(const char *dirname)
} /* while */

closedir(dir);
free(buf);
return(retval);
} /* __PHYSFS_platformEnumerateFiles */

Expand Down

0 comments on commit c7fe9ab

Please sign in to comment.