Golang Create示例

说明

golang create示例是从最受好评的开源项目中提取的实现代码,你可以参考下面示例的使用方式。

编程语言: Golang

命名空间/包名称: os

示例#1
文件: util.go 项目: sreis/go

func startProfile() {
	if cpuprofile != "" {
		f, err := os.Create(cpuprofile)
		if err != nil {
			log.Fatalf("%v", err)
		}
		if err := pprof.StartCPUProfile(f); err != nil {
			log.Fatalf("%v", err)
		}
		AtExit(pprof.StopCPUProfile)
	}
	if memprofile != "" {
		if memprofilerate != 0 {
			runtime.MemProfileRate = int(memprofilerate)
		}
		f, err := os.Create(memprofile)
		if err != nil {
			log.Fatalf("%v", err)
		}
		AtExit(func() {
			runtime.GC() // profile all outstanding allocations
			if err := pprof.WriteHeapProfile(f); err != nil {
				log.Fatalf("%v", err)
			}
		})
	}
}

示例#2
文件: zfs_snapshots_test.go 项目: ably-forks/flynn

func (s *ZfsSnapshotTests) TestSnapshotShouldIsolateNewChangesToSource(c *C) {
	v, err := s.VolProv.NewVolume()
	c.Assert(err, IsNil)

	// a new volume should start out empty:
	c.Assert(v.Location(), testutils.DirContains, []string{})

	f, err := os.Create(filepath.Join(v.Location(), "alpha"))
	c.Assert(err, IsNil)
	f.Close()

	// sanity check, can we so much as even write a file:
	c.Assert(v.Location(), testutils.DirContains, []string{"alpha"})

	v2, err := s.VolProv.CreateSnapshot(v)
	c.Assert(err, IsNil)

	// write another file to the source
	f, err = os.Create(filepath.Join(v.Location(), "beta"))
	c.Assert(err, IsNil)
	f.Close()

	// the source dir should contain our changes:
	c.Assert(v.Location(), testutils.DirContains, []string{"alpha", "beta"})
	// the snapshot should be unaffected:
	c.Assert(v2.Location(), testutils.DirContains, []string{"alpha"})
}

示例#3
文件: archive_test.go 项目: truedays/docker

// Do the same test as above but with the destination as file, it should fail
func TestUntarPathWithDestinationFile(t *testing.T) {
	tmpFolder, err := ioutil.TempDir("", "docker-archive-test")
	if err != nil {
		t.Fatal(err)
	}
	defer os.RemoveAll(tmpFolder)
	srcFile := filepath.Join(tmpFolder, "src")
	tarFile := filepath.Join(tmpFolder, "src.tar")
	os.Create(filepath.Join(tmpFolder, "src"))

	// Translate back to Unix semantics as next exec.Command is run under sh
	srcFileU := srcFile
	tarFileU := tarFile
	if runtime.GOOS == "windows" {
		tarFileU = "/tmp/" + filepath.Base(filepath.Dir(tarFile)) + "/src.tar"
		srcFileU = "/tmp/" + filepath.Base(filepath.Dir(srcFile)) + "/src"
	}
	cmd := exec.Command("sh", "-c", "tar cf "+tarFileU+" "+srcFileU)
	_, err = cmd.CombinedOutput()
	if err != nil {
		t.Fatal(err)
	}
	destFile := filepath.Join(tmpFolder, "dest")
	_, err = os.Create(destFile)
	if err != nil {
		t.Fatalf("Fail to create the destination file")
	}
	err = UntarPath(tarFile, destFile)
	if err == nil {
		t.Fatalf("UntarPath should throw an error if the destination if a file")
	}
}

示例#4
文件: data_savers.go 项目: jcla1/wikipedia_analyser

func saveSplitPages(input <-chan *PageContainer) {
	featuredFile, err := os.Create(featuredFilePath)
	if err != nil {
		panic(err)
	}
	featuredCompressed := gzip.NewWriter(featuredFile)
	defer featuredFile.Close()

	normalFile, err := os.Create(normalFilePath)
	if err != nil {
		panic(err)
	}
	normalCompressed := gzip.NewWriter(normalFile)
	defer normalFile.Close()

	featuredChannel, featuredWriter := ArticleWriter(featuredCompressed)
	normalChannel, normalWriter := ArticleWriter(normalCompressed)
	//featuredChannel, featuredWriter := ArticleWriter(featuredFile)
	//normalChannel, normalWriter := ArticleWriter(normalFile)

	DistrbuteArticles(input, featuredChannel, normalChannel)

	// Wait for all writers to finish
	<-featuredWriter
	<-normalWriter

	// Close all the gzip streams
	// I tried defering the close
	// call but kept getting some EOF errors
	featuredCompressed.Close()
	normalCompressed.Close()
}

