misc: Introduce internal function ssh_dir_writeable()

The introduced internal function checks if the provided path is for an
existing directory which is accessible for writing.

Signed-off-by: Anderson Toshiyuki Sasaki <ansasaki@redhat.com>
Reviewed-by: Andreas Schneider <asn@cryptomilk.org>
This commit is contained in:
Anderson Toshiyuki Sasaki
2019-08-01 12:45:01 +02:00
committed by Andreas Schneider
parent 3737e5f0e7
commit 7857cd1aa5
3 changed files with 98 additions and 0 deletions

View File

@@ -130,6 +130,31 @@ int ssh_file_readaccess_ok(const char *file) {
return 1;
}
/**
* @brief Check if the given path is an existing directory and that is
* accessible for writing.
*
* @param[in] path Path to the directory to be checked
*
* @return Return 1 if the directory exists and is accessible; 0 otherwise
* */
int ssh_dir_writeable(const char *path)
{
struct _stat buffer;
int rc;
rc = _stat(path, &buffer);
if (rc < 0) {
return 0;
}
if ((buffer.st_mode & _S_IFDIR) && (buffer.st_mode & _S_IWRITE)) {
return 1;
}
return 0;
}
#define SSH_USEC_IN_SEC 1000000LL
#define SSH_SECONDS_SINCE_1601 11644473600LL
@@ -247,6 +272,31 @@ int ssh_file_readaccess_ok(const char *file)
return 1;
}
/**
* @brief Check if the given path is an existing directory and that is
* accessible for writing.
*
* @param[in] path Path to the directory to be checked
*
* @return Return 1 if the directory exists and is accessible; 0 otherwise
* */
int ssh_dir_writeable(const char *path)
{
struct stat buffer;
int rc;
rc = stat(path, &buffer);
if (rc < 0) {
return 0;
}
if (S_ISDIR(buffer.st_mode) && (buffer.st_mode & S_IWRITE)) {
return 1;
}
return 0;
}
char *ssh_get_local_username(void)
{
struct passwd pwd;