summaryrefslogtreecommitdiff
path: root/common/filelist-vfs-intf.c
diff options
context:
space:
mode:
authorTomas Bzatek <tbzatek@users.sourceforge.net>2009-11-28 13:11:51 +0100
committerTomas Bzatek <tbzatek@users.sourceforge.net>2009-11-28 13:11:51 +0100
commit9382f127ccebdd59917c97c61d008ed0e127cd75 (patch)
tree0a48c8296199b343c76ef532eef014f908bc2e4d /common/filelist-vfs-intf.c
parent70eeaa4ec712895539ca6ecd60a42b93ec9b0904 (diff)
downloadtuxcmd-modules-0.6.72.tar.xz
Engine and VFS API cleanupv0.6.72
Also enable symlink resolving by default.
Diffstat (limited to 'common/filelist-vfs-intf.c')
-rw-r--r--common/filelist-vfs-intf.c263
1 files changed, 263 insertions, 0 deletions
diff --git a/common/filelist-vfs-intf.c b/common/filelist-vfs-intf.c
new file mode 100644
index 0000000..e806334
--- /dev/null
+++ b/common/filelist-vfs-intf.c
@@ -0,0 +1,263 @@
+/* Tux Commander VFS: convenient interface for VFS modules to a filelist tree
+ * Copyright (C) 2007-2009 Tomas Bzatek <tbzatek@users.sourceforge.net>
+ * Check for updates on tuxcmd.sourceforge.net
+ *
+ * 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
+ */
+
+#include <stdio.h>
+#include <string.h>
+#include <glib.h>
+
+#include "tuxcmd-vfs.h"
+#include "strutils.h"
+#include "vfsutils.h"
+#include "filelist.h"
+#include "filelist-vfs-intf.h"
+
+
+
+
+
+
+struct VfsFilelistData *
+vfs_filelist_new (struct PathTree *files)
+{
+ struct VfsFilelistData * data;
+
+ data = g_malloc0 (sizeof (struct VfsFilelistData));
+
+ log ("vfs_filelist_new()\n");
+ data->files = files;
+
+ return data;
+}
+
+void
+vfs_filelist_free (struct VfsFilelistData *data)
+{
+ if (! data) {
+ fprintf (stderr, "vfs_filelist_free: data == NULL !\n");
+ return;
+ }
+ g_free (data->list_dir_path);
+ g_free (data);
+}
+
+
+/* -------------------------------------------------------------------------------------- */
+
+guint64
+internal_get_dir_size (struct VfsFilelistData *data, struct PathTree *tree)
+{
+ guint64 Size;
+ struct PathTree *n;
+ unsigned long idx;
+
+ Size = 0;
+ if (! data->break_get_dir_size && tree) {
+ n = NULL;
+ idx = 0;
+ while ((n = filelist_tree_get_item_by_index (tree, idx))) {
+ if (data->break_get_dir_size)
+ break;
+ if (n->data) {
+ log ("internal_get_dir_size: found item '%s', size = %zd \n", n->node, n->data->iSize);
+ Size += (n->data->ItemType == vDirectory) ? internal_get_dir_size (data, n) : n->data->iSize;
+ }
+ idx++;
+ }
+ }
+ return Size;
+}
+
+guint64
+vfs_filelist_get_dir_size (struct VfsFilelistData *data, const char *APath)
+{
+ struct PathTree *node;
+
+ if (! data)
+ return 0;
+ data->break_get_dir_size = FALSE;
+
+ node = filelist_tree_find_node_by_path (data->files, APath);
+ if (node) {
+ return internal_get_dir_size (data, node);
+ } else {
+ printf ("(EE) VFSGetDirSize: path '%s' not found\n", APath);
+ return 0;
+ }
+}
+
+void
+vfs_filelist_get_dir_size_break (struct VfsFilelistData *data)
+{
+ if (data)
+ data->break_get_dir_size = TRUE;
+}
+
+
+/* -------------------------------------------------------------------------------------- */
+
+static void
+assign_file_info (struct PathTree* node, struct TVFSItem *Item, const char *reference_full_path, gboolean follow_symlinks, gboolean add_full_path)
+{
+ copy_vfs_item (node->data, Item);
+ if (add_full_path && reference_full_path) {
+ g_free (Item->FName);
+ g_free (Item->FDisplayName);
+ Item->FName = g_strdup (reference_full_path);
+ Item->FDisplayName = g_filename_display_name (reference_full_path);
+ }
+ if (follow_symlinks && node->symlink_target_data) {
+ Item->iSize = node->symlink_target_data->iSize;
+ Item->iPackedSize = node->symlink_target_data->iPackedSize;
+ Item->ItemType = node->symlink_target_data->ItemType;
+ }
+}
+
+TVFSResult
+vfs_filelist_file_info (struct VfsFilelistData *data, const char *AFileName, struct TVFSItem *Item, gboolean FollowSymlinks, gboolean AddFullPath)
+{
+ struct PathTree *node;
+
+ if (data && data->files) {
+ node = filelist_tree_find_node_by_path (data->files, AFileName);
+ if (node) {
+ if (node->data) {
+ assign_file_info (node, Item, AFileName, FollowSymlinks, AddFullPath);
+ printf ("(II) VFSFileInfo: found file: '%s'\n", Item->FName);
+ return cVFS_OK;
+ } else {
+ printf ("(EE) VFSFileInfo: node->data == NULL! \n");
+ return cVFS_Failed;
+ }
+ } else {
+ printf ("(EE) VFSFileInfo: file specified not found\n");
+ return cVFS_No_More_Files;
+ }
+ } else {
+ printf ("(EE) VFSFileInfo: Invalid pointers to data objects.\n");
+ return cVFS_Failed;
+ }
+}
+
+TVFSResult
+vfs_filelist_list_first (struct VfsFilelistData *data, const char *sDir, struct TVFSItem *Item, gboolean FollowSymlinks, gboolean AddFullPath)
+{
+ struct PathTree *node;
+ char *full_path;
+
+ data->list_dir_index = -1;
+ data->list_dir_node = NULL;
+
+ if (sDir == NULL) {
+ printf ("(EE) VFSListFirst: sDir is NULL!\n");
+ return cVFS_Failed;
+ }
+
+ data->list_dir_index = 0;
+ data->list_dir_node = filelist_tree_find_node_by_path (data->files, sDir);
+ data->follow_symlinks = FollowSymlinks;
+ data->add_full_path = AddFullPath;
+ data->list_dir_path = include_trailing_path_sep (sDir);
+
+ /* Find the directory in the filelist */
+ if (data->list_dir_node) {
+ node = filelist_tree_get_item_by_index (data->list_dir_node, data->list_dir_index);
+ if (node) {
+ full_path = NULL;
+ if (AddFullPath)
+ full_path = g_build_filename (sDir, node->data->FName, NULL);
+ assign_file_info (node, Item, full_path, FollowSymlinks, AddFullPath);
+ g_free (full_path);
+ printf ("(II) VFSListFirst: found file: %s\n", Item->FName);
+ return cVFS_OK;
+ } else {
+ printf ("(II) VFSListFirst: no more files\n");
+ return cVFS_No_More_Files;
+ }
+ } else {
+ printf ("(EE) VFSListFirst: Directory '%s' not found.\n", sDir);
+ return cVFS_Failed;
+ }
+}
+
+TVFSResult
+vfs_filelist_list_next (struct VfsFilelistData *data, struct TVFSItem *Item)
+{
+ struct PathTree *node;
+ char *full_path;
+
+ full_path = NULL;
+ if (! data->list_dir_node) {
+ printf ("(EE) VFSListNext: data->list_dir_node is NULL!\n");
+ return cVFS_Failed;
+ }
+ data->list_dir_index++;
+
+ node = filelist_tree_get_item_by_index (data->list_dir_node, data->list_dir_index);
+ if (node) {
+ if (data->add_full_path)
+ full_path = g_build_filename (data->list_dir_path, node->data->FName, NULL);
+ assign_file_info (node, Item, full_path, data->follow_symlinks, data->add_full_path);
+ g_free (full_path);
+ printf ("(II) VFSListNext: found file: %s\n", Item->FName);
+ return cVFS_OK;
+ } else {
+ printf ("(II) VFSListNext: no more files\n");
+ return cVFS_No_More_Files;
+ }
+}
+
+TVFSResult
+vfs_filelist_list_close (struct VfsFilelistData *data)
+{
+ data->list_dir_index = -1;
+ data->list_dir_node = NULL;
+ g_free (data->list_dir_path);
+ data->list_dir_path = NULL;
+ return cVFS_OK;
+}
+
+
+/* -------------------------------------------------------------------------------------- */
+
+char *
+vfs_filelist_change_dir (struct VfsFilelistData *data, const char *NewPath)
+{
+ char *ANewPath;
+
+ if (NewPath == NULL) {
+ printf("(EE) VFSChangeDir: NewPath is NULL!\n");
+ return NULL;
+ }
+
+ /* make up the target path */
+ printf ("(--) VFSChangeDir: Going to change dir from '%s'\n", NewPath);
+ ANewPath = exclude_trailing_path_sep (NewPath);
+ if (! ANewPath || strlen (ANewPath) <= 0)
+ ANewPath = g_strdup ("/");
+ printf ("(--) VFSChangeDir: Going to change dir to '%s'\n", ANewPath);
+
+ /* find the directory in the filelist */
+ if (filelist_tree_find_node_by_path (data->files, ANewPath)) {
+ return ANewPath;
+ } else {
+ printf ("(EE) VFSChangeDir: Directory '%s' not found.\n", ANewPath);
+ g_free (ANewPath);
+ return NULL;
+ }
+}