Results 1 to 7 of 7

Thread: QTemporaryFile: unique filename across platforms?

  1. #1
    Join Date
    Dec 2009
    Posts
    47
    Thanks
    11
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android

    Default QTemporaryFile: unique filename across platforms?

    Hi

    Are the filenames used by QTemporaryFile unique across platforms? E.g., two files created in the same directory in Linux on a USB stick must not be named FILE1.txt and file1.txt, as this would create problems when the USB stick is used on a Windows machine.

    Al_


    Background:
    I have a database where files can be added: the file content is copied to a dedicated directory, the file name is stored in the database together with tags. As over time the same filename could be added (e.g., img00001.jpg is a common filename ...), I need to rename the files when copying to the dedicated directory. And I consider to use QTemporaryFile to create unique filenames.

  2. #2
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTemporaryFile: unique filename across platforms?

    Typically, if you wish to use the same USB stick across OSs such as Windows and Linux, you would not be using a case sensitive file system such as Ext2 anyway, but rather a case insensitive one like VFAT, so it doesn't really matter if linux calls a file "file1.txt", it would still be called FILE1.TXT on Windows, and if Windows creates a file called IMAGE1.JPG, it could be accessed as image1.jpg under Linux.

    (Of course, Windows can use LFNs to state the case of a file rather than uppercasing it, but the result is the same)

  3. #3
    Join Date
    Dec 2009
    Posts
    47
    Thanks
    11
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android

    Default Re: QTemporaryFile: unique filename across platforms?

    Thanks. So, QTemporaryFile should do the work as long as I create the files from the beginning on a USB stick (FAT32 formatted).

    What if I create the directory on a local harddisk (ext3 formatted) and later copy that directory plus the database (using a USB stick or the like) to another computer running Windows? Would QTemporaryFile still guarantee that no conflict emerges?

  4. #4
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTemporaryFile: unique filename across platforms?

    If the numbers used in the filename are unique, the filename will be unique.

    The only problem you have is if you have two files that only differ in character case. What will happen then is that the second file will overwrite the first, thus you must ensure that will never happen.

  5. The following user says thank you to squidge for this useful post:

    Al_ (27th December 2010)

  6. #5
    Join Date
    Aug 2009
    Location
    Belgium
    Posts
    310
    Thanks
    10
    Thanked 31 Times in 25 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTemporaryFile: unique filename across platforms?

    Hi,

    You could use a QUuid::toString() as filename if you trim the bracelets. This is guaranteed to be unique.

    Regards,
    Marc

  7. The following user says thank you to marcvanriet for this useful post:

    Al_ (27th December 2010)

  8. #6
    Join Date
    Dec 2009
    Posts
    47
    Thanks
    11
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android

    Default [SOLVED] QTemporaryFile: unique filename across platforms?

    Thanks to marcvanriet and squidge for their suggestions. Both are valid, both have some minor drawbacks: if using QTemporaryFile I need to check for file names differing only in capitalization; QUuid (as I understand the Qt documentation) is on Linux not truly guaranteed to be unique (although the same uuid is very unlikely to be produced twice). Options:
    1. I use QUuid andI live with the remote possibility of failure
    2. I use QTemporaryFile::filename().toLower() and check that this filename is still unique

    As a purist, I rather go for option 2 and will try to implement this in a class derived from QTemporaryFile where I re-implement the virtual function bool open(OpenMode flags)

  9. #7
    Join Date
    Dec 2009
    Posts
    47
    Thanks
    11
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android

    Default Re: [SOLVED] QTemporaryFile: unique filename across platforms?

    I wrote a small class QXUniqueFile that uses QTemporaryFile in the back to create truly unique file names, see attached (main,cpp and QXUniqueFile.pro are a console application that demonstrates use of the class).
    Attached Files Attached Files

Similar Threads

  1. QTemporaryFile and his destructor
    By SABROG in forum Qt Programming
    Replies: 3
    Last Post: 19th May 2009, 19:55
  2. QHttp and QTemporaryFile
    By maximAL in forum Qt Programming
    Replies: 1
    Last Post: 16th January 2009, 14:06
  3. QTemporaryFile problems
    By C167 in forum Qt Programming
    Replies: 11
    Last Post: 22nd June 2008, 09:54
  4. Unique ID for a PC to use for Key Validation
    By nbkhwjm in forum Qt Programming
    Replies: 1
    Last Post: 9th April 2008, 23:13
  5. The unique key
    By igor.alexandrov in forum Qt Programming
    Replies: 9
    Last Post: 26th January 2008, 00:27

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.