示例#5
文件: profile.go 项目: rhino1998/god

func main() {
	f, err := os.Create("cpuprofile")
	if err != nil {
		panic(err.Error())
	}
	f2, err := os.Create("memprofile")
	if err != nil {
		panic(err.Error())
	}

	pprof.StartCPUProfile(f)
	defer pprof.StopCPUProfile()
	defer pprof.WriteHeapProfile(f2)

	m := radix.NewTree()
	var k []byte
	for i := 0; i < 100000; i++ {
		k = murmur.HashString(fmt.Sprint(i))
		m.Put(k, k, 1)
		x, _, _ := m.Get(k)
		if bytes.Compare(x, k) != 0 {
			panic("humbug")
		}
	}
}

示例#6
文件: util.go 项目: Comcast/rulio

// Profile starts CPU and memory profiling and returns a function that will stop that.
//
//
// Writes "cpu" + filename and "mem" + filename.
//
// Usage:
//
//   defer Profile("logfast.prof")()
//
func Profile(filename string) func() {
	cpu, err := os.Create("cpu" + filename)
	if err != nil {
		panic(err)
	}

	mem, err := os.Create("mem" + filename)
	if err != nil {
		panic(err)
	}

	then := time.Now()
	log.Printf("Profile starting %s\n", filename)
	pprof.StartCPUProfile(cpu)
	pprof.WriteHeapProfile(mem)

	return func() {
		elapsed := time.Now().Sub(then)
		log.Printf("Profile stopping %s (elapsed %v)\n", filename, elapsed)
		pprof.StopCPUProfile()
		if err := mem.Close(); err != nil {
			log.Printf("error closing mem profile (%v)", err)
		}
	}
}

示例#7
文件: main_search.go 项目: verdverm/go-pge

func (DS *MainSearch) initLogs(logdir string) {

	// open logs
	DS.logDir = logdir
	os.Mkdir(DS.logDir, os.ModePerm)
	tmpF0, err5 := os.Create(DS.logDir + "main:err.log")
	if err5 != nil {
		log.Fatal("couldn't create errs log", err5)
	}
	DS.errLogBuf = bufio.NewWriter(tmpF0)
	DS.errLogBuf.Flush()
	DS.errLog = log.New(DS.errLogBuf, "", log.LstdFlags)

	tmpF1, err1 := os.Create(DS.logDir + "main:main.log")
	if err1 != nil {
		log.Fatal("couldn't create main log", err1)
	}
	DS.mainLogBuf = bufio.NewWriter(tmpF1)
	DS.mainLogBuf.Flush()
	DS.mainLog = log.New(DS.mainLogBuf, "", log.LstdFlags)

	tmpF2, err2 := os.Create(DS.logDir + "main:eqns.log")
	if err2 != nil {
		log.Fatal("couldn't create eqns log", err2)
	}
	DS.eqnsLogBuf = bufio.NewWriter(tmpF2)
	DS.eqnsLogBuf.Flush()
	DS.eqnsLog = log.New(DS.eqnsLogBuf, "", log.LstdFlags)

}

示例#8
文件: xor.go 项目: ninedraft/xorefy

func encrypt_part_of_file(input_file io.ReaderAt, key_filename, output_filename string, chunk_len, chunk_num, from int64) {
	data := make([]byte, chunk_len)
	key := make([]byte, chunk_len)
	xored_data := make([]byte, chunk_len)
	var err error
	output_file, err := os.Create(output_filename)
	check(err)
	defer output_file.Close()

	key_file, err := os.Create(key_filename)
	check(err)
	defer output_file.Close()

	for i := int64(0); i < chunk_num; i++ {
		n, err := input_file.ReadAt(data, from+i*chunk_len)
		if err != nil && err != io.EOF {
			panic(err)
		}

		_, err = rand.Read(key[:n])
		check(err)
		_, err = key_file.WriteAt(key[:n], from+i*chunk_len)
		check(err)

		xor(data[:n], key[:n], xored_data[:n])
		_, err = output_file.WriteAt(xored_data[:n], from+i*chunk_len)
		check(err)
	}
}

