예제 #1
0
 /// <summary>
 /// Removes the file share created by the installer
 /// </summary>
 private void CleanUpFileShare()
 {
     if (Directory.Exists(_fileSharePath))
     {
         if (SharedDrive.IsShared(_ticket.FileShareName))
         {
             SharedDrive drive = SharedDrive.GetNamedShare(_ticket.FileShareName);
             drive.Delete();
             Directory.Delete(_ticket.FileSharePath, true);
         }
     }
 }
예제 #2
0
        /// <summary>
        /// Gets all shares.
        /// </summary>
        /// <returns>IList&lt;SharedDrive&gt;.</returns>
        public static IList <SharedDrive> GetAllShares()
        {
            IList <SharedDrive>        result          = new List <SharedDrive>();
            ManagementClass            management      = new ManagementClass("Win32_Share");
            ManagementObjectCollection managementItems = management.GetInstances();

            foreach (ManagementObject item in managementItems)
            {
                SharedDrive share = new SharedDrive(item);
                result.Add(share);
            }

            return(result);
        }
예제 #3
0
        /// <summary>
        /// Creates a file share if one doesn't already exist.
        /// It may also perform some modifications in the file share if needed.
        /// </summary>
        private void CreateFileShare()
        {
            _fileSharePath = _ticket.FileSharePath;

            // Create a share for the system.  It will be used for test documents and other
            // shared resources used by the system.
            if (SharedDrive.IsShared(_ticket.FileShareName))
            {
                // If there is already a share in place, then find it's path
                _fileSharePath = WindowsManagementInstrumentation
                                 .GetFileShares(Environment.MachineName)
                                 .First(x => x.Key.Equals(_ticket.FileShareName))
                                 .Value;

                SystemTrace.Instance.Debug("Share location: {0}".FormatWith(_fileSharePath));
            }
            else
            {
                try
                {
                    // Create the file share path if it doesn't already exist
                    if (!Directory.Exists(_fileSharePath))
                    {
                        Directory.CreateDirectory(_fileSharePath);
                    }

                    SystemTrace.Instance.Debug("Share will be created on {0}".FormatWith(_fileSharePath));
                    SystemTrace.Instance.Debug("Share name: {0}".FormatWith(_ticket.FileShareName));
                    string      desc  = "Solution Test Bench System Share";
                    SharedDrive drive = new SharedDrive(_ticket.FileShareName);

                    // Create NT Level access control for the shared directory
                    DirectorySecurity    accessControl = Directory.GetAccessControl(_fileSharePath);
                    IdentityReference    everybody     = new SecurityIdentifier(WellKnownSidType.WorldSid, null);
                    FileSystemAccessRule accessRule    = new FileSystemAccessRule
                                                         (
                        everybody,
                        FileSystemRights.FullControl,
                        InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit,
                        PropagationFlags.None,
                        AccessControlType.Allow
                                                         );
                    accessControl.AddAccessRule(accessRule);
                    Directory.SetAccessControl(_fileSharePath, accessControl);

                    // Now create the share, and then add user rights for Everyone.
                    FileSystemRights[] rights = new FileSystemRights[] { FileSystemRights.FullControl };
                    var result = drive.Create(_fileSharePath, desc, "Everyone", rights);
                    if (result == SharedDrive.ShareResult.Success)
                    {
                        // If the share was successfully created, then you need to explicitly
                        // add user rights to the share as well.
                        drive.SetUserRights("Everyone", rights);
                    }
                    else
                    {
                        SendError($"Unable to create shared folder on {_fileSharePath} : {result}");
                    }
                }
                catch (Exception ex)
                {
                    SendError($"Unable to create shared folder on {_fileSharePath}: {ex.Message}", ex);
                }
            }

            // Read in all the shared folder paths from the resource file.  Create a subdirectory
            // for each of these locations in the shared folder space.
            StringReader reader       = new StringReader(new EmbeddedResource("HP.SolutionTest.FileShare").Read("FolderList.txt"));
            string       subdirectory = reader.ReadLine();

            while (subdirectory != null)
            {
                string path = Path.Combine(_fileSharePath, subdirectory);
                if (!Directory.Exists(path))
                {
                    SystemTrace.Instance.Debug("Created file share location: {0}".FormatWith(path));
                    Directory.CreateDirectory(path);
                }
                subdirectory = reader.ReadLine();
            }

            // In case of an STB upgrade, if the file share contains a folder named 'TestLibrary\Documents\JPEG',
            // move all its contents to the 'TestLibrary\Documents\Image' folder, and then delete the 'JPEG' folder.
            string jpegPath      = Path.Combine(_fileSharePath, @"TestLibrary\Documents\JPEG");
            string imageFilePath = Path.Combine(_fileSharePath, @"TestLibrary\Documents\Image");

            try
            {
                if (Directory.Exists(jpegPath) && Directory.Exists(imageFilePath))
                {
                    foreach (var file in new DirectoryInfo(jpegPath).GetFiles())
                    {
                        file.MoveTo($@"{imageFilePath}\{file.Name}");
                    }
                    SystemTrace.Instance.Debug($"Files in share location \'{jpegPath}\' moved to \'{imageFilePath}\'");
                    Directory.Delete(jpegPath);
                    SystemTrace.Instance.Debug("Deleted file share location: {0}".FormatWith(jpegPath));
                }
            }
            catch (Exception ex)
            {
                SendError($"Unable to rename shared folder on {jpegPath}: {ex.Message}", ex);
            }

            // For any initial test documents provided, save those to the appropriate shared folder location.
            EmbeddedResource resource = new EmbeddedResource("HP.SolutionTest.FileShare.Content");

            foreach (var name in resource.SubNames)
            {
                SystemTrace.Instance.Debug("Saving resource: {0}".FormatWith(name));

                // Convert the sub name into a file path (ie replace '.' with '\')
                string filePath = Path.Combine(_fileSharePath, Path.Combine(name.Split('.')));

                // Fix the last '.' before the file extension that was converted into a '\' above.
                int index = filePath.LastIndexOf('\\');
                filePath = string.Concat(filePath.Select((c, i) => i == index ? '.' : c));

                SystemTrace.Instance.Debug("FilePath: {0}".FormatWith(filePath));

                string directory = Path.GetDirectoryName(filePath);
                if (!Directory.Exists(directory))
                {
                    Directory.CreateDirectory(directory);
                    SystemTrace.Instance.Debug("Created file share folder: {0}".FormatWith(directory));
                }

                resource.Save(filePath, name);
            }
        }