2021-02-26 18:41:38 +08: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 10:32:52 +08:00
|
|
|
package main
|
|
|
|
|
2021-03-30 17:02:48 +08:00
|
|
|
/*
|
|
|
|
struct RcloneRPCResult {
|
|
|
|
char* Output;
|
|
|
|
int Status;
|
|
|
|
};
|
|
|
|
*/
|
|
|
|
import "C"
|
2021-02-26 18:41:38 +08:00
|
|
|
|
2021-03-30 17:02:48 +08:00
|
|
|
import (
|
2020-12-31 10:32:52 +08:00
|
|
|
"context"
|
|
|
|
"encoding/json"
|
2021-02-26 18:41:38 +08:00
|
|
|
"fmt"
|
2020-12-31 10:32:52 +08:00
|
|
|
"net/http"
|
|
|
|
"runtime"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/rclone/rclone/fs"
|
2021-03-30 17:02:48 +08:00
|
|
|
"github.com/rclone/rclone/fs/accounting"
|
|
|
|
"github.com/rclone/rclone/fs/config/configfile"
|
|
|
|
"github.com/rclone/rclone/fs/log"
|
2020-12-31 10:32:52 +08:00
|
|
|
"github.com/rclone/rclone/fs/rc"
|
|
|
|
"github.com/rclone/rclone/fs/rc/jobs"
|
|
|
|
|
|
|
|
_ "github.com/rclone/rclone/backend/all" // import all backends
|
|
|
|
_ "github.com/rclone/rclone/lib/plugin" // import plugins
|
|
|
|
)
|
|
|
|
|
2021-02-26 18:41:38 +08:00
|
|
|
// RcloneInitialize initializes rclone as a library
|
|
|
|
//
|
|
|
|
//export RcloneInitialize
|
|
|
|
func RcloneInitialize() {
|
2021-03-30 17:02:48 +08:00
|
|
|
// A subset of initialisation copied from cmd.go
|
|
|
|
// Note that we don't want to pull in anything which depends on pflags
|
|
|
|
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
// Start the logger
|
|
|
|
log.InitLogging()
|
|
|
|
|
|
|
|
// Load the config - this may need to be configurable
|
|
|
|
configfile.Install()
|
|
|
|
|
|
|
|
// Start accounting
|
|
|
|
accounting.Start(ctx)
|
2020-12-31 10:32:52 +08:00
|
|
|
}
|
|
|
|
|
2021-02-26 18:41:38 +08:00
|
|
|
// RcloneFinalize finalizes the library
|
|
|
|
//
|
|
|
|
//export RcloneFinalize
|
|
|
|
func RcloneFinalize() {
|
2020-12-31 10:32:52 +08:00
|
|
|
// TODO: how to clean up? what happens when rcserver terminates?
|
|
|
|
// what about unfinished async jobs?
|
|
|
|
runtime.GC()
|
|
|
|
}
|
|
|
|
|
2021-03-30 17:02:48 +08: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)
|
|
|
|
type RcloneRPCResult struct {
|
|
|
|
Output *C.char
|
|
|
|
Status C.int
|
|
|
|
}
|
|
|
|
|
2021-02-26 18:41:38 +08: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"
|
|
|
|
// input should be a serialized JSON object
|
2021-03-30 17:02:48 +08:00
|
|
|
// result.Output will be returned as a serialized JSON object
|
|
|
|
// result.Status is a HTTP status return (200=OK anything else fail)
|
2021-02-26 18:41:38 +08:00
|
|
|
//
|
2021-03-30 17:02:48 +08:00
|
|
|
// Caller is responsible for freeing the memory for result.Output,
|
|
|
|
// result itself is passed on the stack.
|
2021-02-26 18:41:38 +08:00
|
|
|
//
|
|
|
|
//export RcloneRPC
|
2021-03-30 17:02:48 +08:00
|
|
|
func RcloneRPC(method *C.char, input *C.char) (result C.struct_RcloneRPCResult) { //nolint:golint
|
|
|
|
output, status := callFunctionJSON(C.GoString(method), C.GoString(input))
|
|
|
|
result.Output = C.CString(output)
|
|
|
|
result.Status = C.int(status)
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
// RcloneMobileRPCResult is returned from RcloneMobileRPC
|
|
|
|
//
|
|
|
|
// Output will be returned as a serialized JSON object
|
|
|
|
// Status is a HTTP status return (200=OK anything else fail)
|
|
|
|
type RcloneMobileRPCResult struct {
|
|
|
|
Output string
|
|
|
|
Status int
|
|
|
|
}
|
|
|
|
|
|
|
|
// RcloneMobileRPCRPC this works the same as RcloneRPC but has an interface
|
|
|
|
// optimised for gomobile, in particular the function signature is
|
|
|
|
// valid under gobind rules.
|
|
|
|
//
|
|
|
|
// https://pkg.go.dev/golang.org/x/mobile/cmd/gobind#hdr-Type_restrictions
|
|
|
|
func RcloneMobileRPCRPC(method string, input string) (result RcloneMobileRPCResult) {
|
|
|
|
output, status := callFunctionJSON(method, input)
|
|
|
|
result.Output = output
|
|
|
|
result.Status = status
|
|
|
|
return result
|
2020-12-31 10:32:52 +08:00
|
|
|
}
|
|
|
|
|
2021-02-26 18:41:38 +08:00
|
|
|
// writeError returns a formatted error string and the status passed in
|
|
|
|
func writeError(path string, in rc.Params, err error, status int) (string, int) {
|
2020-12-31 10:32:52 +08:00
|
|
|
fs.Errorf(nil, "rc: %q: error: %v", path, err)
|
2021-03-30 17:02:48 +08:00
|
|
|
params, status := rc.Error(path, in, err, status)
|
2021-02-26 18:41:38 +08:00
|
|
|
var w strings.Builder
|
2021-03-30 17:02:48 +08:00
|
|
|
err = rc.WriteJSON(&w, params)
|
2020-12-31 10:32:52 +08:00
|
|
|
if err != nil {
|
2021-03-30 17:02:48 +08:00
|
|
|
// ultimate fallback error
|
|
|
|
fs.Errorf(nil, "writeError: failed to write JSON output from %#v: %v", in, err)
|
|
|
|
status = http.StatusInternalServerError
|
|
|
|
w.Reset()
|
|
|
|
fmt.Fprintf(&w, `{
|
|
|
|
"error": %q,
|
|
|
|
"path": %q,
|
|
|
|
"status": %d
|
|
|
|
}`, err, path, status)
|
|
|
|
|
2020-12-31 10:32:52 +08:00
|
|
|
}
|
2021-02-26 18:41:38 +08:00
|
|
|
return w.String(), status
|
2020-12-31 10:32:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// operations/uploadfile and core/command are not supported as they need request or response object
|
|
|
|
// modified from handlePost in rcserver.go
|
|
|
|
// call a rc function using JSON to input parameters and output the resulted JSON
|
|
|
|
func callFunctionJSON(method string, input string) (output string, status int) {
|
|
|
|
// create a buffer to capture the output
|
|
|
|
in := make(rc.Params)
|
|
|
|
err := json.NewDecoder(strings.NewReader(input)).Decode(&in)
|
|
|
|
if err != nil {
|
2021-02-26 18:41:38 +08:00
|
|
|
return writeError(method, in, errors.Wrap(err, "failed to read input JSON"), http.StatusBadRequest)
|
2020-12-31 10:32:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Find the call
|
|
|
|
call := rc.Calls.Get(method)
|
|
|
|
if call == nil {
|
2021-02-26 18:41:38 +08:00
|
|
|
return writeError(method, in, errors.Errorf("couldn't find method %q", method), http.StatusNotFound)
|
2020-12-31 10:32:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: handle these cases
|
|
|
|
if call.NeedsRequest {
|
2021-02-26 18:41:38 +08:00
|
|
|
return writeError(method, in, errors.Errorf("method %q needs request, not supported", method), http.StatusNotFound)
|
2020-12-31 10:32:52 +08:00
|
|
|
// Add the request to RC
|
|
|
|
//in["_request"] = r
|
|
|
|
}
|
|
|
|
if call.NeedsResponse {
|
2021-02-26 18:41:38 +08:00
|
|
|
return writeError(method, in, errors.Errorf("method %q need response, not supported", method), http.StatusNotFound)
|
2020-12-31 10:32:52 +08:00
|
|
|
//in["_response"] = w
|
|
|
|
}
|
|
|
|
|
|
|
|
fs.Debugf(nil, "rc: %q: with parameters %+v", method, in)
|
2021-03-30 17:02:48 +08:00
|
|
|
|
2020-12-31 10:32:52 +08:00
|
|
|
_, out, err := jobs.NewJob(context.Background(), call.Fn, in)
|
|
|
|
if err != nil {
|
2021-02-26 18:41:38 +08:00
|
|
|
return writeError(method, in, err, http.StatusInternalServerError)
|
2020-12-31 10:32:52 +08:00
|
|
|
}
|
|
|
|
if out == nil {
|
|
|
|
out = make(rc.Params)
|
|
|
|
}
|
|
|
|
|
|
|
|
fs.Debugf(nil, "rc: %q: reply %+v: %v", method, out, err)
|
2021-02-26 18:41:38 +08:00
|
|
|
|
|
|
|
var w strings.Builder
|
|
|
|
err = rc.WriteJSON(&w, out)
|
2020-12-31 10:32:52 +08:00
|
|
|
if err != nil {
|
|
|
|
fs.Errorf(nil, "rc: failed to write JSON output: %v", err)
|
2021-02-26 18:41:38 +08:00
|
|
|
return writeError(method, in, err, http.StatusInternalServerError)
|
2020-12-31 10:32:52 +08:00
|
|
|
}
|
2021-03-30 17:02:48 +08:00
|
|
|
|
2021-02-26 18:41:38 +08:00
|
|
|
return w.String(), http.StatusOK
|
2020-12-31 10:32:52 +08:00
|
|
|
}
|
|
|
|
|
2021-02-26 18:41:38 +08:00
|
|
|
// do nothing here - necessary for building into a C library
|
2020-12-31 10:32:52 +08:00
|
|
|
func main() {}
|