Professional Web Applications Themes

Atomic directory swap - Mac Programming

I'm implementing an autoupdate system and I'm running into a snag as far as what to do once the update has finished downloading. The obvious approach is to move or delete the old program and replace it with the new one. However, I don't want anything bad to happen if the user's dog trips over his power cord at exactly the wrong instant. An additional complicating factor is that the program is actually an input manager, so renaming the old version, moving the new version, and deleting the old version wouldn't work (I think), because it would leave incomplete objects ...

  1. #1

    Default Atomic directory swap

    I'm implementing an autoupdate system and I'm running into a snag as far
    as what to do once the update has finished downloading. The obvious
    approach is to move or delete the old program and replace it with the
    new one. However, I don't want anything bad to happen if the user's dog
    trips over his power cord at exactly the wrong instant. An additional
    complicating factor is that the program is actually an input manager, so
    renaming the old version, moving the new version, and deleting the old
    version wouldn't work (I think), because it would leave incomplete
    objects lying around where the user won't normally venture.

    I was helpfully pointed to the function FchangeObjects(), but it
    appears to only work on files, not directories.

    The rename() function replaces directories and seems like it ought to be
    atomic, but then I discovered that it refuses to delete a directory that
    isn't already empty.

    Does anyone have an idea of how to accomplish this? Something that can
    nicely exchange the folders is preferred. Something that can exchange
    them with the possibility of a mess, but with the ability to clean up
    the mess afterwards would be ok too, if a clean atomic swap isn't
    possible.

    If neither one is possible, suggestions for what technique to use in
    order to leave the shortest possible window of opportunity for
    destruction are also welcome.

    Thanks in advance.
    Michael Guest

  2. #2

    Default Re: Atomic directory swap

    In article <mail-5D35E5.22411405022004localhost>,
    Michael Ash <com> wrote:
     

    That was not my understanding; what leads you to believe that? You do know that
    the two objects you are trying to exchange have to be on the same volume (and
    that the file system has to support the exchange operation)?

    meeroh

    --
    If this message helped you, consider buying an item
    from my wish list: <http://web.meeroh.org/wishlist>

    Miro Guest

  3. #3

    Default Re: Atomic directory swap

    In article <mit.edu>,
    Miro Jurisic <org> wrote:
     
    >
    > That was not my understanding; what leads you to believe that? You do know
    > that
    > the two objects you are trying to exchange have to be on the same volume (and
    > that the file system has to support the exchange operation)?[/ref]

    Two things lead me to believe that.

    First, the doentation talks about "files", and doesn't mention
    directories. Of course, it could be a simple 'everything is a file'
    thing, and it never says directories are forbidden.

    Second, when I actually tried it on my directories, it returned error
    -50. Here's my code:

    NSURL *newURL = [NSURL fileURLWithPath:newTempPath];
    NSURL *oldURL = [NSURL fileURLWithPath:inputManagerPath];
    FSRef newRef, oldRef;
    if(!newURL || !oldURL ||
    !CFURLGetFSRef((CFURLRef)newURL, &newRef) ||
    !CFURLGetFSRef((CFURLRef)oldURL, &oldRef))
    return; /**** FIX ME ****/

    OSErr err = FchangeObjects(&newRef, &oldRef);
    if(err != noErr)
    return;

    The URLs are valid URLs to directories. I've checked the FSRefs by
    converting them back to CFURLs in the debugger, and they're good. The
    folders I'm testing on are both on the same filesystem. One is in my
    build products directory, the other one is inside a folder inside the
    build products directory. The filesystem is HFS+.

    It's entirely possible I'm doing something wrong, and I'd love to know
    it if so.

    I had sudden inspiration to look at the MoreFilesX source, and sure
    enough, it has a wrapper function for FchangeObjects to simulate it
    if the underlying filesystem doesn't support it. One of the things it
    checks before emulating the exchange is that both files are actually
    files. If they're directories it aborts. That would seem to support the
    idea that FchangeObjects only works on files.
    Michael Guest

  4. #4

    Default Re: Atomic directory swap

    In article <mail-501F33.11082506022004localhost>,
    Michael Ash <com> wrote:
     

    Well, that s. It especially s because I was going to do the same kind of
    thing in the next couple of months.

    I think that the best thing you can do is make sure they are both on the same
    volume, and move one to Trash and the other to the desired location.

    meeroh

    --
    If this message helped you, consider buying an item
    from my wish list: <http://web.meeroh.org/wishlist>

    Miro Guest

  5. #5

    Default Re: Atomic directory swap

    In article <mit.edu>,
    Miro Jurisic <org> wrote:
     
    >
    > Well, that s. It especially s because I was going to do the same kind
    > of thing in the next couple of months.[/ref]

    I agree. It seems like this would be a good kind of facility to have. I
    wonder how apps whose doents are bundles manage to implement a save
    feature without risking the destruction or at least partial renaming of
    the doent.
     

    Sounds good to me. I'm going to slightly modify this approach and rename
    the old item first, then move the new one in, then finally trash it. I
    think that if I end up with a duplicate in the folder, one of them will
    load and the other one will simply fail, which is fine for me. And
    hopefully the interval will be short enough, since it's just moving
    files around, that nobody will ever run into the situation.

    Thanks for your advice.
    Michael Guest

  6. #6

    Default Re: Atomic directory swap

    In article <mail-8B5AD1.21373706022004localhost>,
    Michael Ash <com> wrote:
     
    >
    > Sounds good to me. I'm going to slightly modify this approach and rename the
    > old item first, then move the new one in, then finally trash it. I think that
    > if I end up with a duplicate in the folder, one of them will load and the
    > other one will simply fail, which is fine for me. And hopefully the interval
    > will be short enough, since it's just moving files around, that nobody will
    > ever run into the situation.
    >
    > Thanks for your advice.[/ref]

    Right, that approach makes sense for the kind of item you are installing.

    meeroh

    --
    If this message helped you, consider buying an item
    from my wish list: <http://web.meeroh.org/wishlist>

    Miro Guest

Similar Threads

  1. Swap image also swap link
    By Timothy Robertson in forum Dreamweaver AppDev
    Replies: 2
    Last Post: May 26th, 05:29 PM
  2. Replies: 1
    Last Post: July 4th, 12:23 AM
  3. inline assembler and atomic byte reversal operations
    By Rob in forum UNIX Programming
    Replies: 8
    Last Post: October 20th, 02:43 PM
  4. Is fwrite atomic or not?
    By Herouth Maoz in forum PHP Development
    Replies: 0
    Last Post: August 6th, 02:25 PM

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
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139