mirror of
https://git.libssh.org/projects/libssh.git
synced 2025-11-30 13:01:23 +03:00
Improve sftp_mkdir().
git-svn-id: svn+ssh://svn.berlios.de/svnroot/repos/libssh/trunk@610 7dcaeef0-15fb-0310-b436-a5af3365683c
This commit is contained in:
133
libssh/sftp.c
133
libssh/sftp.c
@@ -1873,11 +1873,11 @@ int sftp_rmdir(SFTP_SESSION *sftp, const char *directory) {
|
|||||||
buffer_add_ssh_string(buffer, filename) < 0 ||
|
buffer_add_ssh_string(buffer, filename) < 0 ||
|
||||||
sftp_packet_write(sftp, SSH_FXP_RMDIR, buffer) < 0) {
|
sftp_packet_write(sftp, SSH_FXP_RMDIR, buffer) < 0) {
|
||||||
buffer_free(buffer);
|
buffer_free(buffer);
|
||||||
string_free(filname);
|
string_free(filename);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
buffer_free(buffer);
|
buffer_free(buffer);
|
||||||
string_free(filname);
|
string_free(filename);
|
||||||
|
|
||||||
while (msg == NULL) {
|
while (msg == NULL) {
|
||||||
if (sftp_read_and_dispatch(sftp) < 0) {
|
if (sftp_read_and_dispatch(sftp) < 0) {
|
||||||
@@ -1918,68 +1918,91 @@ int sftp_rmdir(SFTP_SESSION *sftp, const char *directory) {
|
|||||||
|
|
||||||
/* Code written by Nick */
|
/* Code written by Nick */
|
||||||
int sftp_mkdir(SFTP_SESSION *sftp, const char *directory, mode_t mode) {
|
int sftp_mkdir(SFTP_SESSION *sftp, const char *directory, mode_t mode) {
|
||||||
u32 id = sftp_get_new_id(sftp);
|
STATUS_MESSAGE *status = NULL;
|
||||||
BUFFER *buffer = buffer_new();
|
SFTP_MESSAGE *msg = NULL;
|
||||||
STRING *path = string_from_char(directory);
|
SFTP_ATTRIBUTES *errno_attr = NULL;
|
||||||
SFTP_MESSAGE *msg = NULL;
|
SFTP_ATTRIBUTES attr;
|
||||||
STATUS_MESSAGE *status = NULL;
|
BUFFER *buffer;
|
||||||
SFTP_ATTRIBUTES attr;
|
STRING *path;
|
||||||
SFTP_ATTRIBUTES *errno_attr = NULL;
|
u32 id;
|
||||||
|
|
||||||
ZERO_STRUCT(attr);
|
buffer = buffer_new();
|
||||||
attr.permissions = mode;
|
if (buffer == NULL) {
|
||||||
attr.flags = SSH_FILEXFER_ATTR_PERMISSIONS;
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
buffer_add_u32(buffer, id);
|
path = string_from_char(directory);
|
||||||
buffer_add_ssh_string(buffer, path);
|
if (path == NULL) {
|
||||||
free(path);
|
buffer_free(buffer);
|
||||||
if (buffer_add_attributes(buffer, &attr) < 0) {
|
return -1;
|
||||||
buffer_free(buffer);
|
}
|
||||||
|
|
||||||
|
ZERO_STRUCT(attr);
|
||||||
|
attr.permissions = mode;
|
||||||
|
attr.flags = SSH_FILEXFER_ATTR_PERMISSIONS;
|
||||||
|
|
||||||
|
id = sftp_get_new_id(sftp);
|
||||||
|
if (buffer_add_u32(buffer, id) < 0 ||
|
||||||
|
buffer_add_ssh_string(buffer, path) < 0 ||
|
||||||
|
buffer_add_attributes(buffer, &attr) < 0 ||
|
||||||
|
sftp_packet_write(sftp, SSH_FXP_MKDIR, buffer) < 0) {
|
||||||
|
buffer_free(buffer);
|
||||||
|
string_free(path);
|
||||||
|
}
|
||||||
|
buffer_free(buffer);
|
||||||
|
string_free(path);
|
||||||
|
|
||||||
|
while (msg == NULL) {
|
||||||
|
if (sftp_read_and_dispatch(sftp) < 0) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
sftp_packet_write(sftp, SSH_FXP_MKDIR, buffer);
|
msg = sftp_dequeue(sftp, id);
|
||||||
buffer_free(buffer);
|
}
|
||||||
while (!msg) {
|
|
||||||
if (sftp_read_and_dispatch(sftp))
|
/* By specification, this command only returns SSH_FXP_STATUS */
|
||||||
return -1;
|
if (msg->packet_type == SSH_FXP_STATUS) {
|
||||||
msg = sftp_dequeue(sftp, id);
|
status = parse_status_msg(msg);
|
||||||
|
sftp_message_free(msg);
|
||||||
|
if (status == NULL) {
|
||||||
|
return -1;
|
||||||
}
|
}
|
||||||
if (msg->packet_type == SSH_FXP_STATUS) {
|
sftp_set_error(sftp, status->status);
|
||||||
/* by specification, this command's only supposed to return SSH_FXP_STATUS */
|
switch (status->status) {
|
||||||
status = parse_status_msg(msg);
|
case SSH_FX_FAILURE:
|
||||||
sftp_message_free(msg);
|
/*
|
||||||
if (status == NULL) {
|
* mkdir always returns a failure, even if the path already exists.
|
||||||
return -1;
|
* To be POSIX conform and to be able to map it to EEXIST a stat
|
||||||
|
* call is needed here.
|
||||||
|
*/
|
||||||
|
errno_attr = sftp_lstat(sftp, directory);
|
||||||
|
if (errno_attr != NULL) {
|
||||||
|
SAFE_FREE(errno_attr);
|
||||||
|
sftp_set_error(sftp, SSH_FX_FILE_ALREADY_EXISTS);
|
||||||
}
|
}
|
||||||
sftp_set_error(sftp, status->status);
|
break;
|
||||||
switch (status->status) {
|
case SSH_FX_OK:
|
||||||
case SSH_FX_FAILURE:
|
|
||||||
/*
|
|
||||||
* mkdir always returns a failure, even if the path already exists.
|
|
||||||
* To be POSIX conform and to be able to map it to EEXIST a stat
|
|
||||||
* call should be issued here
|
|
||||||
*/
|
|
||||||
errno_attr = sftp_lstat(sftp, directory);
|
|
||||||
if (errno_attr != NULL) {
|
|
||||||
sftp_set_error(sftp, SSH_FX_FILE_ALREADY_EXISTS);
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case SSH_FX_OK:
|
|
||||||
status_msg_free(status);
|
|
||||||
return 0;
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
/* status should be SSH_FX_OK if the command was successful, if it didn't, then there was an error */
|
|
||||||
ssh_set_error(sftp->session,SSH_REQUEST_DENIED, "sftp server: %s", status->errormsg);
|
|
||||||
status_msg_free(status);
|
status_msg_free(status);
|
||||||
return -1;
|
return 0;
|
||||||
} else {
|
break;
|
||||||
ssh_set_error(sftp->session,SSH_FATAL, "Received message %d when attempting to make directory", msg->packet_type);
|
default:
|
||||||
sftp_message_free(msg);
|
break;
|
||||||
}
|
}
|
||||||
|
/*
|
||||||
|
* The status should be SSH_FX_OK if the command was successful, if it
|
||||||
|
* didn't, then there was an error
|
||||||
|
*/
|
||||||
|
ssh_set_error(sftp->session, SSH_REQUEST_DENIED,
|
||||||
|
"SFTP server: %s", status->errormsg);
|
||||||
|
status_msg_free(status);
|
||||||
return -1;
|
return -1;
|
||||||
|
} else {
|
||||||
|
ssh_set_error(sftp->session, SSH_FATAL,
|
||||||
|
"Received message %d when attempting to make directory",
|
||||||
|
msg->packet_type);
|
||||||
|
sftp_message_free(msg);
|
||||||
|
}
|
||||||
|
|
||||||
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* code written by nick */
|
/* code written by nick */
|
||||||
|
|||||||
Reference in New Issue
Block a user