Пример #1
0
// Locks PID file with a file lock.
// Keeps PID file open until applications exits.
func lockPidFile() (pidFile *os.File, err error) {
	var file *os.File
	file, err = os.OpenFile(
		PidFile, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, PidFileMask,
	)
	if err != nil {
		return
	}
	defer func() {
		// file must be open whole runtime to keep lock on itself
		if err != nil {
			file.Close()
		}
	}()

	if err = syscall.Flock(int(file.Fd()), syscall.LOCK_EX); err != nil {
		return
	}
	var fileLen int
	fileLen, err = fmt.Fprint(file, os.Getpid())
	if err != nil {
		return
	}
	if err = file.Truncate(int64(fileLen)); err != nil {
		return
	}

	return file, err
}
Пример #2
0
func fallocate(f *os.File, sz int64) error {
	err := syscall.Fallocate(int(f.Fd()), 0, 0, sz)
	if err == syscall.ENOTSUP {
		return f.Truncate(sz)
	}
	return err
}
Пример #3
0
func createDataFile(name string, length int64) (err error) {
	if (length & 3) != 0 {
		return fmt.Errorf("createDataFile only supports length that is a multiple of 4. Not %d", length)
	}
	var file *os.File
	file, err = os.Create(name)
	if err != nil {
		return
	}
	defer file.Close()
	err = file.Truncate(length)
	if err != nil {
		return
	}
	w := bufio.NewWriter(file)
	b := make([]byte, 4)
	for i := int64(0); i < length; i += 4 {
		b[0] = byte(i >> 24)
		b[1] = byte(i >> 16)
		b[2] = byte(i >> 8)
		b[3] = byte(i)
		_, err = w.Write(b)
		if err != nil {
			return
		}
	}
	return
}
Пример #4
0
func bundleReset(w *os.File, size int64) error {
	pending_offset := size - ENDSIZE
	if size == 0 {
		pending_offset = 0
	}
	offset, err := w.Seek(pending_offset, 0)
	if err != nil {
		return err
	}
	if offset != pending_offset {
		return errors.New("Failed to seek!")
	}
	err = w.Truncate(size)
	if err != nil {
		return err
	}
	if size == 0 {
		return nil
	}
	n, err := w.Write(zeroEnd)
	if err != nil || n != len(zeroEnd) {
		return errors.New("Failed to write end block")
	}
	return nil
}
Пример #5
0
func FindAndReplaceFd(fd *os.File, oldPattern, newPattern string) error {
	fbuf, err := ioutil.ReadAll(fd)
	if err != nil {
		return err
	}
	fd.Seek(0, -1)
	fd.Truncate(0)
	expr, err := regexp.Compile(oldPattern)
	if err != nil {
		return err
	}
	buffer := bytes.NewBuffer(fbuf)
	for {
		line, err := buffer.ReadString('\n')
		if err != nil {
			if err == io.EOF {
				break
			}
			return err
		}
		if expr.MatchString(line) {
			line = expr.ReplaceAllString(line, newPattern)
		}
		if _, err := fd.WriteString(line); err != nil {
			return err
		}
	}
	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
}
Пример #7
0
Файл: main.go Проект: mvdan/sh
func empty(f *os.File) error {
	if err := f.Truncate(0); err != nil {
		return err
	}
	_, err := f.Seek(0, 0)
	return err
}
Пример #8
0
func New(path string) (pf *PidFile, err error) {
	pf = &PidFile{path, os.Getpid()}
	var f *os.File
	f, err = os.OpenFile(path, os.O_CREATE|os.O_EXCL|os.O_WRONLY, 0600)
	if err != nil {
		f, err = os.OpenFile(path, os.O_CREATE|os.O_RDWR, 0600)
		if err != nil {
			return
		}
		var pidstr [10]byte
		var n int
		n, err = f.Read(pidstr[:])
		if err != nil {
			return
		}
		var pid int
		pid, err = strconv.Atoi(string(pidstr[:n]))
		if err != nil {
			return
		}
		_, err = os.FindProcess(pid)
		if err == nil {
			// process exists
			err = ProcessExists
			return
		}
		f.Truncate(int64(n))
	}
	defer f.Close()
	_, err = f.WriteString(strconv.Itoa(pf.Pid))
	return
}
func testAccCreateZipFromFiles(files map[string]string, zipFile *os.File) error {
	zipFile.Truncate(0)
	zipFile.Seek(0, 0)

	w := zip.NewWriter(zipFile)

	for source, destination := range files {
		f, err := w.Create(destination)
		if err != nil {
			return err
		}

		fileContent, err := ioutil.ReadFile(source)
		if err != nil {
			return err
		}

		_, err = f.Write(fileContent)
		if err != nil {
			return err
		}
	}

	err := w.Close()
	if err != nil {
		return err
	}

	return w.Flush()
}
Пример #10
0
func addTagsToFile(file *os.File, tags []string) (success bool) {
	file.Seek(0, 0) // rewind to start with
	r, err := ioutil.ReadAll(io.Reader(file))
	if err != nil {
		log.Printf("ERROR: %s\n", err)
		return
	}
	loc := bytes.Index(r, gitScissorMarker)
	if loc < 0 {
		if _, err := file.Seek(0, 2); err != nil { // seek to end of file
			log.Printf("ERROR: %s\n", err)
			return
		}
	} else {
		if _, err := file.Seek(int64(loc), 0); err != nil { // seek to scissor line then truncate the file here
			log.Printf("ERROR: %s\n", err)
			return
		}
		if err := file.Truncate(int64(loc + 1)); err != nil {
			log.Printf("ERROR: %s\n", err)
			return
		}
	}
	fmt.Fprintln(file, "") // blank line first to ensure it's not stuck to the summary
	for _, tag := range tags {
		fmt.Fprintln(file, tag)
	}
	return true
}
Пример #11
0
func appendRegion(rid int64, f *os.File, regionSizeB int64) (*region, error) {
	size, err := f.Seek(0, os.SEEK_END)
	if err != nil {
		return nil, err
	}
	if err := f.Truncate(size + regionSizeB); err != nil {
		return nil, err
	}

	// Initialize header
	header := make([]byte, regionHeaderSize)
	copy(header[:8], signature)
	*(*int64)(unsafe.Pointer(&header[regionFreePointerOffset])) = regionHeaderSize
	*(*int64)(unsafe.Pointer(&header[regionSizeOffset])) = regionSizeB
	*(*int64)(unsafe.Pointer(&header[regionId])) = rid
	if _, err := f.Write(header); err != nil {
		return nil, err
	}
	r, err := openRegion(f, true, size)
	if err != nil {
		return nil, err
	}
	r.initBlockList()
	return r, nil
}
Пример #12
0
func TorrentData(md *metainfo.Info, location string) (ret *torrentData, err error) {
	var mms mmap_span.MMapSpan
	defer func() {
		if err != nil {
			mms.Close()
		}
	}()
	for _, miFile := range md.UpvertedFiles() {
		fileName := filepath.Join(append([]string{location, md.Name}, miFile.Path...)...)
		err = os.MkdirAll(filepath.Dir(fileName), 0777)
		if err != nil {
			err = fmt.Errorf("error creating data directory %q: %s", filepath.Dir(fileName), err)
			return
		}
		var file *os.File
		file, err = os.OpenFile(fileName, os.O_CREATE|os.O_RDWR, 0666)
		if err != nil {
			return
		}
		func() {
			defer file.Close()
			var fi os.FileInfo
			fi, err = file.Stat()
			if err != nil {
				return
			}
			if fi.Size() < miFile.Length {
				err = file.Truncate(miFile.Length)
				if err != nil {
					return
				}
			}
			if miFile.Length == 0 {
				// Can't mmap() regions with length 0.
				return
			}
			var mMap mmap.MMap
			mMap, err = mmap.MapRegion(file,
				int(miFile.Length), // Probably not great on <64 bit systems.
				mmap.RDWR, 0, 0)
			if err != nil {
				err = fmt.Errorf("error mapping file %q, length %d: %s", file.Name(), miFile.Length, err)
				return
			}
			if int64(len(mMap)) != miFile.Length {
				panic("mmap has wrong length")
			}
			mms.Append(mMap)
		}()
		if err != nil {
			return
		}
	}
	ret = &torrentData{
		MMapSpan:  mms,
		completed: make([]bool, md.NumPieces()),
	}
	return
}
Пример #13
0
Файл: nagios.go Проект: pjjw/ncd
func touchCheckOkFile(f *os.File) (err os.Error) {
	name := f.Name() + ".ok"
	f, err = os.OpenFile(name, os.O_RDWR|os.O_CREATE|os.O_EXCL, 0660)
	if err == nil {
		f.Truncate(0)
		f.Close()
	}
	return
}
Пример #14
0
func TorrentData(md *metainfo.Info, location string) (mms *mmap_span.MMapSpan, err error) {
	mms = &mmap_span.MMapSpan{}
	defer func() {
		if err != nil {
			mms.Close()
			mms = nil
		}
	}()
	for _, miFile := range md.UpvertedFiles() {
		fileName := filepath.Join(append([]string{location, md.Name}, miFile.Path...)...)
		err = os.MkdirAll(filepath.Dir(fileName), 0777)
		if err != nil {
			err = fmt.Errorf("error creating data directory %q: %s", filepath.Dir(fileName), err)
			return
		}
		var file *os.File
		file, err = os.OpenFile(fileName, os.O_CREATE|os.O_RDWR, 0666)
		if err != nil {
			return
		}
		func() {
			defer file.Close()
			var fi os.FileInfo
			fi, err = file.Stat()
			if err != nil {
				return
			}
			if fi.Size() < miFile.Length {
				err = file.Truncate(miFile.Length)
				if err != nil {
					return
				}
			}
			if miFile.Length == 0 {
				// Can't mmap() regions with length 0.
				return
			}
			var mMap gommap.MMap
			mMap, err = gommap.MapRegion(file.Fd(), 0, miFile.Length, gommap.PROT_READ|gommap.PROT_WRITE, gommap.MAP_SHARED)
			if err != nil {
				err = fmt.Errorf("error mapping file %q, length %d: %s", file.Name(), miFile.Length, err)
				return
			}
			if int64(len(mMap)) != miFile.Length {
				panic("mmap has wrong length")
			}
			mms.Append(mMap)
		}()
		if err != nil {
			return
		}
	}
	return
}
Пример #15
0
func (r *HdCache) WriteAt(p []byte, off int64) []chunk {
	boxI := int(off / int64(r.pieceSize))
	boxOff := int(off % int64(r.pieceSize))

	for i := 0; i < len(p); {
		var box *os.File
		var err error
		if !r.boxExists.IsSet(boxI) { //box doesn't exist, so we'll make one.
			box, err = os.Create(r.boxPrefix + strconv.Itoa(boxI))
			if err != nil {
				log.Panicln("Couldn't create cache file:", err)
				return nil
			}
			r.boxExists.Set(boxI)
			box.Truncate(int64(r.pieceSize))
			r.actualUsage++
		} else { //box exists, so we'll open it
			box, err = os.OpenFile(r.boxPrefix+strconv.Itoa(boxI), os.O_WRONLY, 0777)
			if err != nil {
				log.Println("Error opening cache item we thought we had:", r.boxPrefix+strconv.Itoa(boxI), "error:", err)
				r.removeBox(boxI)
				continue //loop around without incrementing 'i', forget this ever happened
			}
		}
		end := r.pieceSize - boxOff
		if len(p) < end {
			end = len(p)
		}
		copied, err := box.WriteAt(p[i:end], int64(boxOff))
		if err != nil {
			log.Panicln("Error at write cache box:", box.Name(), "error:", err)
		}
		i += copied
		box.Close()
		r.atimes[boxI] = time.Now()
		if copied == r.pieceSize {
			r.isBoxFull.Set(boxI)
		} else {
			if r.isByteSet[boxI].n == 0 {
				r.isByteSet[boxI] = *NewBitset(r.pieceSize)
			}
			for j := boxOff; j < boxOff+copied; j++ {
				r.isByteSet[boxI].Set(j)
			}
		}
		boxI++
		boxOff = 0
	}
	if r.actualUsage > r.getCapacity() {
		return r.trim()
	}
	return nil
}
Пример #16
0
func WriteToFile(f *os.File, b []byte) error {
	end, err := f.Write(b)
	if err != nil {
		return err
	}

	err = f.Truncate(int64(end))
	if err != nil {
		return err
	}

	return nil
}
Пример #17
0
// NewMem returns a new Accessor backed by an os.File.  The returned Accessor
// keeps all of the store content in memory.  The memory and file images are
// synced only by Sync and Close.  Recomended for small amounts of data only
// and content which may be lost on process kill/crash.  NewMem return the
// Accessor or an error of any.
//
// NOTE: The returned Accessor implements BeginUpdate and EndUpdate as a no op.
func NewMem(f *os.File) (store Accessor, err error) {
	a := &memaccessor{f: f}
	if err = f.Truncate(0); err != nil {
		return
	}

	var fi os.FileInfo
	if fi, err = a.f.Stat(); err != nil {
		return
	}

	a.fi = NewFileInfo(fi, a)
	store = a
	return
}
Пример #18
0
func preallocExtendTrunc(f *os.File, sizeInBytes int64) error {
	curOff, err := f.Seek(0, os.SEEK_CUR)
	if err != nil {
		return err
	}
	size, err := f.Seek(sizeInBytes, os.SEEK_END)
	if err != nil {
		return err
	}
	if _, err = f.Seek(curOff, os.SEEK_SET); err != nil {
		return err
	}
	if sizeInBytes > size {
		return nil
	}
	return f.Truncate(sizeInBytes)
}
Пример #19
0
func (pc *PushoverClient) saveSettings(fn string) (err error) {
	var f *os.File
	f, err = os.OpenFile(fn, os.O_CREATE|os.O_RDWR, 0600)
	f.Truncate(0)
	var settings []byte
	settings, err = json.Marshal(pc.Settings)
	if err != nil {
		return
	}
	var fb []byte
	fb, err = json.Marshal(settings)
	if err != nil {
		return err
	}
	_, err = f.Write(fb)
	f.Close()
	return
}
Пример #20
0
func writeString(file *os.File, strs []string) {
	str := strings.Join(strs, "\n")
	str = fmt.Sprintf("%s\n", str)
	_, err := file.Seek(0, 0)
	if err != nil {
		log.Fatalf("Seek:%s\n", err.Error())
	}

	err = file.Truncate(0)
	if err != nil {
		log.Fatalf("WriteString:%s\n", err.Error())
	}

	_, err = file.WriteString(str)
	if err != nil {
		log.Fatalf("WriteString:%s\n", err.Error())
	}
}
Пример #21
0
// MapFile creates a new memory map struct from an os.File
// The file will be truncated to given size if it's empty.
func MapFile(file *os.File, size int64) (m *Map, err error) {
	if size == 0 {
		return nil, ErrZeroSz
	}

	info, err := file.Stat()
	if err != nil {
		return nil, err
	}

	if sz := info.Size(); sz != size {
		if sz != 0 {
			// file already exists with different size
			// this can be caused by corrupted files
			return nil, ErrBadSz
		}

		// If the file size if zero, it should be a new
		// segment file. Truncate it to required size.
		if err := file.Truncate(size); err != nil {
			return nil, err
		}

		sz = size
	}

	fd := file.Fd()
	data, err := syscall.Mmap(int(fd), 0, int(size), mprot, mflag)
	if err != nil {
		return nil, err
	}

	// get slice header to get memory address and length
	head := (*reflect.SliceHeader)(unsafe.Pointer(&data))

	m = &Map{
		Data: data,
		hadr: head.Data,
		hlen: uintptr(head.Len),
	}

	return m, nil
}
Пример #22
0
// SessionRelease Write file session to local file with Gob string
func (fs *FileSessionStore) SessionRelease(w http.ResponseWriter) {
	b, err := EncodeGob(fs.values)
	if err != nil {
		return
	}
	_, err = os.Stat(path.Join(filepder.savePath, string(fs.sid[0]), string(fs.sid[1]), fs.sid))
	var f *os.File
	if err == nil {
		f, err = os.OpenFile(path.Join(filepder.savePath, string(fs.sid[0]), string(fs.sid[1]), fs.sid), os.O_RDWR, 0777)
	} else if os.IsNotExist(err) {
		f, err = os.Create(path.Join(filepder.savePath, string(fs.sid[0]), string(fs.sid[1]), fs.sid))
	} else {
		return
	}
	f.Truncate(0)
	f.Seek(0, 0)
	f.Write(b)
	f.Close()
}
Пример #23
0
func NewMemMapAppender(file *os.File, blockSize int, offset int64) (Writer, error) {
	stat, err := file.Stat()
	if err != nil {
		return nil, err
	}

	//
	if offset+int64(blockSize) > stat.Size() {
		file.Truncate(offset + int64(blockSize))
	}

	m := &mmapWriter{file, nil, offset, 0, blockSize, true}
	err = m.createMap()
	if err != nil {
		return nil, err
	}

	return &writer{m}, nil
}
Пример #24
0
// assign a property
func (proc *SProcess) SetProperty(prop string, value string) {
	var file *os.File

	// file exists; empty
	if proc.files[prop] != nil {
		file = proc.files[prop]
		file.Truncate(0)

		// doesn't exist; create
	} else {
		file, _ = os.Create("/system/process/" + strconv.Itoa(proc.pid) + "/" + prop)
		file.Chmod(0755)
	}

	proc.files[prop] = file

	// write
	file.Seek(0, 0)
	file.WriteString(value)
}
Пример #25
0
// ZeroToEnd zeros a file starting from SEEK_CUR to its SEEK_END. May temporarily
// shorten the length of the file.
func ZeroToEnd(f *os.File) error {
	// TODO: support FALLOC_FL_ZERO_RANGE
	off, err := f.Seek(0, os.SEEK_CUR)
	if err != nil {
		return err
	}
	lenf, lerr := f.Seek(0, os.SEEK_END)
	if lerr != nil {
		return lerr
	}
	if err = f.Truncate(off); err != nil {
		return err
	}
	// make sure blocks remain allocated
	if err = Preallocate(f, lenf, true); err != nil {
		return err
	}
	_, err = f.Seek(off, os.SEEK_SET)
	return err
}
Пример #26
0
// public for external
func CreateClientCredential(file string, d5s *D5ServConf, user string) (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, 0644)
		ThrowErr(e)
		e = f.Truncate(0)
		ThrowErr(e)
		defer func() {
			f.Sync()
			f.Close()
		}()
	}
	u, e := d5s.AuthSys.UserInfo(user)
	if e != nil {
		ThrowErr(e)
	}
	f.WriteString(d5s.Export_d5p(u))
	return
}
func removeLines(fn string, start, n int) (err error) {
	if start < 1 {
		return errors.New("invalid request.  line numbers start at 1.")
	}
	if n < 0 {
		return errors.New("invalid request.  negative number to remove.")
	}
	var f *os.File
	if f, err = os.OpenFile(fn, os.O_RDWR, 0); err != nil {
		return
	}
	defer func() {
		if cErr := f.Close(); err == nil {
			err = cErr
		}
	}()
	var b []byte
	if b, err = ioutil.ReadAll(f); err != nil {
		return
	}
	cut, ok := skip(b, start-1)
	if !ok {
		return fmt.Errorf("less than %d lines", start)
	}
	if n == 0 {
		return nil
	}
	tail, ok := skip(cut, n)
	if !ok {
		return fmt.Errorf("less than %d lines after line %d", n, start)
	}
	t := int64(len(b) - len(cut))
	if err = f.Truncate(t); err != nil {
		return
	}
	if len(tail) > 0 {
		_, err = f.WriteAt(tail, t)
	}
	return
}
Пример #28
0
// WriteFile writes a file from 'src' using
// memory mapping. It overwrites the entire
// contents of the previous file.
// The mapping size is calculated
// using the `Msgsize()` method
// of 'src', so it must produce a result
// equal to or greater than the actual encoded
// size of the object. Otherwise,
// a fault (SIGBUS) will occur.
//
// Reading and writing through file mappings
// is only efficient for large files; small
// files are best read and written using
// the ordinary streaming interfaces.
//
// NOTE: The performance of this call
// is highly OS- and filesystem-dependent.
// Users should take care to test that this
// performs as expected in a production environment.
// (Linux users should run a kernel and filesystem
// that support fallocate(2) for the best results.)
func WriteFile(src MarshalSizer, file *os.File) error {
	sz := src.Msgsize()
	err := fallocate(file, int64(sz))
	if err != nil {
		return err
	}
	data, err := syscall.Mmap(int(file.Fd()), 0, sz, syscall.PROT_READ|syscall.PROT_WRITE, syscall.MAP_SHARED)
	if err != nil {
		return err
	}
	adviseWrite(data)
	chunk := data[:0]
	chunk, err = src.MarshalMsg(chunk)
	if err != nil {
		return err
	}
	uerr := syscall.Munmap(data)
	if uerr != nil {
		return uerr
	}
	return file.Truncate(int64(len(chunk)))
}
Пример #29
0
// fetchAndWritePAMPass attempts to authenticate with the iCAT server using PAM.
// If PAM authentication is successful, it writes the returned PAM authentication token to a file for subsequent use in connections.
func (con *Connection) fetchAndWritePAMPass(pamPassFile *os.File, ipassword *C.char) (*C.char, error) {

	var (
		opassword *C.char
		errMsg    *C.char
	)

	if status := C.gorods_clientLoginPam(con.ccon, ipassword, C.int(con.Options.PAMPassExpire), &opassword, &errMsg); status != 0 {
		return nil, newError(Fatal, fmt.Sprintf("iRODS Connect Failed: clientLoginPam error, invalid password?"))
	}

	if er := pamPassFile.Truncate(0); er != nil {
		return nil, newError(Fatal, fmt.Sprintf("iRODS Connect Failed: Unable to write new password to PAMPassFile"))
	}

	pamPassFormat := strconv.Itoa(int(time.Now().Unix())) + ":" + C.GoString(opassword)

	if _, er := pamPassFile.WriteString(pamPassFormat); er != nil {
		return nil, newError(Fatal, fmt.Sprintf("iRODS Connect Failed: Unable to write new password to PAMPassFile"))
	}

	return opassword, nil
}
func gRepFile(oldb, newb []byte, fn string) (err error) {
	var f *os.File
	if f, err = os.OpenFile(fn, os.O_RDWR, 0); err != nil {
		return
	}
	defer func() {
		if cErr := f.Close(); err == nil {
			err = cErr
		}
	}()
	var b []byte
	if b, err = ioutil.ReadAll(f); err != nil {
		return
	}
	if bytes.Index(b, oldb) < 0 {
		return
	}
	r := bytes.Replace(b, oldb, newb, -1)
	if err = f.Truncate(0); err != nil {
		return
	}
	_, err = f.WriteAt(r, 0)
	return
}