summaryrefslogtreecommitdiffstats
path: root/fluent-bit/lib/chunkio/src/cio_os.c
blob: 0adbc617ce84425dd19ba3912f2915046fd865a8 (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
128
129
130
131
132
133
134
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */

/*  Chunk I/O
 *  =========
 *  Copyright 2018 Eduardo Silva <eduardo@monkey.io>
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

#define _GNU_SOURCE

#include <sys/types.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>

#include <chunkio/chunkio_compat.h>

#ifdef _WIN32
#include <Shlobj.h>
#endif

/* Check if a path is a directory */
int cio_os_isdir(const char *dir)
{
    int ret;
    struct stat st;

    ret = stat(dir, &st);
    if (ret == -1) {
        return -1;
    }

    if (st.st_mode & S_IFDIR) {
        return 0;
    }

    return -1;
}

/* Create directory */
int cio_os_mkpath(const char *dir, mode_t mode)
{
    struct stat st;

#ifdef _WIN32
    char path[MAX_PATH];
#else
# ifdef __APPLE__
    char *parent_dir = NULL;
    char *path = NULL;
# endif
    char *dup_dir;
#endif

    if (!dir) {
        errno = EINVAL;
        return 1;
    }

    if (strlen(dir) == 0) {
        errno = EINVAL;
        return 1;
    }

    if (!stat(dir, &st)) {
        return 0;
    }

#ifdef _WIN32
    (void) mode;

    if (_fullpath(path, dir, MAX_PATH) == NULL) {
        return 1;
    }

    if (SHCreateDirectoryExA(NULL, path, NULL) != ERROR_SUCCESS) {
        return 1;
    }
    return 0;
#elif __APPLE__
    dup_dir = strdup(dir);
    if (!dup_dir) {
        return -1;
    }

    /* macOS's dirname(3) should return current directory when slash
     * charachter is not included in passed string.
     * And note that macOS's dirname(3) does not modify passed string.
     */
    parent_dir = dirname(dup_dir);
    if (stat(parent_dir, &st) == 0 && strncmp(parent_dir, ".", 1)) {
        if (S_ISDIR (st.st_mode)) {
            mkdir(dup_dir, mode);
            free(dup_dir);
            return 0;
        }
    }

    /* Create directories straightforward except for the last one hierarchy. */
    for (path = strchr(dup_dir + 1, '/'); path; path = strchr(path + 1, '/')) {
        *path = '\0';
        if (mkdir(dup_dir, mode) == -1) {
            if (errno != EEXIST) {
                *path = '/';
                return -1;
            }
        }
        *path = '/';
    }

    free(dup_dir);
    return mkdir(dir, mode);
#else
    dup_dir = strdup(dir);
    if (!dup_dir) {
        return 1;
    }
    cio_os_mkpath(dirname(dup_dir), mode);
    free(dup_dir);
    return mkdir(dir, mode);
#endif
}