Allow filesystem links to contain leading/trailing whitespace
To make manually editing linked protectors slightly more user-friendly,
automatically strip any leading or trailing whitespace. E.g. treat
"UUID=3a6d9a76-47f0-4f13-81bf-3332fbe984fb\n" the same as
"UUID=3a6d9a76-47f0-4f13-81bf-3332fbe984fb".
Update https://github.com/google/fscrypt/issues/115
Eric Biggers
4 years ago
281 | 281 |
return mnt, nil
|
282 | 282 |
}
|
283 | 283 |
|
284 | |
// getMountsFromLink returns the Mount object which matches the provided link.
|
|
284 |
// getMountFromLink returns the Mount object which matches the provided link.
|
285 | 285 |
// This link is formatted as a tag (e.g. <token>=<value>) similar to how they
|
286 | 286 |
// appear in "/etc/fstab". Currently, only "UUID" tokens are supported. An error
|
287 | 287 |
// is returned if the link is invalid or we cannot load the required mount data.
|
|
289 | 289 |
// functions, run UpdateMountInfo to see the change.
|
290 | 290 |
func getMountFromLink(link string) (*Mount, error) {
|
291 | 291 |
// Parse the link
|
|
292 |
link = strings.TrimSpace(link)
|
292 | 293 |
linkComponents := strings.Split(link, "=")
|
293 | 294 |
if len(linkComponents) != 2 {
|
294 | 295 |
return nil, errors.Wrapf(ErrFollowLink, "link %q format is invalid", link)
|
254 | 254 |
}
|
255 | 255 |
}
|
256 | 256 |
|
|
257 |
// Test making a filesystem link (i.e. "UUID=...") and following it, and test
|
|
258 |
// that leading and trailing whitespace in the link is ignored.
|
|
259 |
func TestGetMountFromLink(t *testing.T) {
|
|
260 |
mnt, err := getTestMount(t)
|
|
261 |
if err != nil {
|
|
262 |
t.Skip(err)
|
|
263 |
}
|
|
264 |
link, err := makeLink(mnt, uuidToken)
|
|
265 |
if err != nil {
|
|
266 |
t.Fatal(err)
|
|
267 |
}
|
|
268 |
linkedMnt, err := getMountFromLink(link)
|
|
269 |
if err != nil {
|
|
270 |
t.Fatal(err)
|
|
271 |
}
|
|
272 |
if linkedMnt != mnt {
|
|
273 |
t.Fatal("Link doesn't point to the same Mount")
|
|
274 |
}
|
|
275 |
if linkedMnt, err = getMountFromLink(link + "\n"); err != nil {
|
|
276 |
t.Fatal(err)
|
|
277 |
}
|
|
278 |
if linkedMnt != mnt {
|
|
279 |
t.Fatal("Link doesn't point to the same Mount")
|
|
280 |
}
|
|
281 |
if linkedMnt, err = getMountFromLink(" " + link + " \r\n"); err != nil {
|
|
282 |
t.Fatal(err)
|
|
283 |
}
|
|
284 |
if linkedMnt != mnt {
|
|
285 |
t.Fatal("Link doesn't point to the same Mount")
|
|
286 |
}
|
|
287 |
}
|
|
288 |
|
257 | 289 |
// Benchmarks how long it takes to update the mountpoint data
|
258 | 290 |
func BenchmarkLoadFirst(b *testing.B) {
|
259 | 291 |
for n := 0; n < b.N; n++ {
|