public SMBFileSystem(ISMBClientFactory ismbClientfactory, ISMBCredentialProvider credentialProvider, uint maxBufferSize = 65536)
 {
     File          = new SMBFile(ismbClientfactory, credentialProvider, this, maxBufferSize);
     Directory     = new SMBDirectory(ismbClientfactory, credentialProvider, this, maxBufferSize);
     DirectoryInfo = new SMBDirectoryInfoFactory(this, credentialProvider, ismbClientfactory, maxBufferSize);
     FileInfo      = new SMBFileInfoFactory(this, credentialProvider, ismbClientfactory, maxBufferSize);
     FileStream    = new SMBFileStreamFactory(this);
     Path          = new SMBPath(this);
     DriveInfo     = new SMBDriveInfoFactory(this, credentialProvider, ismbClientfactory, maxBufferSize);
 }
示例#2
0
        public SMBFileSystem(ISMBClientFactory ismbClientfactory,
                             ISMBCredentialProvider credentialProvider,
                             uint maxBufferSize = 65536,
                             ISmbFileSystemSettings smbFileSystemSettings = null,
                             ILoggerFactory loggerFactory = null)
        {
            if (smbFileSystemSettings == null)
            {
                smbFileSystemSettings = new SmbFileSystemSettings();
            }

            File          = new SMBFile(ismbClientfactory, credentialProvider, this, maxBufferSize, smbFileSystemSettings, loggerFactory);
            Directory     = new SMBDirectory(ismbClientfactory, credentialProvider, this, maxBufferSize, smbFileSystemSettings, loggerFactory);
            DirectoryInfo = new SMBDirectoryInfoFactory(this, credentialProvider, ismbClientfactory, maxBufferSize, loggerFactory);
            FileInfo      = new SMBFileInfoFactory(this, credentialProvider, ismbClientfactory, maxBufferSize, loggerFactory);
            FileStream    = new SMBFileStreamFactory(this);
            Path          = new SMBPath(this);
            DriveInfo     = new SMBDriveInfoFactory(this, credentialProvider, ismbClientfactory, maxBufferSize, loggerFactory);
        }
        internal IDriveInfo[] GetDrives(ISMBCredential smbCredential)
        {
            var credentialsToCheck = new List <ISMBCredential>();

            credentialsToCheck = _smbCredentialProvider.GetSMBCredentials().ToList();

            List <IDriveInfo> driveInfos = new List <IDriveInfo>();

            if (smbCredential == null && credentialsToCheck.Count == 0)
            {
                _logger?.LogTrace($"No provided credentials and no credentials stored credentials in SMBCredentialProvider.");
                return(driveInfos.ToArray());
            }

            NTStatus status = NTStatus.STATUS_SUCCESS;

            var shareHostNames = new List <string>();

            if (smbCredential != null)
            {
                credentialsToCheck.Add(smbCredential);
            }
            else
            {
                credentialsToCheck = _smbCredentialProvider.GetSMBCredentials().ToList();
            }

            shareHostNames = credentialsToCheck.Select(smbCredential => smbCredential.Path.Hostname()).Distinct().ToList();

            var shareHostShareNames = new Dictionary <string, IEnumerable <string> >();

            foreach (var shareHost in shareHostNames)
            {
                var credential = credentialsToCheck.Where(smbCredential => smbCredential.Path.Hostname().Equals(shareHost)).First();
                try
                {
                    var path = credential.Path;
                    if (!path.TryResolveHostnameFromPath(out var ipAddress))
                    {
                        throw new SMBException($"Failed to connect to {path.Hostname()}", new ArgumentException($"Unable to resolve \"{path.Hostname()}\""));
                    }

                    using var connection = SMBConnection.CreateSMBConnection(_smbClientFactory, ipAddress, transport, credential, _maxBufferSize);

                    var shareNames = connection.SMBClient.ListShares(out status);
                    var shareDirectoryInfoFactory = new SMBDirectoryInfoFactory(_fileSystem, _smbCredentialProvider, _smbClientFactory, _maxBufferSize);

                    foreach (var shareName in shareNames)
                    {
                        var sharePath         = path.BuildSharePath(shareName);
                        var relativeSharePath = sharePath.RelativeSharePath();

                        _logger?.LogTrace($"Trying to get drive info for {shareName}");

                        try
                        {
                            ISMBFileStore fileStore = connection.SMBClient.TreeConnect(shareName, out status);

                            status.HandleStatus();

                            var smbFileSystemInformation = new SMBFileSystemInformation(fileStore, sharePath, status);

                            var smbDriveInfo = new SMBDriveInfo(sharePath, _fileSystem, smbFileSystemInformation, credential);

                            driveInfos.Add(smbDriveInfo);
                        }
                        catch (IOException ioEx)
                        {
                            _logger?.LogTrace(ioEx, $"Failed to get drive info for {shareName}");
                            throw new SMBException($"Failed to get drive info for {shareName}", new AggregateException($"Unable to connect to {shareName}", ioEx));
                        }
                        catch (Exception ex)
                        {
                            _logger?.LogTrace(ex, $"Failed to get drive info for {shareName}");
                            continue;
                        }
                    }
                }
                catch (Exception ex)
                {
                    _logger?.LogTrace(ex, $"Failed to GetDrives for {shareHost}.");
                    continue;
                }
            }

            return(driveInfos.ToArray());
        }