コード例 #1
0
ファイル: ShareBase.cs プロジェクト: ume05rw/EzSmb
        /// <summary>
        /// Get Node that PathSet specified.
        /// </summary>
        /// <param name="pathSet"></param>
        /// <param name="paramSet"></param>
        /// <returns></returns>
        public Node GetNode(PathSet pathSet, FixedParamSet paramSet)
        {
            if (!this.ValidatePathSet(pathSet))
            {
                return(null);
            }

            if (pathSet.Elements.Length <= 0)
            {
                // Requested Share Folder.
                return(NodeFactory.Get(
                           pathSet.FullPath,
                           NodeType.Folder,
                           paramSet
                           ));
            }
            else
            {
                // Requested Sub Node on Share.
                var result
                    = this.GetFolderNode(pathSet, paramSet)
                      ?? this.GetFileNode(pathSet, paramSet);

                if (result == null)
                {
                    this.AddError("GetNode", $"Path Not Found: {pathSet.FullPath}");

                    return(null);
                }

                return(result);
            }
        }
コード例 #2
0
        public Connection(PathSet pathSet, ParamSet paramSet) : base()
        {
            this.IsConnected = false;

            if (pathSet == null)
            {
                this.AddError("Constructor", "Required pathSet.");

                return;
            }

            this._pathSet = pathSet;
            var argParamSet = (paramSet == null)
                ? new ParamSet()
                : paramSet.Clone();

            if (argParamSet.SmbType == null)
            {
                this._client
                    = this.GetConnection(SmbType.Smb2)
                      ?? this.GetConnection(SmbType.Smb1);
            }
            else if (argParamSet.SmbType == SmbType.Smb2)
            {
                this._client = this.GetConnection(SmbType.Smb2);
            }
            else if (argParamSet.SmbType == SmbType.Smb1)
            {
                this._client = this.GetConnection(SmbType.Smb1);
            }
            else
            {
                this.AddError("Constructor", $"Unexpected ParamSet.SmbType: {argParamSet.SmbType}");

                return;
            }

            if (this._client == null)
            {
                this.AddError("Constructor", "Connection Failed.");

                return;
            }

            if (!this.Login(argParamSet))
            {
                this.AddError("Constructor", "Authentication Failed.");

                return;
            }

            this._paramSet = FixedParamSet.Parse(
                argParamSet,
                (this._client is SMB2Client)
                    ? SmbType.Smb2
                    : SmbType.Smb1
                );

            this.IsConnected = true;
        }
コード例 #3
0
        private bool Login(FixedParamSet paramSet)
        {
            var status = this._client.Login(
                paramSet.DomainName ?? string.Empty,
                paramSet.UserName ?? string.Empty,
                paramSet.Password ?? string.Empty
                );

            return(status == NTStatus.STATUS_SUCCESS);
        }
コード例 #4
0
        /// <summary>
        /// Get Node from only Path.
        /// </summary>
        /// <param name="fullPath"></param>
        /// <param name="nodeType"></param>
        /// <param name="paramSet"></param>
        /// <returns></returns>
        public static Node Get(
            string fullPath,
            NodeType nodeType,
            FixedParamSet paramSet
            )
        {
            var pathSet = PathSet.Parse(fullPath);

            return(NodeFactory.InnerGet(nodeType, pathSet, paramSet));
        }
コード例 #5
0
        public Connection(PathSet pathSet, FixedParamSet paramSet) : base()
        {
            this.IsConnected = false;

            if (pathSet == null)
            {
                this.AddError("Constructor", "Required pathSet.");

                return;
            }

            if (paramSet == null)
            {
                this.AddError("Constructor", "Required paramSet.");

                return;
            }

            this._pathSet = pathSet;

            if (paramSet.SmbType == SmbType.Smb2)
            {
                this._client = this.GetConnection(SmbType.Smb2);
            }
            else if (paramSet.SmbType == SmbType.Smb1)
            {
                this._client = this.GetConnection(SmbType.Smb1);
            }
            else
            {
                this.AddError("Constructor", $"Unexpected ParamSet.SmbType: {paramSet.SmbType}");

                return;
            }

            if (this._client == null)
            {
                this.AddError("Constructor", "Connection Failed.");

                return;
            }

            if (!this.Login(paramSet))
            {
                this.AddError("Constructor", "Authentication Failed.");

                return;
            }

            this._paramSet = paramSet;

            this.IsConnected = true;
        }
