Commit 082e1628 authored by Marc Vef's avatar Marc Vef
Browse files

Merge branch 'nonexisting_open' into 'master'



Correctly set errno on open() and other open-related improovements

See merge request zdvresearch_bsc/adafs!33

Signed-off-by: default avatarMarc Vef <vef@uni-mainz.de>
parents d98b7c3c 4f5a367a
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) {
+23 −16
Original line number Diff line number Diff line
@@ -76,7 +76,7 @@ int rpc_send_access(const std::string& path, const int mask) {
    hg_handle_t handle;
    rpc_access_in_t in{};
    rpc_err_out_t out{};
    int err = EUNKNOWN;
    int err = 0;
    // fill in
    in.path = path.c_str();
    in.mask = mask;
@@ -94,24 +94,31 @@ int rpc_send_access(const std::string& path, const int mask) {
    ret = margo_forward_timed_wrap(handle, &in);
#endif
    // Get response
    if (ret == HG_SUCCESS) {
        ld_logger->trace("{}() Waiting for response", __func__);
        ret = margo_get_output(handle, &out);
        if (ret == HG_SUCCESS) {
            ld_logger->debug("{}() Got response success: {}", __func__, out.err);
            err = out.err;
        } else {
            // something is wrong
    if (ret != HG_SUCCESS) {
        ld_logger->error("{}() timed out");
        errno = EBUSY;
            ld_logger->error("{}() while getting rpc output", __func__);
        margo_destroy(handle);
        return -1;
    }
        /* clean up resources consumed by this rpc */
        margo_free_output(handle, &out);
    } else {
        ld_logger->warn("{}() timed out");

    ret = margo_get_output(handle, &out);
    if (ret != HG_SUCCESS) {
        ld_logger->error("{}() while getting rpc output", __func__);
        errno = EBUSY;
        margo_destroy(handle);
        return -1;
    }
    
    ld_logger->debug("{}() Got response with error: {}", __func__, out.err);
    
    if(out.err != 0){
        //In case of error out.err contains the
        //corresponding value of errno
        errno = out.err;
        err = -1;
    }
    
    margo_free_output(handle, &out);
    margo_destroy(handle);
    return err;
}
+11 −1
Original line number Diff line number Diff line
@@ -21,11 +21,21 @@ int main(int argc, char* argv[]) {
    string p = "/tmp/mountdir/file"s;
    char buffIn[] = "oops.";
    char *buffOut = new char[strlen(buffIn)];
    int fd;

    fd = open(p.c_str(), O_RDONLY);
    if(fd >= 0 ){
        cerr << "ERROR: Succeeded on opening non-existing file" << endl;
        return -1;
    }
    if(errno != ENOENT){
        cerr << "ERROR: wrong error number while opening non-existing file: " << errno << endl;
        return -1;
    }
    
    /* Write the file */

    auto fd = open(p.c_str(), O_WRONLY | O_CREAT, 0777);
    fd = open(p.c_str(), O_WRONLY | O_CREAT, 0777);
    if(fd < 0){
        cerr << "Error opening file (write)" << endl;
        return -1;