Example #1
0
 /// <exception cref="SharpCifs.Smb.SmbException"></exception>
 /// <exception cref="UnknownHostException"></exception>
 /// <exception cref="System.UriFormatException"></exception>
 internal virtual void DoNetServerEnum(List<object> list, bool files, string wildcard
     , int searchAttributes, ISmbFilenameFilter fnf, ISmbFileFilter ff)
 {
     int listType = Url.GetHost().Length == 0 ? 0 : GetType();
     SmbComTransaction req;
     SmbComTransactionResponse resp;
     if (listType == 0)
     {
         Connect0();
         req = new NetServerEnum2(Tree.Session.transport.Server.OemDomainName, NetServerEnum2
             .SvTypeDomainEnum);
         resp = new NetServerEnum2Response();
     }
     else
     {
         if (listType == TypeWorkgroup)
         {
             req = new NetServerEnum2(Url.GetHost(), NetServerEnum2.SvTypeAll);
             resp = new NetServerEnum2Response();
         }
         else
         {
             throw new SmbException("The requested list operations is invalid: " + Url);
         }
     }
     bool more;
     do
     {
         int n;
         Send(req, resp);
         if (resp.Status != WinError.ErrorSuccess && resp.Status != WinError.ErrorMoreData)
         {
             throw new SmbException(resp.Status, true);
         }
         more = resp.Status == WinError.ErrorMoreData;
         n = more ? resp.NumEntries - 1 : resp.NumEntries;
         for (int i = 0; i < n; i++)
         {
             IFileEntry e = resp.Results[i];
             string name = e.GetName();
             if (fnf != null && fnf.Accept(this, name) == false)
             {
                 continue;
             }
             if (name.Length > 0)
             {
                 // if !files we don't need to create SmbFiles here
                 SmbFile f = new SmbFile(this, name, e.GetType(), AttrReadonly
                      | AttrDirectory, 0L, 0L, 0L);
                 if (ff != null && ff.Accept(f) == false)
                 {
                     continue;
                 }
                 if (files)
                 {
                     list.Add(f);
                 }
                 else
                 {
                     list.Add(name);
                 }
             }
         }
         if (GetType() != TypeWorkgroup)
         {
             break;
         }
         req.SubCommand = unchecked(SmbComTransaction.NetServerEnum3);
         req.Reset(0, ((NetServerEnum2Response)resp).LastName);
         resp.Reset();
     }
     while (more);
 }
Example #2
0
 /// <exception cref="SharpCifs.Smb.SmbException"></exception>
 /// <exception cref="UnknownHostException"></exception>
 /// <exception cref="System.UriFormatException"></exception>
 internal virtual void DoFindFirstNext(List<object> list, bool files, string wildcard
     , int searchAttributes, ISmbFilenameFilter fnf, ISmbFileFilter ff)
 {
     SmbComTransaction req;
     Trans2FindFirst2Response resp;
     int sid;
     string path = GetUncPath0();
     string p = Url.AbsolutePath;
     if (p.LastIndexOf('/') != (p.Length - 1))
     {
         throw new SmbException(Url + " directory must end with '/'");
     }
     req = new Trans2FindFirst2(path, wildcard, searchAttributes);
     resp = new Trans2FindFirst2Response();
     if (Log.Level >= 3)
     {
         Log.WriteLine("doFindFirstNext: " + req.Path);
     }
     Send(req, resp);
     sid = resp.Sid;
     req = new Trans2FindNext2(sid, resp.ResumeKey, resp.LastName);
     resp.SubCommand = SmbComTransaction.Trans2FindNext2;
     for (; ; )
     {
         for (int i = 0; i < resp.NumEntries; i++)
         {
             IFileEntry e = resp.Results[i];
             string name = e.GetName();
             if (name.Length < 3)
             {
                 int h = name.GetHashCode();
                 if (h == HashDot || h == HashDotDot)
                 {
                     if (name.Equals(".") || name.Equals(".."))
                     {
                         continue;
                     }
                 }
             }
             if (fnf != null && fnf.Accept(this, name) == false)
             {
                 continue;
             }
             if (name.Length > 0)
             {
                 SmbFile f = new SmbFile(this, name, TypeFilesystem, e.GetAttributes
                     (), e.CreateTime(), e.LastModified(), e.Length());
                 if (ff != null && ff.Accept(f) == false)
                 {
                     continue;
                 }
                 if (files)
                 {
                     list.Add(f);
                 }
                 else
                 {
                     list.Add(name);
                 }
             }
         }
         if (resp.IsEndOfSearch || resp.NumEntries == 0)
         {
             break;
         }
         req.Reset(resp.ResumeKey, resp.LastName);
         resp.Reset();
         Send(req, resp);
     }
     try
     {
         Send(new SmbComFindClose2(sid), Blank_resp());
     }
     catch (SmbException se)
     {
         if (Log.Level >= 4)
         {
             Runtime.PrintStackTrace(se, Log);
         }
     }
 }
