Skip to content

Commit f6a2938

Browse files
committed
Added program options to control file/directory placement
Running ipp-usb in a container—such as a Snap or OCI container—may result in a non-standard filesystem layout (see #108 for details). To simplify using ipp-usb in such environments, the following command-line options have been added: -path-conf-files-srch dir1[:dir2...] List of directories where configuration files (ipp-usb.conf) are searched (/etc/ipp-usb) -path-log-dir dir Path to the directory where log files (main.log and per-device <DEVICE>.log) are written (/var/log/ipp-usb) -path-lock-file file Path to the program's lock file (/var/ipp-usb/lock/ipp-usb.lock) -path-dev-state-dir dir Path to the directory where per-device state files are written (/var/ipp-usb/dev) -path-ctrl-sock file Path to the program's control socket (/var/ipp-usb/ctrl) -path-quirks-files-srch dir1[:dir2...] List of directories where quirks files (*.conf) is searched (/etc/ipp-usb/quirks:/usr/share/ipp-usb/quirks) The manual page was updated appropriately.
1 parent 6cae7ae commit f6a2938

6 files changed

Lines changed: 157 additions & 30 deletions

File tree

conf.go

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,9 @@ var Conf = Configuration{
6060
// ConfLoad loads the program configuration
6161
func ConfLoad() error {
6262
// Build list of configuration files
63-
files := []string{
64-
filepath.Join(PathConfDir, ConfFileName),
65-
filepath.Join(PathExecutableDir, ConfFileName),
63+
files := filepath.SplitList(PathConfDirList)
64+
for i := range files {
65+
files[i] = filepath.Join(files[i], ConfFileName)
6666
}
6767

6868
// Load file by file
@@ -74,11 +74,7 @@ func ConfLoad() error {
7474
}
7575

7676
// Load quirks
77-
quirksDirs := []string{
78-
PathLocalQuirksDir,
79-
PathGlobalQuirksDir,
80-
filepath.Join(PathExecutableDir, "ipp-usb-quirks"),
81-
}
77+
quirksDirs := filepath.SplitList(PathQuirksDirList)
8278

8379
var err error
8480
Conf.Quirks, err = LoadQuirksSet(quirksDirs...)

ipp-usb.8

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,35 @@ check configuration and exit\. It also prints a list of all connected devices
2929
\fBstatus\fR
3030
print status of the running \fBipp\-usb\fR daemon, including information of all connected devices
3131
.SS "Options are"
32-
.TP
32+
.IP "\(bu" 4
3333
\fB\-bg\fR
34+
.br
3435
run in background (ignored in debug mode)
36+
.IP "\(bu" 4
37+
\fB\-path\-conf\-files\-srch dir1[:dir2\|\.\|\.\|\.]\fR
38+
.br
39+
List of directories where configuration files (ipp\-usb\.conf) are searched (/etc/ipp\-usb)
40+
.IP "\(bu" 4
41+
\fB\-path\-log\-dir dir\fR
42+
.br
43+
Path to the directory where log files (main\.log and per\-device \fIDEVICE\fR\.log) are written (/var/log/ipp\-usb)
44+
.IP "\(bu" 4
45+
\fB\-path\-lock\-file file\fR
46+
.br
47+
Path to the program's lock file (/var/ipp\-usb/lock/ipp\-usb\.lock)
48+
.IP "\(bu" 4
49+
\fB\-path\-dev\-state\-dir dir\fR
50+
.br
51+
Path to the directory where per\-device state files are written (/var/ipp\-usb/dev)
52+
.IP "\(bu" 4
53+
\fB\-path\-ctrl\-sock file\fR
54+
.br
55+
Path to the program's control socket (/var/ipp\-usb/ctrl)
56+
.IP "\(bu" 4
57+
\fB\-path\-quirks\-files\-srch dir1[:dir2\|\.\|\.\|\.]\fR
58+
.br
59+
List of directories where quirks files (*\.conf) is searched (/etc/ipp\-usb/quirks:/usr/share/ipp\-usb/quirks)
60+
.IP "" 0
3561
.SH "NETWORKING"
3662
Essentially, \fBipp\-usb\fR makes printer or scanner accessible from the network, converting network\-side HTTP operations to the USB operations\.
3763
.P

ipp-usb.8.md

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,32 @@ IPP printing, eSCL scanning and web console are fully supported.
4141

4242
### Options are
4343

44-
* `-bg`:
44+
* `-bg`<br>
4545
run in background (ignored in debug mode)
4646

47+
* `-path-conf-files-srch dir1[:dir2...]`<br>
48+
List of directories where configuration files (ipp-usb.conf)
49+
are searched (/etc/ipp-usb)
50+
51+
* `-path-log-dir dir`<br>
52+
Path to the directory where log files (main.log and per-device
53+
<DEVICE>.log) are written (/var/log/ipp-usb)
54+
55+
* `-path-lock-file file`<br>
56+
Path to the program's lock file (/var/ipp-usb/lock/ipp-usb.lock)
57+
58+
* `-path-dev-state-dir dir`<br>
59+
Path to the directory where per-device state files are written
60+
(/var/ipp-usb/dev)
61+
62+
* `-path-ctrl-sock file`<br>
63+
Path to the program's control socket
64+
(/var/ipp-usb/ctrl)
65+
66+
* `-path-quirks-files-srch dir1[:dir2...]`<br>
67+
List of directories where quirks files (\*.conf) is searched
68+
(/etc/ipp-usb/quirks:/usr/share/ipp-usb/quirks)
69+
4770
## NETWORKING
4871

4972
Essentially, `ipp-usb` makes printer or scanner accessible from the

logger.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,12 +171,12 @@ func (l *Logger) ToFile(path string) *Logger {
171171

172172
// ToMainFile redirects log to the main log file
173173
func (l *Logger) ToMainFile() *Logger {
174-
return l.ToFile(PathMainLogFile)
174+
return l.ToFile(filepath.Join(PathLogDir, "main.log"))
175175
}
176176

177177
// ToDevFile redirects log to per-device log file
178178
func (l *Logger) ToDevFile(info UsbDeviceInfo) *Logger {
179-
return l.ToFile(filepath.Join(PathDevLogDir, info.Ident()+".log"))
179+
return l.ToFile(filepath.Join(PathLogDir, info.Ident()+".log"))
180180
}
181181

182182
// HasDestination reports if Logger destination is already

main.go

Lines changed: 65 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,29 @@ Modes are:
3030
3131
Options are
3232
-bg - run in background (ignored in debug mode)
33+
34+
-path-conf-files-srch dir1[:dir2...]
35+
List of directories where configuration files (ipp-usb.conf)
36+
are searched (%s)
37+
38+
-path-log-dir dir
39+
Path to the directory where log files (main.log and per-device
40+
<DEVICE>.log) are written (%s)
41+
42+
-path-lock-file file
43+
Path to the program's lock file (%s)
44+
45+
-path-dev-state-dir dir
46+
Path to the directory where per-device state files are written
47+
(%s)
48+
49+
-path-ctrl-sock file
50+
Path to the program's control socket
51+
(%s)
52+
53+
-path-quirks-files-srch dir1[:dir2...]
54+
List of directories where quirks files (*.conf) is searched
55+
(%s)
3356
`
3457

3558
// RunMode represents the program run mode
@@ -81,7 +104,15 @@ type RunParameters struct {
81104

82105
// usage prints detailed usage and exits
83106
func usage() {
84-
fmt.Printf(usageText, os.Args[0])
107+
fmt.Printf(usageText,
108+
os.Args[0],
109+
PathConfDirList,
110+
PathLogDir,
111+
PathLockFile,
112+
PathDevStateDir,
113+
PathControlSocket,
114+
PathQuirksDirList,
115+
)
85116
os.Exit(0)
86117
}
87118

@@ -110,7 +141,10 @@ func parseArgv() (params RunParameters) {
110141
params.Mode = RunDebug
111142

112143
modes := 0
113-
for _, arg := range os.Args[1:] {
144+
for i := 1; i < len(os.Args); i++ {
145+
arg := os.Args[i]
146+
var optarg *string
147+
114148
switch arg {
115149
case "-h", "-help", "--help":
116150
usage()
@@ -131,9 +165,38 @@ func parseArgv() (params RunParameters) {
131165
modes++
132166
case "-bg":
133167
params.Background = true
168+
169+
case "-path-log-dir":
170+
optarg = &PathLogDir
171+
172+
case "-path-lock-file":
173+
optarg = &PathLockFile
174+
175+
case "-path-dev-state-dir":
176+
optarg = &PathDevStateDir
177+
178+
case "-path-conf-files-srch":
179+
optarg = &PathConfDirList
180+
181+
case "-path-ctrl-sock":
182+
optarg = &PathControlSocket
183+
184+
case "-path-quirks-files-srch":
185+
optarg = &PathQuirksDirList
186+
134187
default:
135188
usageError("Invalid argument %s", arg)
136189
}
190+
191+
if optarg != nil {
192+
if i+1 == len(os.Args) {
193+
usageError(
194+
"Option requires an argument: %s", arg)
195+
}
196+
197+
i++
198+
*optarg = os.Args[i]
199+
}
137200
}
138201

139202
if modes > 1 {

paths.go

Lines changed: 35 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,30 +12,19 @@ import (
1212
"fmt"
1313
"os"
1414
"path/filepath"
15+
"strings"
1516
)
1617

1718
// Effective paths, may be altered with the command-line options:
1819
var (
19-
// Configuration directory
20-
PathConfDir = DefaultPathConfDir
21-
2220
// Control socket
2321
PathControlSocket = DefaultPathControlSocket
2422

25-
// Global (installed) quirks files
26-
PathGlobalQuirksDir = DefaultPathGlobalQuirksDir
27-
28-
// Local (admin-defined) quirks files
29-
PathLocalQuirksDir = DefaultPathLocalQuirksDir
30-
3123
// Lock file
3224
PathLockFile = DefaultPathLockFile
3325

3426
// Directory for per-device logs
35-
PathDevLogDir = DefaultPathLogDir
36-
37-
// Main log file
38-
PathMainLogFile = DefaultPathMainLogFile
27+
PathLogDir = DefaultPathLogDir
3928

4029
// Directory that contains per-device state files
4130
PathDevStateDir = DefaultPathDevStateDir
@@ -47,6 +36,18 @@ var (
4736
// Path to the directory that contains the executable file.
4837
// Initialized by PathInit()
4938
PathExecutableDir string
39+
40+
// List of configuration directories.
41+
// Initialized by PathInit():
42+
// DefaultPathConfDir + ":" + PathExecutableDir
43+
PathConfDirList string
44+
45+
// List of quirks directories.
46+
// Initialized by PathInit():
47+
// DefaultPathLocalQuirksDir + ":" +
48+
// DefaultPathGlobalQuirksDir + ":" +
49+
// filepath.Join(PathExecutableDir, "ipp-usb-quirks")
50+
PathQuirksDirList string
5051
)
5152

5253
// Default paths:
@@ -81,9 +82,6 @@ const (
8182

8283
// DefaultPathLogDir defines path to log directory
8384
DefaultPathLogDir = "/var/log/ipp-usb"
84-
85-
// DefaultPathMainLogFile defines path to the main log file
86-
DefaultPathMainLogFile = DefaultPathLogDir + "/main.log"
8785
)
8886

8987
// PathsInit initializes paths handling.
@@ -99,6 +97,27 @@ func PathsInit() error {
9997

10098
PathExecutableDir = filepath.Dir(PathExecutableFile)
10199

100+
// Initialize derived paths
101+
PathConfDirList =
102+
strings.Join(
103+
[]string{
104+
DefaultPathConfDir,
105+
PathExecutableDir,
106+
},
107+
string(filepath.ListSeparator),
108+
)
109+
110+
PathQuirksDirList =
111+
strings.Join(
112+
[]string{
113+
DefaultPathLocalQuirksDir,
114+
DefaultPathGlobalQuirksDir,
115+
filepath.Join(PathExecutableDir,
116+
"ipp-usb-quirks"),
117+
},
118+
string(filepath.ListSeparator),
119+
)
120+
102121
return nil
103122
}
104123

0 commit comments

Comments
 (0)