/// <inheritdoc />
        public string GetPluginResourcePath(ISMAPlugin plugin)
        {
            var path = SMA.Collection.GetSMAPluginsFolder(plugin);

            DirectoryEx.EnsureExists(path);

            return(path);
        }
        private async Task OnSMStarted(object sender, SMProcessArgs e)
        {
            LogTo.Debug($"Initializing {GetType().Name}");

            DirectoryEx.EnsureExists(SMAFileSystem.PluginPackageDir.FullPath);

            StartIpcServer();
            //StartMonitoringPlugins();

            await RefreshPlugins();
            await StartPlugins();
        }
Пример #3
0
        private static FileStream EnsurePluginConf(ISMAPlugin plugin, Type confType, FileAccess fileAccess)
        {
            string filePath = Path.Combine(SMAConst.Paths.ConfigPath, plugin.Id.ToString("D"));

            if (!DirectoryEx.EnsureExists(filePath))
            {
                return(null);
            }

            filePath = Path.Combine(filePath, confType.Name);

            return(File.Open(filePath, fileAccess == FileAccess.Read ? FileMode.OpenOrCreate : FileMode.Create, fileAccess));
        }
Пример #4
0
        public void Run()
        {
            try
            {
                DirectoryEx.EnsureExists(this.PackagesBackupPath);

                IList <PackageEdit> edits;
                using (var connection = this.PackageDatabase.ConnectTo())
                {
                    edits = connection.Query <PackageEdit>(GetEditsBaseSql).ToList();
                }

                Log.Info("Fetched {2} queued edits from {0}/{1}", PackageDatabase.DataSource, PackageDatabase.InitialCatalog, edits.Count);

                // Group by package and take just the most recent edit for each package
                edits = edits.GroupBy(e => e.PackageKey)
                        .Select(g => g.OrderByDescending(e => e.Timestamp).FirstOrDefault())
                        .Where(e => e != null)
                        .ToList();

                foreach (var edit in edits)
                {
                    Exception thrown = null;
                    try
                    {
                        this.ApplyEdit(edit);
                    }
                    catch (Exception ex)
                    {
                        thrown = ex;
                    }

                    if (thrown != null)
                    {
                        using (var connection = this.PackageDatabase.ConnectTo())
                        {
                            connection.Query <int>(@"
                            UPDATE  PackageEdits
                            SET
                                    TriedCount = TriedCount + 1,
                                    LastError = @error
                            WHERE   [Key] = @key", new { error = thrown.ToString(), key = edit.Key });
                        }
                    }
                }
            }
            finally
            {
                DirectoryEx.TryDelete(this.PackagesTempPath);
            }
        }
Пример #5
0
        public static (AppDomain domain, PluginHost runner) Create(SMCollection collection)
        {
            DirectoryEx.EnsureExists(SMAConst.Paths.AppDomainCachePath);
            DirectoryEx.EnsureExists(SMAConst.Paths.PluginPath);
            DirectoryEx.EnsureExists(collection.GetSMAFolder());
            DirectoryEx.EnsureExists(collection.GetSMAElementsFolder());
            DirectoryEx.EnsureExists(collection.GetSMAPluginsFolder());
            DirectoryEx.EnsureExists(collection.GetSMASystemFolder());

            var assemblyPaths = String.Join(";",
                                            GetAssemblyPaths());

            var setup = new AppDomainSetup()
            {
                ApplicationBase       = AppDomain.CurrentDomain.SetupInformation.ApplicationBase,
                CachePath             = SMAConst.Paths.AppDomainCachePath,
                PrivateBinPath        = assemblyPaths,
                ShadowCopyFiles       = "true",
                ShadowCopyDirectories = assemblyPaths
            };

            var permissions = GetPermissions(collection);

            var domain = AppDomain.CreateDomain(
                AppDomainName,
                AppDomain.CurrentDomain.Evidence,
                setup,
                permissions
                );

            var runner = (PluginHost)domain.CreateInstanceAndUnwrap(
                typeof(PluginHost).Assembly.FullName,
                // ReSharper disable once AssignNullToNotNullAttribute
                typeof(PluginHost).FullName
                );

            return(domain, runner);
        }
        public CollectionFile Create(
            [NotNull] ISMAPlugin requester,
            int elementId,
            [NotNull] Action <Stream> streamWriter,
            string extension,
            string crc32 = null)
        {
            if (elementId <= 0)
            {
                return(null);
            }

            CollectionFSFile dbFile = null;

            try
            {
                extension = extension?.TrimStart('.');

                dbFile = new CollectionFSFile
                {
                    ElementId = elementId,
                    Extension = extension ?? string.Empty,
                    PluginId  = requester.Id
                };

                dbFile.Id = DbFiles.Insert(dbFile).AsInt32;

                CollectionFile colFile = FromDbFile(dbFile);

                DirectoryEx.EnsureExists(Path.GetDirectoryName(colFile.Path));

                using (var stream = File.Open(colFile.Path,
                                              System.IO.FileMode.Create,
                                              FileAccess.ReadWrite))
                    streamWriter(stream);

                if (crc32 != null)
                {
                    var fsCrc32 = FileEx.GetCrc32(colFile.Path);

                    if (fsCrc32 != crc32)
                    {
                        throw new IOException($"CRC32 did not match for file {colFile.Path}. Expected {crc32}, got {fsCrc32}");
                    }
                }

                return(colFile);
            }
            catch (Exception ex)
            {
                LogTo.Error(ex,
                            "CollectionFS: Create threw an exception.");

                try
                {
                    if (dbFile != null)
                    {
                        DbFiles.Delete(dbFile.Id);
                    }
                }
                catch (Exception dbEx)
                {
                    LogTo.Error(dbEx,
                                "CollectionFS: Create threw an exception. Exception's DB cleanup code threw an exception");
                }

                throw;
            }
        }