VSCode open file to be locked? - windows

All
is it possible to have file locked when opened by VSCode ?
any extensions?
so if I check file by handle64 from sysinternals, it will tell me something like
> handle64.exe -a c:\XXX.txt
code.exe pid: 123 type: File c:\XXX.txt
Windows solution would be fine, cross-platform would be the best

The Windows-specific file API (CreateFile) has a number of access and sharing options, so yes, technically it would be possible. But that doesn't mean it would be desirable. There are a number of use-cases where it is quite desirable to allow another process to read, change or delete a file while it is "open" (really visible) in vscode.
I put "open" in quotes because to achieve the behavior it currently has, vscode probably closes the file as soon as it's done reading it, intentionally avoiding the sort of behavior you seem to be asking for. Since your question deals with a specific solution, rather than the problem motivating you, it's hard to provide more detail.

Related

Bash on OSX: How to determine if network file (AFP) is in use?

I've got a bash script that runs on OSX.
It needs to manipulate some files on a network-share (AFP share on a Synology NAS).
Unfortunately those files are sometimes still being written when the script runs.
How do I determine if the file is in use or not ?
The normal method is by using "lsof", but that doesn't seem to work on network files if the other user is coming from another client on the LAN.
I could just attempt to rename the file. I suppose that will fail if the file is in use, but that is far from elegant.
Anybody have a better solution ?
This is not a generally solvable problem. The typical solution is to write the file to a temporary location and then move it to the final processing directory (since move within a filesystem is generally atomic). If you cannot control how or where the file is written, then you are left with heuristics, particularly doing things like looking at the file and seeing if it hasn't grown in "awhile," but none of these are particularly good compared to separating the writing from the enqueuing.
Are the other potential accesses being done by arbitrary programs or can it be assumed that it's being done by other instances of your program running on other clients?
If the file is private to your program, then all instances of your program can participate in a cooperative locking scheme. You might use the lockfile command, for example. Be very sure to clean up your lock files even in the face of signals/exceptions. You can use the trap built-in command to help with that. See here for an explanation.

Prevent all non system shell extensions from loading in a GetOpenFileName, CFileDialog, IFileOpenDialog, etc

I'm looking for a programmatic way to prompt the user for filenames using the explorer shell and I only want system shell extensions loaded.
The reason I'm looking for this feature is that I want to eliminate 3rd party shell extension as a possible cause for crashes and other nondeterministic behavior.
Ideally, there's a flag somewhere that I missed that I can pass in to a function that means something like a "safe mode" for an explorer instance where it only loads the system shell extensions. This seems like such an essential feature and I've spent a good amount of time poring over docs to find it to no avail.
I've looked through the API docs for CFileDialog, GetOpenFileName and IFileOpenDialog. It looks like the only way to prevent the loading of non system shell extensions is by doing some sort of global hackery via registry twiddling or by using software utilities. Neither of these are satisfactory for well-behaved apps.
I know that I can use the "old style" file dialogs that aren't explorer based, but my users would kill me if I forced that on them. :)
The only way I can think of around this is to (ugh) reinvent the wheel and write an explorer-like file open dialog.

Programatic file associations in OS X

Is it possible to use an Apple Script or a Unix executable to associate a file type with an app?
My problem is I'm using File Vault and it forgets previous associations (it's a well known bug so it seems). For instance, I like to use Flying Meat's Acorn for my graphics files rather than Preview. I can Cmd-I, change all, and while it sticks for one file the next time I reboot it's forgotten the association for everything else.
This has been driving me nuts for literally years. Any ideas?
I use Magic Launch and would highly recommend it. Not only can you associate programs with certain file types, but you can establish complex rules for when to use what app.
Or, to better answer your original question, you could try this:
defaults write com.apple.LaunchServices LSHandlers -array-add \
"<dict><key>LSHandlerContentType</key><string>public.png</string><key>LSHandlerRoleAll</key><string>com.flyingmeat.acorn</string></dict>"

How do I hook into other programs in Windows?

Can anyone explain how does one program hook into and modify behavior of other programs in Windows?
How is it even possible? Don't windows programs protect themselves from other programs going into their memory, etc? (I don't know the internals how it works so I just said "into their memory" -- I bet it's more complex than that.)
Also does modern Windows like Windows 7 still allow it?
Thanks, Boda Cydo
There are several different ways to hook into and modify the behavior of other programs.
For example, you can directly write to another program's memory (WriteProcessMemory) or you can inject a thread into another program's memory (CreateRemoteThread). This presumes you have some rights to control that other program.
You can also inject a window hook via SetWindowsHookEx. This presumes you are running in the user's session at the same or higher integrity level of the program you are injecting into.
This is still allowed for several reasons. Without a way to modify behavior of other programs you would not be able to implement a debugger. Windows hooks are used by testing programs, accessibility programs, programs that change the look and feel of Windows, etc.
Imagine an application that saves data to file X.txt
you can grab the x.txt contents, and attempt to find a difference in the saved x.txt against the current x.txt, once it changes you can have an event fire knowing that program X modified its x.txt file.
You can do this on a lower level but the concept remains the same, (monitor something for change).

Adding SMB to Windows, how safe is this?

I came across a small hack, which claims it enables smb:// on windows.
The complaint was that things like text weren't working.
While true that you can use file:///// in your url's, the user wanted to use smb:// so that it's cross-platform.
The hack goes as follows:
1) Create this Reg file, save and execute it:
REGEDIT4
[HKEY_CLASSES_ROOT\smb]
#="URL:smb Protocol"
"URL Protocol"=""
[HKEY_CLASSES_ROOT\smb\shell]
[HKEY_CLASSES_ROOT\smb\shell\open]
[HKEY_CLASSES_ROOT\smb\shell\open\command]
#="\"C:\\smb.bat\" \"%1\""
And then create the smb.bat file in your C-folder, containing this:
#echo off
cd C:\
set url=%~dpnx1
explorer \%url:~7%
exit
My question: how safe is this, and any other thoughts on the matter? Besides the file:///// thing I mean.
To me, it looks damn dangerous because it allows any website to place "\\RESOURCENAME" URLs, which will work regardless of context, and smb.bat will be called if you click such a link. I don't entirely understand the batch syntax (the ~ part especially) but it seems to me it's possible to pass any kind of argument to explorer.exe.
There's probably no immediate danger because it's very unlikely an outside attacker would guess you have this set up. Still, safe it's not.
I'd much rather have the server detect windows clients output \servername\path scheme for those and smb:// for everything else.
Not to mention, SMB isn't the only protocol that uses that syntax, so does any other filesystem such as WebDAV. Somewhat clever idea though, and I wish that smb:// worked too.

Resources