File admin script both Linux and Windows compatible - ruby

I've written a command-line application that allows me to copy certain files to and from the Desktop. And up until now it's been fine since I was on Ubuntu, but now I'm adding a Windows 7 machine that doesn't play well with my current implementation.
My current solution is to duplicate my scripts and hard code paths.
CANVAS_DIR = "#{HOME}/GitHub/canvas"
gets replaced with:
CANVAS_DIR = "C:\\Users\\wurde\\GitHub\\canvas"
I've read that I should avoid hard coding paths and instead use joins. Is that the best way? If so, how does that look implemented?

Looking at the File API in Ruby I think I have the a working join solution. The important bit was to include the home directory via environment variable.
CANVAS_DIR = File.join(ENV['HOME'], 'GitHub', 'canvas')

Related

Is it possible to create a binary that can run on both windows and linux

As the title said, with any possible tricks or bugs,can we create a binary file can run both on Linux and Windows without any change?
(The file must be directly created by a compiler)
I know it's nearly impossible,but with some tricky methods,is that possible?

Does Windows have an equivalent to Linux/FreeBSD's `RESOLVE_BENEATH` and `O_RESOLVE_BENEATH`?

I want to open a file, while ensuring that no component of the path goes outside a certain directory.
On Linux, I can accomplish this using openat2 and RESOLVE_BENEATH, and on FreeBSD there is the O_RESOLVE_BENEATH flag with openat.
Is there any way to achieve the same effect on Windows? Note that this needs to be on a per-call basis — globally restricting my process to a given directory is not useful.

bizarre behavior of system() in Ruby

I have set up shuffle_play.rb in Ruby by Example to work on Windows, with mpg123 instead of ogg123. The critical part is a method called play_file, which initially I wrote like this
def play_file(file)
system("mpg123 \"#{file}\"")
end
I have mpg123 in the same directory as my script ... it didn't work. But this does work:
def play_file(file)
system("mpg123.exe \"#{file}\"")
end
I reckon it's because I don't have working directory in %PATH% (and indeed the problem goes away when I add it) but even then I don't know enough about Windows to know the difference. Could someone explain the rationale for this?
Probably the examples assume that you're on a *nix variant such as Linux or Mac. In those Operating Systems, the program is called mpg123, because those OS, don't care about extensions, the just check that the file has an executable attribute
On windows things are very different. Windows decides if something is a program depending on the extension (.exe, .com, .bat, .cmd, etc.). So the program in windows has to be called mpg123.exe. If you open a command line on windows, you can run the program without specifying the extension, as windows automatically tries the different extensions. This behaviour of trying different extensions happens ONLY in the command line and not when you try to invoke a program from another one.
There a environment variable called PATHEXT that list in which order windows tries the different extensions. On my computer that list is:
C:\Windows\System32>echo %PATHEXT%
.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC;.RB;.RBW
I hope it was clear. And a suggestion if you want to code in ruby, install Linux or get a Mac.

Why don't programs on Windows usually add themselves to the PATH variable?

I couldn't even count the number of times I've had to manually add a directory. Is there a security reason for it, or do developers/installers not think about it?
Usually a program is started via a link, which has the path to the executable hard coded within it. There's no need to add to the general $PATH unless the program will be commonly executed from a command line.
What, exactly, are you trying to do? Windows, not being a relic of the steam age, has a number of built in features that usually make fooling with the Path environmental variable unnecessary.

Mysterious, Native "A" Registry Key with Path: Registry\A

I recently wrote a native NT registry editor for Windows, and ran it on Windows 7. To my surprise, in addition to the two standard root keys, MACHINE and USER, that are present on Windows XP, there was also a mysterious key named "A", that cannot be opened in any way, whether by permission changes or backup privileges or otherwise:
Does anyone know what this key is for? I don't believe it's for any software, because it was there before I installed anything on the machine, and I believe I saw it on another fresh installation as well. It's rather very suspicious, and I'm curious as to why it's there. (If I'm curious enough, I might end up writing a driver to open it up without a privilege check, to see what happens!)
(I wasn't sure whether to put this on SuperUser or StackOverflow, since I think it could go in either one. I could be wrong, though; sorry if this isn't the appropriate place.)
Edit:
If forgot to say, I don't believe you can even see this key using the Win32 API, like RegOpenKey -- you have to use the native API like NtEnumerateKey instead.
Here is the comment from one of our driver writers: "DISCACHE.sys driver seems to be caching system file attributes and using \REGISTRY\A in an undocumented way. This driver is part of the kernel so it can load any hive wherever it wants."
Interesting...
The key indeed can be opened with a relative path, but not with an absolute path.
And it seems to contain information about all file systems and whatnot. Looks mysterious, indeed...

Resources