How can I simulate a "locked" file (one which has a write lock)
Asked Answered
J

4

21

I am trying to debug a problem where users occasionally have locked files which they try to open. The code appears to have correct exception handling but users are still reporting seeing error messages. How can I simulate a locked file so that I can debug this myself?

EDIT: For Windows.

Janeth answered 2/5, 2011 at 17:51 Comment(4)
What error messages are they seeing? Do you know for sure that they have locked files when they get the error message?Trawl
@Ralph, no I don't know if it's a locked file but it seems the most likely. I was also thinking it may be a file which was accessed over a network which lost a connection. This is just my first step in debugging.Janeth
Also check to see if there are any intermittent system processes which run during that time which may be locking the folder.Trawl
Some very useful answers to the same question on Superuser, including one to just invoke notepad > fileToLock from the command line (though it'll overwrite the file contents of course, but useful for simulating locks).Vanvanadate
D
7

depends, but in case, MS word locks
if you are wonderig if your application lock files and it do not relase locks:
just modify a bit your aplication (to create a testapp) and never close the file (and keep it runnig)

Debose answered 2/5, 2011 at 18:1 Comment(2)
Word or Excel were going to be my suggestions, so an upvote well and truly deserved... :)Harakiri
Luis, thank you for your help. I will give this method a try. :)Janeth
C
40

try this:

( >&2 pause ) >> yourfile.txt

>> opens yourfile.txt in append mode

see this for a reference

Careen answered 11/5, 2015 at 18:57 Comment(2)
I really like this. Is easier than use big MS-Software to lock my fileChamplain
With PowerShell powershell -c ( >&2 pause ) >> yourfile.txtVenetis
D
7

depends, but in case, MS word locks
if you are wonderig if your application lock files and it do not relase locks:
just modify a bit your aplication (to create a testapp) and never close the file (and keep it runnig)

Debose answered 2/5, 2011 at 18:1 Comment(2)
Word or Excel were going to be my suggestions, so an upvote well and truly deserved... :)Harakiri
Luis, thank you for your help. I will give this method a try. :)Janeth
C
1

I used LockFileEx function from the Windows API to write a unittest in Python. This worked well for me (shutil.copy() with a locked target fails).

import win32con
import win32file
import pywintypes

p = "yourfile.txt"
f = file(p, "w")
hfile = win32file._get_osfhandle(f.fileno())
flags = win32con.LOCKFILE_EXCLUSIVE_LOCK | win32con.LOCKFILE_FAIL_IMMEDIATELY

win32file.LockFileEx(hfile, flags, 0, 0xffff0000, pywintypes.OVERLAPPED())

See: https://msdn.microsoft.com/en-us/library/windows/desktop/aa365203%28v=vs.85%29.aspx

Champlain answered 14/9, 2016 at 7:56 Comment(0)
N
0

I am using this command prompt

type yourfile.txt | more

to insert inside yourfile.txt a very long text of "few pages" and want to pipe via | more to batch the insertion. However the file seems to lock, any reason why?

Non answered 10/12, 2019 at 11:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.