remi-fileset.cpp 7.93 KB
Newer Older
Matthieu Dorier's avatar
Matthieu Dorier committed
1 2 3 4 5
/*
 * (C) 2018 The University of Chicago
 * 
 * See COPYRIGHT in top-level directory.
 */
6 7 8 9 10 11 12 13 14
#include <sys/stat.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/mman.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <fcntl.h>
Matthieu Dorier's avatar
Matthieu Dorier committed
15
#include <cstring>
16
#include "remi/remi-common.h"
17
#include "fs-util.hpp"
Matthieu Dorier's avatar
Matthieu Dorier committed
18
#include "remi-fileset.hpp"
19 20 21 22 23 24

extern "C" int remi_fileset_create(
        const char* fileset_class,
        const char* fileset_root,
        remi_fileset_t* fileset) 
{
Matthieu Dorier's avatar
Matthieu Dorier committed
25 26 27 28 29 30 31 32 33 34 35 36
    if(fileset_class == NULL || fileset_root == NULL)
        return REMI_ERR_INVALID_ARG;
    if(fileset_root[0] != '/')
        return REMI_ERR_INVALID_ARG;
    auto fs = new remi_fileset;
    fs->m_class = fileset_class;
    fs->m_root  = fileset_root;
    if(fs->m_root[fs->m_root.size()-1] != '/') {
        fs->m_root += "/";
    }
    *fileset = fs;
    return REMI_SUCCESS;
37 38 39 40
}

extern "C" int remi_fileset_free(remi_fileset_t fileset)
{
Matthieu Dorier's avatar
Matthieu Dorier committed
41 42 43 44
    if(fileset == REMI_FILESET_NULL)
        return REMI_SUCCESS;
    delete fileset;
    return REMI_SUCCESS;
45 46
}

47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
extern "C" int remi_fileset_set_xfer_size(
        remi_fileset_t fileset,
        size_t size)
{
    if(fileset == REMI_FILESET_NULL
    || size == 0)
        return REMI_ERR_INVALID_ARG;
    fileset->m_xfer_size = size;
    return REMI_SUCCESS;
}

extern "C" int remi_fileset_get_xfer_size(
        remi_fileset_t fileset,
        size_t* size)
{
    if(fileset == REMI_FILESET_NULL
    || size == nullptr)
        return REMI_ERR_INVALID_ARG;
    *size = fileset->m_xfer_size;
    return REMI_SUCCESS;
}

69 70 71 72 73
extern "C" int remi_fileset_get_class(
        remi_fileset_t fileset,
        char* buf,
        size_t* size)
{
Matthieu Dorier's avatar
Matthieu Dorier committed
74 75 76 77 78 79 80 81 82 83 84 85
    if(fileset == REMI_FILESET_NULL)
        return REMI_ERR_INVALID_ARG;
    if(buf == nullptr) {
        *size = fileset->m_class.size()+1;
        return REMI_SUCCESS;
    } else if(*size < fileset->m_class.size()+1) {
        return REMI_ERR_SIZE;
    } else {
        std::memcpy(buf, fileset->m_class.c_str(), fileset->m_class.size()+1);
        *size = fileset->m_class.size()+1;
    }
    return REMI_SUCCESS;
86 87 88 89 90 91 92 93
}

extern "C" int remi_fileset_get_root(
        remi_fileset_t fileset,
        char* buf,
        size_t* size)
{

Matthieu Dorier's avatar
Matthieu Dorier committed
94 95 96 97 98 99 100 101 102 103 104 105
    if(fileset == REMI_FILESET_NULL)
        return REMI_ERR_INVALID_ARG;
    if(buf == nullptr) {
        *size = fileset->m_root.size()+1;
        return REMI_SUCCESS;
    } else if(*size < fileset->m_root.size()+1) {
        return REMI_ERR_SIZE;
    } else {
        std::strcpy(buf, fileset->m_root.c_str());
        *size = fileset->m_root.size()+1;
    }
    return REMI_SUCCESS;
106 107 108 109 110 111
}

extern "C" int remi_fileset_register_file(
        remi_fileset_t fileset,
        const char* filename)
{
Matthieu Dorier's avatar
Matthieu Dorier committed
112 113 114 115 116 117
    if(fileset == REMI_FILESET_NULL || filename == NULL)
        return REMI_ERR_INVALID_ARG;
    unsigned i = 0;
    while(filename[i] == '/') i += 1;
    fileset->m_files.insert(filename+i);
    return REMI_SUCCESS;
118 119 120 121 122 123
}

extern "C" int remi_fileset_deregister_file(
        remi_fileset_t fileset,
        const char* filename)
{
Matthieu Dorier's avatar
Matthieu Dorier committed
124 125 126 127 128 129 130 131 132 133
    if(fileset == REMI_FILESET_NULL || filename == NULL)
        return REMI_ERR_INVALID_ARG;
    unsigned i = 0;
    while(filename[i] == '/') i += 1;
    auto it = fileset->m_files.find(filename+i);
    if(it == fileset->m_files.end())
        return REMI_ERR_UNKNOWN_FILE;
    else
        fileset->m_files.erase(filename+i);
    return REMI_SUCCESS;
134 135 136 137 138 139 140
}

extern "C" int remi_fileset_foreach_file(
        remi_fileset_t fileset,
        remi_fileset_callback_t callback,
        void* uargs)
{
Matthieu Dorier's avatar
Matthieu Dorier committed
141 142 143 144 145 146
    if(fileset == REMI_FILESET_NULL || callback == NULL)
        return REMI_ERR_INVALID_ARG;
    for(auto& filename : fileset->m_files) {
        callback(filename.c_str(), uargs);
    }
    return REMI_SUCCESS;
147 148
}

