This commit is contained in:
ge
2026-01-08 04:30:38 +03:00
commit 7f6e1cd414
6 changed files with 130 additions and 0 deletions

8
.editorconfig Normal file
View File

@@ -0,0 +1,8 @@
[*]
charset = utf-8
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true
[*.v]
indent_style = tab

8
.gitattributes vendored Normal file
View File

@@ -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

24
.gitignore vendored Normal file
View File

@@ -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

22
UNLICENSE Normal file
View File

@@ -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 <http://unlicense.org/>

61
pidfile.v Normal file
View File

@@ -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())!
}

7
v.mod Normal file
View File

@@ -0,0 +1,7 @@
Module {
name: 'pidfile'
description: 'Manage pid-files'
version: '0.1.0'
license: 'Unlicense'
dependencies: []
}