Seite 1 von 1

Bug. Qfile exists() ist File und Dir? sogar URL? isValid()

Verfasst: 16. April 2006 19:27
von patrik08
Vom titel alles ist true!

Vor eine woche .... habe ich gesehen das

qt file.exists() & url.isValid() is true/true
beide sind einfach gleich...

dieses problem habe ich noch harmlos geloest mit...

Code: Alles auswählen

bool Config::IsNetFile( QString fullFileName )
{
    if (fullFileName.startsWith("http://", Qt::CaseInsensitive) or 
        fullFileName.startsWith("ftp://", Qt::CaseInsensitive) or
        fullFileName.startsWith("webdav://", Qt::CaseInsensitive)  )
       { 
         return true;
       } else {
         return false;  
       }
}


Doch nun das selbe mit dir und file ... fuer qt ist einfach beides dass selbe.

( f.exists() ) ist auch true bei dir...


mit php wenn ich frage ob es ein file ist oder ein dir bekomme ich eine richtige antwort..

um ein cache rekursiv su loeschen mit eine "leichtere" funktion ist es etwas so...

Code: Alles auswählen

bool Config::DownDirRemove( QString cleandir)
{
    /* check on mac win PATH_SEPARATOR! */
    /* to trust this function check to send test to "a:" and a:/dir/ (floppy)*/
    file_put_contents_append("removefunc.log","1");
    file_put_contents_append("removefunc.log",cleandir);
    QDir dirqtmethod(cleandir);
    if (dirqtmethod.isAbsolutePath(cleandir)) {
    /*
    #include <errno.h>
    #include <dirent.h>
    #include <stdio.h>
    #include <stdlib.h>
    */
     file_put_contents_append("removefunc.log","absolut path ok...");
     DIR *pdir;
     struct dirent *pent;
     pdir=opendir(qtchars(cleandir));
     if (!pdir){
     return false;
     }
     errno=0;
     while ((pent=readdir(pdir))){
      /* translate to qt dir and file no  */
      QString onlyfilename = QString( "%1" ).arg( pent->d_name  );
        if (onlyfilename =="." or onlyfilename =="..") {
            continue;
        }
      QString absolutelink = QString( "%1%2" ).arg( cleandir , pent->d_name  );   /* dir no slasch on end! */
     if (is_file(absolutelink)) {
         file_put_contents_append("removefunc.log","found file... \""+absolutelink+"\"  ");
       if (qt_unlink(absolutelink)) {
         file_put_contents_append("removefunc.log","found file... \""+absolutelink+"\"  ");
       } else {
         file_put_contents_append("removefunc.log","not remove this file... \""+absolutelink+"\"  ");
         break;    
       }
     } else {
      DownDirRemove(QString( "%1/" ).arg( absolutelink ));
      file_put_contents_append("removefunc.log","found dir...\""+absolutelink+"\"  ");
     }
 }
 if (errno){
  file_put_contents_append("removefunc.log","erno true");
 return false;
 }
closedir(pdir);
 /* dir end final */
 if (dirqtmethod.rmdir(cleandir)) { 
  file_put_contents_append("removefunc.log","dir is confirm rmdir");   
 } else {
   file_put_contents_append("removefunc.log","dir false confirm rmdir action ");    
 }
 
return true;
 } else {
  file_put_contents_append("removefunc.log","Not absolute path ...!");
 return false; 
    
 }
}

Doch da bekommt mann lust mit einem
system("rm -rf /path"); daruber zu fahren... oder mit del rek auf window...


also ...




Code: Alles auswählen


bool is_file(QString fullFileName)
{
    QFile f( fullFileName );
	if ( f.exists() ) {
    return true;
	} else {
	return false;
    }
}


Verfasst: 18. April 2006 08:07
von Christian81
Für dein File/Dir - Problem gibt es QFileInfo.
Ein Verzeichnis ist nunmal eine spezielle Datei.

Re: Bug. Qfile exists() ist File und Dir? sogar URL? isVali

Verfasst: 18. April 2006 08:20
von macman
Also ich hab nicht ein Wort verstanden :-) Ich sehe nicht ganz wo dein Problem ist.
patrik08 hat geschrieben:qt file.exists() & url.isValid() is true/true
beide sind einfach gleich...
Ja und? Äpfel und Birnen, beides ist Obst. Die beiden Funktionen haben doch nichts miteinander zu tun. isValid() prüft doch nicht ob etwas existiert, das prüft doch nur die Schreibweise einer URL.

Du willst ein Verzeichnis löschen? Probier es hiermit:

Code: Alles auswählen

void myClass::removeDir(const QString d)
{
	QDir dir(d);

	if (dir.exists())
	{
		const QFileInfoList list = dir.entryInfoList();
		QFileInfo fi;

		for (int l = 0; l < list.size(); l++)
		{
			fi = list.at(l);
			if (fi.isDir() && fi.fileName() != "." && fi.fileName() != "..") 
				removeDir(fi.absoluteFilePath());
			else if (fi.isFile())
			{
				bool ret = dir.remove(pathConv(fi.fileName()));
				if (!ret)
					writeLog("Can't remove: " + fi.absoluteFilePath() + " (write-protect?)");
			}
		}

		dir.rmdir(d);
	}
}

