-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathos.go
173 lines (159 loc) · 7.25 KB
/
os.go
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package idefixgo
import (
"encoding/hex"
"os"
"time"
m "github.com/nayarsystems/idefix-go/messages"
)
// The following constants define command topics for inter-system communication regarding file operations
// and other commands in the operating system context. Each command topic is a string that specifies
// the type of command being executed, allowing for structured messaging and organization.
//
// TopicCmd serves as the base topic for OS commands, while the other constants extend this base to
// specify particular file operations, such as reading, writing, copying, and managing files. This
// organization helps in routing messages correctly within the system.
const (
TopicCmd = "os.cmd"
TopicCmdFile = TopicCmd + ".file"
TopicCmdFileRead = TopicCmdFile + ".read"
TopicCmdFileWrite = TopicCmdFile + ".write"
TopicCmdFileSize = TopicCmdFile + ".size"
TopicCmdFileCopy = TopicCmdFile + ".copy"
TopicCmdFileSHA256 = TopicCmdFile + ".sha256"
TopicCmdExec = TopicCmd + ".exec"
TopicCmdMkdir = TopicCmd + ".mkdir"
TopicCmdPatch = TopicCmd + ".patch"
TopicCmdRemove = TopicCmd + ".remove"
TopicCmdMove = TopicCmd + ".move"
TopicCmdFree = TopicCmd + ".free"
TopicCmdListDir = TopicCmd + ".listdir"
)
// FileWrite uploads data to a file at the specified path on the remote system. It sends a request
// using the client's Call2 method, including the file's path, content, and mode for the file's
// permissions. Upon successful upload, the function returns the hex representation of the hash (sha256) of
// the data or an error if the write operation fails.
func FileWrite(ic *Client, address, path string, data []byte, mode os.FileMode, tout time.Duration) (hash string, err error) {
msg := &m.FileWriteMsg{
Path: path,
Data: data,
Mode: uint32(mode),
}
resp := &m.FileWriteResMsg{}
err = ic.Call2(address, &m.Message{To: TopicCmdFileWrite, Data: msg}, resp, tout)
hash = hex.EncodeToString(resp.Hash)
return
}
// FileRead retrieves the contents of a file located at the specified path on the remote system.
// It sends a request using the client's Call2 method to read the file data. The function returns
// the file's content as a byte slice or an error if the read operation fails.
func FileRead(ic *Client, address, path string, tout time.Duration) (data []byte, err error) {
msg := &m.FileReadMsg{
Path: path,
}
resp := &m.FileReadResMsg{}
err = ic.Call2(address, &m.Message{To: TopicCmdFileRead, Data: msg}, resp, tout)
data = resp.Data
return
}
// FileSHA256 computes the SHA256 hash of a file located at the specified path on the remote system.
// It sends a request using the client's Call2 method to retrieve the hash. The function returns the
// computed hash as a byte slice or an error if the request fails.
func FileSHA256(ic *Client, address, path string, tout time.Duration) (hash []byte, err error) {
msg := &m.FileSHA256Msg{
Path: path,
}
resp := &m.FileSHA256ResMsg{}
err = ic.Call2(address, &m.Message{To: TopicCmdFileSHA256, Data: msg}, resp, tout)
hash = resp.Hash
return
}
// FileSHA256Hex computes the SHA256 hash of a file located at the specified path on the remote system
// and returns it as a hex-encoded string. It first calls [FileSHA256] to obtain the raw hash bytes.
// The function returns the hex string representation of the hash or an error if the hash computation fails.
func FileSHA256Hex(ic *Client, address, path string, tout time.Duration) (hashHex string, err error) {
hash, err := FileSHA256(ic, address, path, tout)
if err != nil {
return
}
hashHex = hex.EncodeToString(hash)
return
}
// FileCopy sends a request to copy a file from srcPath to dstPath on the remote system.
// It uses the client's Call2 method to execute the file copy operation at the specified address.
// The function returns an error if the file copy operation fails.
func FileCopy(ic *Client, address, srcPath, dstPath string, tout time.Duration) (err error) {
msg := m.FileCopyMsg{
SrcPath: srcPath,
DstPath: dstPath,
}
resp := &m.FileCopyResMsg{}
err = ic.Call2(address, &m.Message{To: TopicCmdFileCopy, Data: msg}, resp, tout)
return
}
// Remove sends a request to delete a file or directory at the specified path on the remote system.
// It uses the client's Call2 method to perform the removal operation at the given address.
// The function returns an error if the removal operation fails.
func Remove(ic *Client, address, path string, tout time.Duration) (err error) {
msg := m.RemoveMsg{
Path: path,
}
resp := &m.RemoveResMsg{}
err = ic.Call2(address, &m.Message{To: TopicCmdRemove, Data: msg}, resp, tout)
return
}
// Move sends a request to move a file or directory from srcPath to dstPath on the remote system.
// It uses the client's Call2 method to perform the move operation at the specified address.
// The function returns an error if the move operation fails.
func Move(ic *Client, address, srcPath, dstPath string, tout time.Duration) (err error) {
msg := m.MoveMsg{
SrcPath: srcPath,
DstPath: dstPath,
}
resp := &m.MoveResMsg{}
err = ic.Call2(address, &m.Message{To: TopicCmdMove, Data: msg}, resp, tout)
return
}
// GetFree requests the available free space for a given path from the remote address.
// The function sends a request using the client's Call2 method, asking for the free disk space at the specified path.
// It returns the amount of free space in bytes or an error if the request fails.
func GetFree(ic *Client, address, path string, tout time.Duration) (freeSpace uint64, err error) {
msg := m.FreeSpaceMsg{
Path: path,
}
resp := &m.FreeSpaceResMsg{}
err = ic.Call2(address, &m.Message{To: TopicCmdFree, Data: msg}, resp, tout)
freeSpace = resp.Free
return
}
// ListDir sends a request to the specified address to list the contents of the directory at the given path.
// It uses the client's Call2 method to send the request and retrieve a response containing the file information.
// The function returns a slice of FileInfo pointers representing the files in the directory or an error if
// the request fails.
func ListDir(ic *Client, address, path string, tout time.Duration) (files []*m.FileInfo, err error) {
msg := m.ListDirMsg{
Path: path,
}
resp := &m.ListDirResMsg{}
err = ic.Call2(address, &m.Message{To: TopicCmdListDir, Data: msg}, resp, tout)
files = resp.Files
return
}
// ExitToUpdate sends an update command to the given address to initiate a graceful exit and update process.
// The update command includes the update type, cause, stop delay, and wait halt delay, allowing for
// customizable control over the update behavior. It sends the command via the client's Call2 method and
// waits for the response.
//
// The updateType defines the type of update being performed, and cause provides a reason for the update.
// stopDelay and waitHaltDelay specify the durations to wait before stopping and halting operations, respectively.
// tout sets the timeout for the update request.
func ExitToUpdate(ic *Client, address string, updateType int, cause string, stopDelay, waitHaltDelay, tout time.Duration) (resp *m.UpdateResMsg, err error) {
msg := m.UpdateMsg{
Type: updateType,
Cause: cause,
StopDelay: stopDelay,
WaitHaltDelay: waitHaltDelay,
}
resp = &m.UpdateResMsg{}
err = ic.Call2(address, &m.Message{To: "updater.cmd.update", Data: msg}, resp, tout)
return
}