// samba3 includes
#include <fcntl.h>
#include "includes.h"

static NTSTATUS open_shared_create_file(vfs_handle_struct *handle,
	struct smb_request *req, uint16_t root_dir_fid,
	struct smb_filename *smb_fname, uint32_t access_mask,
	uint32_t share_access, uint32_t create_disposition,
	uint32_t create_options, uint32_t file_attributes,
	uint32_t oplock_request, uint64_t allocation_size,
	struct security_descriptor *sd, struct ea_list *ea_list,
	files_struct **result, int *pinfo)
{
	NTSTATUS retval;
	struct share_mode_lock *lck;
	char *parent_dir;


#if 0	
	fprintf(stderr, "\n");
	fprintf(stderr, "smb_fname->base_name: '%s'\n", smb_fname->base_name);
	fprintf(stderr, "%s:%d "
		"E(handle:%p, req:%p, root_dir_fid:%d, smb_fname:%p, access_mask:0x%X, "
		"share_access:0x%X, create_disposition:0x%X, create_options:0x%X, "
		"file_attributes:0x%X, oplock_request:%d, allocation_size:%ld, "
		"sd:%p, ea_list:%p, result:%p, pinfo:%p)\n",
		__FUNCTION__, __LINE__,
		handle, req, root_dir_fid, smb_fname, access_mask,
		share_access, create_disposition, create_options,
		file_attributes, oplock_request, allocation_size, sd, ea_list,
		result, pinfo);
#endif	


	// Don't allow to lock share delete access;
	if (create_disposition != FILE_OVERWRITE_IF && create_disposition != FILE_OPEN_IF)
	{
		share_access |= FILE_SHARE_DELETE;
		share_access |= FILE_SHARE_WRITE;

		file_attributes |= FILE_FLAG_POSIX_SEMANTICS;
		if (!parent_dirname(talloc_tos(), smb_fname->base_name, &parent_dir,
				    NULL)) {
			return NT_STATUS_NO_MEMORY;
		}
		file_attributes |= unix_mode(handle->conn, file_attributes | aARCH, smb_fname, parent_dir);
	}

	return SMB_VFS_NEXT_CREATE_FILE(handle,
					req,
					root_dir_fid,
					smb_fname,
					access_mask,
					share_access,
					create_disposition,
					create_options,
					file_attributes,
					oplock_request,
					allocation_size,
					sd,
					ea_list,
					result,
					pinfo);
}

static struct vfs_fn_pointers open_shared_fns = {
	.create_file = open_shared_create_file,
};

NTSTATUS vfs_open_shared_init(void)
{
	return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "open_shared", &open_shared_fns);
}