示例#9
文件: bgproute_test.go 项目: JReyLBC/jresponse

func TestWriteCLITo(t *testing.T) {

	modelBuf := bytes.Buffer{}

	if err := bgpRouteXMLModel.WriteCLITo(&modelBuf); err != nil {
		t.Error(err)
	}

	fileBuf := bytes.Buffer{}
	if file, err := os.Open(BGP_CLI_FILE); err != nil {
		t.Error(err)
	} else if _, err := fileBuf.ReadFrom(file); err != nil {
		t.Error(err)
	}

	if !bytes.Equal(modelBuf.Bytes(), fileBuf.Bytes()) {
		f1, f2 := "model.cli", "current.cli"
		if file1, err := os.Create(f1); err != nil {
			t.Error(err)
		} else if file2, err := os.Create(f2); err != nil {
			t.Error(err)
		} else {
			t.Logf("writing output files for diff: %s, %s", f1, f2)
			file1.Write(modelBuf.Bytes())
			file2.Write(fileBuf.Bytes())
			t.Error("model for CLI parse does not match")
		}
	}
}

示例#10
文件: main.go 项目: gulinfang/GarageEngine

func main() {
	runtime.GOMAXPROCS(8)
	flag.Parse()
	if *cpuprofile != "" {
		f, err := os.Create(*cpuprofile)
		if err != nil {
			fmt.Errorf("%s\n", err)
		}
		pprof.StartCPUProfile(f)

		defer pprof.StopCPUProfile()
	}

	file, _ := os.Create("./log.txt")
	os.Stdout = file
	os.Stderr = file
	os.Stdin = file
	defer file.Close()

	Start()

	if *memprofile != "" {
		f, err := os.Create(*memprofile)
		if err != nil {
			fmt.Errorf("%s\n", err)
		}
		pprof.WriteHeapProfile(f)
		f.Close()
		return
	}
}

示例#11
文件: main.go 项目: cdupuis/strava

// Starts all profiles set on the options.
func (cmd *BenchCommand) startProfiling(options *BenchOptions) {
	var err error

	// Start CPU profiling.
	if options.CPUProfile != "" {
		cpuprofile, err = os.Create(options.CPUProfile)
		if err != nil {
			fmt.Fprintf(cmd.Stderr, "bench: could not create cpu profile %q: %v\n", options.CPUProfile, err)
			os.Exit(1)
		}
		pprof.StartCPUProfile(cpuprofile)
	}

	// Start memory profiling.
	if options.MemProfile != "" {
		memprofile, err = os.Create(options.MemProfile)
		if err != nil {
			fmt.Fprintf(cmd.Stderr, "bench: could not create memory profile %q: %v\n", options.MemProfile, err)
			os.Exit(1)
		}
		runtime.MemProfileRate = 4096
	}

	// Start fatal profiling.
	if options.BlockProfile != "" {
		blockprofile, err = os.Create(options.BlockProfile)
		if err != nil {
			fmt.Fprintf(cmd.Stderr, "bench: could not create block profile %q: %v\n", options.BlockProfile, err)
			os.Exit(1)
		}
		runtime.SetBlockProfileRate(1)
	}
}

示例#12
文件: bin2go.go 项目: Nightgunner5/bin2go

func writeOutput(filename string, data []byte) {
	var file *os.File
	var err error

	// prepare output file
	if *out == "" {
		file, err = os.Create(filename + ".go")
		checkOutputFailure(err)
		defer file.Close()
	} else {
		file, err = os.Create(*out)
		checkOutputFailure(err)
		defer file.Close()
	}

	output := bufio.NewWriter(file)

	// write package clause if any
	if *pkg == "" {
		path, err := filepath.Abs(*out)
		checkOutputFailure(err)
		_, err = fmt.Fprintf(output, "package %s\n\n", filepath.Base(filepath.Dir(path)))
		checkOutputFailure(err)
	} else {
		_, err = fmt.Fprintf(output, "package %s\n\n", *pkg)
		checkOutputFailure(err)
	}

	// write data
	writeData(filename, data, output)

	// flush
	err = output.Flush()
	checkOutputFailure(err)
}

示例#13
文件: deploy.go 项目: kevinxusz/shubc

