2021-02-26 11:41:38 +01:00
|
|
|
// Package librclone exports shims for C library use
|
|
|
|
//
|
|
|
|
// This directory contains code to build rclone as a C library and the
|
|
|
|
// shims for accessing rclone from C.
|
|
|
|
//
|
|
|
|
// The shims are a thin wrapper over the rclone RPC.
|
|
|
|
//
|
|
|
|
// Build a shared library like this:
|
|
|
|
//
|
|
|
|
// go build --buildmode=c-shared -o librclone.so github.com/rclone/rclone/librclone
|
|
|
|
//
|
|
|
|
// Build a static library like this:
|
|
|
|
//
|
|
|
|
// go build --buildmode=c-archive -o librclone.a github.com/rclone/rclone/librclone
|
|
|
|
//
|
|
|
|
// Both the above commands will also generate `librclone.h` which should
|
|
|
|
// be `#include`d in `C` programs wishing to use the library.
|
|
|
|
//
|
|
|
|
// The library will depend on `libdl` and `libpthread`.
|
2020-12-31 03:32:52 +01:00
|
|
|
package main
|
|
|
|
|
2021-03-30 11:02:48 +02:00
|
|
|
/*
|
2021-10-11 00:10:01 +02:00
|
|
|
#include <stdlib.h>
|
|
|
|
|
2021-03-30 11:02:48 +02:00
|
|
|
struct RcloneRPCResult {
|
|
|
|
char* Output;
|
|
|
|
int Status;
|
|
|
|
};
|
|
|
|
*/
|
|
|
|
import "C"
|
2021-02-26 11:41:38 +01:00
|
|
|
|
2021-03-30 11:02:48 +02:00
|
|
|
import (
|
2021-10-11 00:10:01 +02:00
|
|
|
"unsafe"
|
|
|
|
|
2021-04-28 13:58:08 +02:00
|
|
|
"github.com/rclone/rclone/librclone/librclone"
|
2020-12-31 03:32:52 +01:00
|
|
|
|
2021-05-27 19:23:38 +02:00
|
|
|
_ "github.com/rclone/rclone/backend/all" // import all backends
|
2022-08-03 11:11:16 +02:00
|
|
|
_ "github.com/rclone/rclone/cmd/cmount" // import cmount
|
|
|
|
_ "github.com/rclone/rclone/cmd/mount" // import mount
|
|
|
|
_ "github.com/rclone/rclone/cmd/mount2" // import mount2
|
2021-05-27 19:23:38 +02:00
|
|
|
_ "github.com/rclone/rclone/fs/operations" // import operations/* rc commands
|
|
|
|
_ "github.com/rclone/rclone/fs/sync" // import sync/*
|
|
|
|
_ "github.com/rclone/rclone/lib/plugin" // import plugins
|
2020-12-31 03:32:52 +01:00
|
|
|
)
|
|
|
|
|
2021-02-26 11:41:38 +01:00
|
|
|
// RcloneInitialize initializes rclone as a library
|
|
|
|
//
|
|
|
|
//export RcloneInitialize
|
|
|
|
func RcloneInitialize() {
|
2021-04-28 13:58:08 +02:00
|
|
|
librclone.Initialize()
|
2020-12-31 03:32:52 +01:00
|
|
|
}
|
|
|
|
|
2021-02-26 11:41:38 +01:00
|
|
|
// RcloneFinalize finalizes the library
|
|
|
|
//
|
|
|
|
//export RcloneFinalize
|
|
|
|
func RcloneFinalize() {
|
2021-04-28 13:58:08 +02:00
|
|
|
librclone.Finalize()
|
2020-12-31 03:32:52 +01:00
|
|
|
}
|
|
|
|
|
2021-03-30 11:02:48 +02:00
|
|
|
// RcloneRPCResult is returned from RcloneRPC
|
|
|
|
//
|
|
|
|
// Output will be returned as a serialized JSON object
|
|
|
|
// Status is a HTTP status return (200=OK anything else fail)
|
2021-03-30 12:37:59 +02:00
|
|
|
type RcloneRPCResult struct { //nolint:deadcode
|
2021-03-30 11:02:48 +02:00
|
|
|
Output *C.char
|
|
|
|
Status C.int
|
|
|
|
}
|
|
|
|
|
2021-02-26 11:41:38 +01:00
|
|
|
// RcloneRPC does a single RPC call. The inputs are (method, input)
|
|
|
|
// and the output is (output, status). This is an exported interface
|
|
|
|
// to the rclone API as described in https://rclone.org/rc/
|
|
|
|
//
|
|
|
|
// method is a string, eg "operations/list"
|
2021-10-25 16:51:13 +02:00
|
|
|
// input should be a string with a serialized JSON object
|
|
|
|
// result.Output will be returned as a string with a serialized JSON object
|
2021-03-30 11:02:48 +02:00
|
|
|
// result.Status is a HTTP status return (200=OK anything else fail)
|
2021-02-26 11:41:38 +01:00
|
|
|
//
|
2021-10-25 16:51:13 +02:00
|
|
|
// All strings are UTF-8 encoded, on all platforms.
|
|
|
|
//
|
|
|
|
// Caller is responsible for freeing the memory for result.Output
|
|
|
|
// (see RcloneFreeString), result itself is passed on the stack.
|
2021-02-26 11:41:38 +01:00
|
|
|
//
|
|
|
|
//export RcloneRPC
|
2021-03-30 11:02:48 +02:00
|
|
|
func RcloneRPC(method *C.char, input *C.char) (result C.struct_RcloneRPCResult) { //nolint:golint
|
2021-04-28 13:58:08 +02:00
|
|
|
output, status := librclone.RPC(C.GoString(method), C.GoString(input))
|
2021-03-30 11:02:48 +02:00
|
|
|
result.Output = C.CString(output)
|
|
|
|
result.Status = C.int(status)
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2021-10-11 00:10:01 +02:00
|
|
|
// RcloneFreeString may be used to free the string returned by RcloneRPC
|
|
|
|
//
|
|
|
|
// If the caller has access to the C standard library, the free function can
|
|
|
|
// normally be called directly instead. In some cases the caller uses a
|
|
|
|
// runtime library which is not compatible, and then this function can be
|
|
|
|
// used to release the memory with the same library that allocated it.
|
|
|
|
//
|
|
|
|
//export RcloneFreeString
|
|
|
|
func RcloneFreeString(str *C.char) {
|
|
|
|
C.free(unsafe.Pointer(str))
|
|
|
|
}
|
|
|
|
|
2021-02-26 11:41:38 +01:00
|
|
|
// do nothing here - necessary for building into a C library
|
2020-12-31 03:32:52 +01:00
|
|
|
func main() {}
|