/*
 * filesystem.h
 *
 * Copyright (C) 2017 Aleksandar Andrejevic <theflash@sdf.lonestar.org>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#ifndef __MONOLITIHUM_FILESYSTEM_H__
#define __MONOLITIHUM_FILESYSTEM_H__

#include "defs.h"
#include "clock.h"
#include "object.h"

#define PATH_DELIMITER_STRING "/"
#define PATH_DELIMITER_CHAR   (*PATH_DELIMITER_STRING)

#define FILE_MODE_READ            (1 << 0)
#define FILE_MODE_WRITE           (1 << 1)
#define FILE_MODE_SHARE_READ      (1 << 2)
#define FILE_MODE_SHARE_WRITE     (1 << 3)
#define FILE_MODE_NO_CACHE        (1 << 4)
#define FILE_MODE_DELETE_ON_CLOSE (1 << 29)
#define FILE_MODE_CREATE          (1 << 30)
#define FILE_MODE_TRUNCATE        (1 << 31)

#define FILE_ATTR_DIRECTORY      (1 << 0)
#define FILE_ATTR_OWNER_READABLE (1 << 1)
#define FILE_ATTR_OWNER_WRITABLE (1 << 2)
#define FILE_ATTR_WORLD_READABLE (1 << 3)
#define FILE_ATTR_WORLD_WRITABLE (1 << 4)
#define FILE_ATTR_DELETED        (1 << 31)

#define MOUNT_FLAG_READONLY (1 << 0)

typedef enum
{
    FILE_INFO_ATTRIBUTES,
    FILE_INFO_NAME,
    FILE_INFO_TIME,
    FILE_INFO_SIZE,
    FILE_INFO_OWNER,
} file_info_type_t;

typedef struct
{
    clock_time_t creation_time;
    clock_time_t modification_time;
    clock_time_t last_access_time;
} file_time_info_t;

typedef struct
{
    dword_t type;
    char filename[VARIABLE_SIZE];
} file_event_t;

sysret_t syscall_open_file(const char *path, handle_t *handle, dword_t mode, dword_t attributes);
sysret_t syscall_delete_file(const char *path);
sysret_t syscall_query_file(handle_t handle, file_info_type_t info_type, void *buffer, size_t size);
sysret_t syscall_set_file(handle_t handle, file_info_type_t set_type, void *buffer, size_t size);
sysret_t syscall_list_directory(handle_t handle, char *filename, bool_t continue_scan);
sysret_t syscall_read_file(handle_t handle, void *buffer, qword_t offset, size_t size, size_t *bytes_read);
sysret_t syscall_write_file(handle_t handle, const void *buffer, qword_t offset, size_t size, size_t *bytes_written);
sysret_t syscall_mount(const char *device, const char *mountpoint, const char *filesystem, dword_t flags);
sysret_t syscall_unmount(const char *device);
sysret_t syscall_wait_directory_event(handle_t handle, dword_t event_mask, file_event_t *buffer, size_t size, dword_t timeout);

#endif
