AddNotefile adds a new notefile to the notebox, and return "nil" if it already exists
(ctx context.Context, notefileID string, notefileInfo *note.NotefileInfo)
| 832 | |
| 833 | // AddNotefile adds a new notefile to the notebox, and return "nil" if it already exists |
| 834 | func (box *Notebox) AddNotefile(ctx context.Context, notefileID string, notefileInfo *note.NotefileInfo) error { |
| 835 | // First, do an immediate check to see if it already exists. If so, short circuit everything |
| 836 | // and return a clean "no error", which is relied upon by callers. |
| 837 | if box.NotefileExists(notefileID) { |
| 838 | |
| 839 | // Refresh the notefile info, which may likely have changed |
| 840 | if notefileInfo == nil { |
| 841 | notefileInfo = ¬e.NotefileInfo{} |
| 842 | } |
| 843 | |
| 844 | return box.SetNotefileInfo(ctx, notefileID, *notefileInfo) |
| 845 | |
| 846 | } |
| 847 | |
| 848 | // Find out if it's a queue |
| 849 | isQueue, _, _, _, _, err := NotefileAttributesFromID(notefileID) |
| 850 | if err != nil { |
| 851 | return err |
| 852 | } |
| 853 | |
| 854 | // Add the notefile |
| 855 | box.Openfile().lock.Lock() |
| 856 | defer box.Openfile().lock.Unlock() |
| 857 | |
| 858 | // Re-check existence while holding the lock to prevent race conditions |
| 859 | if box.NotefileExists(notefileID) { |
| 860 | // Refresh the notefile info, which may likely have changed |
| 861 | if notefileInfo == nil { |
| 862 | notefileInfo = ¬e.NotefileInfo{} |
| 863 | } |
| 864 | return box.SetNotefileInfo(ctx, notefileID, *notefileInfo) |
| 865 | } |
| 866 | |
| 867 | boxfile := box.Notefile() |
| 868 | boxfile.lock.Lock() |
| 869 | err = box.uAddNotefile(ctx, notefileID, notefileInfo, CreateNotefile(isQueue)) |
| 870 | boxfile.lock.Unlock() |
| 871 | |
| 872 | // Done |
| 873 | return err |
| 874 | } |
| 875 | |
| 876 | // GetNotefileInfo retrieves info about a specific notefile |
| 877 | func (box *Notebox) GetNotefileInfo(notefileID string) (notefileInfo note.NotefileInfo, err error) { |