1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
|
/*
* This file is part of the Green End SFTP Server.
* Copyright (C) 2007, 2011 Richard Kettlewell
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
* USA
*/
/** @file handle.c @brief File handle implementation */
#include "sftpserver.h"
#include "debug.h"
#include "utils.h"
#include "sftp.h"
#include "handle.h"
#include "thread.h"
#include "types.h"
#include <assert.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
/** @brief Handle data structure */
struct handle {
int type; /**< @brief @ref SSH_FXP_OPEN or @ref SSH_FXP_OPENDIR */
uint32_t tag; /**< @brief Unique tag or 0 for unused */
union {
int fd; /**< @brief File descriptor for a file */
DIR *dir; /**< @brief Directory stream */
} u;
char *path; /**< @brief Name of file or directory */
unsigned flags; /**< @brief Flags */
};
/** @brief Table of handles */
static struct handle *handles;
/** @brief Size of @p handles array */
static size_t nhandles;
/** @brief Next sequence number */
static uint32_t sequence;
#if NTHREADS > 1
/** @brief Lock protecting handles data structure */
static pthread_mutex_t sftp_handle_lock = PTHREAD_MUTEX_INITIALIZER;
#endif
/** @brief Find a free slot in @ref handles
* @param id Where to store handle
* @param type @ref SSH_FXP_OPEN or @ref SSH_FXP_OPENDIR
*/
static void find_free_handle(struct handleid *id, int type) {
size_t n;
for(n = 0; n < nhandles && handles[n].tag; ++n)
;
if(n == nhandles && nhandles < MAXHANDLES) {
/* need more space */
nhandles = (nhandles ? 2 * nhandles : 16);
assert(nhandles != 0);
handles = xrecalloc(handles, nhandles, sizeof (*handles));
memset(handles + n, 0, (nhandles - n) * sizeof (*handles));
}
while(!sequence)
++sequence; /* never have a tag of 0 */
handles[n].tag = sequence++;
handles[n].type = type;
id->id = n;
id->tag = handles[n].tag;
}
void sftp_handle_new_file(struct handleid *id,
int fd, const char *path, unsigned flags) {
ferrcheck(pthread_mutex_lock(&sftp_handle_lock));
find_free_handle(id, SSH_FXP_OPEN);
handles[id->id].u.fd = fd;
handles[id->id].path = xstrdup(path);
handles[id->id].flags = flags;
ferrcheck(pthread_mutex_unlock(&sftp_handle_lock));
}
void sftp_handle_new_dir(struct handleid *id, DIR *dp, const char *path) {
ferrcheck(pthread_mutex_lock(&sftp_handle_lock));
find_free_handle(id, SSH_FXP_OPENDIR);
handles[id->id].u.dir = dp;
handles[id->id].path = xstrdup(path);
ferrcheck(pthread_mutex_unlock(&sftp_handle_lock));
}
uint32_t sftp_handle_get_fd(const struct handleid *id,
int *fd, unsigned *flagsp) {
uint32_t rc;
ferrcheck(pthread_mutex_lock(&sftp_handle_lock));
if(id->id < nhandles
&& id->tag == handles[id->id].tag
&& handles[id->id].type == SSH_FXP_OPEN) {
*fd = handles[id->id].u.fd;
if(flagsp)
*flagsp = handles[id->id].flags;
rc = 0;
} else
rc = SSH_FX_INVALID_HANDLE;
ferrcheck(pthread_mutex_unlock(&sftp_handle_lock));
return rc;
}
uint32_t sftp_handle_get_dir(const struct handleid *id,
DIR **dp, const char **pathp) {
uint32_t rc;
ferrcheck(pthread_mutex_lock(&sftp_handle_lock));
if(id->id < nhandles
&& id->tag == handles[id->id].tag
&& handles[id->id].type == SSH_FXP_OPENDIR) {
*dp = handles[id->id].u.dir;
if(pathp)
*pathp = handles[id->id].path;
rc = 0;
} else
rc = SSH_FX_INVALID_HANDLE;
ferrcheck(pthread_mutex_unlock(&sftp_handle_lock));
return rc;
}
uint32_t sftp_handle_close(const struct handleid *id) {
uint32_t rc;
if(!id->tag)
return SSH_FX_INVALID_HANDLE;
ferrcheck(pthread_mutex_lock(&sftp_handle_lock));
if(id->id < nhandles
&& id->tag == handles[id->id].tag) {
handles[id->id].tag = 0; /* free up */
switch(handles[id->id].type) {
case SSH_FXP_OPEN:
if(close(handles[id->id].u.fd) < 0)
rc = HANDLER_ERRNO;
else
rc = 0;
break;
case SSH_FXP_OPENDIR:
if(closedir(handles[id->id].u.dir) < 0)
rc = HANDLER_ERRNO;
else
rc = 0;
break;
default:
rc = SSH_FX_INVALID_HANDLE;
}
free(handles[id->id].path);
handles[id->id].path = NULL;
}
else
rc = SSH_FX_INVALID_HANDLE;
ferrcheck(pthread_mutex_unlock(&sftp_handle_lock));
return rc;
}
unsigned sftp_handle_flags(const struct handleid *id) {
unsigned rc;
ferrcheck(pthread_mutex_lock(&sftp_handle_lock));
if(id->id < nhandles
&& id->tag == handles[id->id].tag)
rc = handles[id->id].flags;
else
rc = 0;
ferrcheck(pthread_mutex_unlock(&sftp_handle_lock));
return rc;
}
/*
Local Variables:
c-basic-offset:2
comment-column:40
fill-column:79
indent-tabs-mode:nil
End:
*/
|