コード例 #1
0
func (d *GlusterDiskFile) Commit(a ...interface{}) error {
	d.file.Sync()

	err := d.file.Close()
	if err != nil {
		hummingbird.GetLogger(d.request).LogError("file.Close() failed: %s", err.Error())
		return err
	}
	d.file = nil

	err = d.volume.Rename(d.tempFileName, d.dataFile)
	if err != nil {
		hummingbird.GetLogger(d.request).LogError("Error renaming file: %s -> %s", d.tempFileName, d.dataFile)
		return err
	}
	d.commitSucceeded = true

	return nil
}
コード例 #2
0
func (d *GlusterDiskFile) Create(a ...interface{}) (io.Writer, error) {

	err := d.volume.MkdirAll(d.dataDir, 0755)
	if err != nil {
		hummingbird.GetLogger(d.request).LogError("Error creating directory %s:%s - %s",
			d.vars["device"], d.dataDir, err.Error())
		if err.(syscall.Errno) == syscall.ENOSPC || err.(syscall.Errno) == syscall.EDQUOT {
			return nil, hummingbird.ResponseToReturn{StatusInsufficientStorage}
		} else {
			return nil, hummingbird.ResponseToReturn{http.StatusInternalServerError}
		}
	}

	u := uuid.NewV4()
	tempFile := d.dataDir + "/" + "." + path.Base(d.dataFile) + "." + fmt.Sprintf("%x", u[0:16])
	d.file, err = d.volume.OpenFile(tempFile, os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0666)
	if err != nil {
		hummingbird.GetLogger(d.request).LogError("Error creating temporary file in %s:%s - %s",
			d.vars["device"], tempFile, err.Error())
		if err.(syscall.Errno) == syscall.ENOSPC || err.(syscall.Errno) == syscall.EDQUOT {
			return nil, hummingbird.ResponseToReturn{StatusInsufficientStorage}
		} else {
			return nil, hummingbird.ResponseToReturn{http.StatusInternalServerError}
		}
	}
	d.tempFileName = d.file.Name()

	if !d.disableFallocate && d.request.ContentLength > 0 {
		err = d.file.Fallocate(FALLOC_FL_KEEP_SIZE, 0, int64(d.request.ContentLength))
		if err != nil {
			hummingbird.GetLogger(d.request).LogError("Fallocate(%d, 0, %d) failed in volume %s - %s",
				FALLOC_FL_KEEP_SIZE, d.request.ContentLength, d.vars["device"], err.Error())
			if err.(syscall.Errno) == syscall.ENOSPC || err.(syscall.Errno) == syscall.EDQUOT {
				return nil, hummingbird.ResponseToReturn{StatusInsufficientStorage}
			} else {
				return nil, hummingbird.ResponseToReturn{http.StatusInternalServerError}
			}
		}
	}

	return d.file, nil
}