summaryrefslogtreecommitdiff
path: root/sd_login/util_valid.go
diff options
context:
space:
mode:
Diffstat (limited to 'sd_login/util_valid.go')
-rw-r--r--sd_login/util_valid.go49
1 files changed, 23 insertions, 26 deletions
diff --git a/sd_login/util_valid.go b/sd_login/util_valid.go
index e0dd906..46493b3 100644
--- a/sd_login/util_valid.go
+++ b/sd_login/util_valid.go
@@ -1,4 +1,4 @@
-// Copyright (C) 2016 Luke Shumaker <lukeshu@sbcglobal.net>
+// Copyright (C) 2016-2017 Luke Shumaker <lukeshu@sbcglobal.net>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@@ -30,34 +30,14 @@ func valid_slice_name(s string) bool {
return strings.HasSuffix(s, ".slice") && valid_unit_name(s) == unit_name_plain
}
-func valid_machine_name(s string) bool {
- if len(s) > host_name_max {
- // Note that Linux HOST_NAME_MAX is 64, but DNS allows
- // 255, so names from DNS might be invalid.
+func valid_filename(s string) bool {
+ switch s {
+ case "", ".", "..":
return false
}
-
- n_dots := 0
- dot := true
- for _, c := range s {
- if c == '.' {
- if dot {
- return false
- }
- dot = true
- n_dots++
- } else {
- if !strings.ContainsRune(letters+digits+"-_.", c) {
- return false
- }
- dot = false
- }
- }
-
- if dot { // trailing dot or empty
+ if strings.ContainsRune(s, '/') {
return false
}
-
return true
}
@@ -66,9 +46,26 @@ const (
unit_name_plain = 1 << 0 // foo.service
unit_name_instance = 1 << 1 // foo@bar.service
unit_name_template = 1 << 2 // foo@.service
- unit_name_any = unit_name_plain | unit_name_instance | unit_name_template
)
+// valid_unit_name returns returns which type of unit the given unit
+// name is valid for.
+//
+// To simply check whether a unit name is valid, without caring about
+// the type, you can simply check that it doesn't return the "invalid"
+// type:
+//
+// is_valid := valid_unit_name(unitname) != unit_name_invalid
+//
+// To check whether it matches a specific type (in this example,
+// "template"), you can test equality against that type:
+//
+// is_valid_template := valid_unit_name(unitname) == unit_name_template
+//
+// If there are several acceptable types, you can treat multiple types
+// as bitmasks, and use the usual bitfield checks:
+//
+// is_valid_plain_or_instance := valid_unit_name(unitname)&(unit_name_plain|unit_name_instance) != 0
func valid_unit_name(unit string) int {
const_unit_name_max := 256
const_unit_types := []string{