コード例 #1
0
        public NtStatus GetFileInformation(
            string filename,
            out FileInformation fileinfo,
            DokanFileInfo info)
        {
            fileinfo = new FileInformation();
            try
            {
                string path = GetPath(filename);
                fileinfo.FileName = path;
                SftpATTRS attr = GetChannel().stat(path);

                fileinfo.Attributes = attr.isDir() ?
                                      FileAttributes.Directory :
                                      FileAttributes.Normal;

                if (DokanSSHFS.UseOffline)
                {
                    fileinfo.Attributes |= FileAttributes.Offline;
                }

                DateTime org = new DateTime(1970, 1, 1, 0, 0, 0, 0);

                fileinfo.CreationTime   = org.AddSeconds(attr.getMTime());
                fileinfo.LastAccessTime = org.AddSeconds(attr.getATime());
                fileinfo.LastWriteTime  = org.AddSeconds(attr.getMTime());
                fileinfo.Length         = attr.getSize();

                return(NtStatus.Success);
            }
            catch (SftpException)
            {
                return(NtStatus.Error);
            }
            catch (Exception e)
            {
                connectionError_ = true;
                Debug(e.ToString());
                Reconnect();
                return(NtStatus.Error);
            }
        }
コード例 #2
0
        public int SetFileTime(
            String filename,
            DateTime ctime,
            DateTime atime,
            DateTime mtime,
            DokanFileInfo info)
        {
            Debug("SetFileTime {0}", filename);
            try
            {
                Debug(" filetime {0} {1} {2}", ctime.ToString(), atime.ToString(), mtime.ToString());

                string      path    = GetPath(filename);
                ChannelSftp channel = GetChannel();
                SftpATTRS   attr    = channel.stat(path);

                TimeSpan at = (atime - new DateTime(1970, 1, 1, 0, 0, 0));
                TimeSpan mt = (mtime - new DateTime(1970, 1, 1, 0, 0, 0));

                int uat = (int)at.TotalSeconds;
                int umt = (int)mt.TotalSeconds;

                if (mtime == DateTime.MinValue)
                {
                    umt = attr.getMTime();
                }
                if (atime == DateTime.MinValue)
                {
                    uat = attr.getATime();
                }

                attr.setACMODTIME(uat, umt);
                channel.setStat(path, attr);
                return(0);
            }
            catch (SftpException)
            {
                return(-1);
            }
            catch (Exception e)
            {
                connectionError_ = true;
                Debug(e.ToString());
                Reconnect();
                return(-1);
            }
        }
コード例 #3
0
ファイル: Scp.cs プロジェクト: radtek/Ketarin4Linux
        /// <summary>
        /// Initiates a new SCP response on sourceforge.net for downloading a file.
        /// </summary>
        /// <param name="uri">URI to download (includes username and password)</param>
        /// <param name="timeout">Timeout for this session</param>
        public ScpWebResponse(Uri uri, int timeout)
        {
            JSch jsch = new JSch();

            string[] userPass = uri.UserInfo.Split(':');
            if (userPass.Length != 2)
            {
                throw new WebException("Username and password information for sourceforge.net incomplete");
            }

            session = jsch.getSession(userPass[0], "frs.sourceforge.net", 22);

            // username and password will be given via UserInfo interface.
            //UserInfo ui = new UserInfo();
            //session.setUserInfo(ui);
            session.setPassword(userPass[1]);
            Hashtable hastable = new Hashtable();

            hastable.put("StrictHostKeyChecking", "no");
            session.setConfig(hastable);

            if (DbManager.Proxy != null)
            {
                session.setProxy(new ProxyHTTP(DbManager.Proxy.Address.Host, DbManager.Proxy.Address.Port));
            }

            try
            {
                session.connect(timeout);
            }
            catch (JSchException e)
            {
                if (e.Message == "Auth fail")
                {
                    throw new WebException("Invalid username or password for sourceforge");
                }
                throw;
            }

            // exec 'scp -f rfile' remotely
            string sfPath = GetSourceforgePath(uri.LocalPath);

            // Determine file modified date
            ChannelSftp channelSftp = (ChannelSftp)session.openChannel("sftp");

            channelSftp.connect();
            try
            {
                SftpATTRS attrs = channelSftp.lstat(sfPath);
                this.lastModified = RpcApplication.UnixToDotNet(attrs.getMTime());
            }
            catch (SftpException)
            {
                throw new WebException("The file \"" + sfPath + "\" could not be found.");
            }
            finally
            {
                channelSftp.disconnect();
            }

            String  command = "scp -f " + sfPath.Replace(" ", "\\ ");
            Channel channel = session.openChannel("exec");

            ((ChannelExec)channel).setCommand(command);

            // get I/O streams for remote scp
            Stream outs = channel.getOutputStream();
            Stream ins  = channel.getInputStream();

            channel.connect();

            byte[] buf = new byte[1024];

            // send '\0'
            buf[0] = 0; outs.Write(buf, 0, 1); outs.Flush();


            int c = checkAck(ins);

            if (c != 'C')
            {
                return;
            }

            // read '0644 '
            ins.Read(buf, 0, 5);

            while (true)
            {
                ins.Read(buf, 0, 1);
                if (buf[0] == ' ')
                {
                    break;
                }
                this.contentLength = this.contentLength * 10 + (buf[0] - '0');
            }

            for (int i = 0; ; i++)
            {
                ins.Read(buf, i, 1);
                if (buf[i] == (byte)0x0a)
                {
                    Util.getString(buf, 0, i);
                    break;
                }
            }

            this.responseUri = new Uri("scp://" + session.getHost() + sfPath);
            // send '\0'
            buf[0] = 0; outs.Write(buf, 0, 1); outs.Flush();

            this.responseStream = ins;
        }