···152152let config_dirs t = t.config_dirs
153153let data_dirs t = t.data_dirs
154154155155-let subdir base_dir name =
156156- let path = Eio.Path.(base_dir / name) in
157157- ensure_dir path;
158158- path
159159-;;
160160-161161-let config_path t name = subdir t.config_dir name
162162-let data_path t name = subdir t.data_dir name
163163-let cache_path t name = subdir t.cache_dir name
164164-let state_path t name = subdir t.state_dir name
165155166156let pp ?(brief = false) ?(sources = false) ppf t =
167157 let pp_source ppf = function
-70
xdg-eio/lib/xdge.mli
···250250 @see <https://specifications.freedesktop.org/basedir-spec/latest/#variables> XDG_DATA_DIRS specification *)
251251val data_dirs : t -> Eio.Fs.dir_ty Eio.Path.t list
252252253253-(** {1 Application-specific Paths} *)
254254-255255-(** [config_path t name] returns a path within the application's config directory.
256256-257257- This is a convenience function that creates a subdirectory within the
258258- application's configuration directory. The subdirectory is automatically
259259- created if it doesn't exist.
260260-261261- @param t The XDG context
262262- @param name The subdirectory name within the config directory
263263- @return Path to [{config_dir}/name]
264264-265265- {b Example:}
266266- {[
267267- let profiles_dir = Xdg_eio.config_path xdg "profiles" in
268268- (* Returns path to ~/.config/myapp/profiles/ *)
269269- ]} *)
270270-val config_path : t -> string -> Eio.Fs.dir_ty Eio.Path.t
271271-272272-(** [data_path t name] returns a path within the application's data directory.
273273-274274- This is a convenience function that creates a subdirectory within the
275275- application's data directory. The subdirectory is automatically created
276276- if it doesn't exist.
277277-278278- @param t The XDG context
279279- @param name The subdirectory name within the data directory
280280- @return Path to [{data_dir}/name]
281281-282282- {b Example:}
283283- {[
284284- let db_dir = Xdg_eio.data_path xdg "databases" in
285285- (* Returns path to ~/.local/share/myapp/databases/ *)
286286- ]} *)
287287-val data_path : t -> string -> Eio.Fs.dir_ty Eio.Path.t
288288-289289-(** [cache_path t name] returns a path within the application's cache directory.
290290-291291- This is a convenience function that creates a subdirectory within the
292292- application's cache directory. The subdirectory is automatically created
293293- if it doesn't exist.
294294-295295- @param t The XDG context
296296- @param name The subdirectory name within the cache directory
297297- @return Path to [{cache_dir}/name]
298298-299299- {b Example:}
300300- {[
301301- let thumbnails = Xdg_eio.cache_path xdg "thumbnails" in
302302- (* Returns path to ~/.cache/myapp/thumbnails/ *)
303303- ]} *)
304304-val cache_path : t -> string -> Eio.Fs.dir_ty Eio.Path.t
305305-306306-(** [state_path t name] returns a path within the application's state directory.
307307-308308- This is a convenience function that creates a subdirectory within the
309309- application's state directory. The subdirectory is automatically created
310310- if it doesn't exist.
311311-312312- @param t The XDG context
313313- @param name The subdirectory name within the state directory
314314- @return Path to [{state_dir}/name]
315315-316316- {b Example:}
317317- {[
318318- let logs = Xdg_eio.state_path xdg "logs" in
319319- (* Returns path to ~/.local/state/myapp/logs/ *)
320320- ]} *)
321321-val state_path : t -> string -> Eio.Fs.dir_ty Eio.Path.t
322322-323253(** {1 Pretty Printing} *)
324254325255(** [pp ?brief ?sources ppf t] pretty prints the XDG directory configuration.
···11+let () =
22+ Eio_main.run @@ fun env ->
33+ let xdg = Xdge.create env#fs "path_test" in
44+55+ (* Test config subdirectory *)
66+ let profiles_path = Eio.Path.(Xdge.config_dir xdg / "profiles") in
77+ let profile_file = Eio.Path.(profiles_path / "default.json") in
88+ (try
99+ let content = Eio.Path.load profile_file in
1010+ Printf.printf "config file content: %s" (String.trim content)
1111+ with
1212+ | exn -> Printf.printf "config file error: %s" (Printexc.to_string exn));
1313+1414+ (* Test data subdirectory *)
1515+ let db_path = Eio.Path.(Xdge.data_dir xdg / "databases") in
1616+ let db_file = Eio.Path.(db_path / "main.db") in
1717+ (try
1818+ let content = Eio.Path.load db_file in
1919+ Printf.printf "\ndata file content: %s" (String.trim content)
2020+ with
2121+ | exn -> Printf.printf "\ndata file error: %s" (Printexc.to_string exn));
2222+2323+ (* Test cache subdirectory *)
2424+ let cache_path = Eio.Path.(Xdge.cache_dir xdg / "thumbnails") in
2525+ let cache_file = Eio.Path.(cache_path / "thumb1.png") in
2626+ (try
2727+ let content = Eio.Path.load cache_file in
2828+ Printf.printf "\ncache file content: %s" (String.trim content)
2929+ with
3030+ | exn -> Printf.printf "\ncache file error: %s" (Printexc.to_string exn));
3131+3232+ (* Test state subdirectory *)
3333+ let logs_path = Eio.Path.(Xdge.state_dir xdg / "logs") in
3434+ let log_file = Eio.Path.(logs_path / "app.log") in
3535+ (try
3636+ let content = Eio.Path.load log_file in
3737+ Printf.printf "\nstate file content: %s\n" (String.trim content)
3838+ with
3939+ | exn -> Printf.printf "\nstate file error: %s\n" (Printexc.to_string exn))
+29
xdg-eio/test/xdg.t
···324324 --config-dir=DIR
325325 Override config directory. Can also be set with
326326327327+Test _path functions do not create directories but can access files within them:
328328+329329+ $ export HOME=/tmp/xdg_path_test
330330+ $ mkdir -p /tmp/xdg_path_test
331331+ $ unset XDG_CONFIG_HOME XDG_DATA_HOME XDG_CACHE_HOME XDG_STATE_HOME XDG_RUNTIME_DIR
332332+ $ unset XDG_CONFIG_DIRS XDG_DATA_DIRS
333333+Create config subdirectory manually and write a test file:
334334+ $ mkdir -p "/tmp/xdg_path_test/.config/path_test/profiles"
335335+ $ echo "test profile content" > "/tmp/xdg_path_test/.config/path_test/profiles/default.json"
336336+Create data subdirectory manually and write a test file:
337337+ $ mkdir -p "/tmp/xdg_path_test/.local/share/path_test/databases"
338338+ $ echo "test database content" > "/tmp/xdg_path_test/.local/share/path_test/databases/main.db"
339339+Create cache subdirectory manually and write a test file:
340340+ $ mkdir -p "/tmp/xdg_path_test/.cache/path_test/thumbnails"
341341+ $ echo "test cache content" > "/tmp/xdg_path_test/.cache/path_test/thumbnails/thumb1.png"
342342+Create state subdirectory manually and write a test file:
343343+ $ mkdir -p "/tmp/xdg_path_test/.local/state/path_test/logs"
344344+ $ echo "test log content" > "/tmp/xdg_path_test/.local/state/path_test/logs/app.log"
345345+346346+Now test that we can read the files through the XDG _path functions:
347347+ $ ./test_paths.exe
348348+ config file content: test profile content
349349+ data file content: test database content
350350+ cache file content: test cache content
351351+ state file content: test log content
352352+353353+This test verifies that the _path functions return correct paths that can be used to access
354354+files within XDG subdirectories, without the functions automatically creating those directories.
355355+