Exemple #1
0
        public int WriteFile(
            String filename,
            Byte[] buffer,
            ref uint writtenBytes,
            long offset,
            DokanFileInfo info)
        {
            Debug("WriteFile {0} bufferLen {1} Offset {2}", filename, buffer.Length, offset);

            string path = GetPath(filename);

            if (path.Contains(":SSHFSProperty.Permission"))
            {
                if (offset == 0)
                {
                    string[] tmp = path.Split(new char[] { ':' });
                    path = tmp[0];
                    int permission = 0;
                    permission = Convert.ToInt32(System.Text.Encoding.ASCII.GetString(buffer), 8);
                    WritePermission(path, permission);
                    writtenBytes = (uint)buffer.Length;
                    return(0);
                }
                else
                {
                    writtenBytes = 0;
                    return(0);
                }
            }

            try
            {
                ChannelSftp channel = GetChannel();
                //GetMonitor monitor = new GetMonitor(buffer.Length);
                Tamir.SharpSsh.java.io.OutputStream stream = channel.put(path, null, 3 /*HACK: 存在しないモード */, offset);
                stream.Write(buffer, 0, buffer.Length);
                stream.Close();
                writtenBytes = (uint)buffer.Length;
                return(0);
            }
            catch (IOException)
            {
                return(0);
            }
            catch (Exception e)
            {
                Debug(e.ToString());
                return(-1);
            }
        }
