6.1. Single-User File StorageΒΆ

Here is an example code snippet to help you understand some of the functionality detailed in the Design Requirements.

f1 := []byte("content")
f2 := []byte("different content")

// Alice and Bob each start a users session by authenticating to the client.
alice_session_1, _ := InitUser("user_alice", "password1")
bob_session_1, _ := InitUser("user_bob", "password2")

// Alice stores byte slice f1 with name "filename" and Bob stores byte slice
// f2 also with name "filename".
alice_session_1.StoreFile("filename", f1)
bob_session_1.StoreFile("filename", f2)

// Alice and Bob each confirm that they can load the file they previously
// stored and that the file contents is the same.

f1_loaded, _ := alice_session_1.LoadFile("filename")
f2_loaded, _ := bob_session_1.LoadFile("filename")

if f1 != f1_loaded {
    panic("file contents are different.")
}
if f2 != f2_loaded {
    panic("file contents are different.")
}

// Alice gets an error when trying to load a file that does not exist in her
// namespace.
_, err := alice_session_1.LoadFile("nonexistent")
if err != nil {
    panic("downloaded a nonexistent file.")
}

// Bob creates a second user session by authenticating to the client again.
bob_session_2, _ := GetUser("user_bob", "password2")

// Bob stores byte slice f2 with name "newfile" using his second user
// session.
bob_session_2.StoreFile("newfile", f2)

// Bob loads "newfile" using his first user session. Notice that Bob does
// not need to reauthenticate. File changes must be available to all active
// sessions for a given user.

f2_newfile := bob_session_1.LoadFile("newfile")

if f2 != f2_newfile {
    panic("file contents are different.")
}