Skip to content

SwirX/SXFS

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

SXFS

SXFS (SwirX FileSystem) is a lightweight, "pure C from scratch" file system library designed to make interacting with the file system easier and more intuitive. It provides a human-friendly API for common operations like reading/writing files, manipulating paths, and traversing directories.

Features

  • Simple API: No more complex fopen/fread/fclose boilerplate for simple tasks.
  • Text & Binary Support: dedicated functions for handling strings and raw bytes.
  • Smart Path Manipulation: Join paths, get extensions, and resolve absolute paths easily.
  • Directory Traversal: Get children and siblings of files using dynamic lists.
  • File Management: Copy, move, delete, and check existence with one-liners.

Installation

SXFS depends on SXList for dynamic list management.

  1. Clone the repository and its submodules:

    git clone --recursive https://github.com/SwirX/sxfs.git

    Or if you already cloned it:

    git submodule update --init --recursive
  2. Include sxfs.h in your project and compile sxfs.c along with your code. Ensure libs/SXList is in your include path.

Usage

Reading & Writing

#include "sxfs.h"
#include <stdio.h>
#include <stdlib.h>

int main() {
    // Write text to a file
    if (sxfs_write_text("hello.txt", "Hello, World!")) {
        printf("File written successfully.\n");
    }

    // Read text from a file
    char* content = sxfs_read_text("hello.txt");
    if (content) {
        printf("Read: %s\n", content);
        free(content); // Don't forget to free!
    }
    
    // Append text
    sxfs_append_text("hello.txt", " How are you?");
    
    return 0;
}

Path Manipulation

char* path = sxfs_join("folder", "file.txt"); // Returns "folder/file.txt"
char* ext = sxfs_get_ext(path);               // Returns ".txt"
char* abs = sxfs_abspath(path);               // Returns absolute path

printf("Extension: %s\n", ext);

free(path);
free(ext);
free(abs);

Directory Listing

#include "dynamic_list.h"

// List all files in a directory
list* children = sxfs_listdir("my_folder");
if (children) {
    for (size_t i = 0; i < list_length(children); i++) {
        char* name = list_get(children, i);
        printf("Found: %s\n", name);
    }
    // Deep free if you don't need the strings anymore
    list_free_deep(children, free);
}

API Reference

File I/O

  • char* sxfs_read_text(const char* path): Reads entire file as a string.
  • void* sxfs_read_bytes(const char* path, size_t* size): Reads file into a byte buffer.
  • bool sxfs_write_text(const char* path, const char* content): Writes string to file (overwrites).
  • bool sxfs_write_bytes(const char* path, const void* data, size_t size): Writes bytes to file.
  • bool sxfs_append_text(const char* path, const char* content): Appends string to file.
  • bool sxfs_append_bytes(const char* path, const void* data, size_t size): Appends bytes.

Management

  • bool sxfs_create(const char* path): Creates an empty file if it doesn't exist.
  • bool sxfs_delete(const char* path): Deletes a file or empty directory.
  • bool sxfs_copy(const char* src, const char* dest): Copies a file.
  • bool sxfs_move(const char* src, const char* dest): Moves/renames a file.

Path Operations

  • char* sxfs_join(const char* path1, const char* path2): Joins two paths safely.
  • bool sxfs_exists(const char* path): Checks if path exists.
  • bool sxfs_isfile(const char* path): Checks if path is a regular file.
  • bool sxfs_isdir(const char* path): Checks if path is a directory.
  • char* sxfs_get_ext(const char* path): Gets file extension.
  • char* sxfs_parent(const char* path): Gets parent directory path.
  • char* sxfs_filename(const char* path): Gets filename from path.
  • char* sxfs_abspath(const char* path): Resolves absolute path.

Directory Operations

  • bool sxfs_mkdir(const char* path): Creates a directory.
  • list* sxfs_listdir(const char* path): Returns a list of strings containing directory entries.
  • list* sxfs_siblings(const char* path): Returns a list of siblings for a given path.

Dependencies

  • SXList: Dynamic list implementation.

Build

A Makefile is provided for testing:

make test

Releases

No releases published

Packages

 
 
 

Contributors