### Manage Files and Directories using cURL Source: https://github.com/ballerina-platform/module-ballerina-file/blob/master/examples/file-management-system/File management system in Ballerina.md Examples of cURL commands to interact with the Ballerina file manager's HTTP API. These commands demonstrate various operations like creating, modifying, deleting, copying, renaming, and retrieving metadata for files and directories. ```curl curl -v -X POST http://localhost:9090/file_manager/file/test.txt ``` ```curl curl -v -X PUT http://localhost:9090/file_manager/file/test.txt --data "hi" ``` ```curl curl -v -X DELETE http://localhost:9090/file_manager/file/test.txt ``` ```curl curl -v -X POST http://localhost:9090/file_manager/directory/file_dir ``` ```curl curl -v -X GET http://localhost:9090/file_manager/directory/metadata/file_dir ``` ```curl curl -v -X POST http://localhost:9090/file_manager/directory/file_dir/new_dir ``` ```curl curl -v -X PUT http://localhost:9090/file_manager/directory/new_dir/new_diretory ``` ```curl curl -v -X DELETE http://localhost:9090/file_manager/directory/file_dir ``` -------------------------------- ### Run Ballerina File System Observer Source: https://github.com/ballerina-platform/module-ballerina-file/blob/master/examples/file-management-system/File management system in Ballerina.md Navigate to the observer component's directory and execute the Ballerina application. This component listens for file system events and sends notifications. ```ballerina $ cd examples/file_management_system/observer $ bal run ``` -------------------------------- ### Run Ballerina File System Manager Source: https://github.com/ballerina-platform/module-ballerina-file/blob/master/examples/file-management-system/File management system in Ballerina.md Navigate to the manager component's directory and execute the Ballerina application. This component exposes an HTTP API for managing files and directories. ```ballerina $ cd examples/file_management_system/manager $ bal run ``` -------------------------------- ### Implement Ballerina Service for File Creation Events Source: https://github.com/ballerina-platform/module-ballerina-file/blob/master/ballerina/README.md This example demonstrates how to define a Ballerina `Service` named 'localObserver' that attaches to a `file:Listener` endpoint (e.g., `inFolder`). It includes a `remote function onCreate(file:FileEvent m)` which is invoked when a new file is created in the monitored directory, logging the name of the created file. ```ballerina service "localObserver" on inFolder { remote function onCreate(file:FileEvent m) { string msg = "Create: " + m.name; log:printInfo(msg); } } ``` -------------------------------- ### Get File Metadata (Ballerina) Source: https://github.com/ballerina-platform/module-ballerina-file/blob/master/docs/spec/spec.md Obtains metadata information for a file at a given path. This function returns a `MetaData` record on success or an `Error` if the operation fails. ```ballerina public isolated function getMetaData(string path) returns MetaData|Error; ``` -------------------------------- ### Get Path Basename (Ballerina) Source: https://github.com/ballerina-platform/module-ballerina-file/blob/master/docs/spec/spec.md Retrieves the base name (the last component) of the file or directory at the provided path. This function returns the basename string or an `Error` if the operation fails. ```ballerina public isolated function basename(string path) returns string|Error; ``` -------------------------------- ### Get Current Working Directory API Source: https://github.com/ballerina-platform/module-ballerina-file/blob/master/docs/spec/spec.md This Ballerina function `getCurrentDir` retrieves the absolute path of the current working directory. It is an isolated function and returns a string. ```APIDOC public isolated function getCurrentDir() returns string; ``` -------------------------------- ### Normalize Path (Ballerina) Source: https://github.com/ballerina-platform/module-ballerina-file/blob/master/docs/spec/spec.md Normalizes a given path based on specified options, such as getting the shortest equivalent name, evaluating symbolic links, or normalizing case. The function returns the normalized path string or an `Error`. ```ballerina public isolated function normalizePath(string path, NormOption option) returns string|Error; ``` -------------------------------- ### Get Absolute Path (Ballerina) Source: https://github.com/ballerina-platform/module-ballerina-file/blob/master/docs/spec/spec.md Retrieves the absolute path reference from a provided relative path. This function ensures cross-platform compatibility. It returns the absolute path string or an `Error` if the path cannot be resolved. ```ballerina public isolated function getAbsolutePath(string path) returns string|Error; ``` -------------------------------- ### Get Relative Path (Ballerina) Source: https://github.com/ballerina-platform/module-ballerina-file/blob/master/docs/spec/spec.md Generates a logically equivalent relative path from a base path to a target path. This function returns the relative path string or an `Error` if the relative path cannot be determined. ```ballerina public isolated function relativePath(string base, string target) returns string|Error; ``` -------------------------------- ### Get Parent Directory Path (Ballerina) Source: https://github.com/ballerina-platform/module-ballerina-file/blob/master/docs/spec/spec.md Retrieves the parent directory of the provided file or directory path. This function returns the parent path string or an `Error` if the parent path cannot be determined. ```ballerina public isolated function parentPath(string path) returns string|Error; ``` -------------------------------- ### Build Ballerina File Library with Gradle Source: https://github.com/ballerina-platform/module-ballerina-file/blob/master/README.md Provides a series of Gradle commands for building, testing, debugging, and publishing the Ballerina File Library from its source. These commands cover common development workflows, including running tests, skipping tests, debugging specific components, and publishing artifacts to local or central repositories. ```Shell ./gradlew clean build ./gradlew clean test ./gradlew clean build -x test ./gradlew clean build -Pdebug= ./gradlew clean build -PbalJavaDebug= ./gradlew clean build publishToMavenLocal ./gradlew clean build -PpublishToLocalCentral=true ./gradlew clean build -PpublishToCentral=true ``` -------------------------------- ### Implement Ballerina Service for File Creation Events Source: https://github.com/ballerina-platform/module-ballerina-file/blob/master/README.md Demonstrates how to create a Ballerina `Service` that attaches to a `file:Listener` endpoint and implements the `onCreate` remote method. This service will automatically execute its `onCreate` function when a new file is detected in the monitored directory, logging the name of the created file. ```Ballerina service "localObserver" on inFolder { remote function onCreate(file:FileEvent m) { string msg = "Create: " + m.name; log:printInfo(msg); } } ``` -------------------------------- ### Create Temporary File (Ballerina) Source: https://github.com/ballerina-platform/module-ballerina-file/blob/master/docs/spec/spec.md Creates a temporary file. Optional parameters include a suffix, prefix, and a specific directory. If no directory is provided, the operating system's default temporary directory is used. Returns the path to the created file as a string or an `Error`. ```ballerina public isolated function createTemp(string? suffix = (), string? prefix = (), string? dir = ()) returns string|Error; ``` -------------------------------- ### Create Temporary Directory (Ballerina) Source: https://github.com/ballerina-platform/module-ballerina-file/blob/master/docs/spec/spec.md Creates a temporary directory. Optional parameters include a suffix, prefix, and a specific directory. If no directory is provided, the operating system's default temporary directory is used. Returns the path to the created directory as a string or an `Error`. ```ballerina public isolated function createTempDir(string? suffix = (), string? prefix = (), string? dir = ()) returns string|Error; ``` -------------------------------- ### Ballerina OS-Specific Path and Path List Separator Constants Initialization Source: https://github.com/ballerina-platform/module-ballerina-file/blob/master/docs/proposals/introduce_separators.md This Ballerina code snippet demonstrates the initialization of `pathSeparator` and `pathListSeparator` constants. It dynamically determines the operating system (Windows or UNIX-like) using `os:getEnv("OS")` and assigns the appropriate separator characters. These constants are declared as public and final, making them readily available for use in file operations. ```ballerina final boolean isWindows = os:getEnv("OS") != ""; // OS-specific path separator. public final string pathSeparator = isWindows ? "\\" : "/"; // OS-specific path list separator. public final string pathListSeparator = isWindows ? ";" : ":"; ``` -------------------------------- ### Define Ballerina File Listener Endpoint Source: https://github.com/ballerina-platform/module-ballerina-file/blob/master/README.md Illustrates how to define a `file:Listener` endpoint in Ballerina. This configuration specifies the directory path to monitor and whether to recursively watch subdirectories. The `recursive` parameter defaults to `false` if not explicitly set. ```Ballerina listener file:Listener inFolder = new ({ path: "", recursive: false }); ``` -------------------------------- ### Define Ballerina File System Listener Source: https://github.com/ballerina-platform/module-ballerina-file/blob/master/ballerina/README.md This Ballerina code snippet illustrates how to instantiate a `file:Listener` endpoint. It configures the listener with a specific directory `path` to monitor and sets the `recursive` flag to `false` by default, meaning it will not monitor subdirectories unless explicitly set to `true`. ```ballerina listener file:Listener inFolder = new ({ path: "", recursive: false }); ``` -------------------------------- ### Join Path Components (Ballerina) Source: https://github.com/ballerina-platform/module-ballerina-file/blob/master/docs/spec/spec.md Combines multiple path components into a single, valid path string. This function is variadic, accepting multiple string arguments. It returns the combined path string or an `Error` if the joining operation fails. ```ballerina public isolated function joinPath(string... parts) returns string|Error; ``` -------------------------------- ### Create New Directory API Source: https://github.com/ballerina-platform/module-ballerina-file/blob/master/docs/spec/spec.md This Ballerina function `createDir` creates a new directory at the specified path. It accepts a directory path and an optional `DirOption` to control the creation of non-existent parent directories. ```APIDOC public isolated function createDir(string dir, DirOption option); ``` -------------------------------- ### OS-Specific Path Constants (APIDOC) Source: https://github.com/ballerina-platform/module-ballerina-file/blob/master/docs/spec/spec.md Describes OS-specific constants used for path manipulation, providing details on the characters used for separating directories and paths in a list. ```APIDOC pathSeparator: The character used to separate the parent directories that make up the path to a specific location. For windows, it’s '\' and for UNIX it’s '/' pathListSeparator: The character commonly used by the operating system to separate paths in the path list. For windows, it’s ';' and for UNIX it’s ':' ``` -------------------------------- ### Create New File API Source: https://github.com/ballerina-platform/module-ballerina-file/blob/master/docs/spec/spec.md This Ballerina function `create` creates a new file in the provided path. It returns an `Error?` type, indicating that the operation might fail. ```APIDOC public isolated function create(string path) returns Error?; ``` -------------------------------- ### Split Path into Components (Ballerina) Source: https://github.com/ballerina-platform/module-ballerina-file/blob/master/docs/spec/spec.md Splits a given path into an array of its individual components. This function returns an array of strings representing path components or an `Error` if the path cannot be split. ```ballerina public isolated function splitPath(string path) returns string[]|Error; ``` -------------------------------- ### Ballerina File Listener Service Remote Methods Source: https://github.com/ballerina-platform/module-ballerina-file/blob/master/README.md Documents the remote methods supported by a Ballerina `Service` that can be attached to a `file:Listener` endpoint. These methods are invoked automatically when specific file events (creation, deletion, modification) occur in the monitored directory, providing a `file:FileEvent` object with details. ```APIDOC Service Remote Methods: onCreate: This method is invoked once a new file is created in the listening directory. onDelete: This method is invoked once an existing file is deleted from the listening directory. onModify: This method is invoked once an existing file is modified in the listening directory. ``` -------------------------------- ### Test File/Directory Condition (Ballerina) Source: https://github.com/ballerina-platform/module-ballerina-file/blob/master/docs/spec/spec.md Tests if a file or directory meets a specified condition, such as existence, type (directory/symlink), or permissions (read/write). The function returns `true` if the condition is met, `false` otherwise, or an `Error` if the test cannot be performed. ```ballerina public isolated function test(string path, TestOption testOption) returns boolean|Error; ``` -------------------------------- ### Copy File or Directory API Source: https://github.com/ballerina-platform/module-ballerina-file/blob/master/docs/spec/spec.md This Ballerina function `copy` copies a file or directory from `sourcePath` to `destinationPath`. It supports various `CopyOption`s to control behavior like replacing existing files, copying attributes, or handling symbolic links. ```APIDOC public isolated function copy(string sourcePath, string destinationPath, CopyOption... options) returns Error?; ``` -------------------------------- ### Read Directory Contents (Ballerina) Source: https://github.com/ballerina-platform/module-ballerina-file/blob/master/docs/spec/spec.md Retrieves a list of files and directories within a specified path, including their metadata. The function returns an array of `MetaData` records for each entry or an `Error` if the directory cannot be read. ```ballerina public isolated function readDir(string path) returns MetaData[]|Error; ``` -------------------------------- ### Define Ballerina File Metadata Structure Source: https://github.com/ballerina-platform/module-ballerina-file/blob/master/docs/spec/spec.md This Ballerina type definition `MetaData` describes the structure for file and directory metadata. It includes fields for absolute path, size, last modified time, type (file/directory), and read/write permissions. ```ballerina public type MetaData record{| string absPath; int size; time:Utc modifiedTime; boolean dir; boolean readable; boolean writable; |}; ``` -------------------------------- ### Rename or Move File/Directory API Source: https://github.com/ballerina-platform/module-ballerina-file/blob/master/docs/spec/spec.md This Ballerina function `rename` renames or moves a file or directory from `oldPath` to `newPath`. If the `newPath` provided already exists and is not a directory, it will be replaced. ```APIDOC public isolated function rename(string oldPath, string newPath) returns Error?; ``` -------------------------------- ### Check if Path is Absolute (Ballerina) Source: https://github.com/ballerina-platform/module-ballerina-file/blob/master/docs/spec/spec.md Determines whether the provided path is absolute or not. This function returns `true` if the path is absolute, `false` otherwise, or an `Error` if the determination fails. ```ballerina public isolated function isAbsolutePath(string path) returns boolean|Error; ``` -------------------------------- ### Remove File or Directory API Source: https://github.com/ballerina-platform/module-ballerina-file/blob/master/docs/spec/spec.md This Ballerina function `remove` deletes a file or directory. If the provided path is a directory, a `DirOption` can be passed to configure whether all files and directories inside the given directory should be recursively removed. ```APIDOC public isolated function remove(string path, DirOption option) returns Error?' ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.