private FileConfigurationManager GetFileConfigurationManager(string name, IDirectoryHandler directory)
        {
            string configurationFilename = name + ".json";
            if (!directory.IsFilePresent(configurationFilename))
                return null;

            ITextHandler configurationFileHandler = directory.OpenFile(configurationFilename).CastFileHandler<ITextHandler>();
            return new FileConfigurationManager(configurationFileHandler);
        }
        public override void Syncronize(IDirectoryHandler rootDirectoryHandler)
        {
            base.Syncronize(rootDirectoryHandler);

            IDirectoryHandler statsDirectory;

            if (!rootDirectoryHandler.IsFilePresent("Stats"))
            {
                statsDirectory = rootDirectoryHandler.CreateFile(
                    "Stats",
                    "directory",
                    FileHandlerFactoryLocator.UserFactory.RootUser.Id).FileContainer.CastFileHandler<IDirectoryHandler>();

                rootDirectoryHandler.SetPermission(
                    null,
                    "Stats",
                    new ID<IUserOrGroup, Guid>[] { FileHandlerFactoryLocator.UserFactory.AnonymousUser.Id },
                    FilePermissionEnum.Read,
                    false,
                    false);
            }
            else
                statsDirectory = rootDirectoryHandler.OpenFile("Stats").CastFileHandler<IDirectoryHandler>();

            if (!statsDirectory.IsFilePresent("CallHomeTracker"))
            {
                statsDirectory.CreateFile(
                    "CallHomeTracker",
                    "callhome",
                    FileHandlerFactoryLocator.UserFactory.RootUser.Id);

                statsDirectory.SetPermission(
                    null,
                    "CallHomeTracker",
                    new ID<IUserOrGroup, Guid>[] { FileHandlerFactoryLocator.UserFactory.AnonymousUser.Id },
                    FilePermissionEnum.Read,
                    false,
                    false);
            }
        }
        private void DoUpgrades(IDirectoryHandler rootDirectoryHandler)
        {
            IDirectoryHandler systemDirectory = rootDirectoryHandler.OpenFile("System").CastFileHandler<IDirectoryHandler>();

            if (!systemDirectory.IsFilePresent("BrowserInfo"))
            {
                systemDirectory.CreateFile(
                    "BrowserInfo",
                    "browserinfo",
                    FileHandlerFactoryLocator.UserFactory.RootUser.Id);

                systemDirectory.SetPermission(
                    null,
                    "BrowserInfo",
                    new ID<IUserOrGroup, Guid>[] { FileHandlerFactoryLocator.UserFactory.Everybody.Id },
                    FilePermissionEnum.Read,
                    true,
                    false);
            }

            if (!systemDirectory.IsFilePresent("Comet"))
            {
                IDirectoryHandler cometDirectory = (IDirectoryHandler)systemDirectory.CreateFile(
                    "Comet",
                    "directory",
                    FileHandlerFactoryLocator.UserFactory.RootUser.Id);

                systemDirectory.SetPermission(
                    null,
                    "Comet",
                    new ID<IUserOrGroup, Guid>[] { FileHandlerFactoryLocator.UserFactory.Everybody.Id },
                    FilePermissionEnum.Read,
                    true,
                    false);

                cometDirectory.CreateFile(
                    "Loopback",
                    "cometloopback",
                    FileHandlerFactoryLocator.UserFactory.RootUser.Id);

                cometDirectory.SetPermission(
                    null,
                    "Loopback",
                    new ID<IUserOrGroup, Guid>[] { FileHandlerFactoryLocator.UserFactory.Everybody.Id },
                    FilePermissionEnum.Read,
                    true,
                    false);

                cometDirectory.CreateFile(
                    "Echo",
                    "cometecho",
                    FileHandlerFactoryLocator.UserFactory.RootUser.Id);

                cometDirectory.SetPermission(
                    null,
                    "Echo",
                    new ID<IUserOrGroup, Guid>[] { FileHandlerFactoryLocator.UserFactory.Everybody.Id },
                    FilePermissionEnum.Read,
                    true,
                    false);

                cometDirectory.CreateFile(
                    "Multiplexer",
                    "cometmultiplex",
                    FileHandlerFactoryLocator.UserFactory.RootUser.Id);

                cometDirectory.SetPermission(
                    null,
                    "Multiplexer",
                    new ID<IUserOrGroup, Guid>[] { FileHandlerFactoryLocator.UserFactory.Everybody.Id },
                    FilePermissionEnum.Read,
                    true,
                    false);

                cometDirectory.CreateFile(
                    "LoopbackQuality",
                    "cometloopbackqueuingreliable",
                    FileHandlerFactoryLocator.UserFactory.RootUser.Id);

                cometDirectory.SetPermission(
                    null,
                    "LoopbackQuality",
                    new ID<IUserOrGroup, Guid>[] { FileHandlerFactoryLocator.UserFactory.Everybody.Id },
                    FilePermissionEnum.Read,
                    true,
                    false);
            }

            if (!systemDirectory.IsFilePresent("TemplateEngine"))
            {
                systemDirectory.CreateFile(
                    "TemplateEngine",
                    "templateengine",
                    FileHandlerFactoryLocator.UserFactory.RootUser.Id);

                systemDirectory.SetPermission(
                    null,
                    "TemplateEngine",
                    new ID<IUserOrGroup, Guid>[] { FileHandlerFactoryLocator.UserFactory.Everybody.Id },
                    FilePermissionEnum.Read,
                    true,
                    false);
            }

            if (!systemDirectory.IsFilePresent("JavascriptInterpreter"))
            {
                systemDirectory.CreateFile(
                    "JavascriptInterpreter",
                    "javascriptinterpreter",
                    FileHandlerFactoryLocator.UserFactory.RootUser.Id);

                systemDirectory.SetPermission(
                    null,
                    "JavascriptInterpreter",
                    new ID<IUserOrGroup, Guid>[] { FileHandlerFactoryLocator.UserFactory.Everybody.Id },
                    FilePermissionEnum.Read,
                    true,
                    false);
            }

            IDirectoryHandler usersDirectory = FileHandlerFactoryLocator.FileSystemResolver.ResolveFile("Users").CastFileHandler<IDirectoryHandler>();

            if (!systemDirectory.IsFilePresent("Documentation"))
            {
                systemDirectory.CreateFile(
                    "Documentation",
                    "documentation",
                    FileHandlerFactoryLocator.UserFactory.RootUser.Id);

                systemDirectory.SetPermission(
                    null,
                    "Documentation",
                    new ID<IUserOrGroup, Guid>[] { FileHandlerFactoryLocator.UserFactory.Everybody.Id },
                    FilePermissionEnum.Read,
                    true,
                    false);
            }

            if (!rootDirectoryHandler.IsFilePresent("Actions"))
            {
                // Create actions directory
                rootDirectoryHandler.RestoreFile(
                    "Actions",
                    "directory",
                    "." + Path.DirectorySeparatorChar + "DefaultFiles" + Path.DirectorySeparatorChar + "Actions",
                    FileHandlerFactoryLocator.UserFactory.RootUser.Id);

                rootDirectoryHandler.SetPermission(
                    null,
                    "Actions",
                    new ID<IUserOrGroup, Guid>[] { FileHandlerFactoryLocator.UserFactory.Everybody.Id },
                    FilePermissionEnum.Read,
                    true,
                    false);
            }

            // Let the root user see information about the anonymous user
            IFileContainer anonymousUserFileContainer = usersDirectory.OpenFile("anonymous.user");
            usersDirectory.Chown(null, anonymousUserFileContainer.FileId, FileHandlerFactoryLocator.UserFactory.RootUser.Id);
            usersDirectory.RemovePermission("anonymous.user", new ID<IUserOrGroup, Guid>[] { FileHandlerFactoryLocator.UserFactory.AnonymousUser.Id });

            if (!usersDirectory.IsFilePresent("ParticleAvatars"))
            {
                usersDirectory.CreateFile("ParticleAvatars", "directory", null);
                usersDirectory.SetPermission(
                    null,
                    "ParticleAvatars",
                    new ID<IUserOrGroup, Guid>[] { FileHandlerFactoryLocator.UserFactory.LocalUsers.Id },
                    FilePermissionEnum.Read,
                    true,
                    false);
            }

            // Make sure every user has a friends group
            // TODO: At some point this needs to go away
            foreach (ID<IUserOrGroup, Guid> userId in FileHandlerFactoryLocator.UserManagerHandler.GetAllLocalUserIds())
            {
                IUser user = FileHandlerFactoryLocator.UserManagerHandler.GetUser(userId);

                if (!user.BuiltIn)
                    if (FileHandlerFactoryLocator.LocalIdentityProvider == user.IdentityProvider)
                    {
                        bool hasFriendsGroup = false;

                        foreach (IGroupAndAlias group in FileHandlerFactoryLocator.UserManagerHandler.GetGroupsThatUserOwns(userId))
                        {
                            if (group.Alias == "friends")
                                hasFriendsGroup = true;
                        }

                        if (!hasFriendsGroup)
                            try
                            {
                                FileHandlerFactoryLocator.UserManagerHandler.CreateGroup("friends", user.DisplayName + "'s friends", user.Id, GroupType.Personal);
                            }
                            catch (Exception e)
                            {
                                log.Warn("Exception creating friends group for user " + user.Name, e);
                            }
                    }
            }

            /*if (!rootDirectoryHandler.IsFilePresent("Config"))
            {
                IDirectoryHandler configDirectory = (IDirectoryHandler)rootDirectoryHandler.CreateFile(
                    "Config", "directory", FileHandlerFactoryLocator.UserFactory.RootUser.Id);

                CreateConfigDirectory(configDirectory, "ByExtension");
                CreateConfigDirectory(configDirectory, "ByType");
            }*/
        }
        public virtual void Syncronize(IDirectoryHandler rootDirectoryHandler)
        {
            IFileHandler dir;

            if (rootDirectoryHandler.IsFilePresent("Actions"))
            {
                dir = rootDirectoryHandler.OpenFile("Actions").FileHandler;
                dir.SyncFromLocalDisk("." + Path.DirectorySeparatorChar + "DefaultFiles" + Path.DirectorySeparatorChar + "Actions", false, DateTime.MinValue);
            }

            dir = rootDirectoryHandler.OpenFile("Shell").FileHandler;
            dir.SyncFromLocalDisk("." + Path.DirectorySeparatorChar + "DefaultFiles" + Path.DirectorySeparatorChar + "Shell", false, DateTime.MinValue);

            dir = rootDirectoryHandler.OpenFile("API").FileHandler;
            dir.SyncFromLocalDisk("." + Path.DirectorySeparatorChar + "DefaultFiles" + Path.DirectorySeparatorChar + "API", false, DateTime.MinValue);

            dir = rootDirectoryHandler.OpenFile("Templates").FileHandler;
            dir.SyncFromLocalDisk("." + Path.DirectorySeparatorChar + "DefaultFiles" + Path.DirectorySeparatorChar + "Templates", false, DateTime.MinValue);

            dir = rootDirectoryHandler.OpenFile("Tests").FileHandler;
            dir.SyncFromLocalDisk("." + Path.DirectorySeparatorChar + "DefaultFiles" + Path.DirectorySeparatorChar + "Tests", false, DateTime.MinValue);

            dir = rootDirectoryHandler.OpenFile("Pages").FileHandler;
            dir.SyncFromLocalDisk("." + Path.DirectorySeparatorChar + "DefaultFiles" + Path.DirectorySeparatorChar + "Pages", false, DateTime.MinValue);

            dir = rootDirectoryHandler.OpenFile("Docs").FileHandler;
            dir.SyncFromLocalDisk("." + Path.DirectorySeparatorChar + "DefaultFiles" + Path.DirectorySeparatorChar + "Docs", false, DateTime.MinValue);

            dir = rootDirectoryHandler.OpenFile("Classes").FileHandler;
            dir.SyncFromLocalDisk("." + Path.DirectorySeparatorChar + "DefaultFiles" + Path.DirectorySeparatorChar + "Classes", false, DateTime.MinValue);

            if (!rootDirectoryHandler.IsFilePresent("Config"))
            {
                dir = (IDirectoryHandler)rootDirectoryHandler.CreateFile(
                    "Config", "directory", FileHandlerFactoryLocator.UserFactory.RootUser.Id);

                rootDirectoryHandler.SetPermission(
                    null,
                    "Config",
                    new ID<IUserOrGroup, Guid>[] { FileHandlerFactoryLocator.UserFactory.Everybody.Id },
                    ObjectCloud.Interfaces.Security.FilePermissionEnum.Read,
                    false,
                    false);

                rootDirectoryHandler.SetPermission(
                    null,
                    "Config",
                    new ID<IUserOrGroup, Guid>[] { FileHandlerFactoryLocator.UserFactory.Administrators.Id },
                    ObjectCloud.Interfaces.Security.FilePermissionEnum.Administer,
                    false,
                    false);
            }
            else
                dir = rootDirectoryHandler.OpenFile("Config").FileHandler;

            dir.SyncFromLocalDisk("." + Path.DirectorySeparatorChar + "DefaultFiles" + Path.DirectorySeparatorChar + "Config", false, DateTime.MinValue);

            if (!rootDirectoryHandler.IsFilePresent("DefaultTemplate"))
            {
                dir = rootDirectoryHandler.CreateFile(
                    "DefaultTemplate",
                    "directory",
                    null);

                rootDirectoryHandler.SetPermission(
                    null,
                    "DefaultTemplate",
                    new ID<IUserOrGroup, Guid>[] { FileHandlerFactoryLocator.UserFactory.Everybody.Id },
                    FilePermissionEnum.Read,
                    true,
                    false);
            }
            else
                dir = rootDirectoryHandler.OpenFile("DefaultTemplate").FileHandler;

            /*// Only sync if the DefaultTemplate is empty
            List<IFileContainer> defaultTemplateFiles = new List<IFileContainer>(
                dir.FileContainer.CastFileHandler<IDirectoryHandler>().Files);

            if (defaultTemplateFiles.Count == 0)
                // Because most installations will modify the DefaultTemplate folder, syncing only happens if the folder is
                // missing.  This will prevent minor system upgrades from overwriting custom look and feel.
                dir.SyncFromLocalDisk("." + Path.DirectorySeparatorChar + "DefaultFiles" + Path.DirectorySeparatorChar + "DefaultTemplate", false);*/

            dir.FileContainer.CastFileHandler<IDirectoryHandler>().SyncFromLocalDisk(
                "." + Path.DirectorySeparatorChar + "DefaultFiles" + Path.DirectorySeparatorChar + "DefaultTemplate",
                false,
                true);

            // Do not syncronize the index file; this is for the user to update.  It's just a web component anyway
            //IFileHandler indexFile = rootDirectoryHandler.OpenFile("index.oc").FileHandler;
            //indexFile.SyncFromLocalDisk("." + Path.DirectorySeparatorChar + "DefaultFiles" + Path.DirectorySeparatorChar + "index.oc");

            DoUpgrades(rootDirectoryHandler);
        }