Looking for a simple File Locking

Just starting out? Need help? Post your questions and find answers here.
User avatar
charvista
Addict
Addict
Posts: 943
Joined: Tue Sep 23, 2008 11:38 pm
Location: Belgium

Looking for a simple File Locking

Post by charvista »

I am thinking/searching for a way to LOCK a FILE within PB (Ascii or Binair file doesn't matter), so nobody else can modify that file while my current program is busy with it, as a precaution.
Windows solution only needed, so an API would be perfect.
And of course, a way to UNLOCK it afterwards. :wink:
Any idea?
I think that it would be obvious enough to use the APIs LockFile_() and UnlockFile_(), but I am not sure of the parameters...
- Windows 11 Home 64-bit
- PureBasic 6.10 LTS (x64)
- 64 Gb RAM
- 13th Gen Intel(R) Core(TM) i9-13900K 3.00 GHz
- 5K monitor with DPI @ 200%
User avatar
skywalk
Addict
Addict
Posts: 3995
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: Looking for a simple File Locking

Post by skywalk »

No api, if you move it or rename it and hide it. :wink:
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
User avatar
kenmo
Addict
Addict
Posts: 1967
Joined: Tue Dec 23, 2003 3:54 am

Re: Looking for a simple File Locking

Post by kenmo »

Can't you use OpenFile/CloseFile and just not make any changes?

On Win XP here, if I use OpenFile() on a text file, I cannot delete, rename, or edit it from Windows or other programs. Just tested it to be sure.
User avatar
charvista
Addict
Addict
Posts: 943
Joined: Tue Sep 23, 2008 11:38 pm
Location: Belgium

Re: Looking for a simple File Locking

Post by charvista »

@skywalk
Very smart idea, but then, when the file is seen by other users as non-existent, it will be created, and then we have a conflict when the first user closes the file. But I might think about it. Your idea is much appreciated, thanks!

@kenmo
Here Win7. If I open a text file, it appears to be locked to the other users, indeed. I was not aware of that.
I opened a file in PB, and while it is open, I tried to view a file through Total Commander: it can. Then I tried to edit it: it can be loaded, so I could edit it. I cannot save it however. But if I wait until it is free from PB, then the changes I made in PB on that file will be lost! So, it is not good. It must be impossible to load it.
- Windows 11 Home 64-bit
- PureBasic 6.10 LTS (x64)
- 64 Gb RAM
- 13th Gen Intel(R) Core(TM) i9-13900K 3.00 GHz
- 5K monitor with DPI @ 200%
User avatar
skywalk
Addict
Addict
Posts: 3995
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: Looking for a simple File Locking

Post by skywalk »

Sorry, I assumed you would only spend a few moments with the file. Then return it to normal access.
I prefer not to lock files since it can be defeated.
Instead whisk it out of the way.
If you are the arbiter of these files and there is an identical one in its place, then open it and decide what you need to merge or delete.
Also, 'date_time_ms' stamping suffixes to your files prevents a duplicate file creation.
Pud
User
User
Posts: 28
Joined: Mon Dec 28, 2009 2:23 am

Re: Looking for a simple File Locking

Post by Pud »

Procedure lock(name$)
Define h,result

h=CreateFile_(name$,0,0,0,#CREATE_NEW,#FILE_ATTRIBUTE_NORMAL,0)
If h<>-1
CloseHandle_(h) : result=1
EndIf
ProcedureReturn result

EndProcedure

Procedure unlock(name$)

DeleteFile(name$)

EndProcedure

The above can be used to reliably lock files and records without interfering with the actual file you wish to use.
It works by trying to create a stub file that will fail if the file already exists.

So, if you have a file called "text.txt" that you wish to lock use...

if lock("test.txt"+".LOC")
;do something with locked file
unlock("test.txt"+".LOC")
else
;no can do try later
endif

This assumes the file is to be accessed only through your software. If this is what you mean?
User avatar
charvista
Addict
Addict
Posts: 943
Joined: Tue Sep 23, 2008 11:38 pm
Location: Belgium

Re: Looking for a simple File Locking

Post by charvista »

@Pud
Welcome to the forum Pud, I see it is your very first post!
The creation of a doubled file with .LOC seems very interesting.
This assumes the file is to be accessed only through your software. If this is what you mean?
Yes, it is what I mean.
Thank you very much, I will test your idea and post an answer next week.

@Skywalk
The suffix of the timestamp is similar to Pud's idea with .LOC, but was not sure how to use that... I have always worked as a single-user, and thinking multi-users for access to a same file is rather new to me. Even myself trying opening the same file while calling another procedure is the same "danger", and I must be careful not to be locked by myself!!!
- Windows 11 Home 64-bit
- PureBasic 6.10 LTS (x64)
- 64 Gb RAM
- 13th Gen Intel(R) Core(TM) i9-13900K 3.00 GHz
- 5K monitor with DPI @ 200%
TerryHough
Enthusiast
Enthusiast
Posts: 781
Joined: Fri Apr 25, 2003 6:51 pm
Location: NC, USA
Contact:

Re: Looking for a simple File Locking

Post by TerryHough »

I have a group of files that I store in a "container" pack file. While any of those files are open and in use no other user should be able to access or modify those files.

When that file is opened I unpack the enclosed file for action and lock the "container"

Code: Select all

SetFileAttributes(packfilename,#PB_FileSystem_ReadOnly) ; Lock the file(
The routine that opens and unpacks the "container" will not unpack a readonly file.

When the activity on the contained files is complete I unlock the "container" pack file

Code: Select all

SetFileAttributes(packfilename,#PB_FileSystem_Normal) ; Unlock the file

and pack the group of files back into the "container" pack file.

This simple method has also provided me another bit of security. I store the authorized access key in the "container" file as well. Then I can require the correct access key to be entered before I unpack any of the contained files.

One downside... if the system locks up or the program bombs out while the "container" is locked it will remain locked. That means you must have some method of unlocking the file available at the user level beyond that of Window's ability to remove the "readonly" attribute.

Maybe something like this idea will be of use to you.
fluent
User
User
Posts: 68
Joined: Sun Jan 24, 2021 10:57 am

Re: Looking for a simple File Locking

Post by fluent »

This way of locking a file for exclusive access seems to work for any files, except if they are located within the "Program Files" directory.
(I can still open the text file in Notepad)
Why?

Code: Select all


name$ = #PB_Compiler_Home + "\SDK\Readme.txt"

fileH = CreateFile_(name$,
                    #GENERIC_READ ,
                    0, ; For Exclusive access
                    0,
                    #OPEN_EXISTING,
                    #FILE_ATTRIBUTE_NORMAL,
                    #NULL);

OpenWindow(0, 100, 100, 140, 80, "")

Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow 

User avatar
Tenaja
Addict
Addict
Posts: 1949
Joined: Tue Nov 09, 2010 10:15 pm

Re: Looking for a simple File Locking

Post by Tenaja »

fluent wrote:This way of locking a file for exclusive access seems to work for any files, except if they are located within the "Program Files" directory.
(I can still open the text file in Notepad)
Why?
Are you running the commands in administrator mode? You need that to write to program files, iirc.
ZX80
Enthusiast
Enthusiast
Posts: 331
Joined: Mon Dec 12, 2016 1:37 pm

Re: Looking for a simple File Locking

Post by ZX80 »

Code: Select all

_lopen_(fname$,#OF_SHARE_DENY_READ)

Repeat
  Delay(10)
ForEver
fluent
User
User
Posts: 68
Joined: Sun Jan 24, 2021 10:57 am

Re: Looking for a simple File Locking

Post by fluent »

ZX80 wrote:

Code: Select all

_lopen_(fname$,#OF_SHARE_DENY_READ)

Repeat
  Delay(10)
ForEver
Does not seem to have any effect (I can still open readme.txt in notepad)

Code: Select all

fname$ = #PB_Compiler_Home + "\SDK\Readme.txt"

_lopen_(fname$,#OF_SHARE_DENY_READ)

Repeat
  Delay(10)
Forever
fluent
User
User
Posts: 68
Joined: Sun Jan 24, 2021 10:57 am

Re: Looking for a simple File Locking

Post by fluent »

Tenaja wrote:
fluent wrote:This way of locking a file for exclusive access seems to work for any files, except if they are located within the "Program Files" directory.
(I can still open the text file in Notepad)
Why?
Are you running the commands in administrator mode? You need that to write to program files, iirc.
Really? I was not aware that admin mode was required to open a file in "Program Files" for exclusive read-only access. You may be right, but would you have any references / msdn articles?
Post Reply