MCPcopy Create free account
hub / github.com/blues/note / create

Method create

lib/storage.go:16–63  ·  view source on GitHub ↗
(ctx context.Context, storageHint string, filenameHint string)

Source from the content-addressed store, hash-verified

14type storage struct{}
15
16func (s storage) create(ctx context.Context, storageHint string, filenameHint string) (storageObject string, err error) {
17 // Find a path that doesn't yet exist
18 container, _ := fsNamesFromFileStorageObject(storageHint)
19 name := fsFilename(container, filenameHint)
20
21 // If and only if we're cleaning, select a path that doesn't exist - else overwrite the file
22 if cleanWithJSONExtension {
23 for i := 1; ; i++ {
24
25 exists, err2 := fio.Exists(ctx, fsPath(name))
26 if err2 != nil {
27 err = err2
28 return
29 }
30 if !exists {
31 break
32 }
33
34 name = fsFilename(container, fmt.Sprintf("%s%d", filenameHint, i))
35
36 }
37 }
38
39 // Create it, thus reserving its name. Note that if it exists we must overwrite it (as the
40 // comment above specifies). This has the effect of self-healing rather than blocking
41 // operations where the high level file descriptor was lost and the file is now being recreated.
42 // (Note that this is particularly important when !cleanWithJSONExtension, else a new unique
43 // file object would have been created in that case.)
44 path := fsPath(name)
45 err = fio.Create(ctx, path)
46 if err != nil {
47 if deletionForbidden(ctx, path) {
48 err = fmt.Errorf("file: can't re-create %s: %s", path, err)
49 return
50 }
51 _ = fio.Delete(ctx, path)
52 err = fio.Create(ctx, path)
53 if err != nil {
54 return
55 }
56 }
57
58 // Regenerate the name by removing the storage container
59 _, name = fsNamesFromFileStorageObject(name)
60
61 // Create an object name from this name WITHOUT the container
62 return fileStorageClass + ":" + name, nil
63}
64
65func (s storage) createObject(ctx context.Context, storageObject string) (err error) {
66 // Get the filename from the storage object name

Callers 1

uAddNotefileMethod · 0.80

Calls 7

fsFilenameFunction · 0.85
fsPathFunction · 0.85
deletionForbiddenFunction · 0.85
ExistsMethod · 0.65
CreateMethod · 0.65
DeleteMethod · 0.65

Tested by

no test coverage detected