refactor: reuse open file in storage interface (#16970)

This commit is contained in:
ferhat elmas 2023-04-10 08:09:28 +02:00 committed by GitHub
parent 25f7a8e406
commit 056ca0c68e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1671,35 +1671,10 @@ func (s *xlStorage) openFileDirect(path string, mode int) (f *os.File, err error
}
func (s *xlStorage) openFileSync(filePath string, mode int) (f *os.File, err error) {
// Create top level directories if they don't exist.
// with mode 0777 mkdir honors system umask.
if err = mkdirAll(pathutil.Dir(filePath), 0o777); err != nil {
return nil, osErrToFileErr(err)
}
w, err := OpenFile(filePath, mode|writeMode, 0o666)
if err != nil {
// File path cannot be verified since one of the parents is a file.
switch {
case isSysErrIsDir(err):
return nil, errIsNotRegular
case osIsPermission(err):
return nil, errFileAccessDenied
case isSysErrNotDir(err):
return nil, errFileAccessDenied
case isSysErrIO(err):
return nil, errFaultyDisk
case isSysErrTooManyFiles(err):
return nil, errTooManyOpenFiles
default:
return nil, err
}
}
return w, nil
return s.openFile(filePath, mode|writeMode)
}
func (s *xlStorage) openFileNoSync(filePath string, mode int) (f *os.File, err error) {
func (s *xlStorage) openFile(filePath string, mode int) (f *os.File, err error) {
// Create top level directories if they don't exist.
// with mode 0777 mkdir honors system umask.
if err = mkdirAll(pathutil.Dir(filePath), 0o777); err != nil {
@ -1950,7 +1925,7 @@ func (s *xlStorage) writeAll(ctx context.Context, volume string, path string, b
}
w, err = s.openFileSync(filePath, flags)
} else {
w, err = s.openFileNoSync(filePath, flags)
w, err = s.openFile(filePath, flags)
}
if err != nil {
return err