summaryrefslogtreecommitdiff
path: root/src/util/file/file_stdio.c
blob: fa74be77376a59624460096cf4f547b9f1968a71 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#define AL_LOG_SECTION "file"
#include <al/log.h>
#include <al/lib.h>

#include "../error.h"

#include "file.h"

/*
#if defined NAUNET_ON_WINDOWS && defined AL_WE_32BIT
AL_ASSERT_TYPE_SIZE(off_t, 4);
#else
AL_ASSERT_TYPE_SIZE(off_t, 8);
#endif
*/

static inline bool open_stdio_file(FILE **file, str *path, const char *mode)
{
    char *c_str = al_str_to_c_str(path);
#if defined NAUNET_ON_WINDOWS && !defined NAUNET_WIN32_COMPAT_MODE
    // Still sets errno, according to the Microsoft docs.
    errno_t ret = fopen_s(file, c_str, mode);
#else
    s32 ret = (*file = fopen(c_str, mode)) ? 0 : errno;
#endif
    al_free(c_str);
    return ret == 0;
}

static bool query_filesize_stdio(struct nn_file *file)
{
    off_t pos = nn_file_seek(file, 0, SEEK_END);
    if (pos == -1) return false;
    file->size = pos;
    rewind(file->file);
    return true;
}

bool nn_file_open(struct nn_file *file, str *path, s32 flags)
{
    const char *mode = (flags & NNWT_FILE_CREATE) ? "wb+" : "rb+";
    if (!open_stdio_file(&file->file, path, mode)) {
        log_error("fopen(%.*s) failed (%d: %s).", al_str_x(path), errno, nn_strerror(errno));
        return false;
    }

    if (!query_filesize_stdio(file)) {
        nn_file_close(file);
        return false;
    }

    al_str_clone(&file->path, path);

    return true;
}

bool nn_file_exists(str *path)
{
    FILE *file;
    bool opened = open_stdio_file(&file, path, "r");
    if (opened) fclose(file);
    return opened;
}

bool nn_file_create(str *path)
{
    FILE *file;
    bool opened = open_stdio_file(&file, path, "w+");
    if (opened) fclose(file);
    return opened;
}

off_t nn_file_seek(struct nn_file *file, off_t offset, s32 whence)
{
    if (fseek(file->file, offset, whence) != 0) {
        log_error("fseek(%zd, %d) failed (%s).", offset, whence, nn_strerror(errno));
        return -1;
    }
    return ftell(file->file);
}

bool nn_file_truncate(struct nn_file *file, off_t size)
{
    file->size = size;
    return true;
}

bool nn_file_write(struct nn_file *file, void *buf, size_t size)
{
    size_t ret = fwrite(buf, size, 1, file->file);
    if (ret == 0) {
        log_error("fwrite(%zu) failed (%s).", size, nn_strerror(errno));
        return false;
    }
    return true;
}

bool nn_file_read(struct nn_file *file, void *buf, size_t size)
{
    size_t ret = fread(buf, size, 1, file->file);
    if (ret == 0) {
        log_error("fread(%zu) failed (%s).", size, nn_strerror(errno));
        return false;
    }
    return true;
}

void *nn_file_mmap(struct nn_file *file)
{
    (void)file;
    al_assert_and_return(NULL);
}

void nn_file_close(struct nn_file *file)
{
    fclose(file->file);
    al_str_free(&file->path);
}

bool nn_dir_open(struct nn_dir *dir, str *path) { (void)dir; (void)path; return false; }
void nn_dir_close(struct nn_dir *dir) { (void)dir; }
bool nn_dir_exists(str *path) { (void)path; return false; }
bool nn_dir_create(str *path) { (void)path; return false; }
bool nn_dir_read(struct nn_dir *dir, struct nn_dir_entry *entry) { (void)dir; (void)entry; return false; }
void nn_dir_entry_get_name(struct nn_dir_entry *entry, str *name) { (void)entry; (void)name; };
const char *nn_dir_entry_get_os_name(struct nn_dir_entry *entry) { (void)entry; return NULL; };
void nn_dir_entry_get_path(struct nn_dir_entry *entry, struct nn_dir *dir, str *path) { (void)entry; (void)dir; (void)path; }