func main() { var tmp *os.File var err os.Error flag.Parse() args := flag.Args() if len(args) == 1 { dir, _ := path.Split(args[0]) tmp, err = ioutil.TempFile(dir, "") exitOnErr(err) } else if len(args) == 2 { tmp, err = os.Open(args[1], os.O_RDWR|os.O_CREAT, 0644) exitOnErr(err) } else { usage() } fname := args[0] tmpname := tmp.Name() exitOnErr(cdb.Make(tmp, bufio.NewReader(os.Stdin))) exitOnErr(tmp.Sync()) exitOnErr(tmp.Close()) exitOnErr(os.Rename(tmpname, fname)) }
// CopyFileContents copies the contents of the file named src to the file named // by dst. The file will be created if it does not already exist. If the // destination file exists, all it's contents will be replaced by the contents // of the source file. // // From http://stackoverflow.com/a/21067803 func CopyFileContents(src, dst string) (err error) { in, err := os.Open(src) if err != nil { return } defer in.Close() ext := filepath.Ext(dst) var out *os.File if ext == ".sh" { out, err = os.OpenFile(dst, os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0766) } else { out, err = os.Create(dst) } if err != nil { return } defer func() { cerr := out.Close() if err == nil { err = cerr } }() if _, err = io.Copy(out, in); err != nil { return } err = out.Sync() return }
// CopyAs copies a file func (fh *FileHandle) CopyAs(path string) (err error) { var src, dst *os.File // open src if src, err = os.Open(fh.Path); err != nil { return err } defer src.Close() // open dest if dst, err = os.Create(path); err != nil { return err } defer func() { closeErr := dst.Close() if err == nil { err = closeErr if err == nil { // copy success - update file name / path fh.Name = filepath.Base(path) fh.Path = path } } }() // copy if _, err = io.Copy(dst, src); err != nil { return } // sync err = dst.Sync() return }
// persistMetaData atomically writes state to the filesystem func (d *DiskQueue) persistMetaData() error { var f *os.File var err error d.metaMutex.Lock() depth := d.depth readFileNum := d.readFileNum readPos := d.readPos writeFileNum := d.writeFileNum writePos := d.writePos d.metaMutex.Unlock() fileName := d.metaDataFileName() tmpFileName := fileName + ".tmp" // write to tmp file f, err = os.OpenFile(tmpFileName, os.O_RDWR|os.O_CREATE, 0600) if err != nil { return err } _, err = fmt.Fprintf(f, "%d\n%d,%d\n%d,%d\n", depth, readFileNum, readPos, writeFileNum, writePos) if err != nil { f.Close() return err } f.Sync() f.Close() // atomically rename return os.Rename(tmpFileName, fileName) }
func CreateFile(name string) (flvFile *File, err error) { var file *os.File // Create file if file, err = os.Create(name); err != nil { return } // Write flv header if _, err = file.Write(HEADER_BYTES); err != nil { file.Close() return } // Sync to disk if err = file.Sync(); err != nil { file.Close() return } flvFile = &File{ file: file, name: name, readOnly: false, headerBuf: make([]byte, 11), duration: 0.0, } return }
func mapPairToCSV(colHeader string, inMap PairList, fh *os.File, geo *geoip.GeoIP) int { nb, err := fh.WriteString(colHeader + "\n") check(err) totalBytes := nb i := 0 for _, v1 := range inMap { matches := regexp.MustCompile(`([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)`).FindStringSubmatch(v1.Key) if len(matches) >= 1 && geo != nil { record := geo.GetRecord(v1.Key) if record != nil { nb, err = fh.WriteString(v1.Key + csvd + strconv.Itoa(v1.Value) + csvd + record.City + csvd + record.CountryName + "\n") } } else { nb, err = fh.WriteString(v1.Key + csvd + strconv.Itoa(v1.Value) + "\n") } check(err) totalBytes += nb i++ } fh.Sync() return totalBytes }
func (d *Daemon) handle(input link.Input, process *os.Process, stdin *os.File) error { if input.WindowSize != nil { setWinSize(stdin, input.WindowSize.Columns, input.WindowSize.Rows) process.Signal(syscall.SIGWINCH) } else if input.EOF { stdin.Sync() err := stdin.Close() if d.WithTty { process.Signal(syscall.SIGHUP) } if err != nil { return err } } else if input.Signal != nil { if input.Signal.Signal == garden.SignalTerminate { process.Signal(syscall.SIGTERM) } else if input.Signal.Signal == garden.SignalKill { process.Signal(syscall.SIGKILL) } } else { _, err := stdin.Write(input.StdinData) if err != nil { return err } } return nil }
// Loop receiving and processing link requests on the given connection. // The loop terminates when the connection is closed or an error occurs. func processLinkRequests(conn net.Conn, stdinW *os.File, cmd *exec.Cmd, withTty bool) { decoder := gob.NewDecoder(conn) for { var input linkpkg.Input err := decoder.Decode(&input) if err != nil { break } if input.WindowSize != nil { setWinSize(stdinW, input.WindowSize.Columns, input.WindowSize.Rows) cmd.Process.Signal(syscall.SIGWINCH) } else if input.EOF { stdinW.Sync() err := stdinW.Close() if withTty { cmd.Process.Signal(syscall.SIGHUP) } if err != nil { conn.Close() break } } else { _, err := stdinW.Write(input.Data) if err != nil { conn.Close() break } } } }
// DumpStacks appends the runtime stack into file in dir and returns full path // to that file. func DumpStacks(dir string) (string, error) { var ( buf []byte stackSize int ) bufferLen := 16384 for stackSize == len(buf) { buf = make([]byte, bufferLen) stackSize = runtime.Stack(buf, true) bufferLen *= 2 } buf = buf[:stackSize] var f *os.File if dir != "" { path := filepath.Join(dir, fmt.Sprintf(stacksLogNameTemplate, strings.Replace(time.Now().Format(time.RFC3339), ":", "", -1))) var err error f, err = os.OpenFile(path, os.O_CREATE|os.O_WRONLY, 0666) if err != nil { return "", errors.Wrap(err, "failed to open file to write the goroutine stacks") } defer f.Close() defer f.Sync() } else { f = os.Stderr } if _, err := f.Write(buf); err != nil { return "", errors.Wrap(err, "failed to write goroutine stacks") } return f.Name(), nil }
// WriteConfigFile wirtes a valid nginx config to f // Proxies are indicated in routes // Other configuration specified in config func WriteConfigFile(f *os.File, routes map[string]int, config Config) { var routesBuffer bytes.Buffer for path, port := range routes { locationStr := "\t\tlocation /%s {\n\t\t\treturn 302 /%s/;\n\t\t}\n\n\t\tlocation /%s/ {\n\t\t\tproxy_pass http://127.0.0.1:%d/;\n\t\t}\n" fmt.Fprintf(&routesBuffer, locationStr, path, path, path, port) } serverName := config.ServerName if serverName == "" { serverName = "localhost" } var rootRule string defaultPort := config.DefaultPort if defaultPort == 0 { rootRule = "return 404;" } else { rootRule = "proxy_pass http://127.0.0.1:" + strconv.Itoa(defaultPort) + ";" } fmt.Fprintf(f, configText, serverName, routesBuffer.String(), rootRule) err := f.Sync() if err != nil { log.Fatal("Error saving config file: ", err) } }
func readUCHAR8(file *os.File, s int64) ([]byte, int64) { buf := make([]byte, s) ntotal := int64(0) for { // read several times because Read might decide to stop intermittently buffer := make([]byte, int64(1024)*int64(1024)) // read a little bit n, err := file.Read(buffer) if ntotal+int64(n) > s { n = int(s - ntotal) } // copy to the real buffer buf copy(buf[ntotal:(ntotal+int64(n))], buffer[0:n]) ntotal = ntotal + int64(n) if err == io.EOF { break } if err != nil { panic(err) } } file.Sync() return buf, ntotal }
func Write(fn, str string, append bool) error { var ( file *os.File err error ) if append { file, err = os.OpenFile(fn, os.O_CREATE|os.O_APPEND|os.O_WRONLY, os.FileMode(0666)) } else { file, err = os.OpenFile(fn, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, os.FileMode(0666)) } defer file.Close() if err != nil { return err } _, err = file.WriteString(str) if err != nil { return err } file.Sync() return nil }
func Generate_d5sFile(file string, d5sConf *D5ServConf) (e error) { var f *os.File if file == NULL { f = os.Stdout } else { f, e = os.OpenFile(file, os.O_CREATE|os.O_RDWR|os.O_TRUNC, 0600) ThrowErr(e) e = f.Truncate(0) ThrowErr(e) defer func() { f.Sync() f.Close() }() } if d5sConf == nil { d5sConf = new(D5ServConf) d5sConf.RSAKeys = GenerateRSAKeyPair() } desc := getImportableDesc(d5sConf) f.WriteString("#\n# deblocus server configuration\n#\n\n") for k, d := range desc { defaultVal := d.sType.Tag.Get("importable") f.WriteString(fmt.Sprintf("%-16s %s\n", k, defaultVal)) } f.WriteString("\n### Please take good care of this secret file during the server life cycle.\n") f.WriteString("### DON'T modify the following lines, unless you known what happens.\n\n") k := d5sConf.RSAKeys keyBytes := x509.MarshalPKCS1PrivateKey(k.priv) keyText := pem.EncodeToMemory(&pem.Block{ Type: SER_KEY_TYPE, Bytes: keyBytes, }) f.Write(keyText) return }
func ensureSlot(item string) { var err error var fi *os.File slot, err := filepath.Glob(Slots) if err != nil { panic(err) } fi, err = os.OpenFile(fmt.Sprintf("%v/slots", slot[0]), os.O_RDWR|os.O_APPEND, 0666) if err != nil { panic(err) } defer fi.Close() // ensure the slot is not already written into the capemanager // (from: https://github.com/mrmorphic/hwio/blob/master/module_bb_pwm.go#L190) scanner := bufio.NewScanner(fi) for scanner.Scan() { line := scanner.Text() if strings.Index(line, item) > 0 { return } } fi.WriteString(item) fi.Sync() scanner = bufio.NewScanner(fi) for scanner.Scan() { line := scanner.Text() if strings.Index(line, item) > 0 { return } } }
// persistMetaData atomically writes state to the filesystem func (d *diskQueue) persistMetaData() error { var f *os.File var err error fileName := d.metaDataFileName() tmpFileName := fmt.Sprintf("%s.%d.tmp", fileName, rand.Int()) // write to tmp file f, err = os.OpenFile(tmpFileName, os.O_RDWR|os.O_CREATE, 0600) if err != nil { return err } _, err = fmt.Fprintf(f, "%d\n%d,%d\n%d,%d\n", atomic.LoadInt64(&d.depth), d.readFileNum, d.readPos, d.writeFileNum, d.writePos) if err != nil { f.Close() return err } f.Sync() f.Close() // atomically rename return atomicRename(tmpFileName, fileName) }
func (m *Morass) write() { writing := <-m.writable defer func() { m.pool <- writing[:0] }() sort.Sort(&writing) var tf *os.File if tf, m.err = ioutil.TempFile(m.dir, m.prefix); m.err != nil { return } enc := gob.NewEncoder(tf) dec := gob.NewDecoder(tf) f := &file{head: nil, file: tf, encoder: enc, decoder: dec} m.files = append(m.files, f) for _, e := range writing { if m.err = enc.Encode(&e); m.err != nil { return } } m.err = tf.Sync() }
func build_config( rules string, c *C) (conf string, cleanup func()) { var fconf, rconf *os.File var err error var conf_test Config fconf, err = ioutil.TempFile("", "") if err != nil { panic(err) } rconf, err = ioutil.TempFile("", "") if err != nil { panic(err) } cleanup = func() { fconf.Close() rconf.Close() } // Trailing \n is required by go-config issue #3. fconf.WriteString( "[Settings]\nloglevel=debug\nrules=" + rconf.Name() + "\n") fconf.Sync() rconf.WriteString(rules + "\n") rconf.Sync() conf_test, err = NewFileConfig(fconf.Name()) c.Assert(conf_test, Not(Equals), nil) c.Assert(err, Equals, nil) conf = fconf.Name() return }
func writeLog(values string, log_type string) error { // LOG_2016_06_17.ERROR // 2016-06-17 :: log_string //timestr := time.Now() //outfile = "." //file,err := os.Create(outfile) //1 find log file if exists //2 if not exists create file //3 write append log filepath := "../logs/" infile := filepath + Today() + "_" + log_type + ".log" fileExist := checkFileIsExist(infile) var file *os.File var err error if fileExist { file, err = os.OpenFile(infile, os.O_APPEND, 0666) fmt.Println("file exsits") } else { file, err = os.Create(infile) fmt.Println("file not exists") } checkError(err) n, err := io.WriteString(file, values+"\n") fmt.Println("write len ====%d", n) checkError(err) file.Sync() defer file.Close() return nil }
// persistMetaData atomically writes state to the filesystem func (d *diskQueueReader) persistMetaData() error { var f *os.File var err error fileName := d.metaDataFileName(true) tmpFileName := fmt.Sprintf("%s.%d.tmp", fileName, rand.Int()) // write to tmp file f, err = os.OpenFile(tmpFileName, os.O_RDWR|os.O_CREATE, 0644) if err != nil { return err } _, err = fmt.Fprintf(f, "%d\n%d\n%d,%d,%d\n%d,%d,%d\n", d.confirmedQueueInfo.TotalMsgCnt(), d.queueEndInfo.totalMsgCnt, d.confirmedQueueInfo.EndOffset.FileNum, d.confirmedQueueInfo.EndOffset.Pos, d.confirmedQueueInfo.Offset(), d.queueEndInfo.EndOffset.FileNum, d.queueEndInfo.EndOffset.Pos, d.queueEndInfo.Offset()) if err != nil { f.Close() return err } f.Sync() f.Close() // atomically rename return util.AtomicRename(tmpFileName, fileName) }
func (t *FileConfigTest) TestRulesFileDoesNotExist(c *C) { var fconf, rconf *os.File var err error var conf Config fconf, err = ioutil.TempFile("", "") if err != nil { panic(err) } rconf, err = ioutil.TempFile("", "") if err != nil { panic(err) } defer (func() { fconf.Close() rconf.Close() })() // Trailing \n is required by go-config issue #3. fconf.WriteString("[Settings]\nloglevel=debug\nrules=\n") fconf.Sync() conf, err = NewFileConfig(fconf.Name()) c.Check(conf, Equals, (*FileConfig)(nil)) c.Check(err, Not(Equals), nil) }
func (t *FileConfigTest) TestLoadsRelativeRulesPath(c *C) { var fconf, rconf *os.File var err error var conf Config fconf, err = ioutil.TempFile("", "") if err != nil { panic(err) } rconf, err = ioutil.TempFile("", "") if err != nil { panic(err) } defer (func() { fconf.Close() rconf.Close() })() path, _ := filepath.Rel(filepath.Dir(fconf.Name()), rconf.Name()) // Trailing \n is required by go-config issue #3. fconf.WriteString("[Settings]\nloglevel=debug\nrules=" + path + "\n") fconf.Sync() conf, err = NewFileConfig(fconf.Name()) c.Assert(conf, Not(Equals), nil) c.Assert(err, Equals, nil) }
func load_config( config string, rules string, c *C) (conf *FileConfig, cleanup func()) { var fconf, rconf *os.File var err error fconf, err = ioutil.TempFile("", "") if err != nil { panic(err) } rconf, err = ioutil.TempFile("", "") if err != nil { panic(err) } cleanup = func() { fconf.Close() rconf.Close() } // Trailing \n is required by go-config issue #3. fconf.WriteString( "[Settings]\n" + config + "\n" + "rules=" + rconf.Name() + "\n") fconf.Sync() rconf.WriteString(rules + "\n") rconf.Sync() conf, err = NewFileConfig(fconf.Name()) c.Assert(conf, Not(Equals), nil) c.Assert(err, Equals, nil) return }
func fileCleanup(f *os.File) { if err := f.Sync(); err != nil { log.Printf("Error syncing %s, %v", f.Name(), err) } if err := f.Close(); err != nil { log.Printf("Error closing file %s, %v", f.Name(), err) } }
func writeToFile(file *os.File, data []byte, offset int64, whence int) { _, err := file.Write(data) Expect(err).ToNot(HaveOccurred()) err = file.Sync() Expect(err).ToNot(HaveOccurred()) _, err = file.Seek(offset, whence) Expect(err).ToNot(HaveOccurred()) }
func flushLogFile(File *os.File) { for _ = range time.NewTicker(300 * time.Second).C { if File == nil { return } File.Sync() } }
func shutdownLogging(logFile *os.File) { // flush and close the file if logFile != nil { log.Println("INFO: closing log file") logFile.Sync() logFile.Close() } }
func TestFsnotifyDeleteWatchedDir(t *testing.T) { watcher := newWatcher(t) defer watcher.Close() // Create directory to watch testDir := tempMkdir(t) defer os.RemoveAll(testDir) // Create a file before watching directory testFileAlreadyExists := filepath.Join(testDir, "TestFsnotifyEventsExisting.testfile") { var f *os.File f, err := os.OpenFile(testFileAlreadyExists, os.O_WRONLY|os.O_CREATE, 0666) if err != nil { t.Fatalf("creating test file failed: %s", err) } f.Sync() f.Close() } addWatch(t, watcher, testDir) // Add a watch for testFile addWatch(t, watcher, testFileAlreadyExists) // Receive errors on the error channel on a separate goroutine go func() { for err := range watcher.Error { t.Fatalf("error received: %s", err) } }() // Receive events on the event channel on a separate goroutine eventstream := watcher.Event var deleteReceived counter go func() { for event := range eventstream { // Only count relevant events if event.Name == filepath.Clean(testDir) || event.Name == filepath.Clean(testFileAlreadyExists) { t.Logf("event received: %s", event) if event.IsDelete() { deleteReceived.increment() } } else { t.Logf("unexpected event received: %s", event) } } }() os.RemoveAll(testDir) // We expect this event to be received almost immediately, but let's wait 500 ms to be sure time.Sleep(500 * time.Millisecond) dReceived := deleteReceived.value() if dReceived < 2 { t.Fatalf("did not receive at least %d delete events, received %d after 500 ms", 2, dReceived) } }
func copyFile(sourceFile, targetFile *os.File) { _, err := io.Copy(targetFile, sourceFile) defer sourceFile.Close() defer targetFile.Close() if err != nil { log.Println(err) } targetFile.Sync() }
func logger(f *os.File, c chan string) { for x := range c { f.WriteString(fmt.Sprintf("%s - %s\n", time.Now().Format("2006-01-02 15:04:05"), x)) f.Sync() } f.Close() }
func TestRemovalOfWatch(t *testing.T) { var testDir string = testTempDir() // Create directory to watch if err := os.Mkdir(testDir, 0777); err != nil { t.Fatalf("failed to create test directory: %s", err) } defer os.RemoveAll(testDir) // Create a file before watching directory var testFileAlreadyExists string = filepath.Join(testDir, "TestFsnotifyEventsExisting.testfile") { var f *os.File f, err := os.OpenFile(testFileAlreadyExists, os.O_WRONLY|os.O_CREATE, 0666) if err != nil { t.Fatalf("creating test file failed: %s", err) } f.Sync() f.Close() } watcher, err := NewWatcher() if err != nil { t.Fatalf("NewWatcher() failed: %s", err) } defer watcher.Close() watcher.Watch(testDir) err = watcher.RemoveWatch(testDir) if err != nil { t.Fatalf("Could not remove the watch: %v\n", err) } go func() { select { case ev := <-watcher.Event: t.Fatalf("We received event: %v\n", ev) case <-time.After(500 * time.Millisecond): t.Log("No event received, as expected.") } }() time.Sleep(200 * time.Millisecond) // Modify the file outside of the watched dir f, err := os.Open(testFileAlreadyExists) if err != nil { t.Fatalf("Open test file failed: %s", err) } f.WriteString("data") f.Sync() f.Close() err = os.Chmod(testFileAlreadyExists, 0700) if err != nil { t.Fatalf("chmod failed: %s", err) } time.Sleep(400 * time.Millisecond) }