Looking for a simple File Locking
Looking for a simple File Locking
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.
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 solution only needed, so an API would be perfect.
And of course, a way to UNLOCK it afterwards.
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%
- PureBasic 6.10 LTS (x64)
- 64 Gb RAM
- 13th Gen Intel(R) Core(TM) i9-13900K 3.00 GHz
- 5K monitor with DPI @ 200%
Re: Looking for a simple File Locking
No api, if you move it or rename it and hide it.
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
Re: Looking for a simple File Locking
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.
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.
Re: Looking for a simple File Locking
@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.
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%
- PureBasic 6.10 LTS (x64)
- 64 Gb RAM
- 13th Gen Intel(R) Core(TM) i9-13900K 3.00 GHz
- 5K monitor with DPI @ 200%
Re: Looking for a simple File Locking
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.
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.
Re: Looking for a simple File Locking
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?
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?
Re: Looking for a simple File Locking
@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.
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!!!
Welcome to the forum Pud, I see it is your very first post!
The creation of a doubled file with .LOC seems very interesting.
Yes, it is what I mean.This assumes the file is to be accessed only through your software. If this is what you 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%
- PureBasic 6.10 LTS (x64)
- 64 Gb RAM
- 13th Gen Intel(R) Core(TM) i9-13900K 3.00 GHz
- 5K monitor with DPI @ 200%
-
- Enthusiast
- Posts: 781
- Joined: Fri Apr 25, 2003 6:51 pm
- Location: NC, USA
- Contact:
Re: Looking for a simple File Locking
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"
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
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.
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(
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.
Re: Looking for a simple File Locking
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?
(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
Re: Looking for a simple File Locking
Are you running the commands in administrator mode? You need that to write to program files, iirc.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?
Re: Looking for a simple File Locking
Code: Select all
_lopen_(fname$,#OF_SHARE_DENY_READ)
Repeat
Delay(10)
ForEver
Re: Looking for a simple File Locking
Does not seem to have any effect (I can still open readme.txt in notepad)ZX80 wrote:Code: Select all
_lopen_(fname$,#OF_SHARE_DENY_READ) Repeat Delay(10) ForEver
Code: Select all
fname$ = #PB_Compiler_Home + "\SDK\Readme.txt"
_lopen_(fname$,#OF_SHARE_DENY_READ)
Repeat
Delay(10)
Forever
Re: Looking for a simple File Locking
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?Tenaja wrote:Are you running the commands in administrator mode? You need that to write to program files, iirc.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?