0

I wrote the code in C#, and I'm making a program that uses DokanNet and Renci.SshNet.Sftp libraries to export files from an SFTP server to a virtual drive on a remote computer.

The problem is that photo files and video files open well, and videos play well.

However, when I try to open a document file such as notepad's txt or MSWORD, the document fails.

A message is displayed indicating that the file does not exist on the disk.

And when reading from sftp, the following error occurs. -- 'Renci.SshNet.Common.SftpPermissionDeniedException'(Renci.SshNet.dll)

To solve this problem, I try to adjust the permission when reading the following code.

 public NtStatus ReadFile(
            string filename,
            byte[] buffer,
            out int readBytes,
            long offset,
            IDokanFileInfo info)
        {
            SftpFileStream fileStream = null;
            Debug.WriteLine("[ReadFile]" + filename);
            readBytes = 0;
            try
            {
                fileStream = (SftpFileStream)info.Context;
                
                fileStream.Seek(offset, SeekOrigin.Begin);
                int bytesRead = fileStream.Read(buffer, 0, buffer.Length);
                readBytes = bytesRead;
                return DokanResult.Success;
            }
            catch (SftpPermissionDeniedException)
            {
                if (fileStream != null)
                {
                    fileStream.Close();
                    fileStream.Dispose();
                    fileStream = null;
                }
                
                SftpFile sFile = client.Get(filename);
                sFile.SetPermissions(0777);
                fileStream = client.Open(filename, FileMode.Open);
                fileStream.Seek(offset, SeekOrigin.Begin);
                int bytesRead = fileStream.Read(buffer, 0, buffer.Length);
                readBytes = bytesRead;
                info.Context = fileStream;
                Debug.WriteLine("\t\t\t[ReadFile][ERROR] Permission denied to read the file.");
                return DokanResult.Success;
            }
            catch (Exception errData)
            {
                Debug.WriteLine("[ReadFile][ERROR]" + errData.ToString());
            }
            return DokanResult.Unsuccessful;
        }
public void Cleanup(string filename, IDokanFileInfo info)
        {
            SftpFile sFile = null;

            if (client.Exists(filename) == false)
            {
                return;
            }

            sFile = client.Get(filename);
            if (sFile == null)
            { 
                return;
            }

            if (info.Context != null)
            {
                if (sFile.IsDirectory == false)
                {
                    Debug.WriteLine("[Cleanup]" + filename);
                }

                var fileStream = (SftpFileStream)info.Context;
                fileStream.Close();
                fileStream.Dispose();
                info.Context = null;
            }
        }

public void CloseFile(string fileName, IDokanFileInfo info)
        {
            if (info.Context != null)
            {
                var fileStream = (SftpFileStream)info.Context;
                fileStream.Close();
                fileStream.Dispose();
                info.Context = null;
            }
        }

If a permission error occurs in this code, I adjust the permission to a value of 777 and try to read the file again. However, if I still read from sftp, the same permission problem occurs.

I think this is why the document won't open.

Photos and videos open fine, but I don't know why document files don't open.

And when I open one file, Dokan's callback function CreateFile request and ReadFile come too many times.

I want to know why.

0 Answers0