* * *

Author Topic: RTL: FileCreateNew ()  (Read 512 times)

joho

  • New member
  • *
  • Posts: 47
  • Joaquim Homrighausen
    • ~/JoHo
RTL: FileCreateNew ()
« on: September 03, 2017, 07:47:43 am »
I may have missed it, but it'd nice if the RTL had FileCreateNew (). The intended behavior is like FileCreate, except that the call will fail if the file to be created already exists. This can, of course, be emulate by a call to FileOpen () followed by FileCreate () if FileOpen failed due to "File not found". But there are native OS functions for FileCreateNew () on a few platforms AFAIK.

molly

  • Hero Member
  • *****
  • Posts: 1799
Re: RTL: FileCreateNew ()
« Reply #1 on: September 03, 2017, 09:07:10 am »
Forgive my ignorance but what benefit would that have over using fileexists ?

Thaddy

  • Hero Member
  • *****
  • Posts: 4429
Re: RTL: FileCreateNew ()
« Reply #2 on: September 03, 2017, 10:21:58 am »
But we have that functionality in multiple ways, not only FileExists.
E.g. on linux we have https://www.freepascal.org/docs-html/rtl/baseunix/fpopen.html which can do exactly what you asked: if you include the O_Excl flag it will fail if the file already exists. E.g.
Code: [Select]
O_Creat or O_Excl
« Last Edit: September 03, 2017, 10:26:15 am by Thaddy »
"Logically, no number of positive outcomes at the level of experimental testing can confirm a scientific theory, but a single counterexample is logically decisive."

joho

  • New member
  • *
  • Posts: 47
  • Joaquim Homrighausen
    • ~/JoHo
Re: RTL: FileCreateNew ()
« Reply #3 on: September 03, 2017, 11:45:12 am »
Forgive my ignorance but what benefit would that have over using fileexists ?

There's no benefit, but it makes sense to be able to do it in one call (IMHO). I simulate it now by first doing a FileOpen and then if that fails, FileCreate. But it's nice to have it as an API function.

joho

  • New member
  • *
  • Posts: 47
  • Joaquim Homrighausen
    • ~/JoHo
Re: RTL: FileCreateNew ()
« Reply #4 on: September 03, 2017, 11:46:41 am »
But we have that functionality in multiple ways, not only FileExists.
E.g. on linux we have https://www.freepascal.org/docs-html/rtl/baseunix/fpopen.html which can do exactly what you asked: if you include the O_Excl flag it will fail if the file already exists. E.g.
Code: [Select]
O_Creat or O_Excl

But things like this should not require specific platforms, specific flags, or whatever ... if there's FileCreateNew () in the RTL, it can isolate OS-specific constructs from the programmer. Isn't that, after all, part of the reason for having complexity in the RTL instead of going directly to the OS specific stuff with IFDEFs ... ?

Thaddy

  • Hero Member
  • *****
  • Posts: 4429
Re: RTL: FileCreateNew ()
« Reply #5 on: September 03, 2017, 12:14:02 pm »
Yeah but then use FileExists.... That's cross-platform.
"Logically, no number of positive outcomes at the level of experimental testing can confirm a scientific theory, but a single counterexample is logically decisive."

Remy Lebeau

  • Sr. Member
  • ****
  • Posts: 323
    • Lebeau Software
Re: RTL: FileCreateNew ()
« Reply #6 on: September 06, 2017, 12:38:03 am »
Forgive my ignorance but what benefit would that have over using fileexists ?

It avoids a race condition.  After FileExists() is called, someone else could come along and create/delete/access the file before you have a chance to call FileOpen()/FileCreate() yourself, so you might end up with access errors and/or corrupted data.

On Windows, CreateFile() has flags for:

- creating a new file only if it doesn't already exist.
- always creating a file, truncating an existing file.
- opening a file only if it exists.
- always opening a file, creating a new file if one does not exist.

These operations are atomic at the OS layer.

Linux's open()/fopen() has similar flags available.

So best to let the OS handle it with a single API call when possible, rather than handle it manually with multiple RTL function calls.
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) open source project - Admin, Developer

molly

  • Hero Member
  • *****
  • Posts: 1799
Re: RTL: FileCreateNew ()
« Reply #7 on: September 06, 2017, 06:53:55 pm »
That is indeed a valid point Remy Lebeau. Thank you for the explanation.

 

Recent

Get Lazarus at SourceForge.net. Fast, secure and Free Open Source software downloads Open Hub project report for Lazarus