Verfasst: 18. April 2006 10:37
von patrik08
super danke ... glaubte bereits ich muss rm -rf auch in winn mit system bauen..

als dank poste ich eine meine gute funktion...

da #include <QHttp> nicht redirect url folgen kann habe ich es so gemacht ...
auch gut .. um zu testen ob ein server up ist!

Code: Alles auswählen

#define _USER_AGENT_CURL_APPS "Mozilla/5.0 (Windows; U; Windows NT 5.1; it-CH; rv:1.7.12) Gecko/20050919 Firefox/1.0.7"
#define _CURL_APPS_TIME_OUT 8

/*
win32 {
LIBS += C:/msys/1.0/bin/lib/libz.a
LIBS += C:/msys/1.0/bin/lib/libcurl.dll.a
LIBS += C:/msys/1.0/bin/lib/libcurl.a
}

*/

/*  Warning get local file contents or remote file contents by curl  */
QString Config::file_get_contents(QString fullFileName)
{
        QString inside = "";
        if (fullFileName.contains("https://", Qt::CaseInsensitive)) { 
        inside = "No build support to https protocoll";
        return inside;
        } 

       if ( this->IsNetFile( fullFileName ) )
       {
        file_put_contents(www_net_file,"");  /* remove last action if exist */
           
         if ( fullFileName.contains("webdav://", Qt::CaseInsensitive )) {
         #ifndef GRABCONFIGPASS
         #define GRABCONFIGPASS
         #endif
         }
         
         char *localfile = append( qt2char( cachedir ) , c_www_net_file );
         char *cookiefile = append( qt2char( cachedir ) , c_www_cookie_file );
         char *url = qt2char( fullFileName );
         
            CURL *curl_handle;
            FILE *outfile;
            curl_global_init(CURL_GLOBAL_ALL);     
            curl_global_init(CURL_GLOBAL_ALL);
            curl_handle = curl_easy_init();
            outfile = fopen(localfile, "w"); 
 
                if (outfile!=NULL) {
                    /* CURLOPT_COOKIE and must have CURLOPT_COOKIEJAR char * (file to write same as php)   */
                    curl_easy_setopt(curl_handle, CURLOPT_URL, url);
                    curl_easy_setopt(curl_handle, CURLOPT_VERBOSE, TRUE);
                    curl_easy_setopt(curl_handle, CURLOPT_COOKIEJAR, cookiefile );
                    curl_easy_setopt(curl_handle, CURLOPT_FOLLOWLOCATION , 3);
                    curl_easy_setopt(curl_handle, CURLOPT_MAXREDIRS , 5);
                    curl_easy_setopt(curl_handle, CURLOPT_TIMEOUT , _CURL_APPS_TIME_OUT );
                    curl_easy_setopt(curl_handle, CURLOPT_USERAGENT, _USER_AGENT_CURL_APPS );
                    curl_easy_setopt(curl_handle, CURLOPT_FILE, outfile);   
                    
                     if (curl_easy_perform(curl_handle)==CURLE_OK) {
                         fclose(outfile);
                         
                            QFile filecurl(www_net_file);
                             if (filecurl.size() > 5) {
                               curlerrormsg = "ok_success";  
                             } else {
                              curlerrormsg = "Error time out to get remote file"+QString( "%1" ).arg( getExactTime() ); 
                             }
                             
                     }  else {
                     curlerrormsg = "Error time out to get remote file"+QString( "%1" ).arg( getExactTime() );
                     file_put_contents(www_net_file,"");
                     }                         
                } 
       /* return grab result from local file */
      return file_get_contents(www_net_file);
      }

    
    
     
    /*  ok is a fake normal local file init .....  */
    QFile file(fullFileName); 
    if (file.exists()) {
                if (file.open(QFile::ReadOnly | QFile::Text)) {
                  inside =file.readAll();
                  file.close();
                  errormsg = "ok_success";
                } else {
                 inside = "no_open_file";
                }
    } else {
    inside = "no_file";  
    }
    
   return inside;
}



bool Config::IsNetFile( QString fullFileName )
{
    if (fullFileName.startsWith("http://", Qt::CaseInsensitive) or 
        fullFileName.startsWith("ftp://", Qt::CaseInsensitive) or
        fullFileName.startsWith("webdav://", Qt::CaseInsensitive)  )
       { 
         return true;
       } else {
         return false;  
       }
}



Verfasst: 18. April 2006 11:06
von patrik08
aus welche classe kommt pathConv... ist das was mit
pathseparator linux versus win?

Verfasst: 18. April 2006 11:13
von Christian81
Ist imho unnötig da sich Qt intern um den korrekten Path-Separator kümmert.

Verfasst: 18. April 2006 11:21
von macman
patrik08 hat geschrieben:aus welche classe kommt pathConv... ist das was mit
pathseparator linux versus win?
Kannst Du entfernen, hatte es vergessen. Erledigt Qt von selbst, hatte es noch drin, da bei Qt3 für manche Dateioperationen noch Win API herhalten musste.