github.com/NVIDIA/aistore@v1.3.23-0.20240517131212-7df6609be51d/core/lfile.go (about) 1 // Package core provides core metadata and in-cluster API 2 /* 3 * Copyright (c) 2018-2024, NVIDIA CORPORATION. All rights reserved. 4 */ 5 package core 6 7 import ( 8 "fmt" 9 "os" 10 "path/filepath" 11 12 "github.com/NVIDIA/aistore/cmn" 13 "github.com/NVIDIA/aistore/cmn/cos" 14 "github.com/NVIDIA/aistore/cmn/debug" 15 ) 16 17 func (lom *LOM) OpenFile() (*os.File, error) { 18 return os.Open(lom.FQN) 19 } 20 21 // (compare with cos.CreateFile) 22 func (lom *LOM) CreateFile(fqn string) (fh *os.File, err error) { 23 fh, err = os.OpenFile(fqn, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, cos.PermRWR) 24 if err == nil || !os.IsNotExist(err) { 25 return 26 } 27 return lom._cf(fqn, os.O_WRONLY) 28 } 29 30 // slow path 31 func (lom *LOM) _cf(fqn string, mode int) (fh *os.File, err error) { 32 bdir := lom.mi.MakePathBck(lom.Bucket()) 33 if err = cos.Stat(bdir); err != nil { 34 return nil, fmt.Errorf("%s (bdir %s): %w", lom, bdir, err) 35 } 36 fdir := filepath.Dir(fqn) 37 if err = cos.CreateDir(fdir); err != nil { 38 return 39 } 40 fh, err = os.OpenFile(fqn, os.O_CREATE|mode|os.O_TRUNC, cos.PermRWR) 41 return 42 } 43 44 func (lom *LOM) CreateFileRW(fqn string) (fh *os.File, err error) { 45 fh, err = os.OpenFile(fqn, os.O_CREATE|os.O_RDWR|os.O_TRUNC, cos.PermRWR) 46 if err == nil || !os.IsNotExist(err) { 47 return 48 } 49 return lom._cf(fqn, os.O_RDWR) 50 } 51 52 func (lom *LOM) Remove(force ...bool) (err error) { 53 // making "rlock" exception to be able to (forcefully) remove corrupted obj in the GET path 54 debug.AssertFunc(func() bool { 55 rc, exclusive := lom.IsLocked() 56 return exclusive || (len(force) > 0 && force[0] && rc > 0) 57 }) 58 lom.Uncache() 59 err = cos.RemoveFile(lom.FQN) 60 if os.IsNotExist(err) { 61 err = nil 62 } 63 for copyFQN := range lom.md.copies { 64 if erc := cos.RemoveFile(copyFQN); erc != nil && !os.IsNotExist(erc) { 65 err = erc 66 } 67 } 68 lom.md.bckID = 0 69 return err 70 } 71 72 // (compare with cos.Rename) 73 func (lom *LOM) RenameFrom(workfqn string) error { 74 bdir := lom.mi.MakePathBck(lom.Bucket()) 75 if err := cos.Stat(bdir); err != nil { 76 return fmt.Errorf("%s(bdir: %s): %w", lom, bdir, err) 77 } 78 if err := cos.Rename(workfqn, lom.FQN); err != nil { 79 return cmn.NewErrFailedTo(T, "finalize", lom.Cname(), err) 80 } 81 return nil 82 }