Source file XDGBaseDir.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
(** XDG basedir specification
@author Sylvain Le Gall
*)
open FilePath
open Unix
type filename = string
type dirname = string
type dirnames = dirname list
type t =
{
data_home: dirname;
data_dirs: dirnames;
config_home: dirname;
config_dirs: dirnames;
cache_home: dirname;
}
let default =
let getenv ?(default=fun () -> raise Not_found) var =
try
Sys.getenv var
with Not_found ->
begin
try
default ()
with Not_found ->
failwith (Printf.sprintf "Environment variable '%s' not defined" var)
end
in
let home =
getenv "HOME" ~default:(fun () -> (getpwuid (getuid ())).pw_dir)
in
let mk_var_gen f env win32 unix =
let lst =
if Sys.os_type = "Win32" then win32 else unix
in
getenv ~default:(fun () -> (f (Lazy.force lst))) env
in
let mk_var =
mk_var_gen make_filename
in
let mk_var_path env win32 unix =
path_of_string (mk_var_gen (fun s -> s) env win32 unix)
in
{
data_home = mk_var
"XDG_DATA_HOME"
(lazy [getenv "AppData"])
(lazy [home; ".local"; "share"]);
data_dirs = mk_var_path
"XDG_DATA_DIRS"
(lazy (getenv "ProgramFiles"))
(lazy "/usr/local/share:/usr/share");
config_home = mk_var
"XDG_CONFIG_HOME"
(lazy [home; "Local Settings"])
(lazy [home; ".config"]);
config_dirs = mk_var_path
"XDG_CONFIG_DIRS"
(lazy (getenv "ProgramFiles"))
(lazy "/etc/xdg");
cache_home = mk_var
"XDG_CACHE_HOME"
(lazy [home; "Local Settings"; "Cache"])
(lazy [home; ".cache"]);
}
let dir_exists ?(exists=false) dn =
not exists || (Sys.file_exists dn && Sys.is_directory dn)
let file_exists ?(exists=false) fn =
not exists || Sys.file_exists fn
let mk_fun data test comb =
let filter_comb lst ?exists a =
List.fold_right
(fun fn acc ->
let fn' = comb fn a in
if test ?exists fn' then
fn' :: acc
else
acc)
lst
[]
in
let user_dir ?(xdg_env=default) ?exists a =
let home, _ = data xdg_env in
let fn = comb home a in
if test ?exists fn then
fn
else
raise Not_found
in
let system_dirs ?(xdg_env=default) =
let _, system = data xdg_env in
filter_comb system
in
let all_dirs ?(xdg_env=default) =
let home, system = data xdg_env in
filter_comb (home :: system)
in
user_dir, system_dirs, all_dirs
module Make (E: sig val data: t -> dirname * dirname list end) =
struct
let user_dir, system_dirs, all_dirs =
mk_fun E.data dir_exists (fun fn () -> fn)
let user_file, system_files, all_files =
mk_fun E.data file_exists FilePath.concat
end
module Data = Make (struct let data t = t.data_home, t.data_dirs end)
module Config = Make (struct let data t = t.config_home, t.config_dirs end)
module Cache =
struct
let user_dir ?(xdg_env=default) ?exists () =
let dn = xdg_env.cache_home in
if dir_exists ?exists dn then
dn
else
raise Not_found
let user_file ?xdg_env ?exists fn =
let fn' = FilePath.concat (user_dir ?exists ?xdg_env ()) fn in
if file_exists ?exists fn' then
fn'
else
raise Not_found
end
let mkdir_openfile file_open fn =
let dn = dirname fn in
FileUtil.mkdir ~parent:true dn;
file_open fn