| 1 |
= cockpit.js: File Access |
| 2 |
|
| 3 |
The `+cockpit.file+` API lets you read, write, and watch regular files |
| 4 |
in their entirety. It cannot efficiently do random access in a big file |
| 5 |
or read non-regular files such as `+/dev/random+`. |
| 6 |
|
| 7 |
.... |
| 8 |
file = cockpit.file(path, |
| 9 |
{ syntax: syntax_object, |
| 10 |
binary: boolean, |
| 11 |
max_read_size: int, |
| 12 |
superuser: string, |
| 13 |
}) |
| 14 |
|
| 15 |
promise = file.read() |
| 16 |
promise |
| 17 |
.then((content, tag) => { ... }) |
| 18 |
.catch(error => { ... }) |
| 19 |
|
| 20 |
promise = file.replace(content, [ expected_tag ]) |
| 21 |
promise |
| 22 |
.then(new_tag => { ... }) |
| 23 |
.catch(error => { ... }) |
| 24 |
|
| 25 |
promise = file.modify(callback, [ initial_content, initial_tag ] |
| 26 |
promise |
| 27 |
.then((new_content, new_tag) => { ... }) |
| 28 |
.catch(error => { ... }) |
| 29 |
|
| 30 |
file.watch((content, tag, [error]) => { }, [ { read: boolean } ]) |
| 31 |
|
| 32 |
file.close() |
| 33 |
.... |
| 34 |
|
| 35 |
[[cockpit-file-simple]] |
| 36 |
== Simple reading and writing |
| 37 |
|
| 38 |
You can read a file with code like this: |
| 39 |
|
| 40 |
.... |
| 41 |
cockpit.file("/path/to/file").read() |
| 42 |
.then((content, tag) => { |
| 43 |
... |
| 44 |
}) |
| 45 |
.catch(error => { |
| 46 |
... |
| 47 |
}); |
| 48 |
.... |
| 49 |
|
| 50 |
It is recommended to use absolute paths. Relative paths are resolved |
| 51 |
against `+/+`. To work with the current user's files |
| 52 |
xref:cockpit-login.adoc#cockpit-user[cockpit.user()] can be used to get the user's home |
| 53 |
directory. |
| 54 |
|
| 55 |
The `+read()+` method returns a |
| 56 |
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise[Promise]. |
| 57 |
|
| 58 |
When successful, the promise will be resolved with the content of the |
| 59 |
file. Unless you specify options to change this (see below), the file is |
| 60 |
assumed to be text in the UTF-8 encoding, and `+content+` will be a |
| 61 |
string. |
| 62 |
|
| 63 |
The tag that is passed to the `+then()+` callback is a short string that |
| 64 |
is associated with the file and changes whenever the content of the file |
| 65 |
changes. It is meant to be used with `+replace()+`. |
| 66 |
|
| 67 |
It is not an error when the file does not exist. In this case, the |
| 68 |
`+then()+` callback will be called with a `+null+` value for `+content+` |
| 69 |
and `+tag+` is `+"-"+`. |
| 70 |
|
| 71 |
The `+superuser+` option can be used the same way as described in the |
| 72 |
xref:cockpit-channels.adoc#cockpit-channels-channel[cockpit.channel()] to provide a different |
| 73 |
access level to the file. |
| 74 |
|
| 75 |
You can use the `+max_read_size+` option to limit the amount of data |
| 76 |
that is read. If the file is larger than the given number of bytes, no |
| 77 |
data is read and the channel is closed with problem code `+too-large+`. |
| 78 |
The default limit is 16 MiB. The limit can be completely removed by |
| 79 |
setting it to -1. |
| 80 |
|
| 81 |
To write to a file, use code like this: |
| 82 |
|
| 83 |
.... |
| 84 |
cockpit.file("/path/to/file").replace("my new content\n") |
| 85 |
.then(tag => { |
| 86 |
... |
| 87 |
}) |
| 88 |
.catch(error => { |
| 89 |
... |
| 90 |
}); |
| 91 |
.... |
| 92 |
|
| 93 |
The `+replace()+` method returns a |
| 94 |
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise[Promise]. |
| 95 |
|
| 96 |
When the promise is resolved, the file has been atomically replaced (via |
| 97 |
the `+rename()+` syscall) with the new content. As with `+read()+`, by |
| 98 |
default the new content is a string and will be written to the file as |
| 99 |
UTF-8. The returned tag corresponds to the new content of the file. |
| 100 |
|
| 101 |
When the promise is rejected because of an error, the file or its meta |
| 102 |
data has not been changed in any way. |
| 103 |
|
| 104 |
As a special case, passing the value `+null+` to `+replace()+` will |
| 105 |
remove the file. |
| 106 |
|
| 107 |
The `+replace()+` method can also check for conflicting changes to a |
| 108 |
file. You can pass a tag (as returned by `+read()+` or `+replace()+`) to |
| 109 |
`+replace()+`, and the file will only be replaced if it still has the |
| 110 |
given tag. If the tag of the file has changed, `+replace()+` will fail |
| 111 |
with an error object that has `+error.problem == "change-conflict"+`. |
| 112 |
See `+modify()+` below for a convenient way to achieve transactional |
| 113 |
updates to a file. |
| 114 |
|
| 115 |
[[cockpit-file-format]] |
| 116 |
== File format |
| 117 |
|
| 118 |
By default, a file is assumed to be text encoded in UTF-8, and the |
| 119 |
`+read()+` and `+replace()+` functions use strings to represent the |
| 120 |
content. |
| 121 |
|
| 122 |
By specifying the `+syntax.parser()+` and `+syntax.stringify()+` |
| 123 |
options, you can cause `+read()+` to parse the content before passing it |
| 124 |
back to you, and `+replace()+` to unparse it before writing. |
| 125 |
|
| 126 |
The main idea is to be able to write `+{ syntax: JSON }+`, of course, |
| 127 |
but you can easily pass in individual functions or make your own |
| 128 |
parser/unparser object: |
| 129 |
|
| 130 |
.... |
| 131 |
cockpit.file("/path/to/file.json", { syntax: JSON }) |
| 132 |
|
| 133 |
var syntax_object = { |
| 134 |
parse: my_parser, |
| 135 |
stringify: my_unparser |
| 136 |
}; |
| 137 |
|
| 138 |
cockpit.file("/path/to/file", { syntax: syntax_object }) |
| 139 |
.... |
| 140 |
|
| 141 |
Any exceptions thrown by the `+parse()+` and `+stringify()+` functions |
| 142 |
are caught and reported as read or write errors. |
| 143 |
|
| 144 |
The `+null+` value that is used to represent the content of a |
| 145 |
non-existing file (see "Simple reading and writing", above) is not |
| 146 |
passed through the `+parse()+` and `+stringify()+` functions. |
| 147 |
|
| 148 |
[[cockpit-file-binary]] |
| 149 |
== Binary files |
| 150 |
|
| 151 |
By default the content of the file is assumed to be text encoded as |
| 152 |
UTF-8 and it can not contain zero bytes. The content is represented as a |
| 153 |
JavaScript string with `+read()+`, `+replace()+`, etc. By setting the |
| 154 |
`+binary+` option to true when creating the proxy, no assumptions are |
| 155 |
placed on the content, and it is represented as a `+Uint8Array+` in |
| 156 |
JavaScript. |
| 157 |
|
| 158 |
[[cockpit-file-atomic]] |
| 159 |
== Atomic modifications |
| 160 |
|
| 161 |
Use `+modify()+` to modify the content of the file safely. A call to |
| 162 |
`+modify()+` will read the content of the file, call `+callback+` on the |
| 163 |
content, and then replace the content of the file with the return value |
| 164 |
of the callback. |
| 165 |
|
| 166 |
The `+modify()+` method uses the `+read()+` and `+replace()+` methods |
| 167 |
internally in the obvious way. Thus, the `+syntax.parse()+` and |
| 168 |
`+syntax.stringify()+` options work as expected, `+null+` represents a |
| 169 |
non-existing file, and the watch callbacks are fired. |
| 170 |
|
| 171 |
It will do this one or more times, until no other conflicting changes |
| 172 |
have been made to the file between reading and replacing it. |
| 173 |
|
| 174 |
The callback is called like this |
| 175 |
|
| 176 |
.... |
| 177 |
new_content = callback (old_content) |
| 178 |
.... |
| 179 |
|
| 180 |
The callback is allowed to mutate `+old_content+`, but note that this |
| 181 |
will also mutate the objects that are passed to the watch callbacks. |
| 182 |
Returning `+undefined+` from the proxy is the same as returning |
| 183 |
`+old_content+`. |
| 184 |
|
| 185 |
The `+modify()+` method returns a |
| 186 |
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise[Promise]. |
| 187 |
|
| 188 |
The promise will be resolved with the new content and its tag, like so |
| 189 |
|
| 190 |
.... |
| 191 |
function shout(old_content) { |
| 192 |
return old_content.toUpperCase(); |
| 193 |
} |
| 194 |
|
| 195 |
cockpit.file("/path/to/file").modify(shout) |
| 196 |
.then((content, tag) => { |
| 197 |
... |
| 198 |
}) |
| 199 |
.catch(error => { |
| 200 |
... |
| 201 |
}); |
| 202 |
.... |
| 203 |
|
| 204 |
If you have cached the last content and tag results of the `+read()+` or |
| 205 |
`+modify()+` method, or the last values passed to a watch callback, you |
| 206 |
can pass them to `+modify()+` as the second and third argument. In this |
| 207 |
case, `+modify()+` will skip the initial read and start with the given |
| 208 |
values. |
| 209 |
|
| 210 |
[[cockpit-file-notify]] |
| 211 |
== Change notifications |
| 212 |
|
| 213 |
Calling `+watch()+` will start monitoring the file for external changes. |
| 214 |
|
| 215 |
.... |
| 216 |
handle = file.watch(callback); |
| 217 |
|
| 218 |
handle_no_read = file.watch(callback, { read: false }); |
| 219 |
.... |
| 220 |
|
| 221 |
Whenever a change occurs, the `+callback()+` is called with the new |
| 222 |
content and tag of the file. This might happen because of external |
| 223 |
changes, but also as part of calls to `+read()+`, `+replace()+`, and |
| 224 |
`+modify()+`. |
| 225 |
|
| 226 |
When a read error occurs, the `+callback()+` is called with an error as |
| 227 |
a third argument. Write errors are not reported via the watch callback. |
| 228 |
|
| 229 |
Calling `+watch()+` will also automatically call `+read()+` to get the |
| 230 |
initial content of the file. Thus, you normally don't need to call |
| 231 |
`+read()+` at all when using `+watch()+`. |
| 232 |
|
| 233 |
To disable the automatic reading, e.g. for large files or unreadable |
| 234 |
file system objects, set the `+read+` option to `+false+`. The first |
| 235 |
`+content+` argument of the callback will then always be `+null+`. |
| 236 |
|
| 237 |
To free the resources used for monitoring, call `+handle.remove()+`. |
| 238 |
|
| 239 |
[[cockpit-file-path]] |
| 240 |
== file.path |
| 241 |
|
| 242 |
A string containing the path that was passed to the `+cockpit.file()+` |
| 243 |
method. |
| 244 |
|
| 245 |
[[cockpit-file-close]] |
| 246 |
== Closing |
| 247 |
|
| 248 |
Call the `+close()+` method on a file proxy to cancel all ongoing |
| 249 |
operations, such as reading, writing, and monitoring. The proxy should |
| 250 |
not be used after closing it. |