mirror of
https://github.com/apache/nuttx-apps.git
synced 2026-08-01 20:29:00 +00:00
testing/sd_stress: Handle stale temporary directories
Fix #3205 by removing stale sdstress work directories left by previous interrupted or failed runs before starting a new test. The cleanup only removes entries matching the generated tmpNNN file pattern. If the directory contains any unexpected entry, the test fails instead of deleting user data. Also make create_dir() and remove_dir() use their path argument instead of the fixed temporary directory names. Signed-off-by: Nightt <87569709+nightt5879@users.noreply.github.com>
This commit is contained in:
parent
8568013436
commit
2d6cd74215
1 changed files with 98 additions and 2 deletions
|
|
@ -47,12 +47,14 @@
|
|||
* Included Files
|
||||
****************************************************************************/
|
||||
|
||||
#include <dirent.h>
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/stat.h>
|
||||
|
||||
|
|
@ -94,7 +96,7 @@ static void usage(void)
|
|||
|
||||
static bool create_dir(const char *path)
|
||||
{
|
||||
int ret = mkdir(TEMPDIR, S_IRWXU | S_IRWXG | S_IRWXO);
|
||||
int ret = mkdir(path, S_IRWXU | S_IRWXG | S_IRWXO);
|
||||
|
||||
if (ret < 0)
|
||||
{
|
||||
|
|
@ -108,7 +110,7 @@ static bool create_dir(const char *path)
|
|||
|
||||
static bool remove_dir(const char *path)
|
||||
{
|
||||
int ret = rmdir(TEMPDIR2);
|
||||
int ret = rmdir(path);
|
||||
|
||||
if (ret < 0)
|
||||
{
|
||||
|
|
@ -120,6 +122,94 @@ static bool remove_dir(const char *path)
|
|||
return true;
|
||||
}
|
||||
|
||||
static bool is_temp_file(const char *name, const char *prefix)
|
||||
{
|
||||
size_t prefix_len = strlen(prefix);
|
||||
int i;
|
||||
|
||||
if (strncmp(name, prefix, prefix_len) != 0)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
for (i = 0; i < 3; i++)
|
||||
{
|
||||
char ch = name[prefix_len + i];
|
||||
|
||||
if (ch < '0' || ch > '9')
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return name[prefix_len + i] == '\0';
|
||||
}
|
||||
|
||||
static bool cleanup_dir(const char *path, const char *name)
|
||||
{
|
||||
FAR DIR *dir;
|
||||
FAR struct dirent *entry;
|
||||
char filepath[MAX_PATH_LEN];
|
||||
int ret;
|
||||
|
||||
dir = opendir(path);
|
||||
if (dir == NULL)
|
||||
{
|
||||
if (errno == ENOENT)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
printf("opendir %s failed, errno: %d -> %s\n",
|
||||
path, errno, strerror(errno));
|
||||
return false;
|
||||
}
|
||||
|
||||
while ((entry = readdir(dir)) != NULL)
|
||||
{
|
||||
if (strcmp(entry->d_name, ".") == 0 ||
|
||||
strcmp(entry->d_name, "..") == 0)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!is_temp_file(entry->d_name, name))
|
||||
{
|
||||
printf("%s contains unexpected entry %s\n", path, entry->d_name);
|
||||
closedir(dir);
|
||||
return false;
|
||||
}
|
||||
|
||||
ret = snprintf(filepath, sizeof(filepath), "%s/%s",
|
||||
path, entry->d_name);
|
||||
if (ret < 0 || (size_t)ret >= sizeof(filepath))
|
||||
{
|
||||
printf("path name too long\n");
|
||||
closedir(dir);
|
||||
return false;
|
||||
}
|
||||
|
||||
ret = unlink(filepath);
|
||||
if (ret < 0)
|
||||
{
|
||||
printf("unlink %s failed, ret: %d, errno %d -> %s\n",
|
||||
filepath, ret, errno, strerror(errno));
|
||||
closedir(dir);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
ret = closedir(dir);
|
||||
if (ret < 0)
|
||||
{
|
||||
printf("closedir %s failed, ret: %d, errno %d -> %s\n",
|
||||
path, ret, errno, strerror(errno));
|
||||
return false;
|
||||
}
|
||||
|
||||
return remove_dir(path);
|
||||
}
|
||||
|
||||
static bool create_files(const char *dir, const char *name,
|
||||
size_t num_files, char *bytes, size_t num_bytes)
|
||||
{
|
||||
|
|
@ -372,6 +462,12 @@ int main(int argc, char *argv[])
|
|||
|
||||
total_time = 0;
|
||||
|
||||
if (!cleanup_dir(TEMPDIR, TEMPFILE) || !cleanup_dir(TEMPDIR2, TEMPFILE))
|
||||
{
|
||||
free(bytes);
|
||||
return -1;
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < num_runs; ++i)
|
||||
{
|
||||
start = get_abs_time();
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue