Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: catch some close error #34

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 25 additions & 15 deletions auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"encoding/binary"
"errors"
"io"
"log"
"os"
)

Expand All @@ -34,7 +35,7 @@ func readAuthority(hostname, display string) (
if len(hostname) == 0 || hostname == "localhost" {
hostname, err = os.Hostname()
if err != nil {
return "", nil, err
return
}
}

Expand All @@ -43,49 +44,58 @@ func readAuthority(hostname, display string) (
home := os.Getenv("HOME")
if len(home) == 0 {
err = errors.New("Xauthority not found: $XAUTHORITY, $HOME not set")
return "", nil, err
return
}
fname = home + "/.Xauthority"
}

r, err := os.Open(fname)
if err != nil {
return "", nil, err
return
}
defer r.Close()
defer func() {
closeErr := r.Close()
if err == nil {
err = closeErr
} else {
log.Printf("error on close file %v: %v", r.Name(), closeErr)
}
}()

for {
var family uint16
if err := binary.Read(r, binary.BigEndian, &family); err != nil {
return "", nil, err
if err = binary.Read(r, binary.BigEndian, &family); err != nil {
return
}

addr, err := getString(r, b)
var addr string
addr, err = getString(r, b)
if err != nil {
return "", nil, err
return
}

disp, err := getString(r, b)
var disp string
disp, err = getString(r, b)
if err != nil {
return "", nil, err
return
}

name0, err := getString(r, b)
name, err = getString(r, b)
if err != nil {
return "", nil, err
return
}

data0, err := getBytes(r, b)
data, err = getBytes(r, b)
if err != nil {
return "", nil, err
return
}

addrmatch := (family == familyWild) ||
(family == familyLocal && addr == hostname)
dispmatch := (disp == "") || (disp == display)

if addrmatch && dispmatch {
return name0, data0, nil
return
}
}
}
Expand Down
51 changes: 35 additions & 16 deletions util/cursor/cursor.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"image/color/palette"
"image/draw"
"image/gif"
"log"
"os"

"github.com/linuxdeepin/go-x11-client"
Expand Down Expand Up @@ -62,30 +63,39 @@ func LoadImageFromFile(filename string, size int) (*Image, error) {
return loadImageFromFile(f, size)
}

func loadImageFromFile(f *os.File, size int) (*Image, error) {
func loadImageFromFile(f *os.File, size int) (img *Image, err error) {
d, err := newDecoder(f)
if err != nil {
return nil, err
return
}
defer d.close()
defer func() {
closeErr := d.close()
if err == nil {
err = closeErr
} else {
log.Printf("error on close decoder %v: %v", f.Name(), closeErr)
}
}()

bestSize, _ := d.findBestSize(size)
if bestSize == 0 {
return nil, errImageNotFound
err = errImageNotFound
return
}

for _, toc := range d.tocs {
if toc.Type == typeImage && int(toc.Subtype) == bestSize {
img, err := d.readImage(toc)
img, err = d.readImage(toc)
if err != nil {
return nil, err
return
}

return img, nil
return
}
}

return nil, errImageNotFound
err = errImageNotFound
return
}

func dist(a, b int) int {
Expand Down Expand Up @@ -121,32 +131,41 @@ func LoadImagesFromFile(filename string, size int) (Images, error) {
return loadImagesFromFile(f, size)
}

func loadImagesFromFile(f *os.File, size int) (Images, error) {
func loadImagesFromFile(f *os.File, size int) (images Images, err error) {
d, err := newDecoder(f)
if err != nil {
return nil, err
return
}
defer d.close()
defer func() {
closeErr := d.close()
if err == nil {
err = closeErr
} else {
log.Printf("error on close decoder %v: %v", f.Name(), closeErr)
}
}()

bestSize, nSizes := d.findBestSize(size)
if bestSize == 0 {
return nil, errImageNotFound
err = errImageNotFound
return
}

images := make(Images, nSizes)
images = make(Images, nSizes)
var idx int
for _, toc := range d.tocs {
if toc.Type == typeImage && int(toc.Subtype) == bestSize {
img, err := d.readImage(toc)
var img *Image
img, err = d.readImage(toc)
if err != nil {
return nil, err
return
}

images[idx] = img
idx++
}
}
return images, nil
return
}

func (images Images) ToGIF() *gif.GIF {
Expand Down
12 changes: 10 additions & 2 deletions util/tool/gen-keysymdef/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,11 @@ func processKeysymdef() []Keysym {
if err != nil {
log.Fatal(err)
}
defer fh.Close()
defer func() {
if err := fh.Close(); err != nil {
log.Fatal(err)
}
}()

var currentSection string
var currentSectionOk bool = true
Expand Down Expand Up @@ -191,7 +195,11 @@ func processXF86Keysymdef() []Keysym {
if err != nil {
log.Fatal(err)
}
defer fh.Close()
defer func() {
if err := fh.Close(); err != nil {
log.Fatal(err)
}
}()
scanner := bufio.NewScanner(fh)
var defOtherCount int
var def3Count int
Expand Down