123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634
/*******************************************************************************

        copyright:      Copyright (c) 2007 Kris Bell. All rights reserved

        license:        BSD style: $(LICENSE)

        version:        Oct 2007: Initial version

        author:         Kris

*******************************************************************************/

module tango.io.vfs.VirtualFolder;

private import tango.core.Exception;

private import tango.io.model.IFile;

private import tango.io.vfs.model.Vfs;

private import tango.io.Path : patternMatch;

private import tango.text.Util : head, locatePrior;

/*******************************************************************************

        Virtual folders play host to other folder types, including both
        concrete folder instances and subordinate virtual folders. You
        can build a (singly rooted) tree from a set of virtual and non-
        virtual folders, and treat them as though they were a combined
        or single entity. For example, listing the contents of such a
        tree is no different than listing the contents of a non-virtual
        tree - there's just potentially more nodes to traverse.

*******************************************************************************/

class VirtualFolder : VfsHost
{
        private const(char)[]                  name_;
        private VfsFile[const(char)[]]         files;
        private VfsFolder[const(char)[]]       mounts;
        private VfsFolderEntry[const(char)[]]  folders;
        private VirtualFolder           parent;

        /***********************************************************************

                All folder must have a name. No '.' or '/' chars are
                permitted

        ***********************************************************************/

        this (const(char)[] name)
        {
                validate (this.name_ = name);
        }

        /***********************************************************************

                Return the (short) name of this folder

        ***********************************************************************/

        @property final const(char)[] name()
        {
                return name_;
        }

        /***********************************************************************

                Return the (long) name of this folder. Virtual folders
                do not have long names, since they don't relate directly
                to a concrete folder instance

        ***********************************************************************/

        override final string toString()
        {
                return name.idup;
        }

        /***********************************************************************

                Add a child folder. The child cannot 'overlap' with others
                in the tree of the same type. Circular references across a
                tree of virtual folders are detected and trapped.

                The second argument represents an optional name that the
                mount should be known as, instead of the name exposed by
                the provided folder (it is not an alias).

        ***********************************************************************/

        VfsHost mount (VfsFolder folder, const(char)[] name = null)
        {
                assert (folder);
                if (name.length is 0)
                    name = folder.name;

                // link virtual children to us
                auto child = cast(VirtualFolder) folder;
                if (child)
                {
                    if (child.parent)
                        error ("folder '"~name.idup~"' belongs to another host");
                    else
                       child.parent = this;
                }
                // reach up to the root, and initiate tree sweep
                auto root = this;
                while (root.parent)
                       if (root is this)
                           error ("circular reference detected at '"~this.name.idup~"' while mounting '"~name.idup~"'");
                       else
                          root = root.parent;
                root.verify (folder, true);

                // all clear, so add the new folder
                mounts [name] = folder;
                return this;
        }

        /***********************************************************************

                Add a set of child folders. The children cannot 'overlap'
                with others in the tree of the same type. Circular references
                are detected and trapped.

        ***********************************************************************/

        VfsHost mount (VfsFolders group)
        {
                foreach (folder; group)
                         mount (folder);
                return this;
        }

        /***********************************************************************

                Unhook a child folder

        ***********************************************************************/

        VfsHost dismount (VfsFolder folder)
        {
                const(char)[] name = null;

                // check this is a child, and locate the mapped name
                foreach (key, value; mounts)
                         if (folder is value)
                             name = key;
                assert (name.ptr);

                // reach up to the root, and initiate tree sweep
                auto root = this;
                while (root.parent)
                       root = root.parent;
                root.verify (folder, false);

                // all clear, so remove it
                mounts.remove (name);
                return this;
        }

        /***********************************************************************

                Add a symbolic link to another file. These are referenced
                by file() alone, and do not show up in tree traversals

        ***********************************************************************/

        final VfsHost map (VfsFile file, const(char)[] name)
        {
                assert (name);
                files[name] = file;
                return this;
        }

        /***********************************************************************

                Add a symbolic link to another folder. These are referenced
                by folder() alone, and do not show up in tree traversals

        ***********************************************************************/

        final VfsHost map (VfsFolderEntry folder, const(char)[] name)
        {
                assert (name);
                folders[name] = folder;
                return this;
        }

        /***********************************************************************

                Iterate over the set of immediate child folders. This is
                useful for reflecting the hierarchy

        ***********************************************************************/

        final int opApply( scope int delegate(ref VfsFolder) dg)
        {
                int result;

                foreach (folder; mounts)
                        {
                        VfsFolder x = folder;
                        if ((result = dg(x)) != 0)
                             break;
                        }
                return result;
        }

