5 Author: Pekka Riikonen <priikone@silcnet.org>
7 Copyright (C) 2001 - 2007 Pekka Riikonen
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; version 2 of the License.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
20 /* XXX TODO Win32 support */
24 #include "silcsftp_fs.h"
25 #include "sftp_util.h"
27 #define DIR_SEPARATOR "/"
29 const struct SilcSFTPFilesystemOpsStruct silc_sftp_fs_memory;
31 typedef struct MemFSEntryStruct {
32 struct MemFSEntryStruct **entry; /* Files and sub-directories */
33 SilcUInt32 entry_count; /* Number of files and sub-directories */
34 struct MemFSEntryStruct *parent; /* non-NULL if `directory' is TRUE,
35 includes parent directory. */
36 SilcUInt32 created; /* Time of creation */
37 char *name; /* Name of the entry */
38 char *data; /* Data of the entry */
39 unsigned int directory : 1; /* Set if this is directory */
40 unsigned int perm : 7; /* Permissions */
45 SilcUInt32 handle; /* Handle index */
46 int fd; /* Real file handle */
47 MemFSEntry entry; /* Filesystem entry */
50 /* Memory filesystem */
52 MemFSEntry root; /* Root of the filesystem hierarchy */
53 SilcSFTPFSMemoryPerm root_perm;
54 MemFSFileHandle *handles; /* Open file handles */
55 SilcUInt32 handles_count;
58 /* Generates absolute path from relative path that may include '.' and '..'
61 static char *mem_expand_path(MemFSEntry root, const char *path)
63 if (!strstr(path, "./") && !strstr(path, "../") &&
64 !strstr(path, "/..") && !strstr(path, "/."))
71 /* Add `entry' to directory `dir'. */
73 static SilcBool mem_add_entry(MemFSEntry dir, MemFSEntry entry,
78 /* Must be both write and exec permissions */
80 !((dir->perm & SILC_SFTP_FS_PERM_WRITE) &&
81 (dir->perm & SILC_SFTP_FS_PERM_EXEC)))
85 dir->entry = silc_calloc(3, sizeof(*entry));
88 dir->entry[0] = entry;
90 entry->created = time(0);
94 for (i = 0; i < dir->entry_count; i++) {
98 dir->entry[i] = entry;
99 entry->created = time(0);
103 dir->entry = silc_realloc(dir->entry, sizeof(*dir->entry) *
104 (dir->entry_count + 3));
107 for (i = dir->entry_count + 1; i < dir->entry_count + 3; i++)
108 dir->entry[i] = NULL;
109 dir->entry[dir->entry_count] = entry;
110 dir->entry_count += 3;
111 entry->created = time(0);
116 /* Removes entry `entry' and all entries under it recursively. */
118 static SilcBool mem_del_entry(MemFSEntry entry, SilcBool check_perm)
122 /* Directories cannot be removed from remote access */
126 silc_free(entry->name);
127 silc_free(entry->data);
129 /* Delete all entries recursively under this entry */
130 for (i = 0; i < entry->entry_count; i++) {
131 if (entry->entry[i]) {
132 if (!mem_del_entry(entry->entry[i], FALSE))
136 silc_free(entry->entry);
138 /* Remove from parent */
140 for (i = 0; i < entry->parent->entry_count; i++) {
141 if (entry->parent->entry[i] == entry) {
142 entry->parent->entry[i] = NULL;
153 /* Finds first occurence of entry named `name' under the directory `dir'.
154 This does not check subdirectories recursively. */
156 static MemFSEntry mem_find_entry(MemFSEntry dir, const char *name,
161 for (i = 0; i < dir->entry_count; i++) {
165 if (!strncmp(name, dir->entry[i]->name, name_len))
166 return dir->entry[i];
172 /* Finds the entry by the `path' which may include full path or
175 static MemFSEntry mem_find_entry_path(MemFSEntry dir, const char *p)
177 MemFSEntry entry = NULL;
181 cp = path = mem_expand_path(dir, p);
183 if (strlen(cp) == 1 && cp[0] == '/')
188 len = strcspn(cp, DIR_SEPARATOR);
190 entry = mem_find_entry(dir, cp, len);
199 len = strcspn(cp, DIR_SEPARATOR);
207 /* Deletes entry by the name `name' from the directory `dir'. This does
208 not check subdirectories recursively. */
210 static SilcBool mem_del_entry_name(MemFSEntry dir, const char *name,
211 SilcUInt32 name_len, SilcBool check_perm)
215 /* Files cannot be removed from remote access */
219 entry = mem_find_entry(dir, name, name_len);
222 return mem_del_entry(entry, check_perm);
227 /* Create new handle and add it to the list of open handles. */
229 static MemFSFileHandle mem_create_handle(MemFS fs, int fd, MemFSEntry entry)
231 MemFSFileHandle handle;
234 handle = silc_calloc(1, sizeof(*handle));
238 handle->entry = entry;
241 fs->handles = silc_calloc(5, sizeof(*fs->handles));
244 fs->handles[0] = handle;
245 fs->handles_count = 5;
252 for (i = 0; i < fs->handles_count; i++) {
256 fs->handles[i] = handle;
263 fs->handles = silc_realloc(fs->handles, sizeof(*fs->handles) *
264 (fs->handles_count + 5));
267 for (i = fs->handles_count + 1; i < fs->handles_count + 5; i++)
268 fs->handles[i] = NULL;
269 fs->handles[fs->handles_count] = handle;
270 handle->handle = fs->handles_count;
271 fs->handles_count += 5;
276 /* Deletes the handle and remove it from the open handle list. */
278 static SilcBool mem_del_handle(MemFS fs, MemFSFileHandle handle)
280 if (handle->handle > fs->handles_count)
283 if (!fs->handles[handle->handle])
286 if (fs->handles[handle->handle] == handle) {
287 fs->handles[handle->handle] = NULL;
288 if (handle->fd != -1)
289 silc_file_close(handle->fd);
297 /* Find handle by handle index. */
299 static MemFSFileHandle mem_find_handle(MemFS fs, SilcUInt32 handle)
301 if (handle > fs->handles_count)
304 if (!fs->handles[handle])
307 if (fs->handles[handle]->handle != handle)
310 return fs->handles[handle];
313 /* Allocates memory filesystem context and returns the context. The
314 context can be given as argument to the silc_sftp_server_start function.
315 The context must be freed by the caller using the function
316 silc_sftp_fs_memory_free. The `perm' is the permissions for the root
317 directory of the filesystem (/ dir). */
319 SilcSFTPFilesystem silc_sftp_fs_memory_alloc(SilcSFTPFSMemoryPerm perm)
321 SilcSFTPFilesystem filesystem;
324 fs = silc_calloc(1, sizeof(*fs));
328 fs->root = silc_calloc(1, sizeof(*fs->root));
334 fs->root->perm = perm;
335 fs->root_perm = perm;
336 fs->root->directory = TRUE;
337 fs->root->name = strdup(DIR_SEPARATOR);
338 if (!fs->root->name) {
343 filesystem = silc_calloc(1, sizeof(*filesystem));
345 silc_free(fs->root->name);
351 filesystem->fs = (struct SilcSFTPFilesystemOpsStruct *)&silc_sftp_fs_memory;
352 filesystem->fs_context = (void *)fs;
357 /* Frees the memory filesystem context. */
359 void silc_sftp_fs_memory_free(SilcSFTPFilesystem fs)
361 MemFS memfs = (MemFS)fs->fs_context;
363 silc_free(memfs->root);
367 /* Adds a new directory to the memory filesystem. Returns the directory
368 context that can be used to add for example files to the directory
369 or new subdirectories under the directory. The `dir' is the parent
370 directory of the directory to be added. If this directory is to be
371 added to the root directory the `dir' is NULL. The `name' is the name
372 of the directory. If error occurs this returns NULL. The caller must
373 not free the returned context. The `perm' will indicate the permissions
374 for the directory and they work in POSIX style. */
376 void *silc_sftp_fs_memory_add_dir(SilcSFTPFilesystem fs, void *dir,
377 SilcSFTPFSMemoryPerm perm,
380 MemFS memfs = (MemFS)fs->fs_context;
383 entry = silc_calloc(1, sizeof(*entry));
388 entry->directory = TRUE;
389 entry->parent = dir ? dir : memfs->root;
390 entry->name = strdup(name);
396 if (!mem_add_entry(dir ? dir : memfs->root, entry, FALSE)) {
397 silc_free(entry->name);
405 /* Deletes a directory indicated by the `dir'. All files and subdirectories
406 in this directory is also removed. If the `dir' is NULL then all
407 directories and files are removed from the filesystem. Returns TRUE
408 if the removing was success. This is the only way to remove directories
409 in memory file system. The filesystem does not allow removing directories
410 with remote access using the filesystem access function sftp_rmdir. */
412 SilcBool silc_sftp_fs_memory_del_dir(SilcSFTPFilesystem fs, void *dir)
414 MemFS memfs = (MemFS)fs->fs_context;
418 return mem_del_entry(dir, FALSE);
420 /* Remove from root */
421 ret = mem_del_entry(memfs->root, FALSE);
423 memfs->root = silc_calloc(1, sizeof(*memfs->root));
427 memfs->root->perm = memfs->root_perm;
428 memfs->root->directory = TRUE;
429 memfs->root->name = strdup(DIR_SEPARATOR);
430 if (!memfs->root->name) {
431 silc_free(memfs->root);
439 /* Adds a new file to the directory indicated by the `dir'. If the `dir'
440 is NULL the file is added to the root directory. The `filename' is the
441 filename in the directory. The `realpath' is the real filepath in the
442 physical filesystem. It is used to actually access the file from the
443 memory filesystem. The `perm' will indicate the permissions for th e
444 file and they work in POSIX style. Returns TRUE if the file was
445 added to the directory. */
447 SilcBool silc_sftp_fs_memory_add_file(SilcSFTPFilesystem fs, void *dir,
448 SilcSFTPFSMemoryPerm perm,
449 const char *filename,
450 const char *realpath)
452 MemFS memfs = (MemFS)fs->fs_context;
455 entry = silc_calloc(1, sizeof(*entry));
460 entry->directory = FALSE;
461 entry->name = strdup(filename);
462 entry->data = strdup(realpath);
463 if (!entry->name || !entry->data) {
464 silc_free(entry->name);
465 silc_free(entry->data);
470 return mem_add_entry(dir ? dir : memfs->root, entry, FALSE);
473 /* Removes a file indicated by the `filename' from the directory
474 indicated by the `dir'. Returns TRUE if the removing was success. */
476 SilcBool silc_sftp_fs_memory_del_file(SilcSFTPFilesystem fs, void *dir,
477 const char *filename)
479 MemFS memfs = (MemFS)fs->fs_context;
484 return mem_del_entry_name(dir ? dir : memfs->root, filename,
485 strlen(filename), FALSE);
488 SilcSFTPHandle mem_get_handle(void *context, SilcSFTP sftp,
489 const unsigned char *data,
492 MemFS fs = (MemFS)context;
498 SILC_GET32_MSB(handle, data);
499 return (SilcSFTPHandle)mem_find_handle(fs, handle);
502 unsigned char *mem_encode_handle(void *context, SilcSFTP sftp,
503 SilcSFTPHandle handle,
504 SilcUInt32 *handle_len)
507 MemFSFileHandle h = (MemFSFileHandle)handle;
509 data = silc_calloc(4, sizeof(*data));
513 SILC_PUT32_MSB(h->handle, data);
518 void mem_open(void *context, SilcSFTP sftp,
519 const char *filename,
520 SilcSFTPFileOperation pflags,
521 SilcSFTPAttributes attrs,
522 SilcSFTPHandleCallback callback,
523 void *callback_context)
525 MemFS fs = (MemFS)context;
527 MemFSFileHandle handle;
530 /* CREAT and TRUNC not supported */
531 if ((pflags & SILC_SFTP_FXF_CREAT) || (pflags & SILC_SFTP_FXF_TRUNC)) {
532 (*callback)(sftp, SILC_SFTP_STATUS_OP_UNSUPPORTED, NULL, callback_context);
537 entry = mem_find_entry_path(fs->root, filename);
539 (*callback)(sftp, SILC_SFTP_STATUS_NO_SUCH_FILE, NULL, callback_context);
543 if (entry->directory || !entry->data) {
544 (*callback)(sftp, SILC_SFTP_STATUS_FAILURE, NULL, callback_context);
548 /* Check for reading */
549 if ((pflags & SILC_SFTP_FXF_READ) &&
550 !(entry->perm & SILC_SFTP_FS_PERM_READ)) {
551 (*callback)(sftp, SILC_SFTP_STATUS_PERMISSION_DENIED, NULL,
556 /* Check for writing */
557 if (((pflags & SILC_SFTP_FXF_WRITE) || (pflags & SILC_SFTP_FXF_APPEND)) &&
558 !(entry->perm & SILC_SFTP_FS_PERM_WRITE)) {
559 (*callback)(sftp, SILC_SFTP_STATUS_PERMISSION_DENIED, NULL,
564 if ((pflags & SILC_SFTP_FXF_READ) && (pflags & SILC_SFTP_FXF_WRITE))
566 else if (pflags & SILC_SFTP_FXF_READ)
568 else if (pflags & SILC_SFTP_FXF_WRITE)
570 if (pflags & SILC_SFTP_FXF_APPEND)
573 /* Attempt to open the file for real. */
574 fd = silc_file_open_mode(entry->data + 7, flags,
575 (attrs->flags & SILC_SFTP_ATTR_PERMISSIONS ?
576 attrs->permissions : 0600));
578 (*callback)(sftp, silc_sftp_map_errno(errno), NULL, callback_context);
582 /* File opened, return handle */
583 handle = mem_create_handle(fs, fd, entry);
585 (*callback)(sftp, SILC_SFTP_STATUS_OK, (SilcSFTPHandle)handle,
588 (*callback)(sftp, SILC_SFTP_STATUS_PERMISSION_DENIED, NULL,
592 void mem_close(void *context, SilcSFTP sftp,
593 SilcSFTPHandle handle,
594 SilcSFTPStatusCallback callback,
595 void *callback_context)
597 MemFS fs = (MemFS)context;
598 MemFSFileHandle h = (MemFSFileHandle)handle;
602 ret = silc_file_close(h->fd);
604 (*callback)(sftp, silc_sftp_map_errno(errno), NULL, NULL,
610 mem_del_handle(fs, h);
611 (*callback)(sftp, SILC_SFTP_STATUS_OK, NULL, NULL, callback_context);
614 void mem_read(void *context, SilcSFTP sftp,
615 SilcSFTPHandle handle,
618 SilcSFTPDataCallback callback,
619 void *callback_context)
621 MemFSFileHandle h = (MemFSFileHandle)handle;
622 unsigned char data[32768];
628 ret = lseek(h->fd, (off_t)offset, SEEK_SET);
631 (*callback)(sftp, SILC_SFTP_STATUS_EOF, NULL, 0, callback_context);
633 (*callback)(sftp, silc_sftp_map_errno(errno), NULL, 0, callback_context);
637 /* Attempt to read */
638 ret = silc_file_read(h->fd, data, len);
641 (*callback)(sftp, SILC_SFTP_STATUS_EOF, NULL, 0, callback_context);
643 (*callback)(sftp, silc_sftp_map_errno(errno), NULL, 0, callback_context);
648 (*callback)(sftp, SILC_SFTP_STATUS_OK, (const unsigned char *)data,
649 ret, callback_context);
652 void mem_write(void *context, SilcSFTP sftp,
653 SilcSFTPHandle handle,
655 const unsigned char *data,
657 SilcSFTPStatusCallback callback,
658 void *callback_context)
660 MemFSFileHandle h = (MemFSFileHandle)handle;
663 lseek(h->fd, (off_t)offset, SEEK_SET);
665 /* Attempt to write */
666 ret = silc_file_write(h->fd, data, data_len);
668 (*callback)(sftp, silc_sftp_map_errno(errno), NULL, NULL,
673 (*callback)(sftp, SILC_SFTP_STATUS_OK, NULL, NULL, callback_context);
676 void mem_remove(void *context, SilcSFTP sftp,
677 const char *filename,
678 SilcSFTPStatusCallback callback,
679 void *callback_context)
681 /* Remove is not supported */
682 (*callback)(sftp, SILC_SFTP_STATUS_OP_UNSUPPORTED, NULL, NULL,
686 void mem_rename(void *context, SilcSFTP sftp,
689 SilcSFTPStatusCallback callback,
690 void *callback_context)
692 /* Rename is not supported */
693 (*callback)(sftp, SILC_SFTP_STATUS_OP_UNSUPPORTED, NULL, NULL,
697 void mem_mkdir(void *context, SilcSFTP sftp,
699 SilcSFTPAttributes attrs,
700 SilcSFTPStatusCallback callback,
701 void *callback_context)
703 /* Mkdir is not supported */
704 (*callback)(sftp, SILC_SFTP_STATUS_OP_UNSUPPORTED, NULL, NULL,
708 void mem_rmdir(void *context, SilcSFTP sftp,
710 SilcSFTPStatusCallback callback,
711 void *callback_context)
713 /* Rmdir is not supported */
714 (*callback)(sftp, SILC_SFTP_STATUS_OP_UNSUPPORTED, NULL, NULL,
718 void mem_opendir(void *context, SilcSFTP sftp,
720 SilcSFTPHandleCallback callback,
721 void *callback_context)
723 MemFS fs = (MemFS)context;
725 MemFSFileHandle handle;
727 if (!path || !strlen(path))
728 path = (const char *)DIR_SEPARATOR;
730 /* Find such directory */
731 entry = mem_find_entry_path(fs->root, path);
733 (*callback)(sftp, SILC_SFTP_STATUS_NO_SUCH_FILE, NULL, callback_context);
737 if (!entry->directory) {
738 (*callback)(sftp, SILC_SFTP_STATUS_FAILURE, NULL, callback_context);
742 /* Must be read permissions to open a directory */
743 if (!(entry->perm & SILC_SFTP_FS_PERM_READ)) {
744 (*callback)(sftp, SILC_SFTP_STATUS_PERMISSION_DENIED, NULL,
749 /* Directory opened, return handle */
750 handle = mem_create_handle(fs, 0, entry);
752 (*callback)(sftp, SILC_SFTP_STATUS_OK, (SilcSFTPHandle)handle,
755 (*callback)(sftp, SILC_SFTP_STATUS_PERMISSION_DENIED, NULL,
759 void mem_readdir(void *context, SilcSFTP sftp,
760 SilcSFTPHandle handle,
761 SilcSFTPNameCallback callback,
762 void *callback_context)
764 MemFSFileHandle h = (MemFSFileHandle)handle;
767 SilcSFTPAttributes attrs;
770 SilcUInt64 filesize = 0;
774 if (!h->entry->directory) {
775 (*callback)(sftp, SILC_SFTP_STATUS_FAILURE, NULL, callback_context);
780 (*callback)(sftp, SILC_SFTP_STATUS_EOF, NULL, callback_context);
784 name = silc_calloc(1, sizeof(*name));
786 (*callback)(sftp, SILC_SFTP_STATUS_EOF, NULL, callback_context);
789 for (i = h->fd; i < 100 + h->fd; i++) {
790 if (i >= h->entry->entry_count)
793 entry = h->entry->entry[i];
797 filesize = sizeof(*entry);
798 memset(long_name, 0, sizeof(long_name));
800 date = (char *)silc_time_string(entry->created);
801 if (strrchr(date, ':'))
802 *strrchr(date, ':') = '\0';
804 if (!entry->directory) {
805 filesize = silc_file_size(entry->data + 7);
806 memset(&stats, 0, sizeof(stats));
807 stat(entry->data + 7, &stats);
810 /* Long name format is:
811 drwx------ 1 324210 Apr 8 08:40 mail/
812 1234567890 123 12345678 123456789012 */
813 silc_snprintf(long_name, sizeof(long_name) - 1,
814 "%c%c%c%c------ %3d %8llu %12s %s%s",
815 (entry->directory ? 'd' : '-'),
816 ((entry->perm & SILC_SFTP_FS_PERM_READ) ? 'r' : '-'),
817 ((entry->perm & SILC_SFTP_FS_PERM_WRITE) ? 'w' : '-'),
818 ((entry->perm & SILC_SFTP_FS_PERM_EXEC) ? 'x' : '-'),
819 (entry->directory ? (int)entry->entry_count : 1),
821 (unsigned long long)filesize,
823 (unsigned long)filesize,
826 (entry->directory ? "/" :
827 ((entry->perm & SILC_SFTP_FS_PERM_EXEC) ? "*" : "")));
830 attrs = silc_calloc(1, sizeof(*attrs));
832 (*callback)(sftp, SILC_SFTP_STATUS_EOF, NULL, callback_context);
835 attrs->flags = (SILC_SFTP_ATTR_SIZE |
836 SILC_SFTP_ATTR_UIDGID);
837 attrs->size = filesize;
838 attrs->uid = 0; /* We use always 0 UID and GID */
840 if (!entry->directory) {
841 attrs->flags |= SILC_SFTP_ATTR_ACMODTIME;
842 attrs->atime = stats.st_atime;
843 attrs->mtime = stats.st_mtime;
847 silc_sftp_name_add(name, entry->name, long_name, attrs);
850 /* If we didn't read all then udpate the index for next read */
851 if (i >= h->entry->entry_count)
856 /* If names was not found then return EOF. */
857 if (name->count == 0) {
858 (*callback)(sftp, SILC_SFTP_STATUS_EOF, NULL, callback_context);
859 silc_sftp_name_free(name);
864 (*callback)(sftp, SILC_SFTP_STATUS_OK, (const SilcSFTPName)name,
867 silc_sftp_name_free(name);
870 void mem_stat(void *context, SilcSFTP sftp,
872 SilcSFTPAttrCallback callback,
873 void *callback_context)
875 MemFS fs = (MemFS)context;
877 SilcSFTPAttributes attrs;
881 if (!path || !strlen(path))
882 path = (const char *)DIR_SEPARATOR;
884 /* Find such directory */
885 entry = mem_find_entry_path(fs->root, path);
887 (*callback)(sftp, SILC_SFTP_STATUS_NO_SUCH_FILE, NULL, callback_context);
891 if (entry->directory || !entry->data) {
892 (*callback)(sftp, SILC_SFTP_STATUS_FAILURE, NULL, callback_context);
897 ret = stat(entry->data + 7, &stats);
899 (*callback)(sftp, silc_sftp_map_errno(errno), NULL, callback_context);
903 attrs = silc_calloc(1, sizeof(*attrs));
905 (*callback)(sftp, SILC_SFTP_STATUS_FAILURE, NULL, callback_context);
908 attrs->flags = (SILC_SFTP_ATTR_SIZE |
909 SILC_SFTP_ATTR_UIDGID |
910 SILC_SFTP_ATTR_ACMODTIME);
911 attrs->size = stats.st_size;
912 attrs->uid = 0; /* We use always 0 UID and GID */
914 attrs->atime = stats.st_atime;
915 attrs->mtime = stats.st_mtime;
917 /* Return attributes */
918 (*callback)(sftp, SILC_SFTP_STATUS_OK, (const SilcSFTPAttributes)attrs,
921 silc_sftp_attr_free(attrs);
924 void mem_lstat(void *context, SilcSFTP sftp,
926 SilcSFTPAttrCallback callback,
927 void *callback_context)
929 MemFS fs = (MemFS)context;
931 SilcSFTPAttributes attrs;
935 if (!path || !strlen(path))
936 path = (const char *)DIR_SEPARATOR;
938 /* Find such directory */
939 entry = mem_find_entry_path(fs->root, path);
941 (*callback)(sftp, SILC_SFTP_STATUS_NO_SUCH_FILE, NULL, callback_context);
945 if (entry->directory || !entry->data) {
946 (*callback)(sftp, SILC_SFTP_STATUS_FAILURE, NULL, callback_context);
952 ret = lstat(entry->data + 7, &stats);
954 ret = stat(entry->data + 7, &stats);
957 (*callback)(sftp, silc_sftp_map_errno(errno), NULL, callback_context);
961 attrs = silc_calloc(1, sizeof(*attrs));
963 (*callback)(sftp, SILC_SFTP_STATUS_FAILURE, NULL, callback_context);
966 attrs->flags = (SILC_SFTP_ATTR_SIZE |
967 SILC_SFTP_ATTR_UIDGID |
968 SILC_SFTP_ATTR_ACMODTIME);
969 attrs->size = stats.st_size;
970 attrs->uid = 0; /* We use always 0 UID and GID */
972 attrs->atime = stats.st_atime;
973 attrs->mtime = stats.st_mtime;
975 /* Return attributes */
976 (*callback)(sftp, SILC_SFTP_STATUS_OK, (const SilcSFTPAttributes)attrs,
979 silc_sftp_attr_free(attrs);
982 void mem_fstat(void *context, SilcSFTP sftp,
983 SilcSFTPHandle handle,
984 SilcSFTPAttrCallback callback,
985 void *callback_context)
987 MemFSFileHandle h = (MemFSFileHandle)handle;
988 SilcSFTPAttributes attrs;
992 if (h->entry->directory || !h->entry->data) {
993 (*callback)(sftp, SILC_SFTP_STATUS_FAILURE, NULL, callback_context);
998 ret = fstat(h->fd, &stats);
1000 (*callback)(sftp, silc_sftp_map_errno(errno), NULL, callback_context);
1004 attrs = silc_calloc(1, sizeof(*attrs));
1006 (*callback)(sftp, SILC_SFTP_STATUS_FAILURE, NULL, callback_context);
1009 attrs->flags = (SILC_SFTP_ATTR_SIZE |
1010 SILC_SFTP_ATTR_UIDGID |
1011 SILC_SFTP_ATTR_ACMODTIME);
1012 attrs->size = stats.st_size;
1013 attrs->uid = 0; /* We use always 0 UID and GID */
1015 attrs->atime = stats.st_atime;
1016 attrs->mtime = stats.st_mtime;
1018 /* Return attributes */
1019 (*callback)(sftp, SILC_SFTP_STATUS_OK, (const SilcSFTPAttributes)attrs,
1022 silc_sftp_attr_free(attrs);
1025 void mem_setstat(void *context, SilcSFTP sftp,
1027 SilcSFTPAttributes attrs,
1028 SilcSFTPStatusCallback callback,
1029 void *callback_context)
1031 /* Setstat is not supported */
1032 (*callback)(sftp, SILC_SFTP_STATUS_OP_UNSUPPORTED, NULL, NULL,
1036 void mem_fsetstat(void *context, SilcSFTP sftp,
1037 SilcSFTPHandle handle,
1038 SilcSFTPAttributes attrs,
1039 SilcSFTPStatusCallback callback,
1040 void *callback_context)
1042 /* Fsetstat is not supported */
1043 (*callback)(sftp, SILC_SFTP_STATUS_OP_UNSUPPORTED, NULL, NULL,
1047 void mem_readlink(void *context, SilcSFTP sftp,
1049 SilcSFTPNameCallback callback,
1050 void *callback_context)
1052 /* Readlink is not supported */
1053 (*callback)(sftp, SILC_SFTP_STATUS_OP_UNSUPPORTED, NULL,
1057 void mem_symlink(void *context, SilcSFTP sftp,
1058 const char *linkpath,
1059 const char *targetpath,
1060 SilcSFTPStatusCallback callback,
1061 void *callback_context)
1063 /* Symlink is not supported */
1064 (*callback)(sftp, SILC_SFTP_STATUS_OP_UNSUPPORTED, NULL, NULL,
1068 void mem_realpath(void *context, SilcSFTP sftp,
1070 SilcSFTPNameCallback callback,
1071 void *callback_context)
1073 MemFS fs = (MemFS)context;
1077 if (!path || !strlen(path))
1078 path = (const char *)DIR_SEPARATOR;
1080 realpath = mem_expand_path(fs->root, path);
1084 name = silc_calloc(1, sizeof(*name));
1088 name->filename = silc_calloc(1, sizeof(*name->filename));
1089 if (!name->filename)
1091 name->filename[0] = realpath;
1092 name->long_filename = silc_calloc(1, sizeof(*name->long_filename));
1093 if (!name->long_filename)
1095 name->long_filename[0] = realpath;
1096 name->attrs = silc_calloc(1, sizeof(*name->attrs));
1099 name->attrs[0] = silc_calloc(1, sizeof(*name->attrs[0]));
1100 if (!name->attrs[0])
1104 (*callback)(sftp, SILC_SFTP_STATUS_OK, (const SilcSFTPName)name,
1107 silc_sftp_name_free(name);
1111 (*callback)(sftp, SILC_SFTP_STATUS_FAILURE, NULL, callback_context);
1114 void mem_extended(void *context, SilcSFTP sftp,
1115 const char *request,
1116 const unsigned char *data,
1117 SilcUInt32 data_len,
1118 SilcSFTPExtendedCallback callback,
1119 void *callback_context)
1121 /* Extended is not supported */
1122 (*callback)(sftp, SILC_SFTP_STATUS_OP_UNSUPPORTED, NULL, 0,
1126 const struct SilcSFTPFilesystemOpsStruct silc_sftp_fs_memory = {