Skip to content

Commit

Permalink
chore: add error handling for NewFile
Browse files Browse the repository at this point in the history
  • Loading branch information
alipourhabibi committed Dec 15, 2023
1 parent 6fc08f3 commit 8fb8935
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
11 changes: 9 additions & 2 deletions http/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,10 @@ func (s *server) DownloadOne(resourse string, size int) error {
return err
}
index := 0
w := writer.NewFile(resourse)
w, err := writer.NewFile(resourse)
if err != nil {
return err
}
var header []byte
if strings.Contains(string(resp), "\r\n\r\n") {
header = []byte(strings.Split(string(resp), "\r\n\r\n")[0])
Expand Down Expand Up @@ -199,7 +202,11 @@ func save(serverFD int, serverAddr *syscall.SockaddrInet4, file string, offset,
}

b := offset * msgSize
w := writer.NewFile(file)
w, err := writer.NewFile(file)
if err != nil {
slog.Error("NewFile save", "error", err)
return
}
defer w.Close()
var msg string
msg = fmt.Sprintf("GET /%s HTTP/1.1\r\nHOST: localhost\r\nConnection: keep-alive\r\nRange: bytes=%d-%d\r\n\r\n", file, b, b+size)
Expand Down
6 changes: 3 additions & 3 deletions writer/write.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ type file struct {
fd int
}

func NewFile(f string) *file {
func NewFile(f string) (*file, error) {
fd, err := syscall.Open(f, syscall.O_RDWR, 0644)
if err != nil {
//TODO
return nil, err
}
return &file{
fd: fd,
}
}, nil
}

func Create(file string) error {
Expand Down

0 comments on commit 8fb8935

Please sign in to comment.