Unverified Commit dc0cf736 authored by Tommaso Tocci's avatar Tommaso Tocci
Browse files

Avoid creation of OpenFile for non-existing files

Postpone the creation of the OpenFile object until we are sure that the
file actually exists and we have enough access rigths for it.
parent d573a07a
Loading
Loading
Loading
Loading
+12 −15
Original line number Diff line number Diff line
@@ -6,14 +6,15 @@ using namespace std;

int adafs_open(const std::string& path, mode_t mode, int flags) {
    init_ld_env_if_needed();
    auto err = 1;
    auto fd = file_map.add(path, flags);
    // TODO the open flags should not be in the map just set the pos accordingly
    // TODO look up if file exists configurable
    if (flags & O_CREAT)
    int err = 0;

    if (flags & O_CREAT){
        // no access check required here. If one is using our FS they have the permissions.
        err = adafs_mk_node(path, mode | S_IFREG);
    else {
        if(err != 0)
            return -1;

    } else {
        auto mask = F_OK; // F_OK == 0
#if defined(CHECK_ACCESS_DURING_OPEN)
        if ((mode & S_IRUSR) || (mode & S_IRGRP) || (mode & S_IROTH))
@@ -26,17 +27,13 @@ int adafs_open(const std::string& path, mode_t mode, int flags) {
#if defined(DO_LOOKUP)
        // check if file exists
        err = rpc_send_access(path, mask);
#else
        // file is assumed to be existing, even though it might not
        err = 0;
#endif
    }
    if (err == 0)
        return fd;
    else {
        file_map.remove(fd);
        if(err != 0)
            return -1;
#endif
    }
    
    // TODO the open flags should not be in the map just set the pos accordingly
    return file_map.add(path, flags);
}

int adafs_mk_node(const std::string& path, const mode_t mode) {