File: paths.go

package info (click to toggle)
ipp-usb 0.9.31-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 828 kB
  • sloc: sh: 68; makefile: 42
file content (145 lines) | stat: -rw-r--r-- 3,997 bytes parent folder | download
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
/* ipp-usb - HTTP reverse proxy, backed by IPP-over-USB connection to device
 *
 * Copyright (C) 2020 and up by Alexander Pevzner (pzz@apevzner.com)
 * See LICENSE for license terms and conditions
 *
 * Common paths
 */

package main

import (
	"fmt"
	"os"
	"path/filepath"
	"strings"
)

// Effective paths, may be altered with the command-line options:
var (
	// Control socket
	PathControlSocket = DefaultPathControlSocket

	// Lock file
	PathLockFile = DefaultPathLockFile

	// Directory for per-device logs
	PathLogDir = DefaultPathLogDir

	// Directory that contains per-device state files
	PathDevStateDir = DefaultPathDevStateDir

	// Path to the program's executable file.
	// Initialized by PathInit()
	PathExecutableFile string

	// Path to the directory that contains the executable file.
	// Initialized by PathInit()
	PathExecutableDir string

	// List of configuration directories.
	// Initialized by PathInit():
	//   DefaultPathConfDir + ":" + PathExecutableDir
	PathConfDirList string

	// List of quirks directories.
	// Initialized by PathInit():
	//   DefaultPathLocalQuirksDir + ":" +
	//   DefaultPathGlobalQuirksDir + ":" +
	//   filepath.Join(PathExecutableDir, "ipp-usb-quirks")
	PathQuirksDirList string
)

// Default paths:
const (
	// DefaultPathConfDir defines path to configuration directory
	DefaultPathConfDir = "/etc/ipp-usb"

	// DefaultPathLocalQuirksDir defines path to locally administered
	// quirks files
	DefaultPathLocalQuirksDir = "/etc/ipp-usb/quirks"

	// DefaultPathGlobalQuirksDir defines path to the "global"
	// quirks files, i.e., files that comes with the ipp-usb package
	DefaultPathGlobalQuirksDir = "/usr/share/ipp-usb/quirks"

	// DefaultPathProgState defines path to program state directory
	DefaultPathProgState = "/var/ipp-usb"

	// DefaultPathLockDir defines path to directory that contains
	// lock files
	DefaultPathLockDir = DefaultPathProgState + "/lock"

	// DefaultPathLockFile defines path to lock file
	DefaultPathLockFile = DefaultPathLockDir + "/ipp-usb.lock"

	// DefaultPathControlSocket defines path to the control socket
	DefaultPathControlSocket = DefaultPathProgState + "/ctrl"

	// DefaultPathDevStateDir defines path to directory where
	// per-device state files are saved to
	DefaultPathDevStateDir = DefaultPathProgState + "/dev"

	// DefaultPathLogDir defines path to log directory
	DefaultPathLogDir = "/var/log/ipp-usb"
)

// PathsInit initializes paths handling.
func PathsInit() error {
	// Initialize PathExecutableFile and PathExecutableDir
	var err error
	PathExecutableFile, err = os.Executable()
	if err != nil {
		err = fmt.Errorf(
			"Error getting path to the executable file: %s", err)
		return err
	}

	PathExecutableDir = filepath.Dir(PathExecutableFile)

	// Initialize derived paths
	PathConfDirList =
		strings.Join(
			[]string{
				DefaultPathConfDir,
				PathExecutableDir,
			},
			string(filepath.ListSeparator),
		)

	PathQuirksDirList =
		strings.Join(
			[]string{
				DefaultPathLocalQuirksDir,
				DefaultPathGlobalQuirksDir,
				filepath.Join(PathExecutableDir,
					"ipp-usb-quirks"),
			},
			string(filepath.ListSeparator),
		)

	return nil
}

// MakeDirectory creates a directory, specified by the path,
// along with any necessary parents.
//
// Possible errors are not checked here, as there are many reasons
// while it can fail (most likely: directory already exists). Instead,
// error checking is implemented when we try to use the resulting directory.
func MakeDirectory(path string) {
	os.MkdirAll(path, 0755)
}

// MakeParentDirectory creates a parent directory for the specified path,
// along with any necessary parents.
//
// Possible errors are not checked here, as there are many reasons
// while it can fail (most likely: directory already exists). Instead,
// error checking is implemented when we try to use the resulting directory.
func MakeParentDirectory(path string) {
	parent := filepath.Dir(path)
	if parent != "" && parent != "." {
		MakeDirectory(parent)
	}
}