        /***********************************************************************

                Return a folder representation of the given path. If the
                path-head does not refer to an immediate child, and does
                not match a symbolic link, it is considered unknown.

        ***********************************************************************/

        final VfsFolderEntry folder (const(char)[] path)
        {
                const(char)[] tail;
                auto text = head (path, cast(const(char)[])FileConst.PathSeparatorString, tail);

                auto child = text in mounts;
                if (child)
                    return child.folder (tail);

                auto sym = text in folders;
                if (sym is null)
                    error ("'"~text.idup~"' is not a recognized member of '"~name.idup~"'");
                return *sym;
        }

        /***********************************************************************

                Return a file representation of the given path. If the
                path-head does not refer to an immediate child folder,
                and does not match a symbolic link, it is considered unknown.

        ***********************************************************************/

        @property VfsFile file (const(char)[] path)
        {
                auto tail = locatePrior (path, cast(const(char))FileConst.PathSeparatorChar);
                if (tail < path.length)
                    return folder(path[0..tail]).open().file(path[tail..$]);

                auto sym = path in files;
                if (sym is null)
                    error ("'"~path.idup~"' is not a recognized member of '"~name.idup~"'");
                return *sym;
        }

        /***********************************************************************

                Clear the entire subtree. Use with caution

        ***********************************************************************/

        final VfsFolder clear ()
        {
                foreach (name, child; mounts)
                         child.clear();
                return this;
        }

        /***********************************************************************

                Returns true if all of the children are writable

        ***********************************************************************/

        @property final bool writable ()
        {
                foreach (name, child; mounts)
                         if (! child.writable)
                               return false;
                return true;
        }

        /***********************************************************************

                Returns a folder set containing only this one. Statistics
                are inclusive of entries within this folder only, which
                should be zero since symbolic links are not included

        ***********************************************************************/

        @property final VfsFolders self ()
        {
                return new VirtualFolders (this, false);
        }

        /***********************************************************************

                Returns a subtree of folders. Statistics are inclusive of
                all files and folders throughout the sub-tree

        ***********************************************************************/

        @property final VfsFolders tree ()
        {
                return new VirtualFolders (this, true);
        }

        /***********************************************************************

                Sweep the subtree of mountpoints, testing a new folder
                against all others. This propogates a folder test down
                throughout the tree, where each folder implementation
                should take appropriate action

        ***********************************************************************/

        final void verify (VfsFolder folder, bool mounting)
        {
                foreach (name, child; mounts)
                         child.verify (folder, mounting);
        }

        /***********************************************************************

                Close and/or synchronize changes made to this folder. Each
                driver should take advantage of this as appropriate, perhaps
                combining multiple files together, or possibly copying to a
                remote location

        ***********************************************************************/

        VfsFolder close (bool commit = true)
        {
                foreach (name, child; mounts)
                         child.close (commit);
                return this;
        }

        /***********************************************************************

                Throw an exception

        ***********************************************************************/

        package final string error (string msg)
        {
                throw new VfsException (msg);
        }

        /***********************************************************************

                Validate path names

        ***********************************************************************/

        private final void validate (const(char)[] name)
        {
                assert (name);
                if (locatePrior(name, '.') != name.length ||
                    locatePrior(name, FileConst.PathSeparatorChar) != name.length)
                    error ("'"~name.idup~"' contains invalid characters");
        }
}


/*******************************************************************************

        A set of virtual folders. For a sub-tree, we compose the results
        of all our subordinates and delegate subsequent request to that
        group.

*******************************************************************************/

private class VirtualFolders : VfsFolders
{
        private VfsFolders[] members;           // folders in group

        /***********************************************************************

                Create a subset group

        ***********************************************************************/

        private this () {}

        /***********************************************************************

                Create a folder group including the provided folder and
                (optionally) all child folders

        ***********************************************************************/

        private this (VirtualFolder root, bool recurse)
        {
                if (recurse)
                    foreach (name, folder; root.mounts)
                             members ~= folder.tree;
        }

        /***********************************************************************

                Iterate over the set of contained VfsFolder instances

        ***********************************************************************/

        final int opApply( scope int delegate(ref VfsFolder) dg)
        {
                int ret;

                foreach (group; members)
                         foreach (folder; group)
                                 {
                                 auto x = cast(VfsFolder) folder;
                                 if ((ret = dg(x)) != 0)
                                      break;
                                 }
                return ret;
        }

        /***********************************************************************

                Return the number of files in this group

        ***********************************************************************/

        @property final size_t files ()
        {
                uint files;
                foreach (group; members)
                         files += group.files;
                return files;
        }