コード例 #6
0
        public Connection(Node node) : base()
        {
            this.IsConnected = false;

            if (node == null)
            {
                this.AddError("Constructor", "Required node.");

                return;
            }

            if (node.ParamSet == null || node.PathSet == null)
            {
                this.AddError("Constructor", "Invalid Node.");

                return;
            }

            this._pathSet = node.PathSet;

            if (node.ParamSet.SmbType == SmbType.Smb2)
            {
                this._client = this.GetConnection(SmbType.Smb2);
            }
            else if (node.ParamSet.SmbType == SmbType.Smb1)
            {
                this._client = this.GetConnection(SmbType.Smb1);
            }
            else
            {
                this.AddError("Constructor", $"Unexpected ParamSet.SmbType: {node.ParamSet.SmbType}");

                return;
            }

            if (this._client == null)
            {
                this.AddError("Constructor", "Connection Failed.");

                return;
            }

            if (!this.Login(node.ParamSet))
            {
                this.AddError("Constructor", "Authentication Failed.");

                return;
            }

            this._paramSet = node.ParamSet;

            this.IsConnected = true;
        }
コード例 #7
0
ファイル: ShareBase.cs プロジェクト: ume05rw/EzSmb
        private Node GetFileNode(PathSet pathSet, FixedParamSet paramSet)
        {
            var path = this.FormatPath(pathSet.ElementsPath);

            using (var hdr = this.GetHandler(path, HandleType.Read, NodeType.File))
            {
                if (!hdr.Succeeded)
                {
                    // DO NOT AddError on failed. Do only GetNode method.
                    return(null);
                }

                return(this.CreateNode(hdr, NodeType.File, pathSet, paramSet));
            }
        }
コード例 #8
0
        /// <summary>
        /// Get Node from Path and SMB-FileInfomation
        /// </summary>
        /// <param name="fullPath"></param>
        /// <param name="paramSet"></param>
        /// <param name="basicInfo"></param>
        /// <param name="stdInfo"></param>
        /// <returns></returns>
        public static Node Get(
            string fullPath,
            FixedParamSet paramSet,
            FileBasicInformation basicInfo,
            FileStandardInformation stdInfo = null
            )
        {
            if (paramSet == null)
            {
                throw new ArgumentException("Required paramSet.");
            }
            if (basicInfo == null)
            {
                throw new ArgumentException("Required info.");
            }

            var pathSet = PathSet.Parse(fullPath);

            if (basicInfo.FileAttributes.HasFlag(SMBLibrary.FileAttributes.Directory))
            {
                // Folder
                var result = NodeFactory.InnerGet(
                    NodeType.Folder,
                    pathSet,
                    paramSet
                    );
                result.Created      = basicInfo.CreationTime;
                result.Updated      = basicInfo.LastWriteTime;
                result.LastAccessed = basicInfo.LastAccessTime;

                return(result);
            }
            else
            {
                // File
                var result = NodeFactory.InnerGet(
                    NodeType.File,
                    pathSet,
                    paramSet
                    );
                result.Size         = stdInfo?.EndOfFile;
                result.Created      = basicInfo.CreationTime;
                result.Updated      = basicInfo.LastWriteTime;
                result.LastAccessed = basicInfo.LastAccessTime;

                return(result);
            }
        }
