1
0
mirror of https://git.libssh.org/projects/libssh.git synced 2025-11-27 13:21:11 +03:00

Change scp API to use integers for perms

This commit is contained in:
Aris Adamantiadis
2009-09-02 16:34:32 +03:00
parent 86e6444656
commit 07a9e6b7c6
3 changed files with 53 additions and 28 deletions

View File

@@ -481,16 +481,16 @@ LIBSSH_API ssh_scp ssh_scp_new(ssh_session session, int mode, const char *locati
LIBSSH_API int ssh_scp_init(ssh_scp scp); LIBSSH_API int ssh_scp_init(ssh_scp scp);
LIBSSH_API int ssh_scp_close(ssh_scp scp); LIBSSH_API int ssh_scp_close(ssh_scp scp);
LIBSSH_API void ssh_scp_free(ssh_scp scp); LIBSSH_API void ssh_scp_free(ssh_scp scp);
LIBSSH_API int ssh_scp_push_directory(ssh_scp scp, const char *dirname, const char *perms); LIBSSH_API int ssh_scp_push_directory(ssh_scp scp, const char *dirname, int mode);
LIBSSH_API int ssh_scp_leave_directory(ssh_scp scp); LIBSSH_API int ssh_scp_leave_directory(ssh_scp scp);
LIBSSH_API int ssh_scp_push_file(ssh_scp scp, const char *filename, size_t size, const char *perms); LIBSSH_API int ssh_scp_push_file(ssh_scp scp, const char *filename, size_t size, int perms);
LIBSSH_API int ssh_scp_write(ssh_scp scp, const void *buffer, size_t len); LIBSSH_API int ssh_scp_write(ssh_scp scp, const void *buffer, size_t len);
LIBSSH_API int ssh_scp_pull_request(ssh_scp scp); LIBSSH_API int ssh_scp_pull_request(ssh_scp scp);
LIBSSH_API int ssh_scp_deny_request(ssh_scp scp, const char *reason); LIBSSH_API int ssh_scp_deny_request(ssh_scp scp, const char *reason);
LIBSSH_API int ssh_scp_accept_request(ssh_scp scp); LIBSSH_API int ssh_scp_accept_request(ssh_scp scp);
LIBSSH_API int ssh_scp_read(ssh_scp scp, void *buffer, size_t size); LIBSSH_API int ssh_scp_read(ssh_scp scp, void *buffer, size_t size);
LIBSSH_API const char *ssh_scp_request_get_filename(ssh_scp scp); LIBSSH_API const char *ssh_scp_request_get_filename(ssh_scp scp);
LIBSSH_API const char *ssh_scp_request_get_permissions(ssh_scp scp); LIBSSH_API int ssh_scp_request_get_permissions(ssh_scp scp);
LIBSSH_API size_t ssh_scp_request_get_size(ssh_scp scp); LIBSSH_API size_t ssh_scp_request_get_size(ssh_scp scp);

View File

@@ -371,7 +371,7 @@ struct ssh_scp_struct {
size_t processed; size_t processed;
enum ssh_scp_request_types request_type; enum ssh_scp_request_types request_type;
char *request_name; char *request_name;
char *request_mode; int request_mode;
}; };
struct ssh_message_struct; struct ssh_message_struct;
@@ -864,6 +864,8 @@ int ssh_execute_message_callbacks(SSH_SESSION *session);
/* scp.c */ /* scp.c */
int ssh_scp_read_string(ssh_scp scp, char *buffer, size_t len); int ssh_scp_read_string(ssh_scp scp, char *buffer, size_t len);
int ssh_scp_integer_mode(const char *mode);
char *ssh_scp_string_mode(int mode);
/* log.c */ /* log.c */

View File

