<commit_before>// Copyright 2014 Joyent, Inc.  All rights reserved.
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

var nfs = require('nfs');
var path = require('path');

var common = require('./common');

var fs = require('fs');


///-- API

function create_lookup_dir(call, reply, next) {
    var log = call.log;

    log.debug('create_lookup_dir(%s): entered', call.where.dir);
    call.fs.fhandle(call.where.dir, function (err, name) {
        if (err) {
            log.warn(err, 'create_lookup_dir(%s): fhandle notfound',
                call.where.dir);
            reply.error(nfs.NFS3ERR_STALE);
            next(false);
        } else {
            call._dirname = name;
            call._filename = path.join(name, call.where.name);
            log.debug('create_lookup_dir(%s): done -> %s', call.where.dir,
                name);
            next();
        }
    });
}


function do_create(call, reply, next) {
    // ignore the passed in mode (call.obj_attributes.mode) since manta does
    // not support modes
    var mode = 0644;

    // We don't use the fd cache here since that only works with existing files
    // and always opens with 'r+'. We need to create the file here with 'w'.
    call.fs.open(call._filename, 'w', mode, function (open_err, fd) {
        if (open_err) {
            call.log.warn(open_err, 'create: open failed');
            reply.error(nfs.NFS3ERR_SERVERFAULT);
            next(false);
            return;
        }

        call.fs.close(fd, function (close_err) {
            // we're ignoring errors on close
            next();
        });
    });
}


function create(call, reply, next) {
    var log = call.log;

    log.debug('create(%s, %d): entered', call.object, call.how);

    if (call.how === nfs.create_how.EXCLUSIVE) {
        log.warn('create: exclusive not allowed');
        reply.error(nfs.NFS3ERR_NOTSUPP);
        next(false);
    } else if (call.how === nfs.create_how.UNCHECKED) {
        do_create(call, reply, next);
    } else {    // call.how === nfs.create_how.GUARDED
        call.fs.stat(call._filename, function (err, stats) {
            if (err && err.code === 'ENOENT') {
                // This is the "normal" code path (i.e. non-error)
                do_create(call, reply, next);<commit_msg>MANTA-1844 exclusive create<commit_after>// Copyright 2014 Joyent, Inc.  All rights reserved.
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

var nfs = require('nfs');
var path = require('path');

var common = require('./common');

var fs = require('fs');


///-- API

function create_lookup_dir(call, reply, next) {
    var log = call.log;

    log.debug('create_lookup_dir(%s): entered', call.where.dir);
    call.fs.fhandle(call.where.dir, function (err, name) {
        if (err) {
            log.warn(err, 'create_lookup_dir(%s): fhandle notfound',
                call.where.dir);
            reply.error(nfs.NFS3ERR_STALE);
            next(false);
        } else {
            call._dirname = name;
            call._filename = path.join(name, call.where.name);
            log.debug('create_lookup_dir(%s): done -> %s', call.where.dir,
                name);
            next();
        }
    });
}


function do_create(flags, call, reply, next) {
    // ignore the passed in mode (call.obj_attributes.mode) since manta does
    // not support modes
    var mode = 0644;

    // We don't use the fd cache here since that only works with existing files
    // and always opens with the 'r+' flag. We need to create the file here
    // with either the 'w' or 'wx' flags.
    call.fs.open(call._filename, flags, mode, function (open_err, fd) {
        if (open_err) {
            call.log.warn(open_err, 'create: open failed');
            reply.error(nfs.NFS3ERR_SERVERFAULT);
            next(false);
            return;
        }

        call.fs.close(fd, function (close_err) {
            // we're ignoring errors on close
            next();
        });
    });
}


function create(call, reply, next) {
    var log = call.log;

    log.debug('create(%s, %d): entered', call.object, call.how);

    if (call.how === nfs.create_how.EXCLUSIVE) {
        call.fs.stat(call._filename, function (err, stats) {
            if (err && err.code === 'ENOENT') {
                // This is the "normal" code path (i.e. non-error)
                do_create('wx', call, reply, next);
            } else {
                log.debug('create (exclusive) file exists');
                reply.error(nfs.NFS3ERR_EXIST);
                next(false);
            }
        });
    } else if (call.how === nfs.create_how.UNCHECKED) {
        do_create('w', call, reply, next);
    } else {    // call.how === nfs.create_how.GUARDED
        call.fs.stat(call._filename, function (err, stats) {
            if (err && err.code === 'ENOENT') {
                // This is the "normal" code path (i.e. non-error)
                do_create('w', call, reply, next);
{'fraction_non_alphanumeric': 0.14190349036810987, 'fraction_numerical': 0.006484836925424375, 'mean_word_length': 2.055944055944056, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5065515', 'n_tokens_mistral': 1674, 'n_tokens_neox': 1616, 'n_words': 454}