1 /**
2  * Copyright (c) 2021, Timothy Stack
3  *
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  * * Redistributions of source code must retain the above copyright notice, this
10  * list of conditions and the following disclaimer.
11  * * Redistributions in binary form must reproduce the above copyright notice,
12  * this list of conditions and the following disclaimer in the documentation
13  * and/or other materials provided with the distribution.
14  * * Neither the name of Timothy Stack nor the names of its contributors
15  * may be used to endorse or promote products derived from this software
16  * without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ''AS IS'' AND ANY
19  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21  * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
22  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
25  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 #include "config.h"
31 
32 #include "paths.hh"
33 #include "fmt/format.h"
34 
35 namespace lnav {
36 namespace paths {
37 
dotlnav()38 ghc::filesystem::path dotlnav()
39 {
40     auto home_env = getenv("HOME");
41     auto xdg_config_home = getenv("XDG_CONFIG_HOME");
42 
43     if (home_env != nullptr) {
44         auto home_path = ghc::filesystem::path(home_env);
45 
46         if (ghc::filesystem::is_directory(home_path)) {
47             auto home_lnav = home_path / ".lnav";
48 
49             if (ghc::filesystem::is_directory(home_lnav)) {
50                 return home_lnav;
51             }
52 
53             if (xdg_config_home != nullptr) {
54                 auto xdg_path = ghc::filesystem::path(xdg_config_home);
55 
56                 if (ghc::filesystem::is_directory(xdg_path)) {
57                     return xdg_path / "lnav";
58                 }
59             }
60 
61             auto home_config = home_path / ".config";
62 
63             if (ghc::filesystem::is_directory(home_config)) {
64                 return home_config / "lnav";
65             }
66 
67             return home_lnav;
68         }
69     }
70 
71     return ghc::filesystem::current_path();
72 }
73 
workdir()74 ghc::filesystem::path workdir()
75 {
76     auto subdir_name = fmt::format("lnav-user-{}-work", getuid());
77     auto tmp_path = ghc::filesystem::temp_directory_path();
78 
79     return tmp_path / ghc::filesystem::path(subdir_name);
80 }
81 
82 }
83 }
84