Skip to content

Commit

Permalink
libc: Implement ftw and nftw function
Browse files Browse the repository at this point in the history
see the reference here:
https://pubs.opengroup.org/onlinepubs/009695399/basedefs/ftw.h.html

Signed-off-by: Xiang Xiao <xiaoxiang@xiaomi.com>
Change-Id: I3b368106a56b0e9d4c653f3ae16304b0a9d55fbc
  • Loading branch information
xiaoxiang781216 committed Jul 30, 2020
1 parent 1078210 commit 3dcc962
Show file tree
Hide file tree
Showing 4 changed files with 277 additions and 4 deletions.
13 changes: 9 additions & 4 deletions include/ftw.h
Expand Up @@ -67,7 +67,7 @@

/* The fourth argument of the ftw/nftw callback is a pointer to an FTW
* structure. The value of base is the offset of the object's filename in
* the pathname passed as the first argument to the callbaqck. The value of
* the pathname passed as the first argument to the callback. The value of
* level indicates depth relative to the root of the walk, where the root
* level is 0.
*/
Expand All @@ -78,10 +78,15 @@ struct FTW
int level; /* Depth relative to the root of the walk */
};

/* This is the type of the ftw/nftw callback */
/* This is the type of the ftw callback */

typedef int (*ftw_cb_t)(FAR const char *path, FAR const struct stat *buf,
int info, FAR struct FTW *pftw);
int info);

/* This is the type of the nftw callback */

typedef int (*nftw_cb_t)(FAR const char *path, FAR const struct stat *buf,
int info, FAR struct FTW *pftw);

/****************************************************************************
* Public Function Prototypes
Expand Down Expand Up @@ -205,7 +210,7 @@ int ftw(FAR const char *path, ftw_cb_t fn, int fdlimit);
*
****************************************************************************/

int nftw(FAR const char *path, ftw_cb_t fn, int fdlimit, int flags);
int nftw(FAR const char *path, nftw_cb_t fn, int fdlimit, int flags);

#undef EXTERN
#ifdef __cplusplus
Expand Down
1 change: 1 addition & 0 deletions libs/libc/dirent/Make.defs
Expand Up @@ -36,6 +36,7 @@
# Add the dirent C files to the build

CSRCS += lib_readdirr.c lib_telldir.c lib_alphasort.c lib_scandir.c
CSRCS += lib_ftw.c lib_nftw.c

# Add the dirent directory to the build

Expand Down
39 changes: 39 additions & 0 deletions libs/libc/dirent/lib_ftw.c
@@ -0,0 +1,39 @@
/****************************************************************************
* libs/libc/dirent/lib_ftw.c
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. The
* ASF licenses this file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
****************************************************************************/

/****************************************************************************
* Included Files
****************************************************************************/

#include <ftw.h>

/****************************************************************************
* Public Functions
****************************************************************************/

int ftw(FAR const char *path, ftw_cb_t fn, int fdlimit)
{
/* The following cast assumes that calling a function with one
* argument more than it needs behaves as expected. This is
* actually undefined, but works on all real-world machines.
*/

return nftw(path, (nftw_cb_t)fn, fdlimit, FTW_PHYS);
}
228 changes: 228 additions & 0 deletions libs/libc/dirent/lib_nftw.c
@@ -0,0 +1,228 @@
/****************************************************************************
* libs/libc/dirent/lib_nftw.c
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. The
* ASF licenses this file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
****************************************************************************/

/****************************************************************************
* Included Files
****************************************************************************/

#include <ftw.h>
#include <dirent.h>
#include <sys/stat.h>
#include <errno.h>
#include <unistd.h>
#include <string.h>
#include <limits.h>

/****************************************************************************
* Private Functions
****************************************************************************/

static int
call_nftw(FAR char *path, nftw_cb_t fn, int flags, int base,
int level, FAR const struct stat *buf, int info)
{
struct FTW ftw =
{
base, level
};

if (flags & FTW_CHDIR)
{
int r;

if (base > 1)
{
path[base - 1] = '\0';
r = chdir(path);
path[base - 1] = '/';
}
else
{
r = chdir("/");
}

if (r < 0)
{
return r;
}
}

return fn(path, buf, info, &ftw);
}

static int
do_nftw(FAR char *path, nftw_cb_t fn, int fdlimit, int flags, int level)
{
DIR *dir = NULL;
struct stat buf;
size_t base;
size_t j;
int info;
int r;

j = strlen(path);
while (j > 1 && path[j - 1] == '/')
{
path[--j] = '\0';
}

base = j - 1;
while (base > 0 && path[base - 1] != '/')
{
--base;
}

r = flags & FTW_PHYS ? lstat(path, &buf) : stat(path, &buf);
if (r < 0)
{
if (!(flags & FTW_PHYS) &&
get_errno() == ENOENT && !lstat(path, &buf))
{
info = FTW_SLN;
}
else if (get_errno() == EACCES)
{
info = FTW_NS;
}
else
{
return -1;
}
}
else if (S_ISDIR(buf.st_mode))
{
if (flags & FTW_DEPTH)
{
info = FTW_DP;
}
else
{
info = FTW_D;
}
}
else if (S_ISLNK(buf.st_mode))
{
if (flags & FTW_PHYS)
{
info = FTW_SL;
}
else
{
info = FTW_SLN;
}
}
else
{
info = FTW_F;
}

if (info == FTW_D || info == FTW_DP)
{
dir = opendir(path);
if (dir)
{
if (fdlimit <= 0)
{
closedir(dir);
dir = NULL;
}
}
else if (get_errno() == EACCES)
{
info = FTW_DNR;
}
else
{
return -1;
}
}

if (!(flags & FTW_DEPTH))
{
r = call_nftw(path, fn, flags, base, level, &buf, info);
if (r)
{
return r;
}
}

if (dir)
{
struct dirent *de;
size_t l = j;

if (path[j - 1] != '/')
{
path[j++] = '/';
}

while ((de = readdir(dir)))
{
if (de->d_name[0] == '.' && (!de->d_name[1] ||
(de->d_name[1] == '.' && !de->d_name[2])))
{
continue;
}

if (strlen(de->d_name) > PATH_MAX - j)
{
set_errno(ENAMETOOLONG);
closedir(dir);
return -1;
}

strcpy(path + j, de->d_name);
r = do_nftw(path, fn, fdlimit - 1, flags, level + 1);
if (r)
{
closedir(dir);
return r;
}
}

path[l] = '\0';
closedir(dir);
}

if (flags & FTW_DEPTH)
{
r = call_nftw(path, fn, flags, base, level, &buf, info);
if (r)
{
return r;
}
}

return 0;
}

/****************************************************************************
* Public Functions
****************************************************************************/

int nftw(FAR const char *path, nftw_cb_t fn, int fdlimit, int flags)
{
char pathbuf[PATH_MAX + 1];

strncpy(pathbuf, path, PATH_MAX);
pathbuf[PATH_MAX] = '\0';

return do_nftw(pathbuf, fn, fdlimit, flags, 0);
}

0 comments on commit 3dcc962

Please sign in to comment.