aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndreas Schneider <asn@cryptomilk.org>2011-02-06 14:57:29 +0100
committerAndreas Schneider <asn@cryptomilk.org>2011-02-06 15:35:18 +0100
commit97d261dc791a3b5e4b30bf384fd8a73da90d2372 (patch)
treed5d2f47c1a88858d8b498a72b836a3d76acbda6f
parent892ebf35f1d4600823e7db55fc6df67599b2f753 (diff)
downloadlibssh-97d261dc791a3b5e4b30bf384fd8a73da90d2372.tar.gz
libssh-97d261dc791a3b5e4b30bf384fd8a73da90d2372.tar.xz
libssh-97d261dc791a3b5e4b30bf384fd8a73da90d2372.zip
torture: Added torture_rmdirs().
-rw-r--r--tests/torture.c73
-rw-r--r--tests/torture.h2
2 files changed, 75 insertions, 0 deletions
diff --git a/tests/torture.c b/tests/torture.c
index a78e8eec..7456cb60 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;
}
diff --git a/tests/torture.h b/tests/torture.h
index 99b34257..cc8abbc8 100644
--- a/tests/torture.h
+++ b/tests/torture.h
@@ -46,6 +46,8 @@ struct argument_s {
void torture_cmdline_parse(int argc, char **argv, struct argument_s *arguments);
+int torture_rmdirs(const char *path);
+
/*
* Returns the verbosity level asked by user
*/