mirror of
https://git.libssh.org/projects/libssh.git
synced 2026-02-11 10:40:27 +09:00
torture: Added torture_rmdirs().
This commit is contained in:
@@ -21,8 +21,15 @@
|
|||||||
* MA 02111-1307, USA.
|
* MA 02111-1307, USA.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#include <sys/types.h>
|
||||||
|
#include <sys/stat.h>
|
||||||
|
#include <dirent.h>
|
||||||
|
#include <errno.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
#ifndef _WIN32
|
||||||
|
# include <unistd.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
#include "torture.h"
|
#include "torture.h"
|
||||||
|
|
||||||
@@ -55,6 +62,72 @@ static int torture_auth_kbdint(ssh_session session,
|
|||||||
return err;
|
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){
|
int torture_libssh_verbosity(void){
|
||||||
return verbosity;
|
return verbosity;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -46,6 +46,8 @@ struct argument_s {
|
|||||||
|
|
||||||
void torture_cmdline_parse(int argc, char **argv, struct argument_s *arguments);
|
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
|
* Returns the verbosity level asked by user
|
||||||
*/
|
*/
|
||||||
|
|||||||
Reference in New Issue
Block a user