Exemple #2
0
        public int CreateFile(
            String filename,
            FileAccess access,
            FileShare share,
            FileMode mode,
            FileOptions options,
            DokanFileInfo info)
        {
            Debug("CreateFile {0}", filename);
            try
            {
                string      path    = GetPath(filename);
                ChannelSftp channel = GetChannel();

                if (CheckAltStream(path))
                {
                    return(0);
                }

                switch (mode)
                {
                case FileMode.Open:
                {
                    Debug("Open");
                    if (isExist(path, info))
                    {
                        return(0);
                    }
                    else
                    {
                        return(-DokanNet.ERROR_FILE_NOT_FOUND);
                    }
                }

                case FileMode.CreateNew:
                {
                    Debug("CreateNew");
                    if (isExist(path, info))
                    {
                        return(-DokanNet.ERROR_ALREADY_EXISTS);
                    }

                    Debug("CreateNew put 0 byte");
                    Tamir.SharpSsh.java.io.OutputStream stream = channel.put(path);
                    stream.Close();
                    return(0);
                }

                case FileMode.Create:
                {
                    Debug("Create put 0 byte");
                    Tamir.SharpSsh.java.io.OutputStream stream = channel.put(path);
                    stream.Close();
                    return(0);
                }

                case FileMode.OpenOrCreate:
                {
                    Debug("OpenOrCreate");

                    if (!isExist(path, info))
                    {
                        Debug("OpenOrCreate put 0 byte");
                        Tamir.SharpSsh.java.io.OutputStream stream = channel.put(path);
                        stream.Close();
                    }
                    return(0);
                }

                case FileMode.Truncate:
                {
                    Debug("Truncate");

                    if (!isExist(path, info))
                    {
                        return(-DokanNet.ERROR_FILE_NOT_FOUND);
                    }

                    Debug("Truncate put 0 byte");
                    Tamir.SharpSsh.java.io.OutputStream stream = channel.put(path);
                    stream.Close();
                    return(0);
                }

                case FileMode.Append:
                {
                    Debug("Append");

                    if (isExist(path, info))
                    {
                        return(0);
                    }

                    Debug("Append put 0 byte");
                    Tamir.SharpSsh.java.io.OutputStream stream = channel.put(path);
                    stream.Close();
                    return(0);
                }

                default:
                    Debug("Error unknown FileMode {0}", mode);
                    return(-1);
                }
            }
            catch (SftpException e)
            {
                Debug(e.ToString());
                return(-DokanNet.ERROR_FILE_NOT_FOUND);
            }
            catch (Exception e)
            {
                connectionError_ = true;
                Debug(e.ToString());
                Reconnect();
                return(-DokanNet.ERROR_FILE_NOT_FOUND);
            }
        }
        /// <summary>
        /// 上傳文件
        /// </summary>
        /// <param name="remotePath">路徑文件夾名稱,不包含sftp://ip:port,只包含路徑文件夾</param>
        /// <param name="localFileName">本地文件完整路徑</param>
        public void Upload(string remotePath, string localFileName)
        {
            Connect();
            FileInfo fileInf = new FileInfo(localFileName);
            //如果ActionWhileFileExist是Rename
            string uri = "";

            string filename = fileInf.Name.Replace("\\", "/");

            if (filename.StartsWith("/"))
            {
                filename = filename.Substring(1, filename.Length - 1);
            }
            if (remotePath.EndsWith("/"))
            {
                remotePath = remotePath.Substring(0, remotePath.Length - 1);
            }
            if (remotePath == "/" || remotePath == "")
            {
                uri = filename;
            }
            else
            {
                uri = remotePath + "/" + filename;
            }
            if (uri.StartsWith("/"))
            {
                uri = uri.Substring(1, uri.Length - 1);
            }


            uri = uri.Replace(@"\", "/");
            //MessageBox.Show(uri);
            int buffLength = 2048;// 缓冲大小设置为kb

            byte[] buff = new byte[buffLength];
            int    contentLen;

            FileStream fs = fileInf.OpenRead();

            //Tamir.SharpSsh.java.io.InputStream iptStream = new Tamir.SharpSsh.java.io.FileInputStream(Filename);/

            Tamir.SharpSsh.java.io.OutputStream optStream = m_sftp.put(uri);
            try
            {
                contentLen = fs.Read(buff, 0, buffLength);
                while (contentLen != 0)
                {
                    optStream.Write(buff, 0, contentLen);
                    contentLen = fs.Read(buff, 0, buffLength);
                }

                //如果是上传过程用临时文件名的,到这里已经上传完毕了,把文件名改回去
                optStream.close();

                filename = null;
                fileInf  = null;
            }
            catch (Exception ex)
            {
                throw new Exception("上傳失敗:\r\n" + ex.Message);
                //MessageBox.Show(ex.Message, "Upload Error");
            }
            finally
            {
                if (optStream != null)
                {
                    optStream = null;
                }
                if (fs != null)
                {
                    fs.Flush();
                    fs.Close();
                    fs = null;
                }
            }
        }
Exemple #4
0
        public NtStatus CreateFile(
            string filename,
            DokanNet.FileAccess access,
            FileShare share,
            FileMode mode,
            FileOptions options,
            FileAttributes attributes,
            DokanFileInfo info)
        {
            if (info.IsDirectory)
            {
                switch (mode)
                {
                case FileMode.Open:
                    Debug("OpenDirectory {0}", filename);
                    try
                    {
                        string    path = GetPath(filename);
                        SftpATTRS attr = GetChannel().stat(path);
                        if (attr.isDir())
                        {
                            return(NtStatus.Success);
                        }
                        else
                        {
                            return(NtStatus.ObjectPathNotFound);    // TODO: return not directory?
                        }
                    }
                    catch (SftpException e)
                    {
                        Debug(e.ToString());
                        return(NtStatus.ObjectPathNotFound);
                    }
                    catch (Exception e)
                    {
                        connectionError_ = true;
                        Debug(e.ToString());
                        Reconnect();
                        return(NtStatus.ObjectPathNotFound);
                    }


                case FileMode.CreateNew:
                    Debug("CreateDirectory {0}", filename);
                    try
                    {
                        string      path    = GetPath(filename);
                        ChannelSftp channel = GetChannel();

                        channel.mkdir(path);
                        return(NtStatus.Success);
                    }
                    catch (SftpException e)
                    {
                        Debug(e.ToString());
                        return(NtStatus.Error);
                    }
                    catch (Exception e)
                    {
                        connectionError_ = true;
                        Debug(e.ToString());
                        Reconnect();
                        return(NtStatus.Error);    // TODO: more appropriate error code
                    }

                default:
                    Debug("Error FileMode invalid for directory {0}", mode);
                    return(NtStatus.Error);
                }
            }
            else
            {
                Debug("CreateFile {0}", filename);
                try
                {
                    string      path    = GetPath(filename);
                    ChannelSftp channel = GetChannel();

                    if (CheckAltStream(path))
                    {
                        return(NtStatus.Success);
                    }

                    switch (mode)
                    {
                    case FileMode.Open:
                    {
                        Debug("Open");
                        if (isExist(path, info))
                        {
                            return(NtStatus.Success);
                        }
                        else
                        {
                            return(NtStatus.ObjectNameNotFound);
                        }
                    }

                    case FileMode.CreateNew:
                    {
                        Debug("CreateNew");
                        if (isExist(path, info))
                        {
                            return(NtStatus.ObjectNameCollision);
                        }

                        Debug("CreateNew put 0 byte");
                        Tamir.SharpSsh.java.io.OutputStream stream = channel.put(path);
                        stream.Close();
                        return(NtStatus.Success);
                    }

                    case FileMode.Create:
                    {
                        Debug("Create put 0 byte");
                        Tamir.SharpSsh.java.io.OutputStream stream = channel.put(path);
                        stream.Close();
                        return(NtStatus.Success);
                    }

                    case FileMode.OpenOrCreate:
                    {
                        Debug("OpenOrCreate");

                        if (!isExist(path, info))
                        {
                            Debug("OpenOrCreate put 0 byte");
                            Tamir.SharpSsh.java.io.OutputStream stream = channel.put(path);
                            stream.Close();
                        }
                        return(NtStatus.Success);
                    }

                    case FileMode.Truncate:
                    {
                        Debug("Truncate");

                        if (!isExist(path, info))
                        {
                            return(NtStatus.ObjectNameNotFound);
                        }

                        Debug("Truncate put 0 byte");
                        Tamir.SharpSsh.java.io.OutputStream stream = channel.put(path);
                        stream.Close();
                        return(NtStatus.Success);
                    }

                    case FileMode.Append:
                    {
                        Debug("Append");

                        if (isExist(path, info))
                        {
                            return(NtStatus.Success);
                        }

                        Debug("Append put 0 byte");
                        Tamir.SharpSsh.java.io.OutputStream stream = channel.put(path);
                        stream.Close();
                        return(NtStatus.Success);
                    }

                    default:
                        Debug("Error unknown FileMode {0}", mode);
                        return(NtStatus.Error);
                    }
                }
                catch (SftpException e)
                {
                    Debug(e.ToString());
                    return(NtStatus.ObjectNameNotFound);
                }
                catch (Exception e)
                {
                    connectionError_ = true;
                    Debug(e.ToString());
                    Reconnect();
                    return(NtStatus.ObjectNameNotFound);
                }
            }
        }
Exemple #5
0
 public void GetWithStream(string fromFilePath, Tamir.SharpSsh.java.io.OutputStream stream)
 {
     cancelled = false;
     SftpChannel.get(fromFilePath, stream, m_monitor);
 }
Exemple #6
0
        public DokanError CreateFile(
            string filename,
            DokanNet.FileAccess access,
            FileShare share,
            FileMode mode,
            FileOptions options,
            FileAttributes attributes,
            DokanFileInfo info)
        {
            Debug("CreateFile {0}", filename);
            try
            {
                string      path    = GetPath(filename);
                ChannelSftp channel = GetChannel();

                if (CheckAltStream(path))
                {
                    return(DokanError.ErrorSuccess);
                }

                switch (mode)
                {
                case FileMode.Open:
                {
                    Debug("Open");
                    if (isExist(path, info))
                    {
                        return(DokanError.ErrorSuccess);
                    }
                    else
                    {
                        return(DokanError.ErrorFileNotFound);
                    }
                }

                case FileMode.CreateNew:
                {
                    Debug("CreateNew");
                    if (isExist(path, info))
                    {
                        return(DokanError.ErrorAlreadyExists);
                    }

                    Debug("CreateNew put 0 byte");
                    Tamir.SharpSsh.java.io.OutputStream stream = channel.put(path);
                    stream.Close();
                    return(DokanError.ErrorSuccess);
                }

                case FileMode.Create:
                {
                    Debug("Create put 0 byte");
                    Tamir.SharpSsh.java.io.OutputStream stream = channel.put(path);
                    stream.Close();
                    return(DokanError.ErrorSuccess);
                }

                case FileMode.OpenOrCreate:
                {
                    Debug("OpenOrCreate");

                    if (!isExist(path, info))
                    {
                        Debug("OpenOrCreate put 0 byte");
                        Tamir.SharpSsh.java.io.OutputStream stream = channel.put(path);
                        stream.Close();
                    }
                    return(DokanError.ErrorSuccess);
                }

                case FileMode.Truncate:
                {
                    Debug("Truncate");

                    if (!isExist(path, info))
                    {
                        return(DokanError.ErrorFileNotFound);
                    }

                    Debug("Truncate put 0 byte");
                    Tamir.SharpSsh.java.io.OutputStream stream = channel.put(path);
                    stream.Close();
                    return(DokanError.ErrorSuccess);
                }

                case FileMode.Append:
                {
                    Debug("Append");

                    if (isExist(path, info))
                    {
                        return(DokanError.ErrorSuccess);
                    }

                    Debug("Append put 0 byte");
                    Tamir.SharpSsh.java.io.OutputStream stream = channel.put(path);
                    stream.Close();
                    return(DokanError.ErrorSuccess);
                }

                default:
                    Debug("Error unknown FileMode {0}", mode);
                    return(DokanError.ErrorError);
                }
            }
            catch (SftpException e)
            {
                Debug(e.ToString());
                return(DokanError.ErrorFileNotFound);
            }
            catch (Exception e)
            {
                connectionError_ = true;
                Debug(e.ToString());
                Reconnect();
                return(DokanError.ErrorFileNotFound);
            }
        }
Exemple #7
0
        public void Upload(object o, int iRow, string FtpPathWithoutIP, string Filename, string initialpath, int offset)
        {
            FileInfo fileInf = new FileInfo(Filename);
            //如果ActionWhileFileExist是Rename
            string uri = "";
            string urifordeleteexistfile = "";
            string filename = fileInf.FullName.Substring(initialpath.Length + 1, fileInf.FullName.Length - initialpath.Length - 1).Replace("\\", "/");

            if (filename.StartsWith("/"))
            {
                filename = filename.Substring(1, filename.Length - 1);
            }
            if (FtpPathWithoutIP == "/" || FtpPathWithoutIP == "")
            {
                uri = filename;
                urifordeleteexistfile = filename;
            }
            else
            {
                uri = FtpPathWithoutIP + filename;
                urifordeleteexistfile = FtpPathWithoutIP + filename;
            }
            if (uri.StartsWith("/"))
            {
                uri = uri.Substring(1, uri.Length - 1);
            }

            //上传已有文件是覆盖还是重命名待上传文件,如果设置是Rename,查询一下文件是否存在,存在则给待上传文件加上时间戳
            string trueFileName = "";

            if (ActionWhileFileExist == "Rename")
            {
                if (CheckFileExist(uri, fileInf.Name.ToString()) == true)
                {
                    //文件uri重新设置
                    string extname = fileInf.Extension.ToString();
                    filename = filename.Substring(0, filename.Length - extname.Length) + "_" + DateTime.Now.ToString("yyyyMMddHHssmm") + extname;
                    if (FtpPathWithoutIP == "/" || FtpPathWithoutIP == "")
                    {
                        uri = filename;
                    }
                    else
                    {
                        uri = FtpPathWithoutIP + filename;
                    }
                }
            }


            //上传文件是否要使用临时文件名,如果要,加上后缀.tmp
            trueFileName = uri;
            if (Renamefile == "Y")
            {
                uri = uri + ".tmp";
            }

            uri = uri.Replace(@"\", "/");
            int buffLength = 2048;// 缓冲大小设置为kb

            byte[] buff         = new byte[buffLength];
            int    contentLen   = 0;
            int    uploadedlen  = 0;
            int    irefreshflag = 0;

            FileStream fs = fileInf.OpenRead();

            //Tamir.SharpSsh.java.io.InputStream iptStream = new Tamir.SharpSsh.java.io.FileInputStream(Filename);/

            Tamir.SharpSsh.java.io.OutputStream optStream = null;
            if (offset > 0)
            {
                optStream = m_sftp.put(uri, 2);
                fs.Seek(offset, System.IO.SeekOrigin.Current);
                uploadedlen = offset;
            }
            else
            {
                optStream = m_sftp.put(uri);
            }
            try
            {
                contentLen  = fs.Read(buff, 0, buffLength);
                uploadedlen = uploadedlen + contentLen;

                MethodInfo mi        = getFormMethod.getMethod(o.GetType().FullName);
                decimal    d_persent = 0;
                // 流内容没有结束

                while (contentLen != 0)
                {
                    if (Uploaddownloadpauseflag == "Y")
                    {
                        break;
                    }

                    optStream.Write(buff, 0, contentLen);
                    d_persent = Convert.ToDecimal(Convert.ToDecimal(uploadedlen) / Convert.ToDecimal(fs.Length));
                    mi.Invoke(o, new object[] { iRow, String.Format("{0}" + "%", Convert.ToInt32(d_persent * 100)) + "--->Uploading " + fileInf.Name, uploadedlen });
                    irefreshflag++;
                    if (irefreshflag == 15)
                    {
                        irefreshflag = 0;//循环15次刷新一下界面,防止因为DoEvents而减慢上传速度
                        Application.DoEvents();
                    }
                    contentLen  = fs.Read(buff, 0, buffLength);
                    uploadedlen = uploadedlen + contentLen;
                }
                optStream.close();
                if (Uploaddownloadpauseflag == "Y")
                {
                    return;
                }

                d_persent = Convert.ToDecimal(1);
                mi.Invoke(o, new object[] { iRow, String.Format("{0}" + "%", Convert.ToInt32(d_persent * 100)) + "--->Uploaded " + fileInf.Name, -1 });

                //如果是上传过程用临时文件名的,到这里已经上传完毕了,把文件名改回去


                if (Renamefile == "Y")
                {
                    try
                    {
                        Delete(urifordeleteexistfile);
                    }
                    catch { }
                    Rename(uri, trueFileName);
                }
                //filenamewithotpath = null;
                trueFileName = null;
                filename     = null;
                fileInf      = null;
            }
            catch (Exception ex)
            {
                throw ex;
                //MessageBox.Show(ex.Message, "Upload Error");
            }
            finally
            {
                if (optStream != null)
                {
                    optStream = null;
                }
                if (fs != null)
                {
                    fs.Flush();
                    fs.Close();
                    fs = null;
                }
            }
        }