Beispiel #1
0
// Test directory with 1 file and 1 non-empty directory
func TestSizeFileAndNestedDirectoryNonempty(t *testing.T) {
	var err error
	if err = os.MkdirAll("/tmp/testSizeFileAndNestedDirectoryEmpty/nested", 0777); err != nil {
		t.Fatalf("failed to create directory: %s", err)
	}

	var file *os.File
	if file, err = os.Create("/tmp/testSizeFileAndNestedDirectoryEmpty/file"); err != nil {
		t.Fatalf("failed to create file: %s", err)
	}

	data := []byte{100, 111, 99, 107, 101, 114}
	file.Write(data)

	var nestedFile *os.File
	if nestedFile, err = os.Create("/tmp/testSizeFileAndNestedDirectoryEmpty/nested/file"); err != nil {
		t.Fatalf("failed to create file: %s", err)
	}

	nestedData := []byte{100, 111, 99, 107, 101, 114}
	nestedFile.Write(nestedData)

	var size int64
	if size, _ = Size("/tmp/testSizeFileAndNestedDirectoryEmpty"); size != 12 {
		t.Fatalf("directory with 6-byte file and empty directory has size: %d", size)
	}
}
Beispiel #2
0
// Tries to write a challenge file to each of the directories.
func webrootWriteChallenge(webroots map[string]struct{}, token string, ka []byte) {
	log.Debugf("writing %d webroot challenge files", len(webroots))

	for wr := range webroots {
		os.MkdirAll(wr, 0755) // ignore errors
		fn := filepath.Join(wr, token)
		log.Debugf("writing webroot file %s", fn)

		// Because /var/run/acme/acme-challenge may not exist due to /var/run
		// possibly being a tmpfs, and because that tmpfs is likely to be world
		// writable, there is a risk of following a maliciously crafted symlink to
		// cause a file to be overwritten as root. Open the file using a
		// no-symlinks flag if the OS supports it, but only for /var/run paths; we
		// want to support symlinks for other paths, which are presumably properly
		// controlled.
		//
		// Unfortunately earlier components in the pathname will still be followed
		// if they are symlinks, but it looks like this is the best we can do.
		var f *os.File
		var err error
		if strings.HasPrefix(wr, "/var/run/") {
			f, err = deos.OpenFileNoSymlinks(fn, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
		} else {
			f, err = os.OpenFile(fn, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
		}
		if err != nil {
			log.Infoe(err, "failed to open webroot file ", fn)
			continue
		}

		f.Write(ka)
		f.Close()
	}
}
Beispiel #3
0
// Lock creates a lockfile which prevents to open more than one instance
// of the same node (on the same machine).
func (ctx *Context) Lock() (err error) {
	var f *os.File
	var p *os.Process
	var pid int

	lockFile := path.Join(ctx.storageDir, ctx.nodeName+".lock")
	if f, err = os.Open(lockFile); err != nil {
		goto lock
	}
	if _, err = fmt.Fscanf(f, "%d", &pid); err != nil && pid == 0 {
		goto lock
	}
	if p, err = os.FindProcess(pid); err == nil && p != nil {
		if err = p.Signal(os.UnixSignal(0)); err == nil {
			return errors.New(
				fmt.Sprintf("node '%s' is already running",
					ctx.NodeName()))
		}
	}
lock:
	// Write a lock file.
	if f, err = os.Create(lockFile); err == nil {
		pid := os.Getppid()
		f.Write([]byte(fmt.Sprintf("%d", pid)))
		f.Close()
	}
	return nil
}
Beispiel #4
0
func assertAst(t *testing.T, root ast.Node, case_name string) {
	var out []byte
	var err error
	if out, err = json.Marshal(root); err != nil {
		t.Fatal(err)
	}

	var buf bytes.Buffer
	if err = json.Indent(&buf, out, "", "\t"); err != nil {
		t.Fatal(err)
	}

	var file *os.File
	jsn := buf.String()
	if file, err = os.Open(kTestingPrefix + case_name + ".json"); err != nil {
		t.Logf("[%s] not found: %v, now dump: \n%s", case_name, err, jsn)
		if file, err = os.Create(kTestingPrefix + case_name + ".json_"); err != nil {
			panic(err)
		}
		file.Write(buf.Bytes())
		file.Close()
		return
	}
	defer file.Close()

	buf.Reset()
	if _, err = buf.ReadFrom(file); err != nil {
		t.Fatal(err)
	}
	if buf.String() != jsn {
		t.Fatalf("[%s] Assert failed, Expected: ----------\n%s\nValue is: ----------\n%s",
			case_name, buf.String(), jsn)
	}
}
Beispiel #5
0
// GetPass reads password from a TTY with no echo.
func GetPass(prompt string, f *os.File, pbuf []byte) ([]byte, error) {
	t, err := Attr(f)
	if err != nil {
		return nil, err
	}
	defer t.Set(f)
	noecho := t
	noecho.Lflag = noecho.Lflag &^ ECHO
	if err := noecho.Set(f); err != nil {
		return nil, err
	}
	b := make([]byte, 1, 1)
	i := 0
	if _, err := f.Write([]byte(prompt)); err != nil {
		return nil, err
	}
	for ; i < len(pbuf); i++ {
		if _, err := f.Read(b); err != nil {
			b[0] = 0
			clearbuf(pbuf[:i+1])
		}
		if b[0] == '\n' || b[0] == '\r' {
			return pbuf[:i], nil
		}
		pbuf[i] = b[0]
		b[0] = 0
	}
	clearbuf(pbuf[:i+1])
	return nil, errors.New("ran out of bufferspace")
}
Beispiel #6
0
func (srv *Grande) Write(req *vtsrv.Req) {
	var f *os.File
	var n int

	s := srv.calcScore(req.Tc.Data)
	dname := fmt.Sprintf("%s/%02x/%02x", srv.topDir, s[0], s[1])
	err := os.MkdirAll(dname, 0777)
	if err != nil {
		req.RespondError(err.Error())
		return
	}

	f, err = os.OpenFile(srv.Name(s), os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0666)
	if err != nil {
		req.RespondError(err.Error())
		return
	}

	defer f.Close()
	n, err = f.Write(req.Tc.Data)
	if err != nil {
		req.RespondError(err.Error())
		return
	}

	if n != len(req.Tc.Data) {
		req.RespondError("short write")
		return
	}

	req.RespondWrite(s)
}
Beispiel #7
0
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 WriteGroups(hostFile *os.File) {
	for groupName, group := range Groups {

		header := fmt.Sprintf("[%s]\n", groupName)
		keys := make([]string, 0)
		for k, v := range AnsibleInstances {
			for _, g := range group {
				if *v.ImageID == g {
					keys = append(keys, k)
				}
			}
		}
		sort.Strings(keys)

		hostFile.Write([]byte(header))
		for _, k := range keys {
			v, _ := AnsibleInstances[k]
			err := hostTemplate.Execute(hostFile, v)
			if err != nil {
				panic(err)
			}
		}
		hostFile.Write([]byte("\n"))
	}

}
Beispiel #9
0
func Sign(privateKeyFileName string, readPass readPasswordCallback, fileToSign string, signatureFile string) (err error) {

	var signer *openpgp.Entity
	if signer, err = readPrivateKeyFile(privateKeyFileName, readPass); err != nil {
		return err
	}

	var message *os.File
	if message, err = os.Open(fileToSign); err != nil {
		return err
	}
	defer message.Close()

	var w *os.File
	if w, err = os.Create(signatureFile); err != nil {
		return err
	}
	defer w.Close()

	if err = openpgp.ArmoredDetachSign(w, signer, message, nil); err != nil {
		return err
	}
	w.Write([]byte("\n"))

	return nil
}
Beispiel #10
0
func (u *U16x16) PutData2(data []byte, key string) (length int64, hash string, err error) {
	s := sha256.New()
	s.Write(data)
	hash = hex.EncodeToString(s.Sum(nil))
	if hash != key {
		fmt.Printf("expected data to have key %s, but content key is %s",
			key, hash)
		err = errors.New("content/key mismatch")
		return
	}
	length = int64(len(data))
	topSubDir := hash[0:1]
	lowerDir := hash[1:2]
	targetDir := filepath.Join(u.path, topSubDir, lowerDir)
	found, err := xf.PathExists(targetDir)
	if err == nil && !found {
		err = os.MkdirAll(targetDir, 0775)
	}
	fullishPath := filepath.Join(targetDir, key[2:])
	found, err = xf.PathExists(fullishPath)
	if !found {
		var dest *os.File
		dest, err = os.Create(fullishPath)
		if err == nil {
			var count int
			defer dest.Close()
			count, err = dest.Write(data)
			if err == nil {
				length = int64(count)
			}
		}
	}
	return
}
Beispiel #11
0
func (l *Logger) receive() {
	today := time.Now()
	var file *os.File
	var err error
	for data := range l.in {
		if l.dir != "" && (file == nil || today.Day() != time.Now().Day()) {
			l.mu.Lock()
			today = time.Now()
			file, err = os.OpenFile(fmt.Sprintf("%s/%s_%s.log", l.dir, l.obj, today.Format("2006-01-02")), os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0666)
			if err != nil {
				panic(err)
			}
			l.mu.Unlock()
			if l.flag&Ldaily != 0 {
				go l.rotate(today)
			}
		}
		if file != nil {
			file.Write(data)
		}
		if l.out != nil {
			l.out.Write(data)
		}
	}
}
Beispiel #12
0
func withFile(name string, content string, action func(f *os.File)) error {
	var (
		err error
		f   *os.File
	)

	withError := func(action func()) {
		if err != nil {
			return
		}

		action()
	}

	func() {
		withError(func() { f, err = os.Create(name) })
		if err == nil {
			defer f.Close()
		}

		withError(func() { _, err = f.Write([]byte(content)) })
		withError(func() { _, err = f.Seek(0, 0) })
		action(f)
	}()

	withError(func() { err = os.Remove(name) })

	if err == nil {
		return nil
	}

	return ioError
}
Beispiel #13
0
func WriteBytesToFile(filePath string, data []byte, append bool) error {
	var f *os.File
	var err error
	if append == true {
		if _, err := os.Stat(filePath); os.IsNotExist(err) {
			f, err = os.Create(filePath)
			if err != nil {
				return err
			}
			f.Close()
		}

		// open files r and w
		f, err = os.OpenFile(filePath, os.O_APPEND|os.O_WRONLY, 0600)
		if err != nil {
			return err
		}
	} else {
		f, err = os.Create(filePath)
		if err != nil {
			return err
		}
	}

	defer f.Close()

	_, err = f.Write(data)
	return err
}
Beispiel #14
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
}
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
}
Beispiel #16
0
func run(pipe *os.File, topic string, threads int, count int) {
	n, err := pipe.Write([]byte(rand.RandStr(RANDSTRLEN, "alphanum")))
	fmt.Println(n, err)
	//var wg sync.WaitGroup
	//for i := 0; i < threads; i++ {
	//	wg.Add(1)
	//	go func(thread int) {
	//		defer wg.Done()
	//		for j := 0; j < count; j++ {
	//			_, err := pipe.WriteString(rand.RandStr(RANDSTRLEN, "alphanum")+ "\n")
	//			if err != nil {
	//				log.Printf("%d write error: %s\n", thread, err)
	//				return
	//			}
	//		}
	//	}(i)
	//}
	//for i := 0; i < COUNT; i++ {
	//	wg.Add(1)
	//	go func() {
	//		defer wg.Done()
	//		for j := 0; j < COUNT; j++ {
	//			_, err := http.Get("http://localhost:8080/events/topic2")
	//			if err != nil {
	//				log.Printf("GET error: %s\n", err)
	//			}
	//		}
	//	}()
	//}
	//wg.Wait()
}
Beispiel #17
0
// bufWrite writes content either to stdout or the requested output file
func bufWrite(content []byte, file *os.File) (int, error) {
	n, err := file.Write(content)
	if err != nil {
		return n, err
	}
	return n, nil
}
Beispiel #18
0
//单个下载
func downOne(url, path string, num int, record *os.File) (name string, err error) {
	resp, err := http.Get(url)
	if err != nil {
		return "", err
	}
	defer resp.Body.Close()

	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		return "", err
	}

	name = path + "/" + time.Now().Format("20060102") + "-" + fmt.Sprint(num) + ".tar"
	out, err := os.Create(name)
	if err != nil {
		return "", err
	}
	defer out.Close()

	_, err = io.Copy(out, bytes.NewReader([]byte(body)))
	if err != nil {
		return "", err
	}

	record.Write([]byte(fmt.Sprint("下载完成 ", num, " :"+time.Now().Format("20060102")+"-"+fmt.Sprint(num)+".tar\r\n")))
	return name, nil
}
Beispiel #19
0
func (e *Engine) writeBlock(f *os.File, id uint64, block []byte) error {
	if _, err := f.Write(append(u64tob(id), u32tob(uint32(len(block)))...)); err != nil {
		return err
	}
	_, err := f.Write(block)
	return err
}
Beispiel #20
0
//解压tar
func unTar(path, pathTo string, record *os.File) (err error) {
	f, err := os.Open(path)
	if err != nil {
		return err
	}
	defer f.Close()
	// tar read
	tr := tar.NewReader(f)
	// 读取文件
	for {
		h, err := tr.Next()
		if err == io.EOF {
			break
		}
		if err != nil {
			return err
		}
		// 记录文件
		record.Write([]byte(h.Name + "\r\n"))

		fw, err := os.OpenFile(pathTo+"/"+h.Name, os.O_CREATE|os.O_WRONLY, 0644)
		if err != nil {
			return err
		}
		defer fw.Close()
		// 复制文件
		_, err = io.Copy(fw, tr)
		if err != nil {
			return err
		}
	}
	return nil
}
Beispiel #21
0
// CreateDataFile creates user's data file.
func (d *Data) CreateDataFile(path string) (err error) {
	err = os.MkdirAll(filepath.Dir(path), 0700)
	if err != nil {
		return
	}

	var f *os.File
	f, err = os.Create(path)
	if err != nil {
		return
	}
	defer f.Close()

	var b []byte
	b, err = json.Marshal(*d)
	if err != nil {
		return
	}

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

	return
}
Beispiel #22
0
func (ftp *FtpConnection) GetFile(local_path, remote_path string) error {
	var err error

	buf := new(bytes.Buffer)
	if err := ftp.Conn.Retrieve(remote_path, buf); err != nil {
		return err
	}

	var stat os.FileInfo
	stat, err = ftp.Stat(remote_path)
	if err != nil {
		return err
	}

	var file *os.File

	if file, err = os.OpenFile(local_path, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, stat.Mode()); err != nil {
		return err
	}
	defer file.Close()

	file.Write(buf.Bytes())

	return nil
}
Beispiel #23
0
func main() {
	var src, dst *os.File
	src, _ = os.Open("base64")
	defer func() {
		src.Close()
		dst.Close()
	}()

	dst, _ = os.OpenFile("base64_2", os.O_WRONLY|os.O_CREATE, 0644)

	//_, err := io.Copy(dst, src)
	//fmt.Println(err)
	buf := make([]byte, 1024)
	for {
		nr, er := src.Read(buf)
		if nr > 0 {
			nw, _ := dst.Write(buf[0:nr])
			fmt.Println("write size:", nw)
		}
		if er == io.EOF {
			break
		}
	}

}
Beispiel #24
0
func TestGet(t *testing.T) {
	if testing.Short() {
		t.Skip("skipping Get test in short mode.")
	}
	yahooImg := "http://k.yimg.jp/images/top/sp2/cmn/logo-ns-130528.png"
	g := Getter{url: yahooImg}
	result, err := g.Get()

	if err != nil {
		t.Error(err)
	} else {

		tmpdir := os.TempDir()
		imgPath := path.Join(tmpdir, "yahooImg.png")
		var file *os.File
		file, err = os.Create(imgPath)

		img, err := ioutil.ReadAll(*result)
		(*result).Close()
		if err != nil {
			t.Error(err)
		}
		t.Log("Wrote the image to:", imgPath)
		file.Write(img)
	}

}
func visit(path string, f os.FileInfo, err error) error {
	var output_path string
	var e error
	var fw *os.File

	if !strings.HasSuffix(path, ".html") {
		return nil
	}

	ptt_article := convert_article_html(path)

	var b []byte
	if b, e = json.MarshalIndent(ptt_article, "", "   "); e != nil {
		log.Fatal(e)
		return nil
	}

	output_path = strings.TrimSuffix(path, ".html") + ".json"
	if fw, e = os.Create(output_path); e != nil {
		log.Fatal(e)
		return nil
	}
	log.Printf("%s", output_path)
	fw.Write(b)
	return nil
}
Beispiel #26
0
func (fm *FileMonitor) updateJournal(bytes_read int64) (ok bool) {
	var seekJournal *os.File
	var file_err error

	if bytes_read == 0 || fm.seekJournalPath == "" {
		return true
	}

	if seekJournal, file_err = os.OpenFile(fm.seekJournalPath,
		os.O_CREATE|os.O_RDWR|os.O_TRUNC,
		0660); file_err != nil {
		fm.LogError(fmt.Sprintf("Error opening seek recovery log: %s", file_err.Error()))
		return false
	}
	defer seekJournal.Close()

	var filemon_bytes []byte
	filemon_bytes, _ = json.Marshal(fm)
	if _, file_err = seekJournal.Write(filemon_bytes); file_err != nil {
		fm.LogError(fmt.Sprintf("Error writing seek recovery log: %s", file_err.Error()))
		return false
	}

	return true
}
Beispiel #27
0
// GenerateCookie generates new random node admin's cookie hash and saves
// it to the storage dir in the 'cookie' file. If no force flag specified
// then cookie will be not overwritten during the further calls of this
// function.
//
// force - If true, then generates new cookie and overwrites existing one.
//
// Returns an error if something went wrong.
func (ctx *Context) GenerateCookie(force bool) (err error) {
	if ctx.storageDir == "" {
		return errors.New("can't generate cookie, storage not set")
	}
	var buf = make([]byte, CookieSize)
	var cookieFile *os.File
	cookiePath := path.Join(ctx.storageDir, ctx.nodeName+".cookie")
	if !force {
		cookieFile, err = os.Open(cookiePath)
		if err == nil {
			n, err := io.ReadFull(cookieFile, buf[:])
			if n == CookieSize && err == nil {
				ctx.cookie = string(buf[:])
				cookieFile.Close()
				return nil
			}
		}
	}
	// Generate new cookie if there's none or the force flag is enabled.
	if _, err = rand.Read(buf[:16]); err != nil {
		return
	}
	hash := sha1.New()
	hash.Write(buf[:16])
	ctx.cookie = fmt.Sprintf("%x", hash.Sum([]byte{}))
	if cookieFile, err = os.Create(cookiePath); err != nil {
		return
	}
	cookieFile.Write([]byte(ctx.cookie))
	cookieFile.Close()
	return
}
Beispiel #28
0
// ToTempFile writes an action into a generated temporary file and returns its filename
func (a Action) ToTempFile() (filename string, err error) {
	var (
		data []byte
		fd   *os.File
		fi   os.FileInfo
	)
	data, err = json.Marshal(a)
	if err != nil {
		return
	}
	fd, err = ioutil.TempFile("", "migaction_")
	defer fd.Close()
	if err != nil {
		return
	}
	_, err = fd.Write(data)
	if err != nil {
		return
	}
	fi, err = fd.Stat()
	if err != nil {
		return
	}
	filename = fmt.Sprintf("%s/%s", os.TempDir(), fi.Name())
	return
}
Beispiel #29
0
func (f *File) Save() (err error) {
	var done sync.Mutex
	done.Lock()
	f.cbs <- func() {
		defer done.Unlock()
		tmpPath := f.path + "." + strconv.FormatInt(rand.Int63(), 10)
		var tmpF *os.File
		tmpF, err = os.Create(tmpPath)
		if err != nil {
			return
		}
		defer tmpF.Close()
		buf := new(bytes.Buffer)
		err = json.NewEncoder(buf).Encode(f.Obj)
		if err != nil {
			return
		}
		// indent
		indentBuf := new(bytes.Buffer)
		err = json.Indent(indentBuf, buf.Bytes(), "", "    ")
		if err != nil {
			return
		}
		_, err = tmpF.Write(indentBuf.Bytes())
		if err != nil {
			return
		}
		err = os.Rename(tmpPath, f.path)
		if err != nil {
			return
		}
	}
	done.Lock()
	return
}
Beispiel #30
0
func (this *TVideoIOYuv) writePlane(fd *os.File, src []Pel, is16bit bool, stride, width, height int) bool {
	var write_len, x, y int

	if is16bit {
		write_len = width * 2
	} else {
		write_len = width
	}

	buf := make([]byte, write_len)
	for y = 0; y < height; y++ {
		if !is16bit {
			for x = 0; x < width; x++ {
				buf[x] = byte(src[y*stride+x])
			}
		} else {
			for x = 0; x < width; x++ {
				buf[2*x] = byte(src[y*stride+x] & 0xff)
				buf[2*x+1] = byte((src[y*stride+x] >> 8) & 0xff)
			}
		}

		n, err := fd.Write(buf)
		if err != nil || n != write_len {
			return false
		}
	}

	return true
}