-
-
Notifications
You must be signed in to change notification settings - Fork 741
Add function for getting the current process path. #1224
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
The edit: And wouldn't |
Huzzah! I can't wait until this gets in (it really should, once minor issues are fixed). |
Hmm, why isn't the comments shown inline? |
Replaced
I don't know, what do other thinks? |
I've replied to the forum post about this. Please consider before merging this. |
Since this function will always return the same value, should it be cached on the first call? |
|
||
@trusted string getProcessPath () | ||
{ | ||
auto buffer = new wchar[MAX_PATH + 1]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note: this will fail for UNC paths that exceed MAX_PATH. But the same is true for many other functions in Phobos.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You should be allocating a stack buffer here, not a heap buffer that you just throw away. Same goes for all of these actually, better to allocate after you know the length.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@CyberShadow right, I could try and allocate more if needed. But how much and how many times? What exception would be thrown if it fails?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@yebblies I guess it would work for the Windows version. But for Mac OS X, I can't return a slice from a stack allocated buffer?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right, you'll need to dup it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@CyberShadow right, I could try and allocate more if needed. But how much and how many times? What exception would be thrown if it fails?
I think it's not the task of this module to worry about memory limits.
From the documentation of GetModuleFileName:
If the buffer is too small to hold the module name, the string is truncated to nSize characters including the terminating null character, the function returns nSize, and the function sets the last error to ERROR_INSUFFICIENT_BUFFER.
So, there you have it. If the first call fails, you'll know exactly how much buffer to allocate.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Erm, sorry, I misread the docs there. It does not return how much you need to allocate.
In cases like this, the usual thing to do is to keep doubling the buffer's length until it's large enough to fit the data.
I needed this the other day, it will be good to have it in phobos. |
@kyllingstad That might be a good idea. |
Hmm, this commit 19512232c19cf2cdb15d1aecfc145a180bbd13fa didn't show up. |
Windows version will fail until this is merged: dlang/druntime#466 |
I failed to find a value for |
Never mind, found it. |
Does anyone have FreeBSD and can help me with its implementation? |
|
||
@trusted string getProcessPath () | ||
{ | ||
static wchar[MAX_PATH + 1] stackBuffer; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a TLS buffer, not a stack buffer (which is not necessarily a problem, but the name is wrong).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right.
EDIT: Actually turns out that's what you're doing. I suck at github :) Hmm I read on stackoverflow that the current way you are doing getProcessPath on FreeBSD relies on procfs. What you can do to always have it supported is this: sysctl CTL_KERN KERN_PROC KERN_PROC_PATHNAME -1 I can't verify that it works though. Maybe the build system can try it out? Source: http://stackoverflow.com/questions/1023306/finding-current-executables-path-without-proc-self-exe |
Guess I have to install FreeBSD. |
unittest | ||
{ | ||
assert(isFile(getProcessPath())); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please leave a trailing newline at the end of the file.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is that?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some historical stupidity about include files. Reality is some tools complain if you don't have it, so you might as well.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's almost the most stupid thing I have ever heard of. But ok, I can do that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One of those tools is Git itself, by the way, and several Unix-y text editors add it by default.
I would be happy to see this in phobos, even without freebsd support. Forcing users on freebsd to implement this manually would still be better than forcing everyone to implement it manually. |
As @jpf91 said:
|
Most of If I was looking for this function (which I was, a couple of months ago) then I would look in |
Argh, what does |
I also think we should rename it to |
The path of the working dir of the current process. Anyway I didn't even know
It's right there in the sentence, |
Now it's named |
I don't see how this name is any better. A process is a different thing from an executable. This function gives you the name of the file that contains the image used to create the process.
My point is there is potential for confusion with that name. A better name can eliminate this potential. (processExecutablePath?) This function is closely related to |
I see. Yeah, "process" might be a bad name. I've had a look at some other languages, but they have bizarre ways of getting the path, e.g.: .net: Java really takes the cake here for brevity. Maybe call it |
To add a couple of more:
I think |
Most functions in Phobos are short, take for example If you have to go for the longer version I'd favor |
I'll change to whatever name you agree on. |
My vote goes to |
The .net |
@yebblies, @AndrejMitrovic: Can we please not do the I suggested I think the function definitely belongs in @jacob-carlborg: Sorry that we are cluttering up your pull request by arguing about this. This has to be the pull request with the greatest discussion/LOC ratio ever. :-) |
168 comments, 169 with this :). As long as you can come to an agreement. |
@kyllingstad I'm happy with 'this' instead of 'get'. As I said before this function is about the executable, not the process. If this goes in the same place as |
Well |
Ok so then would |
@AndrejMitrovic: Semi-happy, but I guess that'll have to do. ;-) |
I would like an answer from @yebblies as well before chaining the name. |
@jacob-carlborg I have no problems with |
Then |
Renamed to |
Any chance this can be pulled so we can leave this behind us? |
Add function for getting the current process path.
Thanks for sticking with it. Yay for teamwork. |
Thanks for merging this. I added an enhancement request for this in http://d.puremagic.com/issues/show_bug.cgi?id=11020. |
is(typeof(getProcessPath)