        /***********************************************************************

                Return the total size of all files in this group

        ***********************************************************************/

        @property final ulong bytes ()
        {
                ulong bytes;
                foreach (group; members)
                         bytes += group.bytes;
                return bytes;
        }

        /***********************************************************************

                Return the number of folders in this group

        ***********************************************************************/

        @property final size_t folders ()
        {
                uint count;
                foreach (group; members)
                         count += group.folders;
                return count;
        }

        /***********************************************************************

                Return the total number of entries in this group

        ***********************************************************************/

        @property final size_t entries ()
        {
                uint count;
                foreach (group; members)
                         count += group.entries;
                return count;
        }

        /***********************************************************************

                Return a subset of folders matching the given pattern

        ***********************************************************************/

        final VfsFolders subset (const(char)[] pattern)
        {
                auto set = new VirtualFolders;

                foreach (group; members)
                         set.members ~= group.subset (pattern);
                return set;
        }

        /***********************************************************************

                Return a set of files matching the given pattern

        ***********************************************************************/

        @property final VfsFiles catalog (const(char)[] pattern)
        {
                return catalog ((VfsInfo info){return patternMatch (info.name, pattern);});
        }

        /***********************************************************************

                Returns a set of files conforming to the given filter

        ***********************************************************************/

        @property final VfsFiles catalog (VfsFilter filter = null)
        {
                return new VirtualFiles (this, filter);
        }
}


/*******************************************************************************

        A set of virtual files, represented by composing the results of
        the given set of folders. Subsequent calls are delegated to the
        results from those folders

*******************************************************************************/

private class VirtualFiles : VfsFiles
{
        private VfsFiles[] members;

        /***********************************************************************

        ***********************************************************************/

        private this (VirtualFolders host, VfsFilter filter)
        {
                foreach (group; host.members)
                         members ~= group.catalog (filter);
        }

        /***********************************************************************

                Iterate over the set of contained VfsFile instances

        ***********************************************************************/

        final int opApply( scope int delegate(ref VfsFile) dg)
        {
                int ret;

                foreach (group; members)
                         foreach (file; group)
                                  if ((ret = dg(file)) != 0)
                                       break;
                return ret;
        }

        /***********************************************************************

                Return the total number of entries

        ***********************************************************************/

        @property final size_t files ()
        {
                uint count;
                foreach (group; members)
                         count += group.files;
                return count;
        }

        /***********************************************************************

                Return the total size of all files

        ***********************************************************************/

        @property final ulong bytes ()
        {
                ulong count;
                foreach (group; members)
                         count += group.bytes;
                return count;
        }
}


debug (VirtualFolder)
{
/*******************************************************************************

*******************************************************************************/

import tango.io.Stdout;
import tango.io.vfs.FileFolder;

void main()
{
        auto root = new VirtualFolder ("root");
        auto sub  = new VirtualFolder ("sub");
        sub.mount (new FileFolder (r"d:/d/import/tango"));

        root.mount (sub)
            .mount (new FileFolder (r"c:/"), "windows")
            .mount (new FileFolder (r"d:/d/import/temp"));

        auto folder = root.folder (r"temp/bar");
        Stdout.formatln ("folder = {}", folder);

        root.map (root.folder(r"temp/subtree"), "fsym")
            .map (root.file(r"temp/subtree/test.txt"), "wumpus");
        auto file = root.file (r"wumpus");
        Stdout.formatln ("file = {}", file);
        Stdout.formatln ("fsym = {}", root.folder(r"fsym").open.file("test.txt"));

        foreach (folder; root.folder(r"temp/subtree").open)
                 Stdout.formatln ("folder.child '{}'", folder.name);

        auto set = root.self;
        Stdout.formatln ("self.files = {}", set.files);
        Stdout.formatln ("self.bytes = {}", set.bytes);
        Stdout.formatln ("self.folders = {}", set.folders);

        set = root.folder("temp").open.tree;
        Stdout.formatln ("tree.files = {}", set.files);
        Stdout.formatln ("tree.bytes = {}", set.bytes);
        Stdout.formatln ("tree.folders = {}", set.folders);

        foreach (folder; set)
                 Stdout.formatln ("tree.folder '{}' has {} files", folder.name, folder.self.files);

        auto cat = set.catalog ("*.txt");
        Stdout.formatln ("cat.files = {}", cat.files);
        Stdout.formatln ("cat.bytes = {}", cat.bytes);
        foreach (file; cat)
                 Stdout.formatln ("cat.name '{}' '{}'", file.name, file.toString);
}
}