Cannot rename by QFile::rename



  • Qt : Qt5.6.2, vc2015 64bits
    OS :windows 10 64bits

    I want to rename
    "C:/Users/yyyy/Pictures/QImageScraperDownload/big-2390565c24.pngv%5Cu003d5" , to: "C:/Users/yyyy/Pictures/QImageScraperDownload/big-2390565c24.png" by QFile::rename but it always return false.

    Following is the whole progress

    1 : I download an image by QNetworkAccessManager, write the buffer into QFile, The QFile is part of the struct, which share with shared_ptr
    2 : Before I rename the file, I set the permission as

    bool const permission = task->file_.setPermissions(QFile::WriteOther | QFile::ReadOther);
    qDebug()<<__func__<<":can set permission:"<<permission; //always return true
    

    3 : rename the file

    QFile::rename("C:/Users/yyyy/Pictures/QImageScraperDownload/big-2390565c24.pngv%5Cu003d5",
    "C:/Users/yyyy/Pictures/QImageScraperDownload/big-2390565c24.pngv%5Cu003d5.png");
    

    But it always fail, I have tried

    task->file_.rename("big-2390565c24.pngv%5Cu003d5.png"); //fail
    task->file_.rename("C:/Users/yyyy/Pictures/QImageScraperDownload/big-2390565c24.pngv%5Cu003d5.png"); //fail
    

    I also five QDir a shot, but it is fail either. However, the files can be renamed if I rename them from another exe written by Qt.

    bool const can_rename = QFile::rename("C:/Users/yyyy/Pictures/QImageScraperDownload/Ding-Ding-Mobile-SK3-Pro-Philippines.jpg", "C:/Users/yyyy/Pictures/QImageScraperDownload/Ding-Ding-Mobile-SK3-Pro-Philippines.jpeg");
    qDebug()<<"can rename:"<<can_rename; //success
    

    However, QFile::remove("file_path") sometimes work, sometimes do not work, I even tried to close the QFile manually before rename and remove, but this do not fixed this issue either.

    I have no idea what makes the rename process fail, do anyone have similar experiences?Thanks


  • Lifetime Qt Champion

    Hi,

    Are these files in use when you try to change their name ?



  • Right now I am using some hack to "fixed" this issue, I store the files cannot be renamed and removed, after all of the download progress are finished, I tried to remove those files again, this time almost every files could be renamed and removed. Wonder what happened.



  • @SGaist said in Cannot rename by QFile::rename:

    Hi,

    Are these files in use when you try to change their name ?

    No, I already close the file when download finished, although I am still try to access the fileName by QFile.
    I even tried to replace QFile by std::unique_ptr, delete(reset(nullptr), in this case I store the fileName in QString) it when download finished, but nothing work



  • Try it on mac os, it works perfectly, haven't tried on linux yet, I guess this maybe some sort of bug


  • Moderators

    @tham I don't think it's a bug. It sounds more like the file is still in use. Windows blocks renaming/deleting of used/opened files, but MacOS/Linux do not. You should check using https://technet.microsoft.com/en-us/sysinternals/filemon.aspx


  • Moderators

    Also to add to @jsulm , I've had situations on windows, especially win8 and 10 where the user has windows explorer open in the same directory with the preview pane active. That might lock the file also.



  • I find out the culprit, it is QImageReader.

    static size_t num = 0;
    QImageReader reader(name);
     reader.setDecideFormatFromContent(true);
     reader.canRead(); //after calling can read, the file will be locked
     bool const can_rename = QFile::rename(name, QString("img/sos%1").arg(num++));
    qDebug()<<"can rename:"<<can_rename;
    

    Is this an expected behavior?

    In case it is a bug, I create a small project to reproduce the issues

    I create a minimal project to reproduce the situation, place at mega.

    Things to noticed :

    1 : copy img_links.txt to your shadow build folder

    shadow_build
    --debug
    --release
    --img_links.txt

    2 : Create a folder "img" in shadow build folder

    shadow_build
    --debug
    --release
    --img



  • @jsulm said in Cannot rename by QFile::rename:

    I don't think it's a bug. It sounds more like the file is still in use. Windows blocks renaming/deleting of used/opened files, but MacOS/Linux do not.

    Thanks, learn new things. Looks like QImageReader blocking the file.


  • Moderators

    @tham That could be actually a bug in QImageReader.
    canRead() should just check whether the image can be read and close it if it was opened to check.


  • Lifetime Qt Champion

    The internals are a bit more complex than that.

    I'd move the QImageReader code in its own scope so it gets destroyed when not needed anymore and then you can go on move your files around.



  • @SGaist said in Cannot rename by QFile::rename:

    I'd move the QImageReader code in its own scope so it gets destroyed when not needed anymore and then you can go on move your files around.

    Thanks for the suggestion, I do that already and now it works :)

    No matter it is a bug or not, I think this api is counter intuitive, if QImageReader would lock the file, it should have a close api or mentioned in the document.

    However, I open a bug report at jira.



  • This post is deleted!

Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.