PxPlus User Forum

Twitter Twitter Twitter

Author Topic: unique file name  (Read 1133 times)

Thomas Bock

  • Diamond Member
  • *****
  • Posts: 177
    • View Profile
unique file name
« on: March 18, 2019, 05:52:45 AM »
Is this loop save to generate a unique file name in a multi user environment?
A customer reports issues which can only occur if that loop creates the same file for 2 processes.

Code: [Select]
mask$      = "0000000"
extension$ = ".txt"
filePart1$ = path$ + "spoolfile"
counter    = 0
repeat
checkFile$ = filePart1$ + str(++counter : mask$) + extension$
serial checkFile$, err = *continue
break
until 0

Mike King

  • Diamond Member
  • *****
  • Posts: 3810
  • Mike King
    • View Profile
    • BBSysco Consulting
Re: unique file name
« Reply #1 on: March 18, 2019, 09:37:46 AM »
The loop, and subsequent OPEN (which I'm assuming you have) may not work properly if you have a prefix set. 

For example if you have a prefix of "dir1/ dir2/" and USER1 doesn't have create/write permissions on dir1 but USER2 does.  The SERIAL directive for User1 will make the file in dir2 (since it cannot add to dir1) yet USER2 will make it in dir1.  A subsequent OPEN directive could then result in both opening the dir1 file.  Similar situations can happen if the users have different prefix values or current directories causing the creation of the file in one directory yet the OPEN finding the file created by another process.

As a rule of thumb we always recommend that you include the PID ('STR(TCB(89))') in any temporary file name.  This avoids any potential problems with permissions or path search rules.


Mike King
President - BBSysco Consulting
eMail: mike.king@bbsysco.com