149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
extern "C" int remi_fileset_register_directory(
        remi_fileset_t fileset,
        const char* dirname)
{
    if(fileset == REMI_FILESET_NULL || dirname == NULL)
        return REMI_ERR_INVALID_ARG;
    unsigned i = 0;
    while(dirname[i] == '/') i += 1;
    fileset->m_directories.insert(dirname+i);
    return REMI_SUCCESS;
}

extern "C" int remi_fileset_deregister_directory(
        remi_fileset_t fileset,
        const char* dirname)
{
    if(fileset == REMI_FILESET_NULL || dirname == NULL)
        return REMI_ERR_INVALID_ARG;
    unsigned i = 0;
    while(dirname[i] == '/') i += 1;
    auto it = fileset->m_directories.find(dirname+i);
    if(it == fileset->m_directories.end())
        return REMI_ERR_UNKNOWN_FILE;
    else
        fileset->m_directories.erase(dirname+i);
    return REMI_SUCCESS;
}

extern "C" int remi_fileset_foreach_directory(
        remi_fileset_t fileset,
        remi_fileset_callback_t callback,
        void* uargs)
{
    if(fileset == REMI_FILESET_NULL || callback == NULL)
        return REMI_ERR_INVALID_ARG;
    for(auto& dirname : fileset->m_directories) {
        callback(dirname.c_str(), uargs);
    }
    return REMI_SUCCESS;
}

extern "C" int remi_fileset_walkthrough(
        remi_fileset_t fileset,
        remi_fileset_callback_t callback,
        void* uargs)
{
    if(fileset == REMI_FILESET_NULL || callback == NULL)
        return REMI_ERR_INVALID_ARG;
    std::set<std::string> files;
    for(const auto& filename : fileset->m_files) {
        files.insert(filename);
    }
    for(const auto& dirname : fileset->m_directories) {
        listFiles(fileset->m_root, dirname, [&files](const std::string& filename) {
            files.insert(filename);
        });
    }
    for(const auto& filename : files) {
        callback(filename.c_str(), uargs);
    }
    return REMI_SUCCESS;
}

212 213 214 215 216
extern "C" int remi_fileset_register_metadata(
        remi_fileset_t fileset,
        const char* key,
        const char* value)
{
Matthieu Dorier's avatar
Matthieu Dorier committed
217 218 219 220
    if(fileset == REMI_FILESET_NULL || key == NULL)
        return REMI_ERR_INVALID_ARG;
    fileset->m_metadata[key] = value;
    return REMI_SUCCESS;
221 222 223 224
}

extern "C" int remi_fileset_deregister_metadata(
        remi_fileset_t fileset,
Matthieu Dorier's avatar
Matthieu Dorier committed
225
        const char* key)
226
{
Matthieu Dorier's avatar
Matthieu Dorier committed
227 228 229 230 231 232 233
    if(fileset == REMI_FILESET_NULL || key == NULL)
        return REMI_ERR_INVALID_ARG;
    auto it = fileset->m_metadata.find(key);
    if(it == fileset->m_metadata.end())
        return REMI_ERR_UNKNOWN_META;
    fileset->m_metadata.erase(it);
    return REMI_SUCCESS;
234 235 236 237 238 239 240
}

extern "C" int remi_fileset_foreach_metadata(
        remi_fileset_t fileset,
        remi_metadata_callback_t callback,
        void* uargs)
{
Matthieu Dorier's avatar
Matthieu Dorier committed
241 242 243 244 245 246
    if(fileset == REMI_FILESET_NULL || callback == NULL)
        return REMI_ERR_INVALID_ARG;
    for(auto& meta : fileset->m_metadata) {
        callback(meta.first.c_str(), meta.second.c_str(), uargs);
    }
    return REMI_SUCCESS;
247
}
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274

struct add_size_args {
    size_t         size;
    remi_fileset_t fileset;
    int            ret;
};

static void add_metadata_size(const char* key, const char* value, void* uargs) {
    add_size_args* args = static_cast<add_size_args*>(uargs);
    args->size += strlen(key) + strlen(value) + 2;
}

static void add_file_size(const char* filename, void* uargs) {
    add_size_args* args = static_cast<add_size_args*>(uargs);
    if(args->ret != 0) return;
    std::string theFilename = args->fileset->m_root + filename;
    int fd = open(theFilename.c_str(), O_RDONLY, 0);
    if(fd == -1) {
        args->ret = -1;
        return;
    }
    struct stat st;
    if(0 != fstat(fd, &st)) {
        close(fd);
        args->ret = -1;
        return;
    }
275
    close(fd);
276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301
    args->size += st.st_size;
}

extern "C" int remi_fileset_compute_size(
        remi_fileset_t fileset,
        int include_metadata,
        size_t* size)
{
    add_size_args args;
    args.size = 0;
    args.ret  = 0;
    args.fileset = fileset;
    int ret = remi_fileset_walkthrough(fileset, add_file_size, static_cast<void*>(&args));
    if(ret != REMI_SUCCESS)
        return ret;
    if(args.ret != 0) {
        return REMI_ERR_IO;
    }
    if(include_metadata) {
        ret = remi_fileset_foreach_metadata(fileset, add_metadata_size, static_cast<void*>(&args));
    }
    if(ret != REMI_SUCCESS)
        return ret;
    *size = args.size;
    return REMI_SUCCESS;
}