protected override Stream DoGetOutputStream(string contentName, string encoding, FileMode mode, FileShare sharing)
 {
     try
     {
         if (string.IsNullOrEmpty(contentName) || contentName == Native.GetInstance().DefaultContentName)
         {
             return(new FileStream(this.fileInfo.FullName, mode, FileAccess.Write, sharing));
         }
         else
         {
             return(Native.GetInstance().OpenAlternateContentStream
                    (
                        this.fileInfo.FullName,
                        contentName,
                        mode, FileAccess.Write, sharing
                    ));
         }
     }
     catch (FileNotFoundException)
     {
         throw new FileNodeNotFoundException(Address);
     }
     catch (DirectoryNotFoundException)
     {
         throw new DirectoryNodeNotFoundException(Address);
     }
 }
        protected override Stream DoGetInputStream(string contentName, out string encoding, FileMode mode, FileShare sharing)
        {
            encoding = null;

            try
            {
                if (string.IsNullOrEmpty(contentName) ||
                    contentName == Native.GetInstance().DefaultContentName)
                {
                    for (var i = 0; i < 10; i++)
                    {
                        try
                        {
                            return(new FileStream(this.fileInfo.FullName, mode, FileAccess.Read, sharing));
                        }
                        catch (DirectoryNotFoundException)
                        {
                            throw;
                        }
                        catch (FileNotFoundException)
                        {
                            throw;
                        }
                        catch (IOException)
                        {
                            if (i == 9)
                            {
                                throw;
                            }

                            Thread.Sleep(500);
                        }
                    }

                    throw new IOException();
                }
                else
                {
                    return(Native.GetInstance().OpenAlternateContentStream
                           (
                               this.fileInfo.FullName,
                               contentName,
                               mode, FileAccess.Read, sharing
                           ));
                }
            }
            catch (FileNotFoundException)
            {
                throw new FileNodeNotFoundException(Address);
            }
            catch (DirectoryNotFoundException)
            {
                throw new DirectoryNodeNotFoundException(Address);
            }
        }
        string INativePathService.GetNativeShortPath()
        {
            var retval = Native.GetInstance().GetShortPath(((LocalNodeAddress)this.Address).AbsoluteNativePath);

            if (!retval.EndsWith(Path.DirectorySeparatorChar))
            {
                retval += Path.DirectorySeparatorChar;
            }

            return(retval);
        }
        protected override Stream DoGetOutputStream(string contentName, string encoding, FileMode mode, FileShare sharing)
        {
            if (contentName == null)
            {
                throw new NotSupportedException();
            }

            return(Native.GetInstance().OpenAlternateContentStream
                   (
                       this.directoryInfo.FullName,
                       contentName,
                       mode, FileAccess.Read, sharing
                   ));
        }
        protected override Stream DoOpenStream(string contentName, FileMode fileMode, FileAccess fileAccess, FileShare fileShare)
        {
            if (contentName == null)
            {
                throw new NotSupportedException();
            }

            return(Native.GetInstance().OpenAlternateContentStream
                   (
                       this.directoryInfo.FullName,
                       contentName,
                       fileMode, fileAccess, fileShare
                   ));
        }
        private void SetExtendedAttribute(string name, object value)
        {
            byte[] buffer = null;

            if (value != null)
            {
                if ((buffer = value as byte[]) == null)
                {
                    buffer = global::System.Text.Encoding.UTF8.GetBytes(value.ToString());
                }
            }

            Native.GetInstance().SetExtendedAttribute(fileSystemInfo.FullName, name, buffer, 0, buffer == null ? 0 : buffer.Length);
        }
 private object GetExtendedAttribute(string name)
 {
     try
     {
         return(Native.GetInstance().GetExtendedAttribute(fileSystemInfo.FullName, name));
     }
     catch (FileNodeNotFoundException)
     {
         throw new FileNodeNotFoundException(this.Node.Address);
     }
     catch (NotSupportedException)
     {
         return(null);
     }
 }
        protected override IFile DoCreateHardLink(IFile targetFile, bool overwrite)
        {
            string target;
            var    path = this.fileInfo.FullName;

            try
            {
                var service = (INativePathService)targetFile.GetService(typeof(INativePathService));

                target = service.GetNativePath();
            }
            catch (NotSupportedException)
            {
                throw new NotSupportedException();
            }

            targetFile.Refresh();

            if (this.Exists && !overwrite)
            {
                throw new IOException("Hardlink already exists");
            }
            else
            {
                ActionUtils.ToRetryAction <object>
                (
                    delegate
                {
                    if (this.Exists)
                    {
                        this.Delete();
                    }

                    try
                    {
                        Native.GetInstance().CreateHardLink(path, target);
                    }
                    catch (TooManyLinksException)
                    {
                        throw new TooManyLinksException(this, targetFile);
                    }
                },
                    TimeSpan.FromSeconds(3), TimeSpan.FromSeconds(0.25)
                )(null);
            }

            return(this);
        }
        protected override INode DoDelete()
        {
            try
            {
                Native.GetInstance().DeleteFileContent(this.fileInfo.FullName, null);

                Refresh();
            }
            catch (FileNotFoundException)
            {
                throw new FileNodeNotFoundException(Address);
            }
            catch (DirectoryNotFoundException)
            {
                throw new DirectoryNodeNotFoundException(Address);
            }

            return(this);
        }
        protected override void DeleteContent(string contentName)
        {
            if (contentName == null)
            {
                contentName = Native.GetInstance().DefaultContentName;
            }

            try
            {
                Native.GetInstance().DeleteFileContent(((LocalNodeAddress)this.Address).AbsoluteNativePath, contentName);
            }
            catch (FileNotFoundException)
            {
                throw new FileNodeNotFoundException(Address);
            }
            catch (DirectoryNotFoundException)
            {
                throw new DirectoryNodeNotFoundException(Address);
            }
        }
 protected override Stream DoOpenStream(string contentName, FileMode fileMode, FileAccess fileAccess, FileShare fileShare)
 {
     try
     {
         if (string.IsNullOrEmpty(contentName) || contentName == Native.GetInstance().DefaultContentName)
         {
             return(new FileStream(this.fileInfo.FullName, fileMode, fileAccess, fileShare));
         }
         else
         {
             return(Native.GetInstance().OpenAlternateContentStream
                    (
                        this.fileInfo.FullName,
                        contentName,
                        fileMode, fileAccess, fileShare
                    ));
         }
     }
     catch (FileNotFoundException)
     {
         throw new FileNodeNotFoundException(Address);
     }
 }
        public override IEnumerable <INode> DoGetChildren(NodeType nodeType, Predicate <INode> acceptNode)
        {
            bool localRefreshNodes;

            lock (this.SyncLock)
            {
                localRefreshNodes = this.refreshNodes;

                this.refreshNodes = false;
            }

            localRefreshNodes = true;

            /**
             * TODO: If refreshNodes is true then we have to manually refresh
             * all children that aren't listed in the current listing but are
             * still in the node cache.
             *
             * Do this by attaching to cache events (thru a weak event handler) and
             * keeping a list of all cached (and thus resolved) nodes that are children
             * of this node.
             */

            //lock (this.SyncLock)
            {
                string[] items;

                foreach (var jumpPoint in GetJumpPoints(NodeType.Directory))
                {
                    yield return(jumpPoint);
                }

                if ((nodeType.Equals(NodeType.Directory)) || nodeType.Equals(NodeType.Any))
                {
                    try
                    {
                        if (Environment.OSVersion.Platform == PlatformID.Unix)
                        {
                            // Hack for mono which does not return symbolic links as directories anymore

                            items = Directory.GetFileSystemEntries(this.directoryInfo.FullName);
                        }
                        else
                        {
                            items = Directory.GetDirectories(this.directoryInfo.FullName);
                        }
                    }
                    catch (System.IO.IOException)
                    {
                        throw new DirectoryNodeNotFoundException(this.Address);
                    }

                    foreach (var name in items)
                    {
                        if (Environment.OSVersion.Platform == PlatformID.Unix)
                        {
                            if (Native.GetInstance().GetSymbolicLinkTarget(name) != null)
                            {
                                if (Native.GetInstance().GetSymbolicLinkTargetType(name) != NodeType.Directory)
                                {
                                    continue;
                                }
                            }
                            else
                            {
                                if (!Directory.Exists(name))
                                {
                                    continue;
                                }
                            }
                        }

                        var dir = (IDirectory)this.FileSystem.Resolve(this.Address.ResolveAddress(Path.GetFileName(name)), NodeType.Directory);

                        if (localRefreshNodes && !String.Equals((string)dir.Attributes["DriveType"], "Removable", StringComparison.CurrentCultureIgnoreCase))
                        {
                            dir.Refresh();
                        }

                        if (!ContainsShortcut(dir.Name, NodeType.Directory))
                        {
                            if (acceptNode(dir))
                            {
                                yield return(dir);
                            }
                        }
                    }
                }

                foreach (INode jumpPoint in GetJumpPoints(NodeType.File))
                {
                    yield return(jumpPoint);
                }

                if ((nodeType.Equals(NodeType.File)) || nodeType.Equals(NodeType.Any))
                {
                    try
                    {
                        if (Environment.OSVersion.Platform == PlatformID.Unix)
                        {
                            items = Directory.GetFileSystemEntries(this.directoryInfo.FullName);
                        }
                        else
                        {
                            items = Directory.GetFiles(this.directoryInfo.FullName);
                        }
                    }
                    catch (System.IO.DirectoryNotFoundException)
                    {
                        throw new DirectoryNodeNotFoundException(this.Address);
                    }

                    foreach (string name in items)
                    {
                        if (Environment.OSVersion.Platform == PlatformID.Unix)
                        {
                            if (Native.GetInstance().GetSymbolicLinkTarget(name) != null)
                            {
                                if (Native.GetInstance().GetSymbolicLinkTargetType(name) != NodeType.File)
                                {
                                    continue;
                                }
                            }
                            else
                            {
                                if (!File.Exists(name))
                                {
                                    continue;
                                }
                            }
                        }

                        var file = (IFile)this.FileSystem.Resolve(this.Address.ResolveAddress(Path.GetFileName(name)), NodeType.File);

                        if (localRefreshNodes)
                        {
                            file.Refresh();
                        }

                        if (!ContainsShortcut(file.Name, NodeType.File))
                        {
                            if (acceptNode(file))
                            {
                                yield return(file);
                            }
                        }
                    }
                }

                localRefreshNodes = false;
            }
        }
 public override System.Collections.Generic.IEnumerable <string> GetContentNames()
 {
     return(Native.GetInstance().GetContentInfos(((LocalNodeAddress)this.Address).AbsoluteNativePath).Select(contentInfo => contentInfo.Name));
 }
 string INativePathService.GetNativeShortPath()
 {
     return(Native.GetInstance().GetShortPath(((LocalNodeAddress)this.Address).AbsoluteNativePath));
 }
        protected internal static DriveInfo GetDriveInfo(string path)
        {
            string    symTarget;
            string    driveFormat = "";
            DriveInfo driveInfo;
            bool      unix = false;

            driveInfo = null;

            if (Environment.OSVersion.Platform == PlatformID.Win32NT ||
                Environment.OSVersion.Platform == PlatformID.Win32S ||
                Environment.OSVersion.Platform == PlatformID.Win32Windows ||
                Environment.OSVersion.Platform == PlatformID.WinCE)
            {
                if (path.Length != 3)
                {
                    return(null);
                }

                if (path[1] != ':')
                {
                    return(null);
                }

                if (!Char.IsLetter(path[0]))
                {
                    return(null);
                }

                path = path.Left(2) + "\\";
            }
            else if (Environment.OSVersion.Platform == PlatformID.Unix)
            {
                unix = true;
            }

            ActionUtils.IgnoreExceptions(() => driveInfo = new DriveInfo(path));

            if (unix && driveInfo != null)
            {
                try
                {
                    driveFormat = driveInfo.DriveFormat;
                }
                catch (IOException)
                {
                }
            }

            if (driveInfo == null || driveFormat == "Unknown")
            {
                if ((symTarget = Native.GetInstance().GetSymbolicLinkTarget(path)) != null)
                {
                    ActionUtils.IgnoreExceptions(() => driveInfo = new DriveInfo(symTarget));

                    if (unix && driveInfo != null)
                    {
                        try
                        {
                            driveFormat = driveInfo.DriveFormat;
                        }
                        catch (IOException)
                        {
                        }
                    }
                }
            }

            if (driveInfo == null || driveFormat == "Unknown")
            {
                return(null);
            }

            return(driveInfo);
        }