// Build an egg and returns:
//  => egg file path, tmpdir with egg creation data, error in case exists
func BuildEgg() (string, string, error) {
	closest_cfg := closest_scrapy_cfg(".", "")
	err := os.Chdir(filepath.Dir(closest_cfg))
	if err != nil {
		return "", "", err
	}
	// if not exists 'setup.py'
	if _, err := os.Stat("setup.py"); err != nil {
		settings, ok := scrapy_get_config()["settings"]
		if !ok {
			return "", "", errors.New("BuildEgg: No 'settings' section found on scrapy.cfg")
		}
		createDefaultSetupPy(settings["default"])
	}
	tmpdir, err := ioutil.TempDir(os.TempDir(), "shubc-deploy")
	if err != nil {
		return "", "", errors.New("BuildEgg: Can't create temporary directory")
	}
	cmd := exec.Command("python", "setup.py", "clean", "-a", "bdist_egg", "-d", tmpdir)
	tout, _ := os.Create(filepath.Join(tmpdir, "stdout"))
	terr, _ := os.Create(filepath.Join(tmpdir, "stderr"))
	cmd.Stdout = tout
	cmd.Stderr = terr
	if err := cmd.Run(); err != nil {
		return "", tmpdir, errors.New(fmt.Sprintf("BuildEgg: Can't create egg file - details: %s", err))
	}
	matches, err := filepath.Glob(filepath.Join(tmpdir, "*.egg"))
	if err != nil {
		return "", tmpdir, errors.New(fmt.Sprintf("BuildEgg: No '.egg' file foun on %d", tmpdir))
	}
	tout.Close()
	terr.Close()
	return matches[0], tmpdir, nil
}

示例#14
文件: massAccess.go 项目: ASPecherkin/Experiments

func main() {
	flag.Parse()
	if *cpuprofile != "" {
		f, err := os.Create(*cpuprofile)
		if err != nil {
			fmt.Println("Error: ", err)
		}
		pprof.StartCPUProfile(f)
		defer pprof.StopCPUProfile()
	}
	if *memprofile != "" {
		f, err := os.Create(*memprofile)
		if err != nil {
			fmt.Println("Error: ", err)
		}
		pprof.WriteHeapProfile(f)
	}
	var wg sync.WaitGroup
	cidrs, err := parseCidrs("./hosts.json")
	if err != nil {
		log.Fatal(err)
	}
	servers := make([]Server, 0, 10)
	for k := range cidrs.Cidrs {
		servers = append(servers, Server{Cidr: cidrs.Cidrs[k]})
	}
	fmt.Println(len(servers))
	for k := range servers {
		wg.Add(1)
		go servers[k].checkIP(&wg)
	}
	wg.Wait()
}

示例#15
文件: restore_test.go 项目: AlexisBruemmer/juju

func (r *RestoreSuite) createTestFiles(c *gc.C) {
	tarDirE := path.Join(r.cwd, "TarDirectoryEmpty")
	err := os.Mkdir(tarDirE, os.FileMode(0755))
	c.Check(err, jc.ErrorIsNil)

	tarDirP := path.Join(r.cwd, "TarDirectoryPopulated")
	err = os.Mkdir(tarDirP, os.FileMode(0755))
	c.Check(err, jc.ErrorIsNil)

	tarSubFile1 := path.Join(tarDirP, "TarSubFile1")
	tarSubFile1Handle, err := os.Create(tarSubFile1)
	c.Check(err, jc.ErrorIsNil)
	tarSubFile1Handle.WriteString("TarSubFile1")
	tarSubFile1Handle.Close()

	tarSubDir := path.Join(tarDirP, "TarDirectoryPopulatedSubDirectory")
	err = os.Mkdir(tarSubDir, os.FileMode(0755))
	c.Check(err, jc.ErrorIsNil)

	tarFile1 := path.Join(r.cwd, "TarFile1")
	tarFile1Handle, err := os.Create(tarFile1)
	c.Check(err, jc.ErrorIsNil)
	tarFile1Handle.WriteString("TarFile1")
	tarFile1Handle.Close()

	tarFile2 := path.Join(r.cwd, "TarFile2")
	tarFile2Handle, err := os.Create(tarFile2)
	c.Check(err, jc.ErrorIsNil)
	tarFile2Handle.WriteString("TarFile2")
	tarFile2Handle.Close()
	r.testFiles = []string{tarDirE, tarDirP, tarFile1, tarFile2}
}

示例#16
文件: goprofile.go 项目: elivoa/gotapestry

