Ask a Question related to PERL Beginners, Design and Development.

  1. #1

    Default Unique file names.

    I have a bit of a problem and I'm wondering if there is a better solution.
    I have a Perl program (of course) which reads STDIN and breaks it into
    separate files based on a "report separator". I can construct an
    appropriate file name based on information in the "trailing" separator.
    I.e. I don't know the name of the report until the report is complete.
    What I am doing at present is using the routine 'tempfile' in the
    File::Temp package to create a unique file in the output directory. Once
    I get the report name from the "trailer" separator, I want to rename this
    file to the correct name. However, it is possible that there will be
    multiple, different reports coming in which have the same name. So, what I
    want to do is put a "sequence number" as the last node in the file name.
    This means that I must generate a test file name, then see if that file
    already exists. If it does, increment the sequence number, generate a new
    test file name and loop. Oh, did I mention that it is possible for this
    program to have several copies all running at the same time, possibly
    producing different reports with identical names? So I must worry about
    "race" conditions. My code, so far, looks like:

    # $fn contains the file name generated by 'tempname'
    # $report contains the report name
    my ($number,$test);
    for ($number=1;;$number++) {
    $test = "$report.$number";
    last if sysopen JUNK, $test, O_WRONLY|O_EXCL|O_CREAT;
    }
    close JUNK;
    mv($fn,$test);

    Using sysopen with those parameters, especially O_EXCL, is the only way
    that I can think to ensure that the name in $test does not exist and is
    created "atomically" so that no other incarnation of this program which
    happens to be running at the same time will exit the for loop with the
    same value in $test. I then close this file (now a 0 length file). I then
    use the "mv" function from File::Copy to rename my real data file to the
    new name, replacing the 0 length file.

    This is running on Linux. I don't know if the sysopen can guarantee the
    "atomicity" of sysopen on other platforms.

    All comments gratefully received.

    --
    Maranatha!
    John McKown

    John McKown Guest

  2. Similar Questions and Discussions

    1. Unique Form inserting into many tables using unique id
      I have a Registration Form that have 3 steps. The data could be inserted into many (4) tables. Some data corresponding to a one table (the main or...
    2. File paths and File names generated by Contribute
      I'm demo-ing Contribute and have a question. I have a some calendar pages to which I am linking. These are normally created incrementally, not...
    3. Specifying URL / file names in version 2
      In Contribute 2, I make a new page, and it writes a random file name for me. I want to specify the URL myself, but can't find the option to do it....
    4. cloning objects and creating unique names
      here's my dilema, i want to clone a model in the scene but i want to assign it a unique name. Each clone should have a beginning prefix to the...
    5. File Names in Blue
      Greetings -- Those are compressed files. By design, WinXP compresses files that do not get used, and, if you've left the default settings...
  3. #2

    Default Re: Unique file names.

    On 2/12/2004 9:53 PM, John McKown wrote:
    > I have a bit of a problem and I'm wondering if there is a better solution.
    > I have a Perl program (of course) which reads STDIN and breaks it into
    > separate files based on a "report separator". I can construct an
    > appropriate file name based on information in the "trailing" separator.
    > I.e. I don't know the name of the report until the report is complete.
    > What I am doing at present is using the routine 'tempfile' in the
    > File::Temp package to create a unique file in the output directory. Once
    > I get the report name from the "trailer" separator, I want to rename this
    > file to the correct name. However, it is possible that there will be
    > multiple, different reports coming in which have the same name. So, what I
    > want to do is put a "sequence number" as the last node in the file name.
    > This means that I must generate a test file name, then see if that file
    > already exists. If it does, increment the sequence number, generate a new
    > test file name and loop. Oh, did I mention that it is possible for this
    > program to have several copies all running at the same time, possibly
    > producing different reports with identical names? So I must worry about
    > "race" conditions.
    If your code is the only code accessing the file, the standard way to
    handle this is to create a lock file. Try to write a file to a specific
    place as a lock file; if it fails, another process has already created
    it, so you poll until you can create it. Then run your routine to get a
    filename, move the file, and delete the lock file so that the next
    process can run.

    Randy.


    Randy W. Sims Guest

  4. #3

    Default Re: Unique file names.

    John McKown wrote:
    >
    > I have a bit of a problem and I'm wondering if there is a better solution.
    > I have a Perl program (of course) which reads STDIN and breaks it into
    > separate files based on a "report separator". I can construct an
    > appropriate file name based on information in the "trailing" separator.
    > I.e. I don't know the name of the report until the report is complete.
    > What I am doing at present is using the routine 'tempfile' in the
    > File::Temp package to create a unique file in the output directory. Once
    > I get the report name from the "trailer" separator, I want to rename this
    > file to the correct name. However, it is possible that there will be
    > multiple, different reports coming in which have the same name. So, what I
    > want to do is put a "sequence number" as the last node in the file name.
    > This means that I must generate a test file name, then see if that file
    > already exists. If it does, increment the sequence number, generate a new
    > test file name and loop. Oh, did I mention that it is possible for this
    > program to have several copies all running at the same time, possibly
    > producing different reports with identical names? So I must worry about
    > "race" conditions. My code, so far, looks like:
    >
    > # $fn contains the file name generated by 'tempname'
    > # $report contains the report name
    > my ($number,$test);
    > for ($number=1;;$number++) {
    > $test = "$report.$number";
    > last if sysopen JUNK, $test, O_WRONLY|O_EXCL|O_CREAT;
    > }
    > close JUNK;
    > mv($fn,$test);
    >
    > Using sysopen with those parameters, especially O_EXCL, is the only way
    > that I can think to ensure that the name in $test does not exist and is
    > created "atomically" so that no other incarnation of this program which
    > happens to be running at the same time will exit the for loop with the
    > same value in $test. I then close this file (now a 0 length file). I then
    > use the "mv" function from File::Copy to rename my real data file to the
    > new name, replacing the 0 length file.
    >
    > This is running on Linux. I don't know if the sysopen can guarantee the
    > "atomicity" of sysopen on other platforms.
    As far as I know 'rename' is atomic, so:

    my $number;

    do {
    $number++;
    my $test = "$report.$number";
    } until rename $fn, $test;

    HTH,

    Rob


    Rob Dixon Guest

  5. #4

    Default Re: Unique file names.

    On Fri, 13 Feb 2004, Rob Dixon wrote:
    >
    > As far as I know 'rename' is atomic, so:
    >
    > my $number;
    >
    > do {
    > $number++;
    > my $test = "$report.$number";
    > } until rename $fn, $test;
    >
    > HTH,
    >
    > Rob
    My Perl book indicates that if a file with the name in $test exists, it is
    destroyed and replaced. I don't want to replace an existing file. Every
    other method other than sysopen seems to have this effect as well. I.e.
    rename, File::Copy "move", etc.

    --
    Maranatha!
    John McKown

    John McKown Guest

Posting Permissions

  • You may not post new threads
  • You may 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