ftp.cc

来自「konqueror3 embedded版本, KDE环境下的当家浏览器的嵌入式版」· CC 代码 · 共 2,199 行 · 第 1/5 页

CC
2,199 行
字号
}void Ftp::chmod( const KURL & url, int permissions ){  if( !ftpOpenConnection(loginImplicit) )        return;  if ( !ftpChmod( url.path(), permissions ) )    error( ERR_CANNOT_CHMOD, url.path() );  else    finished();}void Ftp::ftpCreateUDSEntry( const QString & filename, FtpEntry& ftpEnt, UDSEntry& entry, bool isDir ){  assert(entry.count() == 0); // by contract :-)  UDSAtom atom;  atom.m_uds = UDS_NAME;  atom.m_str = filename;  entry.append( atom );  atom.m_uds = UDS_SIZE;  atom.m_long = ftpEnt.size;  entry.append( atom );  atom.m_uds = UDS_MODIFICATION_TIME;  atom.m_long = ftpEnt.date;  entry.append( atom );  atom.m_uds = UDS_ACCESS;  atom.m_long = ftpEnt.access;  entry.append( atom );  atom.m_uds = UDS_USER;  atom.m_str = ftpEnt.owner;  entry.append( atom );  if ( !ftpEnt.group.isEmpty() )  {    atom.m_uds = UDS_GROUP;    atom.m_str = ftpEnt.group;    entry.append( atom );  }  if ( !ftpEnt.link.isEmpty() )  {    atom.m_uds = UDS_LINK_DEST;    atom.m_str = ftpEnt.link;    entry.append( atom );    KMimeType::Ptr mime = KMimeType::findByURL( KURL("ftp://host/" + filename ) );    // Links on ftp sites are often links to dirs, and we have no way to check    // that. Let's do like Netscape : assume dirs generally.    // But we do this only when the mimetype can't be known from the filename.    // --> we do better than Netscape :-)    if ( mime->name() == KMimeType::defaultMimeType() )    {      kdDebug(7102) << "Setting guessed mime type to inode/directory for " << filename << endl;      atom.m_uds = UDS_GUESSED_MIME_TYPE;      atom.m_str = "inode/directory";      entry.append( atom );      isDir = true;    }  }  atom.m_uds = UDS_FILE_TYPE;  atom.m_long = isDir ? S_IFDIR : ftpEnt.type;  entry.append( atom );  /* atom.m_uds = UDS_ACCESS_TIME;     atom.m_long = buff.st_atime;     entry.append( atom );     atom.m_uds = UDS_CREATION_TIME;     atom.m_long = buff.st_ctime;     entry.append( atom ); */}void Ftp::ftpShortStatAnswer( const QString& filename, bool isDir ){    UDSEntry entry;    UDSAtom atom;    atom.m_uds = KIO::UDS_NAME;    atom.m_str = filename;    entry.append( atom );    atom.m_uds = KIO::UDS_FILE_TYPE;    atom.m_long = isDir ? S_IFDIR : S_IFREG;    entry.append( atom );    atom.m_uds = KIO::UDS_ACCESS;    atom.m_long = S_IRUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH;    entry.append( atom );    // No details about size, ownership, group, etc.    statEntry(entry);    finished();}void Ftp::ftpStatAnswerNotFound( const QString & path, const QString & filename ){    // Only do the 'hack' below if we want to download an existing file (i.e. when looking at the "source")    // When e.g. uploading a file, we still need stat() to return "not found"    // when the file doesn't exist.    QString statSide = metaData("statSide");    kdDebug(7102) << "Ftp::stat statSide=" << statSide << endl;    if ( statSide == "source" )    {        kdDebug(7102) << "Not found, but assuming found, because some servers don't allow listing" << endl;        // MS Server is incapable of handling "list <blah>" in a case insensitive way        // But "retr <blah>" works. So lie in stat(), to get going...        //        // There's also the case of ftp://ftp2.3ddownloads.com/90380/linuxgames/loki/patches/ut/ut-patch-436.run        // where listing permissions are denied, but downloading is still possible.        ftpShortStatAnswer( filename, false /*file, not dir*/ );        return;    }    error( ERR_DOES_NOT_EXIST, path );}void Ftp::stat( const KURL &url){  kdDebug(7102) << "Ftp::stat : path='" << url.path() << "'" << endl;  if( !ftpOpenConnection(loginImplicit) )        return;  QString path = QDir::cleanDirPath( url.path() );  kdDebug(7102) << "Ftp::stat : cleaned path='" << path << "'" << endl;  // We can't stat root, but we know it's a dir.  if( path.isEmpty() || path == "/" )  {    UDSEntry entry;    UDSAtom atom;    atom.m_uds = KIO::UDS_NAME;    atom.m_str = QString::null;    entry.append( atom );    atom.m_uds = KIO::UDS_FILE_TYPE;    atom.m_long = S_IFDIR;    entry.append( atom );    atom.m_uds = KIO::UDS_ACCESS;    atom.m_long = S_IRUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH;    entry.append( atom );    atom.m_uds = KIO::UDS_USER;    atom.m_str = "root";    entry.append( atom );    atom.m_uds = KIO::UDS_GROUP;    entry.append( atom );    // no size    statEntry( entry );    finished();    return;  }  KURL tempurl( url );  tempurl.setPath( path ); // take the clean one  QString listarg; // = tempurl.directory(false /*keep trailing slash*/);  QString parentDir;  QString filename = tempurl.fileName();  Q_ASSERT(!filename.isEmpty());  QString search = filename;  // Try cwd into it, if it works it's a dir (and then we'll list the parent directory to get more info)  // if it doesn't work, it's a file (and then we'll use dir filename)  bool isDir = ftpFolder(path, false);  // if we're only interested in "file or directory", we should stop here  QString sDetails = metaData("details");  int details = sDetails.isEmpty() ? 2 : sDetails.toInt();  kdDebug(7102) << "Ftp::stat details=" << details << endl;  if ( details == 0 )  {     if ( !isDir && !ftpSize( path, 'I' ) ) // ok, not a dir -> is it a file ?     {  // no -> it doesn't exist at all        ftpStatAnswerNotFound( path, filename );        return;     }     ftpShortStatAnswer( filename, isDir ); // successfully found a dir or a file -> done     return;  }  if (!isDir)  {    // It is a file or it doesn't exist, try going to parent directory    parentDir = tempurl.directory(false /*keep trailing slash*/);    // With files we can do "LIST <filename>" to avoid listing the whole dir    listarg = filename;  }  else  {    // --- New implementation:    // Don't list the parent dir. Too slow, might not show it, etc.    // Just return that it's a dir.    UDSEntry entry;    UDSAtom atom;    atom.m_uds = KIO::UDS_NAME;    atom.m_str = filename;    entry.append( atom );    atom.m_uds = KIO::UDS_FILE_TYPE;    atom.m_long = S_IFDIR;    entry.append( atom );    atom.m_uds = KIO::UDS_ACCESS;    atom.m_long = S_IRUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH;    entry.append( atom );    // No clue about size, ownership, group, etc.    statEntry(entry);    finished();    return;    // --- Old implementation:#if 0    // It's a dir, remember that    // Reason: it could be a symlink to a dir, in which case ftpReadDir    // in the parent dir will have no idea about that. But we know better.    isDir = true;    // If the dir starts with '.', we'll need '-a' to see it in the listing.    if ( search[0] == '.' )       listarg = "-a";    parentDir = "..";#endif  }  // Now cwd the parent dir, to prepare for listing  if( !ftpFolder(parentDir, true) )    return;  if( !ftpOpenCommand( "list", listarg, 'I', ERR_DOES_NOT_EXIST ) )  {    kdError(7102) << "COULD NOT LIST" << endl;    return;  }  kdDebug(7102) << "Starting of list was ok" << endl;  Q_ASSERT( !search.isEmpty() && search != "/" );  bool bFound = false;  KURL      linkURL;  FtpEntry  ftpEnt;  while( ftpReadDir(ftpEnt) )  {    // We look for search or filename, since some servers (e.g. ftp.tuwien.ac.at)    // return only the filename when doing "dir /full/path/to/file"    if ( !bFound )    {        if ( ( search == ftpEnt.name || filename == ftpEnt.name ) ) {            if ( !filename.isEmpty() ) {              bFound = true;              UDSEntry entry;              ftpCreateUDSEntry( filename, ftpEnt, entry, isDir );              statEntry( entry );            }        } else if ( isDir && ( ftpEnt.name == listarg || ftpEnt.name+'/' == listarg ) ) {            // Damn, the dir we're trying to list is in fact a symlink            // Follow it and try again            if ( ftpEnt.link.isEmpty() )                kdWarning(7102) << "Got " << listarg << " as answer, but empty link!" << endl;            else            {                linkURL = url;                kdDebug(7102) << "ftpEnt.link=" << ftpEnt.link << endl;                if ( ftpEnt.link[0] == '/' )                    linkURL.setPath( ftpEnt.link ); // Absolute link                else                {                    // Relative link (stat will take care of cleaning ../.. etc.)                    linkURL.setPath( listarg ); // this is what we were listing (the link)                    linkURL.setPath( linkURL.directory() ); // go up one dir                    linkURL.addPath( ftpEnt.link ); // replace link by its destination                    kdDebug(7102) << "linkURL now " << linkURL.prettyURL() << endl;                }                // Re-add the filename we're looking for                linkURL.addPath( filename );            }            bFound = true;        }    }    // kdDebug(7102) << ftpEnt.name << endl;  }  ftpCloseCommand();        // closes the data connection only  if ( !bFound )  {    ftpStatAnswerNotFound( path, filename );    return;  }  if ( !linkURL.isEmpty() )  {      if ( linkURL == url || linkURL == tempurl )      {          error( ERR_CYCLIC_LINK, linkURL.prettyURL() );          return;      }      stat( linkURL );      return;  }  kdDebug(7102) << "stat : finished successfully" << endl;  finished();}void Ftp::listDir( const KURL &url ){  kdDebug(7102) << "Ftp::listDir " << url.prettyURL() << endl;  if( !ftpOpenConnection(loginImplicit) )        return;  // No path specified ?  QString path = url.path();  if ( path.isEmpty() )  {    KURL realURL;    realURL.setProtocol( "ftp" );    if ( m_user != FTP_LOGIN )      realURL.setUser( m_user );    // We set the password, so that we don't ask for it if it was given    if ( m_pass != FTP_PASSWD )      realURL.setPass( m_pass );    realURL.setHost( m_host );    realURL.setPort( m_port );    if ( m_initialPath.isEmpty() )        m_initialPath = "/";    realURL.setPath( m_initialPath );    kdDebug(7102) << "REDIRECTION to " << realURL.prettyURL() << endl;    redirection( realURL );    finished();    return;  }  kdDebug(7102) << "hunting for path '" << path << "'" << endl;  if (!ftpOpenDir( path ) )  {    if ( ftpSize( path, 'I' ) ) // is it a file ?    {      error( ERR_IS_FILE, path );      return;    }    // not sure which to emit    //error( ERR_DOES_NOT_EXIST, path );    error( ERR_CANNOT_ENTER_DIRECTORY, path );    return;  }  UDSEntry entry;  FtpEntry  ftpEnt;  while( ftpReadDir(ftpEnt) )  {    //kdDebug(7102) << ftpEnt.name << endl;    //Q_ASSERT( !ftpEnt.name.isEmpty() );    if ( !ftpEnt.name.isEmpty() )    {      //if ( S_ISDIR( (mode_t)ftpEnt.type ) )      //   kdDebug(7102) << "is a dir" << endl;      //if ( !ftpEnt.link.isEmpty() )      //   kdDebug(7102) << "is a link to " << ftpEnt.link << endl;      entry.clear();      ftpCreateUDSEntry( ftpEnt.name, ftpEnt, entry, false );      listEntry( entry, false );    }  }  listEntry( entry, true ); // ready  ftpCloseCommand();        // closes the data connection only  finished();}void Ftp::slave_status(){  kdDebug(7102) << "Got slave_status host = " << (m_host.ascii() ? m_host.ascii() : "[None]") << " [" << (m_bLoggedOn ? "Connected" : "Not connected") << "]" << endl;  slaveStatus( m_host, m_bLoggedOn );}bool Ftp::ftpOpenDir( const QString & path ){  //QString path( _url.path(-1) );  // We try to change to this directory first to see whether it really is a directory.  // (And also to follow symlinks)  QString tmp = path.isEmpty() ? QString("/") : path;  // We get '550', whether it's a file or doesn't exist...  if( !ftpFolder(tmp, false) )      return false;  // Don't use the path in the list command:  // We changed into this directory anyway - so it's enough just to send "list".  // We use '-a' because the application MAY be interested in dot files.  // The only way to really know would be to have a metadata flag for this...  // Since some windows ftp server seems not to support the -a argument, we use a fallback here.  // In fact we have to use -la otherwise -a removes the default -l (e.g. ftp.trolltech.com)  if( !ftpOpenCommand( "list -la", QString::null, 'I', ERR_CANNOT_ENTER_DIRECTORY ) )  {    if ( !ftpOpenCommand( "list", QString::null, 'I', ERR_CANNOT_ENTER_DIRECTORY ) )    {      kdWarning(7102) << "Can't open for listing" << endl;      return false;    }  }  kdDebug(7102) << "Starting of list was ok" << endl;  return true;}bool Ftp::ftpReadDir(FtpEntry& de){  assert(m_data != NULL);  // get a line from the data connecetion ...  while( !m_data->textEOF() )  {    if(m_data->textRead() <= 0)      continue;    if(m_data->textTooLong())      kdWarning(7102) << "ftpReadDir line too long - truncated" << endl;    const char* buffer = m_data->textLine();    kdDebug(7102) << "dir > " << buffer << endl;    //Normally the listing looks like    // -rw-r--r--   1 dfaure   dfaure        102 Nov  9 12:30 log    // but on Netware servers like ftp://ci-1.ci.pwr.wroc.pl/ it looks like (#76442)    // d [RWCEAFMS] Admin                     512 Oct 13  2004 PSI

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?