コード例 #9
0
        protected override void Dispose(bool disposing)
        {
            if (!this.disposedValue)
            {
                if (disposing)
                {
                    try
                    {
                        this._client?.Disconnect();
                    }
                    catch (Exception)
                    {
                    }

                    this._pathSet  = null;
                    this._paramSet = null;
                    this._client   = null;
                }

                this.disposedValue = true;
            }

            base.Dispose(disposing);
        }
コード例 #10
0
        private static Node InnerGet(
            NodeType nodeType,
            PathSet pathSet,
            FixedParamSet paramSet
            )
        {
            if (pathSet == null)
            {
                throw new ArgumentException("Required pathSet.");
            }
            if (paramSet == null)
            {
                throw new ArgumentException("Required paramSet.");
            }

            var result = new Node();

            switch (nodeType)
            {
            case NodeType.File:
            {
                if (pathSet.Elements.Length <= 0)
                {
                    throw new ArgumentException($"Invalid File Path. : {pathSet.FullPath}");
                }

                result.Type = NodeType.File;
                result.Name = pathSet.Elements.Last();

                break;
            }

            case NodeType.Folder:
            {
                if (string.IsNullOrEmpty(pathSet.Share))
                {
                    throw new ArgumentException($"Invalid Folder Path. : {pathSet.FullPath}");
                }

                result.Type = NodeType.Folder;
                result.Name = (0 < pathSet.Elements.Length)
                            ? pathSet.Elements.Last()
                            : pathSet.Share;

                break;
            }

            case NodeType.Server:
            {
                if (!string.IsNullOrEmpty(pathSet.Share))
                {
                    throw new ArgumentException($"Invalid Server Path. : {pathSet.FullPath}");
                }

                result.Type = NodeType.Server;
                result.Name = pathSet.IpAddressString;

                break;
            }

            default:
                throw new Exception($"Unexpected NodeType: {nodeType}");
            }


            result.PathSet  = pathSet;
            result.ParamSet = paramSet;

            return(result);
        }
コード例 #11
0
ファイル: ShareBase.cs プロジェクト: ume05rw/EzSmb
        /// <summary>
        /// Query Infos, Create Node Instance.
        /// </summary>
        /// <param name="handler"></param>
        /// <param name="nodeType"></param>
        /// <param name="pathSet"></param>
        /// <param name="paramSet"></param>
        /// <returns></returns>
        protected Node CreateNode(
            IHandler handler,
            NodeType nodeType,
            PathSet pathSet,
            FixedParamSet paramSet
            )
        {
            if (handler == null)
            {
                this.AddError("CreateNode", "Required handler.");

                return(null);
            }

            if (!handler.Succeeded)
            {
                this.AddError("CreateNode", "Invalid Handle.");

                return(null);
            }

            if (nodeType != NodeType.File && nodeType != NodeType.Folder)
            {
                this.AddError("CreateNode", $"Invalid Operation: {nodeType}");

                return(null);
            }

            var status = this.Store.GetFileInformation(
                out var basicInfo,
                handler.Handle,
                FileInformationClass.FileBasicInformation
                );

            if (status != NTStatus.STATUS_SUCCESS)
            {
                this.AddError("CreateNode", $"Basic Infomation Query Failed: {pathSet.FullPath}");

                return(null);
            }

            FileInformation standardInfo = null;

            if (nodeType == NodeType.File)
            {
                status = this.Store.GetFileInformation(
                    out standardInfo,
                    handler.Handle,
                    FileInformationClass.FileStandardInformation
                    );

                if (status != NTStatus.STATUS_SUCCESS)
                {
                    this.AddError("CreateNode", $"StandardInfomation Query Failed: {pathSet.FullPath}");

                    return(null);
                }
            }

            return(NodeFactory.Get(
                       pathSet.FullPath,
                       paramSet,
                       (FileBasicInformation)basicInfo,
                       (FileStandardInformation)standardInfo
                       ));
        }