Beispiel #1
0
// ==========================API DOCS=======================================
// API Name: Stream data to specified file
// Action: Write the data from http to the file streamingly
// API URL: /io/{contianer}/{followingpath}
// REQUEST: PUT
// Parameters:
//      - contianer(in URL): the container name
// Returns:
//      - HTTP 200: No error, the file is written by force
//              When success, the returned header Parent-Node(if accessed) will
//              contain its parent inode and File-Node will indicate the file itself.
//      - HTTP 404: Either the container or the filepath does not exist.
//      - HTTP 500: Error. The body is supposed to return error info.
// ==========================API DOCS END===================================
func uploader(req Request, res Response) {
	var pathDetail, _ = req.F()["HandledRR"].([]string)
	if pathDetail == nil {
		pathDetail = req.F()["RR"].([]string)
		pathDetail = append(pathDetail, filesystem.ROOT_INODE_NAME)
	}

	var fs = filesystem.GetFs(outapi.NewSwiftio(outapi.DefaultConnector, pathDetail[1]))
	if fs == nil {
		res.Status("Internal Error: the FS pool is full.", 500)
	}
	defer fs.Release()

	var putErr error
	if base, filename := pathman.SplitPath(pathDetail[2]); filename == "" {
		// TODO: glean user meta
		putErr, _ = fs.Put("", pathDetail[3], nil, req.R().Body)
		res.Set(FILE_NODE, pathDetail[3])
	} else {
		var nodeName, err = fs.Locate(base, pathDetail[3])
		if err != nil {
			res.Status("Nonexist container or path. "+err.Error(), 404)
			return
		}
		// TODO: glean user meta
		var targetNode string
		putErr, targetNode = fs.Put(filename, nodeName, nil, req.R().Body)
		if targetNode != "" {
			res.Set(FILE_NODE, targetNode)
		}
		res.Set(PARENT_NODE, nodeName)
	}

	if !egg.Nil(putErr) {
		if egg.In(putErr, exception.EX_FILE_NOT_EXIST) {
			res.Status("Nonexist container or path. Or you cannot refer to a non-existing inode in ovveride mode.", 404)
			return
		}
		res.Status("Internal Error: "+putErr.Error(), 500)
		return
	}
	res.SendCode(200)
}
Beispiel #2
0
func mvDirectory(req Request, res Response) {
	//===============measurement========================
	var startTime int64 = 0
	if req.Get("Enable-Measure") == "True" {
		startTime = time.Now().UnixNano()
	}
	//==================================================

	var pathDetail, _ = req.F()["HandledRR"].([]string)
	if pathDetail == nil {
		pathDetail = req.F()["RR"].([]string)
		pathDetail = append(pathDetail, filesystem.ROOT_INODE_NAME)
	}

	var base, filename = pathman.SplitPath(pathDetail[2])
	if filename == "" {
		res.Status("The directory/file to move should be specified.", 404)
		return
	}
	var destinationALL = req.Get(HEADER_DESTINATION)
	if destinationALL == "" {
		res.Status("Destination path should be specified in the Header "+HEADER_DESTINATION, 403)
		return
	}
	var destinationSC, destinationPath = pathman.ShortcutResolver(destinationALL)
	if destinationSC == "" {
		destinationSC = filesystem.ROOT_INODE_NAME
	} else {
		if len(destinationPath) > 0 {
			destinationPath = destinationPath[1:]
		}
	}
	var desBase, desFilename = pathman.SplitPath(destinationPath)
	if desFilename == "" {
		res.Status("The destination directory/file should be specified.", 404)
		return
	}

	var fs = filesystem.GetFs(outapi.NewSwiftio(outapi.DefaultConnector, pathDetail[1]))
	if fs == nil {
		res.Status("Internal Error: the FS pool is full.", 500)
	}
	defer fs.Release()

	var srcNodeNames, desNodeNames string
	var err error

	if req.Get(HEADER_DISTABLE_PARALLEL) == "TRUE" {
		srcNodeNames, err = fs.Locate(base, pathDetail[3])
		if err != nil {
			res.Status("Nonexist container or path. "+err.Error(), 404)
			return
		}
		desNodeNames, err = fs.Locate(desBase, destinationSC)
	} else {
		var wg sync.WaitGroup
		var lock sync.Mutex
		wg.Add(2)
		go (func() {
			defer wg.Done()

			var err2 error
			srcNodeNames, err2 = fs.Locate(base, pathDetail[3])
			if err2 != nil {
				lock.Lock()
				if err == nil {
					err = err2
				}
				lock.Unlock()
			}
		})()
		go (func() {
			defer wg.Done()

			var err2 error
			desNodeNames, err = fs.Locate(desBase, destinationSC)
			if err2 != nil {
				lock.Lock()
				if err == nil {
					err = err2
				}
				lock.Unlock()
			}
		})()
		wg.Wait()
	}
	if err != nil {
		res.Status("Nonexist container or path. "+err.Error(), 404)
		return
	}

	var byForce = req.Get(HEADER_MOVE_BY_FORCE) == "TRUE"
	if req.Get(HEADER_DISTABLE_PARALLEL) == "TRUE" {
		err = fs.MvX(filename, srcNodeNames, desFilename, desNodeNames, byForce)
	} else {
		err = fs.MvXParalleled(filename, srcNodeNames, desFilename, desNodeNames, byForce)
	}
	if !egg.Nil(err) {
		if egg.In(err, exception.EX_FILE_NOT_EXIST) {
			res.Status("Not Found", 404)
			return
		}
		if egg.In(err, exception.EX_FOLDER_ALREADY_EXIST) {
			res.Status("The destination has already existed.", 202)
			return
		}
		res.Status("Internal error: "+err.Error(), 500)
		return
	}

	//=============================================
	if startTime > 0 {
		res.Set("Time-Consumed", strconv.FormatInt(time.Now().UnixNano()-startTime, 10))
	}
	//=============================================
	res.SendCode(201)
}
Beispiel #3
0
// ==========================API DOCS=======================================
// API Name: Remove one directory
// Action: remove the directory only if it exists and its parent path exists
// API URL: /fs/{contianer}/{followingpath}
// REQUEST: DELETE
// Parameters:
//      - contianer(in URL): the container name
//      - followingpath(in URL): the path to be removed. Please guarantee its parent node exists.
//      - Disable-Parallel(in Header): if set to TRUE, a non-parallelized mkdir will be
//              operated. Default to FALSE, it is only for test and not recommend to set.
// Returns:
//      - HTTP 204: The deletion succeeds but it is only a patch. to ensure created, another list
//        operation should be carried.
//              When success, 'Parent-Node' will indicate the parent of removed directory.
//      - HTTP 404: Either the container or the parent filepath or the file itself does not exist.
//      - HTTP 500: Error. The body is supposed to return error info.
// ==========================API DOCS END===================================
func rmDirectory(req Request, res Response) {
	//===============measurement========================
	var startTime int64 = 0
	if req.Get("Enable-Measure") == "True" {
		startTime = time.Now().UnixNano()
	}
	//==================================================

	var pathDetail, _ = req.F()["HandledRR"].([]string)
	if pathDetail == nil {
		pathDetail = req.F()["RR"].([]string)
		pathDetail = append(pathDetail, filesystem.ROOT_INODE_NAME)
	}

	var trimer = pathDetail[2]
	var i int
	for i = len(trimer) - 1; i >= 0; i-- {
		if trimer[i] != '/' {
			break
		}
	}
	if i < 0 {
		res.Status("The directory to remove should be specified.", 404)
		return
	}
	trimer = trimer[:i+1]
	var j int
	for j = i; j >= 0; j-- {
		if trimer[j] == '/' {
			break
		}
	}
	var base = trimer[:j+1]
	trimer = trimer[j+1:]
	// now trimer holds the last foldername
	// base holds the parent folder path

	var fs = filesystem.GetFs(outapi.NewSwiftio(outapi.DefaultConnector, pathDetail[1]))
	if fs == nil {
		res.Status("Internal Error: the FS pool is full.", 500)
	}
	defer fs.Release()

	var nodeName, err = fs.Locate(base, pathDetail[3])
	if err != nil {
		res.Status("Nonexist container or path. "+err.Error(), 404)
		return
	}

	res.Set(LAST_PARENT_NODE, nodeName)
	// TODO: what if the src file does not exist?
	if req.Get(HEADER_DISTABLE_PARALLEL) == "TRUE" {
		err = fs.Rm(trimer, nodeName)
	} else {
		err = fs.RmParalleled(trimer, nodeName)
	}
	if !egg.Nil(err) {
		if egg.In(err, exception.EX_INODE_NONEXIST) {
			res.Status("Nonexist container or path.", 404)
			return
		}
		if egg.In(err, exception.EX_FILE_NOT_EXIST) {
			res.Status("Nonexist container or path.", 404)
			return
		}
		res.Status("Internal Error: "+err.Error(), 500)
		return
	}

	//=============================================
	if startTime > 0 {
		res.Set("Time-Consumed", strconv.FormatInt(time.Now().UnixNano()-startTime, 10))
	}
	//=============================================
	res.SendCode(204)
}