github.com/artpar/rclone@v1.67.3/librclone/librclone.go (about)

     1  // Package librclone exports shims for C library use
     2  //
     3  // This directory contains code to build rclone as a C library and the
     4  // shims for accessing rclone from C.
     5  //
     6  // The shims are a thin wrapper over the rclone RPC.
     7  //
     8  // Build a shared library like this:
     9  //
    10  //	go build --buildmode=c-shared -o librclone.so github.com/artpar/rclone/librclone
    11  //
    12  // Build a static library like this:
    13  //
    14  //	go build --buildmode=c-archive -o librclone.a github.com/artpar/rclone/librclone
    15  //
    16  // Both the above commands will also generate `librclone.h` which should
    17  // be `#include`d in `C` programs wishing to use the library.
    18  //
    19  // The library will depend on `libdl` and `libpthread`.
    20  package main
    21  
    22  /*
    23  #include <stdlib.h>
    24  
    25  struct RcloneRPCResult {
    26  	char*	Output;
    27  	int	Status;
    28  };
    29  */
    30  import "C"
    31  
    32  import (
    33  	"unsafe"
    34  
    35  	"github.com/artpar/rclone/librclone/librclone"
    36  
    37  	_ "github.com/artpar/rclone/backend/all"   // import all backends
    38  	_ "github.com/artpar/rclone/cmd/cmount"    // import cmount
    39  	_ "github.com/artpar/rclone/cmd/mount"     // import mount
    40  	_ "github.com/artpar/rclone/cmd/mount2"    // import mount2
    41  	_ "github.com/artpar/rclone/fs/operations" // import operations/* rc commands
    42  	_ "github.com/artpar/rclone/fs/sync"       // import sync/*
    43  	_ "github.com/artpar/rclone/lib/plugin"    // import plugins
    44  )
    45  
    46  // RcloneInitialize initializes rclone as a library
    47  //
    48  //export RcloneInitialize
    49  func RcloneInitialize() {
    50  	librclone.Initialize()
    51  }
    52  
    53  // RcloneFinalize finalizes the library
    54  //
    55  //export RcloneFinalize
    56  func RcloneFinalize() {
    57  	librclone.Finalize()
    58  }
    59  
    60  // RcloneRPCResult is returned from RcloneRPC
    61  //
    62  //	Output will be returned as a serialized JSON object
    63  //	Status is a HTTP status return (200=OK anything else fail)
    64  type RcloneRPCResult struct { //nolint:deadcode
    65  	Output *C.char
    66  	Status C.int
    67  }
    68  
    69  // RcloneRPC does a single RPC call. The inputs are (method, input)
    70  // and the output is (output, status). This is an exported interface
    71  // to the rclone API as described in https://rclone.org/rc/
    72  //
    73  //	method is a string, eg "operations/list"
    74  //	input should be a string with a serialized JSON object
    75  //	result.Output will be returned as a string with a serialized JSON object
    76  //	result.Status is a HTTP status return (200=OK anything else fail)
    77  //
    78  // All strings are UTF-8 encoded, on all platforms.
    79  //
    80  // Caller is responsible for freeing the memory for result.Output
    81  // (see RcloneFreeString), result itself is passed on the stack.
    82  //
    83  //export RcloneRPC
    84  func RcloneRPC(method *C.char, input *C.char) (result C.struct_RcloneRPCResult) { //nolint:golint
    85  	output, status := librclone.RPC(C.GoString(method), C.GoString(input))
    86  	result.Output = C.CString(output)
    87  	result.Status = C.int(status)
    88  	return result
    89  }
    90  
    91  // RcloneFreeString may be used to free the string returned by RcloneRPC
    92  //
    93  // If the caller has access to the C standard library, the free function can
    94  // normally be called directly instead. In some cases the caller uses a
    95  // runtime library which is not compatible, and then this function can be
    96  // used to release the memory with the same library that allocated it.
    97  //
    98  //export RcloneFreeString
    99  func RcloneFreeString(str *C.char) {
   100  	C.free(unsafe.Pointer(str))
   101  }
   102  
   103  // do nothing here - necessary for building into a C library
   104  func main() {}