updates.
[silc.git] / lib / silcsftp / sftp_fs_memory.c
index 9aac497000fbcdfa063b6eeac6822ce47e01967c..33d2fcb88f09361b939b82fde6d212d605e2ec61 100644 (file)
@@ -26,6 +26,8 @@
 
 #define DIR_SEPARATOR "/"
 
+struct SilcSFTPFilesystemOpsStruct silc_sftp_fs_memory;
+
 /* Memory filesystem entry */
 typedef struct MemFSEntryStruct {
   char *name;                       /* Name of the entry */
@@ -275,7 +277,7 @@ static bool mem_del_handle(MemFS fs, MemFSFileHandle handle)
   if (fs->handles[handle->handle] == handle) {
     fs->handles[handle->handle] = NULL;
     if (handle->fd != -1)
-      close(handle->fd);
+      silc_file_close(handle->fd);
     silc_free(handle);
     return TRUE;
   }
@@ -305,8 +307,9 @@ static MemFSFileHandle mem_find_handle(MemFS fs, uint32 handle)
    silc_sftp_fs_memory_free. The `perm' is the permissions for the root
    directory of the filesystem (/ dir). */
 
-void *silc_sftp_fs_memory_alloc(SilcSFTPFSMemoryPerm perm)
+SilcSFTPFilesystem silc_sftp_fs_memory_alloc(SilcSFTPFSMemoryPerm perm)
 {
+  SilcSFTPFilesystem filesystem;
   MemFS fs;
 
   fs = silc_calloc(1, sizeof(*fs));
@@ -316,17 +319,21 @@ void *silc_sftp_fs_memory_alloc(SilcSFTPFSMemoryPerm perm)
   fs->root->directory = TRUE;
   fs->root->name = strdup(DIR_SEPARATOR);
 
-  return (void *)fs;
+  filesystem = silc_calloc(1, sizeof(*filesystem));
+  filesystem->fs = &silc_sftp_fs_memory;
+  filesystem->fs_context = (void *)fs;
+
+  return filesystem;
 }
 
 /* Frees the memory filesystem context. */
 
-void silc_sftp_fs_memory_free(void *context)
+void silc_sftp_fs_memory_free(SilcSFTPFilesystem fs)
 {
-  MemFS fs = (MemFS)context;
+  MemFS memfs = (MemFS)fs->fs_context;
 
-  silc_free(fs->root);
-  silc_free(fs);
+  silc_free(memfs->root);
+  silc_free(memfs);
 }
 
 /* Adds a new directory to the memory filesystem. Returns the directory
@@ -338,20 +345,20 @@ void silc_sftp_fs_memory_free(void *context)
    not free the returned context. The `perm' will indicate the permissions
    for the directory and they work in POSIX style. */
 
-void *silc_sftp_fs_memory_add_dir(void *context, void *dir,
+void *silc_sftp_fs_memory_add_dir(SilcSFTPFilesystem fs, void *dir,
                                  SilcSFTPFSMemoryPerm perm,
                                  const char *name)
 {
-  MemFS fs = (MemFS)context;
+  MemFS memfs = (MemFS)fs->fs_context;
   MemFSEntry entry;
 
   entry = silc_calloc(1, sizeof(*entry));
   entry->perm = perm;
   entry->name = strdup(name);
   entry->directory = TRUE;
-  entry->parent = dir ? dir : fs->root;
+  entry->parent = dir ? dir : memfs->root;
 
-  if (!mem_add_entry(dir ? dir : fs->root, entry, FALSE))
+  if (!mem_add_entry(dir ? dir : memfs->root, entry, FALSE))
     return NULL;
 
   return entry;
@@ -364,21 +371,21 @@ void *silc_sftp_fs_memory_add_dir(void *context, void *dir,
    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(void *context, void *dir)
+bool silc_sftp_fs_memory_del_dir(SilcSFTPFilesystem fs, void *dir)
 {
-  MemFS fs = (MemFS)context;
+  MemFS memfs = (MemFS)fs->fs_context;
   bool ret;
 
   if (dir)
     return mem_del_entry(dir, FALSE);
 
   /* Remove from root */
-  ret = mem_del_entry(fs->root, FALSE);
+  ret = mem_del_entry(memfs->root, FALSE);
 
-  fs->root = silc_calloc(1, sizeof(*fs->root));
-  fs->root->perm = fs->root_perm;
-  fs->root->directory = TRUE;
-  fs->root->name = strdup(DIR_SEPARATOR);
+  memfs->root = silc_calloc(1, sizeof(*memfs->root));
+  memfs->root->perm = memfs->root_perm;
+  memfs->root->directory = TRUE;
+  memfs->root->name = strdup(DIR_SEPARATOR);
 
   return ret;
 }
@@ -391,13 +398,12 @@ bool silc_sftp_fs_memory_del_dir(void *context, void *dir)
    file and they work in POSIX style. Returns TRUE if the file was
    added to the directory. */
 
-bool silc_sftp_fs_memory_add_file(void *context,
-                                 void *dir,
+bool silc_sftp_fs_memory_add_file(SilcSFTPFilesystem fs, void *dir,
                                  SilcSFTPFSMemoryPerm perm,
                                  const char *filename,
                                  const char *realpath)
 {
-  MemFS fs = (MemFS)context;
+  MemFS memfs = (MemFS)fs->fs_context;
   MemFSEntry entry;
 
   entry = silc_calloc(1, sizeof(*entry));
@@ -406,21 +412,21 @@ bool silc_sftp_fs_memory_add_file(void *context,
   entry->data = strdup(realpath);
   entry->directory = FALSE;
 
-  return mem_add_entry(dir ? dir : fs->root, entry, 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(void *context, void *dir,
+bool silc_sftp_fs_memory_del_file(SilcSFTPFilesystem fs, void *dir,
                                  const char *filename)
 {
-  MemFS fs = (MemFS)context;
+  MemFS memfs = (MemFS)fs->fs_context;
 
   if (!filename)
     return FALSE;
 
-  return mem_del_entry_name(dir ? dir : fs->root, filename, 
+  return mem_del_entry_name(dir ? dir : memfs->root, filename, 
                            strlen(filename), FALSE);
 }
 
@@ -508,9 +514,9 @@ void mem_open(void *context, SilcSFTP sftp,
     flags |= O_APPEND;
 
   /* Attempt to open the file for real. */
-  fd = open(entry->data + 7, flags, 
-           (attrs->flags & SILC_SFTP_ATTR_PERMISSIONS ?
-            attrs->permissions : 0600));
+  fd = silc_file_open_mode(entry->data + 7, flags, 
+                          (attrs->flags & SILC_SFTP_ATTR_PERMISSIONS ?
+                           attrs->permissions : 0600));
   if (fd == -1) {
     (*callback)(sftp, silc_sftp_map_errno(errno), NULL, callback_context);
     return;
@@ -532,7 +538,7 @@ void mem_close(void *context, SilcSFTP sftp,
   int ret;
 
   if (h->fd != -1) {
-    ret = close(h->fd);
+    ret = silc_file_close(h->fd);
     if (ret == -1) {
       (*callback)(sftp, silc_sftp_map_errno(errno), NULL, NULL, 
                  callback_context);
@@ -562,7 +568,7 @@ void mem_read(void *context, SilcSFTP sftp,
   lseek(h->fd, (off_t)offset, SEEK_SET);
 
   /* Attempt to read */
-  ret = read(h->fd, data, len);
+  ret = silc_file_read(h->fd, data, len);
   if (ret <= 0) {
     if (!ret)
       (*callback)(sftp, SILC_SFTP_STATUS_EOF, NULL, 0, callback_context);
@@ -593,7 +599,7 @@ void mem_write(void *context, SilcSFTP sftp,
   lseek(h->fd, (off_t)offset, SEEK_SET);
 
   /* Attempt to write */
-  ret = write(h->fd, data, data_len);
+  ret = silc_file_write(h->fd, data, data_len);
   if (ret <= 0) {
     (*callback)(sftp, silc_sftp_map_errno(errno), NULL, NULL, 
                callback_context);
@@ -655,7 +661,7 @@ void mem_opendir(void *context, SilcSFTP sftp,
   MemFSFileHandle handle;
 
   if (!path || !strlen(path))
-    path = (const char *)strdup("/");
+    path = (const char *)DIR_SEPARATOR;
 
   /* Find such directory */
   entry = mem_find_entry_path(fs->root, path);
@@ -693,7 +699,7 @@ void mem_readdir(void *context, SilcSFTP sftp,
   SilcSFTPAttributes attrs;
   int i;
   char long_name[256];
-  unsigned long filesize = 0;
+  uint64 filesize = 0;
   char *date;
   struct stat stats;
 
@@ -724,14 +730,13 @@ void mem_readdir(void *context, SilcSFTP sftp,
       *strrchr(date, ':') = '\0';
 
     if (!entry->directory)
-      if (!lstat(entry->data + 7, &stats))
-       filesize = stats.st_size;
+      filesize = silc_file_size(entry->data + 7);
 
     /* Long name format is:
        drwx------   1   324210 Apr  8 08:40 mail/
        1234567890 123 12345678 123456789012 */
-    snprintf(long_name, sizeof(long_name),
-            "%c%c%c%c------ %3d %8lu %12s %s%s",
+    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' : '-'),
             ((entry->perm & SILC_SFTP_FS_PERM_WRITE) ? 'w' : '-'),
@@ -790,7 +795,7 @@ void mem_stat(void *context, SilcSFTP sftp,
   struct stat stats;
 
   if (!path || !strlen(path))
-    path = (const char *)strdup("/");
+    path = (const char *)DIR_SEPARATOR;
 
   /* Find such directory */
   entry = mem_find_entry_path(fs->root, path);
@@ -840,7 +845,7 @@ void mem_lstat(void *context, SilcSFTP sftp,
   struct stat stats;
 
   if (!path || !strlen(path))
-    path = (const char *)strdup("/");
+    path = (const char *)DIR_SEPARATOR;
 
   /* Find such directory */
   entry = mem_find_entry_path(fs->root, path);
@@ -855,7 +860,11 @@ void mem_lstat(void *context, SilcSFTP sftp,
   }    
 
   /* Get real stat */
+#ifndef SILC_WIN32
   ret = lstat(entry->data + 7, &stats);
+#else
+  ret = stat(entry->data + 7, &stats);
+#endif
   if (ret == -1) {
     (*callback)(sftp, silc_sftp_map_errno(errno), NULL, callback_context);
     return;
@@ -970,7 +979,7 @@ void mem_realpath(void *context, SilcSFTP sftp,
   SilcSFTPName name;
 
   if (!path || !strlen(path))
-    path = (const char *)strdup("/");
+    path = (const char *)DIR_SEPARATOR;
 
   realpath = mem_expand_path(fs->root, path);
   if (!realpath) {
@@ -1005,7 +1014,7 @@ void mem_extended(void *context, SilcSFTP sftp,
              callback_context);
 }
 
-struct SilcSFTPFilesystemStruct silc_sftp_fs_memory = {
+struct SilcSFTPFilesystemOpsStruct silc_sftp_fs_memory = {
   mem_get_handle,
   mem_encode_handle,
   mem_open,