Author: Pekka Riikonen <priikone@silcnet.org>
- Copyright (C) 2001 - 2004 Pekka Riikonen
+ Copyright (C) 2001 - 2007 Pekka Riikonen
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
/* Add `entry' to directory `dir'. */
-static bool mem_add_entry(MemFSEntry dir, MemFSEntry entry,
- bool check_perm)
+static SilcBool mem_add_entry(MemFSEntry dir, MemFSEntry entry,
+ SilcBool check_perm)
{
int i;
/* Removes entry `entry' and all entries under it recursively. */
-static bool mem_del_entry(MemFSEntry entry, bool check_perm)
+static SilcBool mem_del_entry(MemFSEntry entry, SilcBool check_perm)
{
int i;
/* Deletes entry by the name `name' from the directory `dir'. This does
not check subdirectories recursively. */
-static bool mem_del_entry_name(MemFSEntry dir, const char *name,
- SilcUInt32 name_len, bool check_perm)
+static SilcBool mem_del_entry_name(MemFSEntry dir, const char *name,
+ SilcUInt32 name_len, SilcBool check_perm)
{
MemFSEntry entry;
/* Deletes the handle and remove it from the open handle list. */
-static bool mem_del_handle(MemFS fs, MemFSFileHandle handle)
+static SilcBool mem_del_handle(MemFS fs, MemFSFileHandle handle)
{
if (handle->handle > fs->handles_count)
return FALSE;
fs->root_perm = perm;
fs->root->directory = TRUE;
fs->root->name = strdup(DIR_SEPARATOR);
+ if (!fs->root->name) {
+ silc_free(fs->root);
+ silc_free(fs);
+ }
filesystem = silc_calloc(1, sizeof(*filesystem));
if (!filesystem) {
+ silc_free(fs->root->name);
silc_free(fs->root);
silc_free(fs);
return NULL;
return NULL;
entry->perm = perm;
- entry->name = strdup(name);
entry->directory = TRUE;
entry->parent = dir ? dir : memfs->root;
+ entry->name = strdup(name);
+ if (!entry->name) {
+ silc_free(entry);
+ return NULL;
+ }
- if (!mem_add_entry(dir ? dir : memfs->root, entry, FALSE))
+ if (!mem_add_entry(dir ? dir : memfs->root, entry, FALSE)) {
+ silc_free(entry->name);
+ silc_free(entry);
return NULL;
+ }
return entry;
}
in memory file system. The filesystem does not allow removing directories
with remote access using the filesystem access function sftp_rmdir. */
-bool silc_sftp_fs_memory_del_dir(SilcSFTPFilesystem fs, void *dir)
+SilcBool silc_sftp_fs_memory_del_dir(SilcSFTPFilesystem fs, void *dir)
{
MemFS memfs = (MemFS)fs->fs_context;
- bool ret;
+ SilcBool ret;
if (dir)
return mem_del_entry(dir, FALSE);
memfs->root->perm = memfs->root_perm;
memfs->root->directory = TRUE;
memfs->root->name = strdup(DIR_SEPARATOR);
+ if (!memfs->root->name) {
+ silc_free(memfs->root);
+ memfs->root = NULL;
+ return FALSE;
+ }
return ret;
}
file and they work in POSIX style. Returns TRUE if the file was
added to the directory. */
-bool silc_sftp_fs_memory_add_file(SilcSFTPFilesystem fs, void *dir,
- SilcSFTPFSMemoryPerm perm,
- const char *filename,
- const char *realpath)
+SilcBool silc_sftp_fs_memory_add_file(SilcSFTPFilesystem fs, void *dir,
+ SilcSFTPFSMemoryPerm perm,
+ const char *filename,
+ const char *realpath)
{
MemFS memfs = (MemFS)fs->fs_context;
MemFSEntry entry;
return FALSE;
entry->perm = perm;
+ entry->directory = FALSE;
entry->name = strdup(filename);
entry->data = strdup(realpath);
- entry->directory = FALSE;
+ if (!entry->name || !entry->data) {
+ silc_free(entry->name);
+ silc_free(entry->data);
+ silc_free(entry);
+ return FALSE;
+ }
return mem_add_entry(dir ? dir : memfs->root, entry, FALSE);
}
/* Removes a file indicated by the `filename' from the directory
indicated by the `dir'. Returns TRUE if the removing was success. */
-bool silc_sftp_fs_memory_del_file(SilcSFTPFilesystem fs, void *dir,
- const char *filename)
+SilcBool silc_sftp_fs_memory_del_file(SilcSFTPFilesystem fs, void *dir,
+ const char *filename)
{
MemFS memfs = (MemFS)fs->fs_context;
void *callback_context)
{
MemFSFileHandle h = (MemFSFileHandle)handle;
- unsigned char *data;
+ unsigned char data[32768];
int ret;
if (len > 32768)
len = 32768;
- data = silc_malloc(len);
- if (!data) {
- (*callback)(sftp, SILC_SFTP_STATUS_EOF, NULL, 0, callback_context);
+ ret = lseek(h->fd, (off_t)offset, SEEK_SET);
+ if (ret < 0) {
+ if (!ret)
+ (*callback)(sftp, SILC_SFTP_STATUS_EOF, NULL, 0, callback_context);
+ else
+ (*callback)(sftp, silc_sftp_map_errno(errno), NULL, 0, callback_context);
return;
}
- lseek(h->fd, (off_t)offset, SEEK_SET);
/* Attempt to read */
ret = silc_file_read(h->fd, data, len);
(*callback)(sftp, SILC_SFTP_STATUS_EOF, NULL, 0, callback_context);
else
(*callback)(sftp, silc_sftp_map_errno(errno), NULL, 0, callback_context);
- silc_free(data);
return;
}
/* Return data */
(*callback)(sftp, SILC_SFTP_STATUS_OK, (const unsigned char *)data,
ret, callback_context);
-
- silc_free(data);
}
void mem_write(void *context, SilcSFTP sftp,
filesize = sizeof(*entry);
memset(long_name, 0, sizeof(long_name));
- date = (char *)silc_get_time(entry->created);
+ date = (char *)silc_time_string(entry->created);
if (strrchr(date, ':'))
*strrchr(date, ':') = '\0';
/* Long name format is:
drwx------ 1 324210 Apr 8 08:40 mail/
1234567890 123 12345678 123456789012 */
- snprintf(long_name, sizeof(long_name) - 1,
+ silc_snprintf(long_name, sizeof(long_name) - 1,
"%c%c%c%c------ %3d %8llu %12s %s%s",
(entry->directory ? 'd' : '-'),
((entry->perm & SILC_SFTP_FS_PERM_READ) ? 'r' : '-'),