Results 1 to 8 of 8

Thread: A Programming Problem: How to know if a file is already opened?

  1. #1
    Member Fu Siyuan's Avatar
    Join Date
    Apr 2009
    Location
    Mountain View, CA, US
    Posts
    176
    Thanks
    10
    Thanked 17 Times in 2 Posts

    A Programming Problem: How to know if a file is already opened?

    Firstly I'm describing the situlation:

    if current directory (like F:\test\)has some files:

    xxx xxx2 xxx3 dst.arc xxxxx xxxxxx ....

    and we run such commend:
    SomeArc a dst.arc *
    or SomeArc a dst.arc F:\test\*
    or SomeArc a dst.arc F:\*

    As the programmer, how to avoid dst.arc is included in the files to be archived? Or this will obviously cause problem.

  2. #2
    Administrator Shelwien's Avatar
    Join Date
    May 2008
    Location
    Kharkov, Ukraine
    Posts
    3,134
    Thanks
    179
    Thanked 921 Times in 469 Posts
    For now, I just ended up comparing GetFullPathName()-processed archive filename with files I'm adding to archive.
    But as I mentioned, usually archivers just do the scan before archive creation, and thus avoid the problem completely.
    Also I listed quite a few alternatives there.

    And as to your specific question - you can try opening the files for writing (and create the archive with disallowed sharing),
    or locking them (with LockFile)

  3. #3
    The Founder encode's Avatar
    Join Date
    May 2006
    Location
    Moscow, Russia
    Posts
    3,954
    Thanks
    359
    Thanked 332 Times in 131 Posts
    Quote Originally Posted by Fu Siyuan View Post
    As the programmer, how to avoid dst.arc is included in the files to be archived? Or this will obviously cause problem.
    Easiest task. Just think about it! With the PIM archiver I just check what files we are adding:

    void Add(FileName) {
    if (FileName==ArchiveName) return;

    Pretty easy...

  4. #4
    Member Fu Siyuan's Avatar
    Join Date
    Apr 2009
    Location
    Mountain View, CA, US
    Posts
    176
    Thanks
    10
    Thanked 17 Times in 2 Posts
    void Add(FileName) {
    if (FileName==ArchiveName) return;
    But this won't work on:
    SomeArc a -r dst.arc F:\* if there is a dst.arc in F:\xxx\xxx...
    Program don't know if dst.arc in F:\xxxx\xxxx is the archive itself.


    But now I changed my idea, I won't care about it. Now my program will works like nz on this problem.
    Just compress number of bytes of dst.arc that before scaning the files. AddFile will only increase the size, so the content before won't change.

    if SomeArc a dst.arc dst.arc, the archive contains the archive with previous status.

  5. #5
    Administrator Shelwien's Avatar
    Join Date
    May 2008
    Location
    Kharkov, Ukraine
    Posts
    3,134
    Thanks
    179
    Thanked 921 Times in 469 Posts
    http://msdn.microsoft.com/en-us/libr...63(VS.85).aspx
    It would know if you'd do that both for archive file and the files you're adding to archive.
    There're still possibilities that break this (mostly links), but that can be ignored.
    Or, alternatively, you can lock the archive file as I said.

  6. #6
    Expert
    Matt Mahoney's Avatar
    Join Date
    May 2008
    Location
    Melbourne, Florida, USA
    Posts
    3,255
    Thanks
    306
    Thanked 778 Times in 485 Posts
    If I do

    zpaq a archive *

    and the archive is in the current directory, then it appends a compressed copy of itself to itself. This doesn't otherwise damage the archive. All the files can be extracted including a copy of itself before appending. (Maybe I should test this with bigger files).

    So I don't worry about the problem. If you are using the command line, then I assume you know what you are doing. If you want to add the archive to itself then I won't stop you.

  7. #7
    Administrator Shelwien's Avatar
    Join Date
    May 2008
    Location
    Kharkov, Ukraine
    Posts
    3,134
    Thanks
    179
    Thanked 921 Times in 469 Posts
    Well, I was thinking the same too.
    But then Sami said that this specific case happens with his benchmark scripts - and affects both archive size and compression speed.
    Also there's another problem when you're trying to extract such an archive (with a incomplete copy of itself) to the same place -
    it can overwrite the archive file.

  8. #8
    Member Vacon's Avatar
    Join Date
    May 2008
    Location
    Germany
    Posts
    523
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hello everyone,


    Quote Originally Posted by Shelwien View Post
    [...]Also there's another problem when you're trying to extract such an archive (with a incomplete copy of itself) to the same place -
    it can overwrite the archive file.
    Well, as I am only a user (and sadly no programmer with deep knowledge), I can only try to suggest two things:
    - create some unique temporary filename on runtime and exclude this from the list of files to archive. This file in the end can be renamed to <archive-name>
    - on extraction build in an error-handling and ask if files should be overwritten / renamed / skipped (best would be size of <new-file> vs. size of <existing-file>). Though this is nothing particular new.

    Best regards!

Similar Threads

  1. Good programming style & standards
    By encode in forum The Off-Topic Lounge
    Replies: 4
    Last Post: 29th July 2010, 11:00
  2. Programming Fonts
    By Bill Pettis in forum The Off-Topic Lounge
    Replies: 0
    Last Post: 10th February 2010, 18:16
  3. Lisaac programming language
    By lunaris in forum The Off-Topic Lounge
    Replies: 0
    Last Post: 11th December 2008, 18:51
  4. Problem with forum
    By EneergE in forum Forum Archive
    Replies: 1
    Last Post: 28th March 2008, 12:06
  5. [OFFTOPIC]: Problem for hardcore programmers!
    By nimdamsk in forum Forum Archive
    Replies: 4
    Last Post: 25th March 2007, 18:12

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •