diff options
author | Andreas Schneider <asn@cryptomilk.org> | 2011-02-06 14:57:29 +0100 |
---|---|---|
committer | Andreas Schneider <asn@cryptomilk.org> | 2011-02-06 15:35:18 +0100 |
commit | 97d261dc791a3b5e4b30bf384fd8a73da90d2372 (patch) | |
tree | d5d2f47c1a88858d8b498a72b836a3d76acbda6f /tests/torture.c | |
parent | 892ebf35f1d4600823e7db55fc6df67599b2f753 (diff) | |
download | libssh-97d261dc791a3b5e4b30bf384fd8a73da90d2372.tar.gz libssh-97d261dc791a3b5e4b30bf384fd8a73da90d2372.tar.xz libssh-97d261dc791a3b5e4b30bf384fd8a73da90d2372.zip |
torture: Added torture_rmdirs().
Diffstat (limited to 'tests/torture.c')
-rw-r--r-- | tests/torture.c | 73 |
1 files changed, 73 insertions, 0 deletions
diff --git a/tests/torture.c b/tests/torture.c index a78e8ee..7456cb6 100644 --- a/tests/torture.c +++ b/tests/torture.c @@ -21,8 +21,15 @@ * MA 02111-1307, USA. */ +#include <sys/types.h> +#include <sys/stat.h> +#include <dirent.h> +#include <errno.h> #include <stdio.h> #include <stdlib.h> +#ifndef _WIN32 +# include <unistd.h> +#endif #include "torture.h" @@ -55,6 +62,72 @@ static int torture_auth_kbdint(ssh_session session, return err; } +int torture_rmdirs(const char *path) { + DIR *d; + struct dirent *dp; + struct stat sb; + char *fname; + + if ((d = opendir(path)) != NULL) { + while(stat(path, &sb) == 0) { + /* if we can remove the directory we're done */ + if (rmdir(path) == 0) { + break; + } + switch (errno) { + case ENOTEMPTY: + case EEXIST: + case EBADF: + break; /* continue */ + default: + closedir(d); + return 0; + } + + while ((dp = readdir(d)) != NULL) { + size_t len; + /* skip '.' and '..' */ + if (dp->d_name[0] == '.' && + (dp->d_name[1] == '\0' || + (dp->d_name[1] == '.' && dp->d_name[2] == '\0'))) { + continue; + } + + len = strlen(path) + strlen(dp->d_name) + 2; + fname = malloc(len); + if (fname == NULL) { + return -1; + } + snprintf(fname, len, "%s/%s", path, dp->d_name); + + /* stat the file */ + if (lstat(fname, &sb) != -1) { + if (S_ISDIR(sb.st_mode) && !S_ISLNK(sb.st_mode)) { + if (rmdir(fname) < 0) { /* can't be deleted */ + if (errno == EACCES) { + closedir(d); + SAFE_FREE(fname); + return -1; + } + torture_rmdirs(fname); + } + } else { + unlink(fname); + } + } /* lstat */ + SAFE_FREE(fname); + } /* readdir */ + + rewinddir(d); + } + } else { + return -1; + } + + closedir(d); + return 0; +} + int torture_libssh_verbosity(void){ return verbosity; } |