PDA

View Full Version : How to delete a file in use? Thanks.



endring
22nd August 2013, 08:43
Hi, everyone:
Sorry to bother you, but I need some help here.
I am working on a reference management project like endnote or jabref. Generally, the main window has two panels. The left panel is a item list and the right panel is a PDF file viewer. When I click an item, it becomes the "current item" and the corresponding attached PDF file stored in a local attachment directory is shown by the PDF viewer on the right panel. The PDF viewer is extracted from TexMaker project which uses poppler library to handle PDF files. When I try to delete the "current item" on the left panel, of course I would like to delete the corresponding PDF attachment as well. However, the PDF file for the "current item" is opened by the PDF viewer, which means it is in use and I couldn't delete it. On Windows, I would get a message like "Could not delete ***: it is been used by another person or program".

So, could you give me some hints about deleting file even if it is in use? My PDF viewer widget doesn't have a "close file" function and I really don't want to dive into the source code of Texmaker or Poppler.

Thanks a lot!

pkj
22nd August 2013, 08:47
OS denies rights to delete a file if it is currently opened with writing rights. So there is no way you can fix this from qt. However, if the file is opened with ReadONly rights, it should be possible to delete it. In either case, you need to refer to your library on how to open the file in ReadOnly mode.
By the way, welcome to the forum!!

endring
22nd August 2013, 18:46
Thanks for your reply. I solved the problem partially. I use
QFile.readAll() function to read the PDF file to a buffer before being
opened by the PDF viewer widget. Thank God poppler-qt4 has implemented
loadFromData(QByteArray) function. The trick is that one has to close
QFile after opening it. Some simple tests impress me that if there are
some IO devices linking to a file, the OS concludes the file is in
use.
Of course I don't know how to delete a file used by other programs in
general. However, it is not my current problem and I could put off it
to the long long future:)

saman_artorious
22nd August 2013, 20:03
Thanks for your reply. I solved the problem partially. I use
QFile.readAll() function to read the PDF file to a buffer before being
opened by the PDF viewer widget. Thank God poppler-qt4 has implemented
loadFromData(QByteArray) function. The trick is that one has to close
QFile after opening it. Some simple tests impress me that if there are
some IO devices linking to a file, the OS concludes the file is in
use.
Of course I don't know how to delete a file used by other programs in
general. However, it is not my current problem and I could put off it
to the long long future:)

Generally, you can delete a file, only when all file descriptors associated with your file inode table are deleted. This happens with the deletion of the last fd.

Cheers,