@@ -120,30 +120,32 @@ void ssh_scp_free(ssh_scp scp){
if(scp->channel) if(scp->channel)
channel_free(scp->channel); channel_free(scp->channel);
SAFE_FREE(scp->location); SAFE_FREE(scp->location);
SAFE_FREE(scp->request_mode);
SAFE_FREE(scp->request_name); SAFE_FREE(scp->request_name);
SAFE_FREE(scp); SAFE_FREE(scp);
} }
/** @brief creates a directory in a scp in sink mode /** @brief creates a directory in a scp in sink mode
* @param dirname Name of the directory being created. * @param dirname Name of the directory being created.
* @param perms Text form of the unix permissions for the new directory, e.g. "0755". * @param mode Unix permissions for the new directory, e.g. 0755.
* @returns SSH_OK if the directory was created. * @returns SSH_OK if the directory was created.
* @returns SSH_ERROR if an error happened. * @returns SSH_ERROR if an error happened.
* @see ssh_scp_leave_directory * @see ssh_scp_leave_directory
*/ */
int ssh_scp_push_directory(ssh_scp scp, const char *dirname, const char *perms){ int ssh_scp_push_directory(ssh_scp scp, const char *dirname, int mode){
char buffer[1024]; char buffer[1024];
int r; int r;
uint8_t code; uint8_t code;
char *dir; char *dir;
char *perms;
if(scp->state != SSH_SCP_WRITE_INITED){ if(scp->state != SSH_SCP_WRITE_INITED){
ssh_set_error(scp->session,SSH_FATAL,"ssh_scp_push_directory called under invalid state"); ssh_set_error(scp->session,SSH_FATAL,"ssh_scp_push_directory called under invalid state");
return SSH_ERROR; return SSH_ERROR;
} }
dir=ssh_basename(dirname); dir=ssh_basename(dirname);
perms=ssh_scp_string_mode(mode);
snprintf(buffer, sizeof(buffer), "D%s 0 %s\n", perms, dir); snprintf(buffer, sizeof(buffer), "D%s 0 %s\n", perms, dir);
SAFE_FREE(dir); SAFE_FREE(dir);
SAFE_FREE(perms);
r=channel_write(scp->channel,buffer,strlen(buffer)); r=channel_write(scp->channel,buffer,strlen(buffer));
if(r==SSH_ERROR){ if(r==SSH_ERROR){
scp->state=SSH_SCP_ERROR; scp->state=SSH_SCP_ERROR;
@@ -190,20 +192,22 @@ int ssh_scp_push_directory(ssh_scp scp, const char *dirname, const char *perms){
/** @brief initializes the sending of a file to a scp in sink mode /** @brief initializes the sending of a file to a scp in sink mode
* @param filename Name of the file being sent. It should not contain any path indicator * @param filename Name of the file being sent. It should not contain any path indicator
* @param size Exact size in bytes of the file being sent. * @param size Exact size in bytes of the file being sent.
* @param perms Text form of the unix permissions for the new file, e.g. "0644" * @param mode Unix permissions for the new file, e.g. 0644
* @returns SSH_OK if the file is ready to be sent. * @returns SSH_OK if the file is ready to be sent.
* @returns SSH_ERROR if an error happened. * @returns SSH_ERROR if an error happened.
*/ */
int ssh_scp_push_file(ssh_scp scp, const char *filename, size_t size, const char *perms){ int ssh_scp_push_file(ssh_scp scp, const char *filename, size_t size, int mode){
char buffer[1024]; char buffer[1024];
int r; int r;
uint8_t code; uint8_t code;
char *file; char *file;
const char *perms;
if(scp->state != SSH_SCP_WRITE_INITED){ if(scp->state != SSH_SCP_WRITE_INITED){
ssh_set_error(scp->session,SSH_FATAL,"ssh_scp_push_file called under invalid state"); ssh_set_error(scp->session,SSH_FATAL,"ssh_scp_push_file called under invalid state");
return SSH_ERROR; return SSH_ERROR;
} }
file=ssh_basename(filename); file=ssh_basename(filename);
perms=ssh_scp_string_mode(mode);
snprintf(buffer, sizeof(buffer), "C%s %" PRIdS " %s\n", perms, size, file); snprintf(buffer, sizeof(buffer), "C%s %" PRIdS " %s\n", perms, size, file);
SAFE_FREE(file); SAFE_FREE(file);
r=channel_write(scp->channel,buffer,strlen(buffer)); r=channel_write(scp->channel,buffer,strlen(buffer));
@@ -300,8 +304,8 @@ int ssh_scp_read_string(ssh_scp scp, char *buffer, size_t len){
/** @brief waits for a scp request (file, directory) /** @brief waits for a scp request (file, directory)
* @returns SSH_ERROR Some error happened * @returns SSH_ERROR Some error happened
* @returns SSH_SCP_REQUEST_FILE The other side is sending a file * @returns SSH_SCP_REQUEST_NEWFILE The other side is sending a file
* @returns SSH_SCP_REQUEST_DIRECTORY The other side is sending a directory * @returns SSH_SCP_REQUEST_NEWDIRECTORY The other side is sending a directory
* @returns SSH_SCP_REQUEST_END_DIRECTORY The other side has finished with the current directory * @returns SSH_SCP_REQUEST_END_DIRECTORY The other side has finished with the current directory
* @see ssh_scp_read * @see ssh_scp_read
* @see ssh_scp_deny_request * @see ssh_scp_deny_request
@@ -331,7 +335,8 @@ int ssh_scp_pull_request(ssh_scp scp){
goto error; goto error;
*p='\0'; *p='\0';
p++; p++;
mode=strdup(&buffer[1]); //mode=strdup(&buffer[1]);
scp->request_mode=ssh_scp_integer_mode(&buffer[1]);
tmp=p; tmp=p;
p=strchr(p,' '); p=strchr(p,' ');
if(p==NULL) if(p==NULL)
@@ -346,8 +351,6 @@ int ssh_scp_pull_request(ssh_scp scp){
*p=0; *p=0;
name=strdup(tmp); name=strdup(tmp);
SAFE_FREE(scp->request_name); SAFE_FREE(scp->request_name);
SAFE_FREE(scp->request_mode);
scp->request_mode=mode;
scp->request_name=name; scp->request_name=name;
if(buffer[0]=='C'){ if(buffer[0]=='C'){
scp->filelen=size; scp->filelen=size;
@@ -461,7 +464,7 @@ int ssh_scp_read(ssh_scp scp, void *buffer, size_t size){
return r; return r;
} }
/** Gets the name of the directory or file being /** @brief Gets the name of the directory or file being
* pushed from the other party * pushed from the other party
* @returns file name. Should not be freed. * @returns file name. Should not be freed.
*/ */
@@ -469,18 +472,38 @@ const char *ssh_scp_request_get_filename(ssh_scp scp){
return scp->request_name; return scp->request_name;
} }
/** Gets the permissions of the directory or file being /**@brief Gets the permissions of the directory or file being
* pushed from the other party * pushed from the other party
* @returns Unix permission string, e.g "0644". Should not be freed. * @returns Unix permission, e.g 0644.
*/ */
const char *ssh_scp_request_get_permissions(ssh_scp scp){ int ssh_scp_request_get_permissions(ssh_scp scp){
return scp->request_mode; return scp->request_mode;
} }
/** Gets the size of the file being pushed /** @brief Gets the size of the file being pushed
* from the other party * from the other party
* @returns Numeric size of the file being read. * @returns Numeric size of the file being read.
*/ */
size_t ssh_scp_request_get_size(ssh_scp scp){ size_t ssh_scp_request_get_size(ssh_scp scp){
return scp->filelen; return scp->filelen;
} }
/** @brief Converts a scp text mode to an integer one
* @param mode mode to convert, e.g. "0644"
* @returns integer value, e.g. 420 for "0644"
*/
int ssh_scp_integer_mode(const char *mode){
int value=strtoul(mode,NULL,8) & 0xffff;
return value;
}
/** @brief Converts a unix mode into a scp string one.
* @param mode mode to convert, e.g. 420 or 0644
* @retuns pointer to a malloc'ed string containing the scp mode,
* e.g. "0644".
*/
char *ssh_scp_string_mode(int mode){
char buffer[16];
snprintf(buffer,sizeof(buffer),"%4o",mode);
return strdup(buffer);
}