func test() {

	f, _ := os.Create("ls.prof")

	flag.Parse()
	if *cpuprofile != "" {
		f, err := os.Create(*cpuprofile)
		if err != nil {
			log.Fatal(err)
		}
		pprof.StartCPUProfile(f)
		defer func() {
			pprof.StopCPUProfile()
		}()
	}

	// ...
	t := &T{}
	for i := 0; i < 1000000; i++ {
		v := reflect.ValueOf(t)
		if v.Kind() == reflect.Ptr {
			v = v.Elem()
		}
		t.B = append(t.B, i)
		t.A = append(t.A, sumtest())
	}

	// var buffer bytes.Buffer
	pprof.WriteHeapProfile(f)
	// fmt.Println(buffer.String())

}

示例#17
文件: cni_test.go 项目: johndmulhausen/kubernetes

func installPluginUnderTest(t *testing.T, testVendorCNIDirPrefix, testNetworkConfigPath, vendorName string, plugName string) {
	pluginDir := path.Join(testNetworkConfigPath, plugName)
	err := os.MkdirAll(pluginDir, 0777)
	if err != nil {
		t.Fatalf("Failed to create plugin config dir: %v", err)
	}
	pluginConfig := path.Join(pluginDir, plugName+".conf")
	f, err := os.Create(pluginConfig)
	if err != nil {
		t.Fatalf("Failed to install plugin")
	}
	networkConfig := fmt.Sprintf("{ \"name\": \"%s\", \"type\": \"%s\" }", plugName, vendorName)

	_, err = f.WriteString(networkConfig)
	if err != nil {
		t.Fatalf("Failed to write network config file (%v)", err)
	}
	f.Close()

	vendorCNIDir := fmt.Sprintf(VendorCNIDirTemplate, testVendorCNIDirPrefix, vendorName)
	err = os.MkdirAll(vendorCNIDir, 0777)
	if err != nil {
		t.Fatalf("Failed to create plugin dir: %v", err)
	}
	pluginExec := path.Join(vendorCNIDir, vendorName)
	f, err = os.Create(pluginExec)

	const execScriptTempl = `#!/bin/bash
read ignore
env > {{.OutputEnv}}
echo "%@" >> {{.OutputEnv}}
export $(echo ${CNI_ARGS} | sed 's/;/ /g') &> /dev/null
mkdir -p {{.OutputDir}} &> /dev/null
echo -n "$CNI_COMMAND $CNI_NETNS $K8S_POD_NAMESPACE $K8S_POD_NAME $K8S_POD_INFRA_CONTAINER_ID" >& {{.OutputFile}}
echo -n "{ \"ip4\": { \"ip\": \"10.1.0.23/24\" } }"
`
	execTemplateData := &map[string]interface{}{
		"OutputFile": path.Join(pluginDir, plugName+".out"),
		"OutputEnv":  path.Join(pluginDir, plugName+".env"),
		"OutputDir":  pluginDir,
	}

	tObj := template.Must(template.New("test").Parse(execScriptTempl))
	buf := &bytes.Buffer{}
	if err := tObj.Execute(buf, *execTemplateData); err != nil {
		t.Fatalf("Error in executing script template - %v", err)
	}
	execScript := buf.String()
	_, err = f.WriteString(execScript)
	if err != nil {
		t.Fatalf("Failed to write plugin exec - %v", err)
	}

	err = f.Chmod(0777)
	if err != nil {
		t.Fatalf("Failed to set exec perms on plugin")
	}

	f.Close()
}

示例#18
文件: genauth.go 项目: tmroeder/cloudproxy

func main() {
	flag.Parse()

	headerOutput, err := os.Create(*headerFile)
	if err != nil {
		log.Fatalf("Could not create header file '%s': %v", *headerFile, err)
	}

	implOutput, err := os.Create(*implFile)
	if err != nil {
		log.Fatalf("Could not create implementation file '%s': %v", *implFile, err)
	}

	cg := genauth.ParseAuthAst(*binaryFile, *astFile)

	header := cg.Header()
	for _, line := range header {
		fmt.Fprintln(headerOutput, line)
	}

	impl := cg.Implementation()
	for _, line := range impl {
		fmt.Fprintln(implOutput, line)
	}
}

示例#19
文件: parser.go 项目: BellyWong/PonyDebugger

