2017-05-22 03:28:31 +00:00
|
|
|
// Copyright 2017 syzkaller project authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
// syz-fmt re-formats sys files into standard form.
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/google/syzkaller/pkg/ast"
|
2018-12-17 15:56:39 +00:00
|
|
|
"github.com/google/syzkaller/pkg/osutil"
|
2018-11-17 19:46:48 +00:00
|
|
|
"github.com/google/syzkaller/sys/targets"
|
2017-05-22 03:28:31 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
if len(os.Args) < 2 {
|
2018-11-17 19:46:48 +00:00
|
|
|
fmt.Fprintf(os.Stderr, "usage: syz-fmt files... or dirs... or all\n")
|
2017-05-22 03:28:31 +00:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
2018-11-17 19:46:48 +00:00
|
|
|
args := os.Args[1:]
|
|
|
|
if len(args) == 1 && args[0] == "all" {
|
|
|
|
args = nil
|
|
|
|
for os := range targets.List {
|
|
|
|
args = append(args, filepath.Join("sys", os))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, arg := range args {
|
2017-05-22 03:28:31 +00:00
|
|
|
st, err := os.Stat(arg)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "failed to stat %v: %v\n", arg, err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
if st.IsDir() {
|
|
|
|
files, err := ioutil.ReadDir(arg)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "failed to read dir %v: %v\n", arg, err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
for _, file := range files {
|
|
|
|
if !strings.HasSuffix(file.Name(), ".txt") {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
processFile(filepath.Join(arg, file.Name()), file.Mode())
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
processFile(arg, st.Mode())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func processFile(file string, mode os.FileMode) {
|
|
|
|
data, err := ioutil.ReadFile(file)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "failed to read file %v: %v\n", file, err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
2017-08-27 17:55:14 +00:00
|
|
|
desc := ast.Parse(data, filepath.Base(file), nil)
|
2017-08-26 19:36:08 +00:00
|
|
|
if desc == nil {
|
2017-05-22 03:28:31 +00:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
2017-08-26 19:36:08 +00:00
|
|
|
formatted := ast.Format(desc)
|
2017-05-22 03:28:31 +00:00
|
|
|
if bytes.Equal(data, formatted) {
|
|
|
|
return
|
|
|
|
}
|
2017-08-18 09:14:20 +00:00
|
|
|
fmt.Printf("reformatting %v\n", file)
|
2018-12-17 15:56:39 +00:00
|
|
|
if err := osutil.Rename(file, file+"~"); err != nil {
|
2017-05-22 03:28:31 +00:00
|
|
|
fmt.Fprintf(os.Stderr, "%v\n", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
if err := ioutil.WriteFile(file, formatted, mode); err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "%v\n", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}
|