Пример #1
0
// Rename is equivalent to the Linux mv command except that the collection must stay within it's current collection (directory), returns error.
func (col *Collection) Rename(newFileName string) error {

	if strings.Contains(newFileName, "/") {
		return newError(Fatal, fmt.Sprintf("Can't Rename DataObject, path detected in: %v", newFileName))
	}

	var err *C.char

	source := col.path
	destination := path.Dir(col.path) + "/" + newFileName

	s := C.CString(source)
	d := C.CString(destination)

	defer C.free(unsafe.Pointer(s))
	defer C.free(unsafe.Pointer(d))

	ccon := col.con.GetCcon()
	defer col.con.ReturnCcon(ccon)

	if status := C.gorods_move_dataobject(s, d, C.RENAME_COLL, ccon, &err); status != 0 {
		return newError(Fatal, fmt.Sprintf("iRODS Rename Collection Failed: %v, %v", col.path, C.GoString(err)))
	}

	col.name = newFileName
	col.path = destination

	col.chandle = C.int(-1)

	return nil
}
Пример #2
0
// MoveTo moves the collection to the specified collection. Supports Collection struct or string as input. Also refreshes the source and destination collections automatically to maintain correct state. Returns error.
func (col *Collection) MoveTo(iRODSCollection interface{}) error {

	var (
		err                         *C.char
		destination                 string
		destinationCollectionString string
		destinationCollection       *Collection
	)

	switch iRODSCollection.(type) {
	case string:
		destinationCollectionString = iRODSCollection.(string)

		// Is this a relative path?
		if destinationCollectionString[0] != '/' {
			destinationCollectionString = path.Dir(col.path) + "/" + destinationCollectionString
		}

		if destinationCollectionString[len(destinationCollectionString)-1] != '/' {
			destinationCollectionString += "/"
		}

		destination += destinationCollectionString + col.name
	case *Collection:
		destinationCollectionString = (iRODSCollection.(*Collection)).path + "/"
		destination = destinationCollectionString + col.name
	default:
		return newError(Fatal, fmt.Sprintf("iRODS Move Collection Failed, unknown variable type passed as collection"))
	}

	path := C.CString(col.path)
	dest := C.CString(destination)

	defer C.free(unsafe.Pointer(path))
	defer C.free(unsafe.Pointer(dest))

	ccon := col.con.GetCcon()

	if status := C.gorods_move_dataobject(path, dest, C.RENAME_COLL, ccon, &err); status != 0 {
		col.con.ReturnCcon(ccon)
		return newError(Fatal, fmt.Sprintf("iRODS Move Collection Failed: %v, D:%v, %v", col.path, destination, C.GoString(err)))
	}

	col.con.ReturnCcon(ccon)

	// Reload source collection, we are now detached
	col.parent.Refresh()

	// Find & reload destination collection
	switch iRODSCollection.(type) {
	case string:
		var colEr error

		// Can't find, load collection into memory
		destinationCollection, colEr = col.con.Collection(CollectionOptions{
			Path:      destinationCollectionString,
			Recursive: false,
		})
		if colEr != nil {
			return colEr
		}
	case *Collection:
		destinationCollection = (iRODSCollection.(*Collection))
	default:
		return newError(Fatal, fmt.Sprintf("iRODS Move Collection Failed, unknown variable type passed as collection"))
	}

	destinationCollection.Refresh()

	// Reassign obj.col to destination collection
	col.parent = destinationCollection
	col.path = destinationCollection.path + "/" + col.name

	col.chandle = C.int(-1)

	return nil
}