Example #3
0
        /// <exception cref="SharpCifs.Smb.SmbException"></exception>
        /// <exception cref="UnknownHostException"></exception>
        /// <exception cref="System.UriFormatException"></exception>
        internal virtual void DoShareEnum(List<object> list, bool files, string wildcard, int
             searchAttributes, ISmbFilenameFilter fnf, ISmbFileFilter ff)
        {
            string p = Url.AbsolutePath;
            IOException last = null;
            IFileEntry[] entries;
            UniAddress addr;
            IFileEntry e;
            Hashtable map;
            if (p.LastIndexOf('/') != (p.Length - 1))
            {
                throw new SmbException(Url + " directory must end with '/'");
            }
            if (GetType() != TypeServer)
            {
                throw new SmbException("The requested list operations is invalid: " + Url);
            }
            map = new Hashtable();
            if (_enableDfs && Dfs.IsTrustedDomain(GetServer(), Auth))
            {
                try
                {
                    entries = DoDfsRootEnum();
                    for (int ei = 0; ei < entries.Length; ei++)
                    {
                        e = entries[ei];
                        if (map.ContainsKey(e) == false)
                        {
                            map.Put(e, e);
                        }
                    }
                }
                catch (IOException ioe)
                {
                    if (Log.Level >= 4)
                    {
                        Runtime.PrintStackTrace(ioe, Log);
                    }
                }
            }
            addr = GetFirstAddress();
            while (addr != null)
            {
                try
                {
                    last = null;

                    DoConnect();
                    try
                    {
                        entries = DoMsrpcShareEnum();
                    }
                    catch (IOException ioe)
                    {
                        if (Log.Level >= 3)
                        {
                            Runtime.PrintStackTrace(ioe, Log);
                        }
                        entries = DoNetShareEnum();
                    }
                    for (int ei = 0; ei < entries.Length; ei++)
                    {
                        e = entries[ei];
                        if (map.ContainsKey(e) == false)
                        {
                            map.Put(e, e);
                        }
                    }
                    break;
                }
                catch (IOException ioe)
                {
                    if (Log.Level >= 3)
                    {
                        Runtime.PrintStackTrace(ioe, Log);
                    }
                    last = ioe;

                    if (!(ioe is SmbAuthException))
                    {
                        RemoveCurrentAddress();

                        addr = GetNextAddress();
                    }
                    else
                    {
                        break;
                    }
                }

            }
            if (last != null && map.Count == 0)
            {
                if (last is SmbException == false)
                {
                    throw new SmbException(Url.ToString(), last);
                }
                throw (SmbException)last;
            }
            //Iterator iter = map.Keys.Iterator();
            //while (iter.HasNext())
            foreach (var item in map.Keys)
            {
                e = (IFileEntry)item;
                string name = e.GetName();
                if (fnf != null && fnf.Accept(this, name) == false)
                {
                    continue;
                }
                if (name.Length > 0)
                {
                    // if !files we don't need to create SmbFiles here
                    SmbFile f = new SmbFile(this, name, e.GetType(), AttrReadonly
                         | AttrDirectory, 0L, 0L, 0L);
                    if (ff != null && ff.Accept(f) == false)
                    {
                        continue;
                    }
                    if (files)
                    {
                        list.Add(f);
                    }
                    else
                    {
                        list.Add(name);
                    }
                }
            }
        }
Example #4
0
 /// <exception cref="SharpCifs.Smb.SmbException"></exception>
 internal virtual void DoEnum(List<object> list, bool files, string wildcard, int searchAttributes
     , ISmbFilenameFilter fnf, ISmbFileFilter ff)
 {
     if (ff != null && ff is DosFileFilter)
     {
         DosFileFilter dff = (DosFileFilter)ff;
         if (dff.Wildcard != null)
         {
             wildcard = dff.Wildcard;
         }
         searchAttributes = dff.Attributes;
     }
     try
     {
         int hostlen = Url.GetHost() != null ? Url.GetHost().Length : 0;
         if (hostlen == 0 || GetType() == TypeWorkgroup)
         {
             DoNetServerEnum(list, files, wildcard, searchAttributes, fnf, ff);
         }
         else
         {
             if (_share == null)
             {
                 DoShareEnum(list, files, wildcard, searchAttributes, fnf, ff);
             }
             else
             {
                 DoFindFirstNext(list, files, wildcard, searchAttributes, fnf, ff);
             }
         }
     }
     catch (UnknownHostException uhe)
     {
         throw new SmbException(Url.ToString(), uhe);
     }
     catch (UriFormatException mue)
     {
         throw new SmbException(Url.ToString(), mue);
     }
 }
Example #5
0
        /// <exception cref="SharpCifs.Smb.SmbException"></exception>
        internal virtual SmbFile[] ListFiles(string wildcard, int searchAttributes
            , ISmbFilenameFilter fnf, ISmbFileFilter ff)
        {
            List<object> list = new List<object>();
            DoEnum(list, true, wildcard, searchAttributes, fnf, ff);

            return Collections.ToArray<SmbFile>(list); //Collections.ToArray<SmbFile>(list);
        }
Example #6
0
 /// <summary>List the contents of this SMB resource.</summary>
 /// <remarks>
 /// List the contents of this SMB resource. The list returned will be
 /// identical to the list returned by the parameterless <code>listFiles()</code>
 /// method minus files filtered by the specified filename filter.
 /// </remarks>
 /// <param name="filter">a filter to exclude files from the results</param>
 /// <returns>An array of <tt>SmbFile</tt> objects</returns>
 /// <exception cref="SmbException">SmbException</exception>
 /// <exception cref="SharpCifs.Smb.SmbException"></exception>
 public virtual SmbFile[] ListFiles(ISmbFilenameFilter filter)
 {
     return ListFiles("*", AttrDirectory | AttrHidden | AttrSystem, filter, null);
 }
Example #7
0
 /// <summary>
 /// List files async
 /// </summary>
 /// <param name="smbFile"></param>
 /// <param name="wildcard"></param>
 /// <returns></returns>
 public static Task <List <SmbFile> > ListFilesAsync(this SmbFile smbFile, ISmbFilenameFilter fileFilter)
 {
     return(Task.Run(() => smbFile.ListFiles(fileFilter)));
 }