func main() {
	dec := NewSchemaDecoder(os.Stdin)

	root := dec.ReadSchema()

	for _, domain := range root.Domains {
		// Types
		if domain.HasClasses() {
			headerName := (PathPrefix + domain.TypesFileName() + ".h")
			headerFile, _ := os.Create(headerName)
			domain.WriteTypesHeader(headerFile)
			headerFile.Close()

			implementationName := PathPrefix + domain.TypesFileName() + ".m"
			implementationFile, _ := os.Create(implementationName)
			domain.WriteTypesImplementation(implementationFile)
			implementationFile.Close()
		}
	}

	for _, domain := range root.Domains {
		if domain.HasEvents() || domain.HasCommands() {
			headerName := (PathPrefix + domain.DomainFileName() + ".h")
			headerFile, _ := os.Create(headerName)
			domain.WriteDomainHeader(headerFile)

			implementationName := PathPrefix + domain.DomainFileName() + ".m"
			implementationFile, _ := os.Create(implementationName)
			domain.WriteDomainImplementation(implementationFile)
			implementationFile.Close()
		}
	}
}

示例#20

func (dump *MongoDump) getArchiveOut() (out io.WriteCloser, err error) {
	if dump.OutputOptions.Archive == "-" {
		out = &nopCloseWriter{dump.stdout}
	} else {
		targetStat, err := os.Stat(dump.OutputOptions.Archive)
		if err == nil && targetStat.IsDir() {
			defaultArchiveFilePath :=
				filepath.Join(dump.OutputOptions.Archive, "archive")
			if dump.OutputOptions.Gzip {
				defaultArchiveFilePath = defaultArchiveFilePath + ".gz"
			}
			out, err = os.Create(defaultArchiveFilePath)
			if err != nil {
				return nil, err
			}
		} else {
			out, err = os.Create(dump.OutputOptions.Archive)
			if err != nil {
				return nil, err
			}
		}
	}
	if dump.OutputOptions.Gzip {
		return &wrappedWriteCloser{
			WriteCloser: gzip.NewWriter(out),
			inner:       out,
		}, nil
	}
	return out, nil
}

示例#21
文件: benchmark.go 项目: maruel/hugo

func bench(cmd *cobra.Command, args []string) {

	if memProfilefile != "" {
		f, err := os.Create(memProfilefile)

		if err != nil {
			panic(err)
		}
		for i := 0; i < benchmarkTimes; i++ {
			_ = buildSite()
		}
		pprof.WriteHeapProfile(f)
		f.Close()

	} else {
		if cpuProfilefile == "" {
			cpuProfilefile = "/tmp/hugo-cpuprofile"
		}
		f, err := os.Create(cpuProfilefile)

		if err != nil {
			panic(err)
		}

		pprof.StartCPUProfile(f)
		defer pprof.StopCPUProfile()
		for i := 0; i < benchmarkTimes; i++ {
			_ = buildSite()
		}
	}

}

示例#22
文件: main.go 项目: GlenKelley/mathgl

func main() {
	//fmt.Println("Making vectorf.go")
	vecs := GenVec()
	fmt.Println(vecs)
	vecf, err := os.Create("..vectorf.go")
	if err != nil {
		panic(err)
	}
	defer vecf.Close()

	_, err = vecf.Write([]byte(vecs))
	if err != nil {
		panic(err)
	}

	//fmt.Println("Making matrixf.go")
	mats := GenMat()
	//fmt.Println(mats)
	matf, err := os.Create("..matrixf.go")
	if err != nil {
		panic(err)
	}
	defer matf.Close()

	_, err = matf.Write([]byte(mats))
	if err != nil {
		panic(err)
	}
	//fmt.Println(mats)
	//fmt.Println("Done")
}

示例#23
文件: cmd.go 项目: marcopaganini/rclone

