[erlang-questions] Retrieving priv directory path

Garrett Smith g@REDACTED
Wed Oct 26 15:38:58 CEST 2011

2011/10/26 Håkan Mattsson <hm@REDACTED>:
> On Wed, Oct 26, 2011 at 6:50 AM, Garrett Smith <g@REDACTED> wrote:
>> Hi Nikola,
>> On Tue, Oct 25, 2011 at 6:42 PM, Nikola Skoric <nskoric@REDACTED> wrote:
>>> What would be universal way to get priv dir path?
>>> Currently I retrieve priv dir like this:
>>> priv_dir(App) ->
>>>    case code:priv_dir(App) of
>>>        {error, bad_name} ->
>>>            {ok, Cwd} = file:get_cwd(),
>>>            Cwd ++ "/" ++ "priv/";
>>>        Priv ->
>>>            Priv ++ "/"
>>>    end.
>>> First case is when I develop code, it does not work when I make a
>>> release. When I make a release, core:priv_dir returns {error,
>>> bad_name} because application:get_application returns something that
>>> core:priv_dir doesn't agree with.
>>> So, how do you do it?
>> I use this:
>> priv_dir(Mod) ->
>>    Ebin = filename:dirname(code:which(Mod)),
>>    filename:join(filename:dirname(Ebin), "priv").
> Keep in mind that this trick not necessarily will work if you are putting
> parts of your application in an archive. In that case you need to use
> code:priv_dir/1 or code:lib_dir/2 in order to find the correct priv dir.

Yes indeed regarding the archive! Don't use that example, though the
same approach that handles archives isn't that much more complicated.

Although this approach is a trick/hack, it works regardless of how the
code is started.

IMO it seems that code:priv_dir should not depend on how the code was
started -- since it knows where the code resides and (as far as I can
tell) finding priv relative to those bytes is reliable given OTP app
directory structures. Maybe I'm missing something though.


More information about the erlang-questions mailing list