From 7f6e1cd414be277ceac527f01f38d872c3e6ad2c Mon Sep 17 00:00:00 2001 From: ge Date: Thu, 8 Jan 2026 04:30:38 +0300 Subject: [PATCH] init --- .editorconfig | 8 +++++++ .gitattributes | 8 +++++++ .gitignore | 24 ++++++++++++++++++++ UNLICENSE | 22 ++++++++++++++++++ pidfile.v | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++ v.mod | 7 ++++++ 6 files changed, 130 insertions(+) create mode 100644 .editorconfig create mode 100644 .gitattributes create mode 100644 .gitignore create mode 100644 UNLICENSE create mode 100644 pidfile.v create mode 100644 v.mod diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..01072ca --- /dev/null +++ b/.editorconfig @@ -0,0 +1,8 @@ +[*] +charset = utf-8 +end_of_line = lf +insert_final_newline = true +trim_trailing_whitespace = true + +[*.v] +indent_style = tab diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..9a98968 --- /dev/null +++ b/.gitattributes @@ -0,0 +1,8 @@ +* text=auto eol=lf +*.bat eol=crlf + +*.v linguist-language=V +*.vv linguist-language=V +*.vsh linguist-language=V +v.mod linguist-language=V +.vdocignore linguist-language=ignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..7cf391a --- /dev/null +++ b/.gitignore @@ -0,0 +1,24 @@ +# Binaries for programs and plugins +main +pidfile +*.exe +*.exe~ +*.so +*.dylib +*.dll + +# Ignore binary output folders +bin/ + +# Ignore common editor/system specific metadata +.DS_Store +.idea/ +.vscode/ +*.iml + +# ENV +.env + +# vweb and database +*.db +*.js diff --git a/UNLICENSE b/UNLICENSE new file mode 100644 index 0000000..c91541e --- /dev/null +++ b/UNLICENSE @@ -0,0 +1,22 @@ +This is free and unencumbered software released into the public domain. + +Anyone is free to copy, modify, publish, use, compile, sell, or distribute this +software, either in source code form or as a compiled binary, for any purpose, +commercial or non-commercial, and by any means. + +In jurisdictions that recognize copyright laws, the author or authors of this +software dedicate any and all copyright interest in the software to the public +domain. We make this dedication for the benefit of the public at large and to +the detriment of our heirs and +successors. We intend this dedication to be an overt act of relinquishment in +perpetuity of all present and future rights to this software under copyright +law. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS +FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE +LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF +CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE +SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + +For more information, please refer to diff --git a/pidfile.v b/pidfile.v new file mode 100644 index 0000000..e835bc5 --- /dev/null +++ b/pidfile.v @@ -0,0 +1,61 @@ +module pidfile + +import os +import strconv + +fn runtime_dir() string { + if dir := os.getenv_opt('XDG_RUNTIME_DIR') { + return dir + } + euid := os.geteuid() + if euid > 0 { + return '/run/user/${euid}' + } + return '/run' +} + +@[params] +pub struct CreateFileConfig { +pub: + // Path prefix for pid-file, e.g. `/run/1000`. Defaults to XDG_RUNTIME_DIR if set or + // to `/run/user/${EUID}` for normal users and `/run` for root user. + prefix string + + // Path that will be created inside the prefix. Has no default value. + // For example `myapp` subpath with `/run/1000` prefix becomes to `/run/1000/myapp` path. + subpath string + + // If true omit the file extension. Otherwise append the `.pid` extension to filename. + no_extension bool +} + +// create creates the new pid-file and returns path to it. +pub fn create(name string, pid int, config CreateFileConfig) !string { + mut filename := name + if !name.ends_with('.pid') && !config.no_extension { + filename += '.pid' + } + mut path := runtime_dir() + if config.prefix != '' { + path = config.prefix + } + path = os.join_path_single(path, config.subpath) + os.mkdir_all(path)! + path = os.join_path_single(path, filename) + os.write_file(path, pid.str())! + return path +} + +// write writes the pid into file specified in path. +// +// If directories in `path` do not exist, they will be created. +pub fn write(path string, pid int) ! { + os.mkdir_all(os.dir(path))! + os.write_file(path, pid.str())! +} + +// read reads the pid-file and returns its content as integer. +pub fn read(path string) !int { + s := os.read_file(path)! + return strconv.atoi(s.trim_space())! +} diff --git a/v.mod b/v.mod new file mode 100644 index 0000000..50aab46 --- /dev/null +++ b/v.mod @@ -0,0 +1,7 @@ +Module { + name: 'pidfile' + description: 'Manage pid-files' + version: '0.1.0' + license: 'Unlicense' + dependencies: [] +}