// initConfig is run by cobra after initialising the flags
func initConfig() {
	// Log file output
	if *logFile != "" {
		f, err := os.OpenFile(*logFile, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0640)
		if err != nil {
			log.Fatalf("Failed to open log file: %v", err)
		}
		_, err = f.Seek(0, os.SEEK_END)
		if err != nil {
			fs.ErrorLog(nil, "Failed to seek log file to end: %v", err)
		}
		log.SetOutput(f)
		fs.DebugLogger.SetOutput(f)
		redirectStderr(f)
	}

	// Load the rest of the config now we have started the logger
	fs.LoadConfig()

	// Write the args for debug purposes
	fs.Debug("rclone", "Version %q starting with parameters %q", fs.Version, os.Args)

	// Setup CPU profiling if desired
	if *cpuProfile != "" {
		fs.Log(nil, "Creating CPU profile %q\n", *cpuProfile)
		f, err := os.Create(*cpuProfile)
		if err != nil {
			fs.Stats.Error()
			log.Fatal(err)
		}
		err = pprof.StartCPUProfile(f)
		if err != nil {
			fs.Stats.Error()
			log.Fatal(err)
		}
		defer pprof.StopCPUProfile()
	}

	// Setup memory profiling if desired
	if *memProfile != "" {
		defer func() {
			fs.Log(nil, "Saving Memory profile %q\n", *memProfile)
			f, err := os.Create(*memProfile)
			if err != nil {
				fs.Stats.Error()
				log.Fatal(err)
			}
			err = pprof.WriteHeapProfile(f)
			if err != nil {
				fs.Stats.Error()
				log.Fatal(err)
			}
			err = f.Close()
			if err != nil {
				fs.Stats.Error()
				log.Fatal(err)
			}
		}()
	}
}

示例#24
文件: sess_file.go 项目: notlcry/beego

func (fp *FileProvider) SessionRead(sid string) (SessionStore, error) {
	err := os.MkdirAll(path.Join(fp.savePath, string(sid[0]), string(sid[1])), 0777)
	if err != nil {
		println(err.Error())
	}
	_, err = os.Stat(path.Join(fp.savePath, string(sid[0]), string(sid[1]), sid))
	var f *os.File
	if err == nil {
		f, err = os.OpenFile(path.Join(fp.savePath, string(sid[0]), string(sid[1]), sid), os.O_RDWR, 0777)
	} else if os.IsNotExist(err) {
		f, err = os.Create(path.Join(fp.savePath, string(sid[0]), string(sid[1]), sid))
	} else {
		return nil, err
	}
	os.Chtimes(path.Join(fp.savePath, string(sid[0]), string(sid[1]), sid), time.Now(), time.Now())
	var kv map[interface{}]interface{}
	b, err := ioutil.ReadAll(f)
	if err != nil {
		return nil, err
	}
	if len(b) == 0 {
		kv = make(map[interface{}]interface{})
	} else {
		kv, err = decodeGob(b)
		if err != nil {
			return nil, err
		}
	}
	f.Close()
	f, err = os.Create(path.Join(fp.savePath, string(sid[0]), string(sid[1]), sid))
	ss := &FileSessionStore{f: f, sid: sid, values: kv}
	return ss, nil
}

示例#25
文件: bench.go 项目: jebjerg/bolt

// Starts all profiles set on the options.
func benchStartProfiling(options *BenchOptions) {
	var err error

	// Start CPU profiling.
	if options.CPUProfile != "" {
		cpuprofile, err = os.Create(options.CPUProfile)
		if err != nil {
			fatal("bench: could not create cpu profile %q: %v", options.CPUProfile, err)
		}
		pprof.StartCPUProfile(cpuprofile)
	}

	// Start memory profiling.
	if options.MemProfile != "" {
		memprofile, err = os.Create(options.MemProfile)
		if err != nil {
			fatal("bench: could not create memory profile %q: %v", options.MemProfile, err)
		}
		runtime.MemProfileRate = 4096
	}

	// Start fatal profiling.
	if options.BlockProfile != "" {
		blockprofile, err = os.Create(options.BlockProfile)
		if err != nil {
			fatal("bench: could not create block profile %q: %v", options.BlockProfile, err)
		}
		runtime.SetBlockProfileRate(1)
	}
}

示例#26
文件: cloudTask.go 项目: jjz/ztodo

func (l *CloudTasks) CloudTaskToFile() error {
	// work tasks json to file
	workfd, err := os.Create(l.WorkFilename)
	if err != nil {
		return err
	}
	defer workfd.Close()

	workjsonstr, err := json.Marshal(l.WorkTasks)
	if err != nil {
		fmt.Println("error:", err)
	}
	workfd.WriteString(string(workjsonstr))

	// backup task json to file
	backfd, err := os.Create(l.BackupFilename)
	if err != nil {
		return err
	}
	defer backfd.Close()

	backupjsonstr, err := json.Marshal(l.BackupTasks)
	if err != nil {
		fmt.Println("error:", err)
	}
	backfd.WriteString(string(backupjsonstr))

	return nil
}

