Skip to content

Commit

Permalink
HW07 is completed. work on mistakes
Browse files Browse the repository at this point in the history
  • Loading branch information
DimVlas committed Jul 2, 2024
1 parent bcfff5d commit dab24cc
Show file tree
Hide file tree
Showing 3 changed files with 183 additions and 202 deletions.
113 changes: 33 additions & 80 deletions hw07_file_copying/copy.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,117 +13,70 @@ var (
)

func Copy(fromPath, toPath string, offset, limit int64) error {
fromFile, fromSize, err := fileOpen(fromPath, offset)
// fromFile open and check
fromFile, err := os.Open(fromPath)
if err != nil {
return fmt.Errorf("fromFileOpen: %w", err)
}
defer func() {
if fromFile == nil {
return
}
if err := fromFile.Close(); err != nil {
panic(err)
}
}()
defer fromFile.Close()

toFile, err := fileCreate(toPath)
fromInfo, err := fromFile.Stat()
if err != nil {
return fmt.Errorf("toFileCreate: %w", err)
}
defer func() {
if toFile == nil {
return
}
if err := toFile.Close(); err != nil {
panic(err)
}
}()

copySize := fromSize - offset
if limit > 0 && limit < copySize {
copySize = limit
return fmt.Errorf("fromFile: %w", err)
}

if offset > 0 {
_, err = fromFile.Seek(offset, 0)
if err != nil {
return err
}
if !fromInfo.Mode().IsRegular() {
return fmt.Errorf("fromFile: %w", ErrUnsupportedFile)
}

err = copyRW(fromFile, toFile, copySize)
if err != nil {
return err
fromSize := fromInfo.Size()
if offset >= fromSize {
return fmt.Errorf("fromFile: %w", ErrOffsetExceedsFileSize)
}

return nil
}

func fileOpen(fromPath string, offset int64) (*os.File, int64, error) {
file, err := os.Open(fromPath)
// toFile open and check
toFile, err := os.Create(toPath)
if err != nil {
return nil, 0, err
return fmt.Errorf("toFileCreate: %w", err)
}
defer toFile.Close()

defer func() {
// закрыть файл, если ошибка
if err != nil {
if errCl := file.Close(); errCl != nil {
panic(errCl)
}
}
}()

fileInfo, err := file.Stat()
toInfo, err := toFile.Stat()
if err != nil {
return nil, 0, err
}

if !fileInfo.Mode().IsRegular() {
err = ErrUnsupportedFile
return nil, 0, err
return fmt.Errorf("toFile: %w", err)
}

size := fileInfo.Size()
if offset >= size {
err = ErrOffsetExceedsFileSize
return nil, 0, err
if !toInfo.Mode().IsRegular() {
return fmt.Errorf("toFile: %w", ErrUnsupportedFile)
}

return file, size, nil
}

func fileCreate(toPath string) (*os.File, error) {
file, err := os.Create(toPath)
if err != nil {
return nil, err
// prepare copy
copySize := fromSize - offset
if limit > 0 && limit < copySize {
copySize = limit
}

defer func() {
// закрыть файл, если ошибка
if offset > 0 {
_, err = fromFile.Seek(offset, 0)
if err != nil {
errCl := file.Close()
if errCl != nil {
panic(errCl)
}
return fmt.Errorf("fromFile: %w", err)
}
}()

fileInfo, err := file.Stat()
if err != nil {
return nil, err
}

if !fileInfo.Mode().IsRegular() {
return nil, ErrUnsupportedFile
err = copyRW(fromFile, toFile, copySize)
if err != nil {
return fmt.Errorf("copyRW: %w", err)
}

return file, nil
return toFile.Sync()
}

// значение по умолчанию кол-ва байт копируемых за одну итерацию. из io.ReadAll.
const defCopySize int64 = 512

func copyRW(from io.Reader, to io.Writer, size int64) error {
var (
copySize int64 = 512 // кол-во байт копируемых за одну итерацию. из io.ReadAll
copySize = defCopySize // кол-во байт копируемых за одну итерацию.
copied int64
)
if size < copySize {
Expand Down
Loading

0 comments on commit dab24cc

Please sign in to comment.