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
|
|
|
|
|
|
|
|
import (
|
|
|
|
"C"
|
2021-02-26 18:41:38 +08:00
|
|
|
|
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"
|
|
|
|
"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() {
|
2020-12-31 10:32:52 +08:00
|
|
|
// TODO: what need to be initialized manually?
|
|
|
|
}
|
|
|
|
|
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-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
|
|
|
|
// output will be returned as a serialized JSON object
|
|
|
|
// status is a HTTP status return (200=OK anything else fail)
|
|
|
|
//
|
2020-12-31 10:32:52 +08:00
|
|
|
// Caller is responsible for freeing the memory for output
|
2021-02-26 18:41:38 +08:00
|
|
|
//
|
|
|
|
// Note that when calling from C output and status are returned in an
|
|
|
|
// RcloneRPC_return which has two members r0 which is output and r1
|
|
|
|
// which is status.
|
|
|
|
//
|
|
|
|
//export RcloneRPC
|
|
|
|
func RcloneRPC(method *C.char, input *C.char) (output *C.char, status C.int) { //nolint:golint
|
2020-12-31 10:32:52 +08:00
|
|
|
res, s := callFunctionJSON(C.GoString(method), C.GoString(input))
|
|
|
|
return C.CString(res), C.int(s)
|
|
|
|
}
|
|
|
|
|
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-02-26 18:41:38 +08:00
|
|
|
var w strings.Builder
|
|
|
|
// FIXME should factor this
|
2020-12-31 10:32:52 +08:00
|
|
|
// Adjust the error return for some well known errors
|
|
|
|
errOrig := errors.Cause(err)
|
|
|
|
switch {
|
|
|
|
case errOrig == fs.ErrorDirNotFound || errOrig == fs.ErrorObjectNotFound:
|
|
|
|
status = http.StatusNotFound
|
|
|
|
case rc.IsErrParamInvalid(err) || rc.IsErrParamNotFound(err):
|
|
|
|
status = http.StatusBadRequest
|
|
|
|
}
|
|
|
|
// w.WriteHeader(status)
|
2021-02-26 18:41:38 +08:00
|
|
|
err = rc.WriteJSON(&w, rc.Params{
|
2020-12-31 10:32:52 +08:00
|
|
|
"status": status,
|
|
|
|
"error": err.Error(),
|
|
|
|
"input": in,
|
|
|
|
"path": path,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
// can't return the error at this point
|
2021-02-26 18:41:38 +08:00
|
|
|
return fmt.Sprintf(`{"error": "rc: failed to write JSON output: %v"}`, err), 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 {
|
|
|
|
// TODO: handle error
|
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)
|
|
|
|
// TODO: what is r.Context()? use Background() for the moment
|
|
|
|
_, out, err := jobs.NewJob(context.Background(), call.Fn, in)
|
|
|
|
if err != nil {
|
|
|
|
// handle error
|
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-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() {}
|