示例#27
文件: archive_test.go 项目: truedays/docker

func TestUntarPathWithInvalidDest(t *testing.T) {
	tempFolder, err := ioutil.TempDir("", "docker-archive-test")
	if err != nil {
		t.Fatal(err)
	}
	defer os.RemoveAll(tempFolder)
	invalidDestFolder := filepath.Join(tempFolder, "invalidDest")
	// Create a src file
	srcFile := filepath.Join(tempFolder, "src")
	tarFile := filepath.Join(tempFolder, "src.tar")
	os.Create(srcFile)
	os.Create(invalidDestFolder) // being a file (not dir) should cause an error

	// Translate back to Unix semantics as next exec.Command is run under sh
	srcFileU := srcFile
	tarFileU := tarFile
	if runtime.GOOS == "windows" {
		tarFileU = "/tmp/" + filepath.Base(filepath.Dir(tarFile)) + "/src.tar"
		srcFileU = "/tmp/" + filepath.Base(filepath.Dir(srcFile)) + "/src"
	}

	cmd := exec.Command("sh", "-c", "tar cf "+tarFileU+" "+srcFileU)
	_, err = cmd.CombinedOutput()
	if err != nil {
		t.Fatal(err)
	}

	err = UntarPath(tarFile, invalidDestFolder)
	if err == nil {
		t.Fatalf("UntarPath with invalid destination path should throw an error.")
	}
}

示例#28
文件: generator.go 项目: cyprusteam/swagger

func generateSwaggerUiFiles(parser *parser.Parser, outputSpec string) error {
	fd, err := os.Create(path.Join(outputSpec, "index.json"))
	if err != nil {
		return fmt.Errorf("Can not create the master index.json file: %v\n", err)
	}
	defer fd.Close()
	fd.WriteString(string(parser.GetResourceListingJson()))

	for apiKey, apiDescription := range parser.TopLevelApis {
		err = os.MkdirAll(path.Join(outputSpec, apiKey), 0777)
		if err != nil {
			return err
		}

		fd, err = os.Create(path.Join(outputSpec, apiKey, "index.json"))
		if err != nil {
			return fmt.Errorf("Can not create the %s/index.json file: %v\n", apiKey, err)
		}
		defer fd.Close()

		json, err := json.MarshalIndent(apiDescription, "", "    ")
		if err != nil {
			return fmt.Errorf("Can not serialise []ApiDescription to JSON: %v\n", err)
		}

		fd.Write(json)
		log.Printf("Wrote %v/index.json", apiKey)
	}

	return nil
}

示例#29
文件: server.go 项目: hypersleep/influxdb

// StartProfile initializes the cpu and memory profile, if specified.
func startProfile(cpuprofile, memprofile string) {
	if cpuprofile != "" {
		f, err := os.Create(cpuprofile)
		if err != nil {
			log.Fatalf("cpuprofile: %v", err)
		}
		log.Printf("writing CPU profile to: %s\n", cpuprofile)
		prof.cpu = f
		pprof.StartCPUProfile(prof.cpu)
	}

	if memprofile != "" {
		f, err := os.Create(memprofile)
		if err != nil {
			log.Fatalf("memprofile: %v", err)
		}
		log.Printf("writing mem profile to: %s\n", memprofile)
		prof.mem = f
		runtime.MemProfileRate = 4096
	}

	go func() {
		c := make(chan os.Signal, 1)
		signal.Notify(c, os.Interrupt)
		<-c
		stopProfile()
		os.Exit(0)
	}()
}

示例#30
文件: site.go 项目: htruong/oryza

// Make thumbnail for the selected file
func MakeThumb(src string, dst_sane string, dst_thumb string) error {
	file, err := os.Open(src)
	defer file.Close()
	if err != nil {
		return err
	}

	img, err := jpeg.Decode(file)
	if err != nil {
		return err
	}

	sane := resize.Resize(1080, 0, img, resize.Bilinear)

	out_sane, err := os.Create(dst_sane)
	if err != nil {
		return err
	}
	defer out_sane.Close()

	jpeg.Encode(out_sane, sane, nil)

	thumb := resize.Thumbnail(200, 200, img, resize.Bilinear)

	out_thumb, err := os.Create(dst_thumb)
	if err != nil {
		return err
	}
	defer out_thumb.Close()

	jpeg.Encode(out_thumb, thumb, nil)
	return nil
}

展开阅读全文