Esempio n. 1
0
        /// <summary>
        /// Checks access to file
        /// </summary>
        /// <param name="filePath">Path to file for check</param>
        /// <param name="permissionAccess">Permission to check</param>
        private static void CheckFileAccess(string filePath, FileIOPermissionAccess permissionAccess)
        {
            if (!Path.IsPathRooted(filePath))
            {
                var currentExecutablePath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
                if (string.IsNullOrWhiteSpace(currentExecutablePath))
                {
                    throw new ConfigurationException("Failed to determine current executable path");
                }

                filePath = Path.Combine(currentExecutablePath, filePath);
            }

            var configDirectory = Path.GetDirectoryName(filePath);
            if (string.IsNullOrWhiteSpace(configDirectory))
            {
                throw new ConfigurationException("ClusterKit.Web.Nginx.PathToConfig has no defined directory");
            }

            if (!Directory.Exists(configDirectory))
            {
                throw new ConfigurationException($"{configDirectory} does not exists");
            }

            var path = File.Exists(filePath) ? filePath : configDirectory;
            var permission = new FileIOPermission(permissionAccess, path);
            if (!permission.IsGranted())
            {
                throw new ConfigurationException($"Cannot access {path} for writing");
            }
        }