static NTSTATUS smb_set_file_size(connection_struct *conn,
				  struct smb_request *req,
				  files_struct *fsp,
				  const struct smb_filename *smb_fname,
				  const SMB_STRUCT_STAT *psbuf,
				  SMB_OFF_T size,
				  bool fail_after_createfile)
{
	NTSTATUS status = NT_STATUS_OK;
	struct smb_filename *smb_fname_tmp = NULL;
	files_struct *new_fsp = NULL;

	if (!VALID_STAT(*psbuf)) {
		return NT_STATUS_OBJECT_NAME_NOT_FOUND;
	}

	DEBUG(6,("smb_set_file_size: size: %.0f ", (double)size));

	if (size == get_file_size_stat(psbuf)) {
		return NT_STATUS_OK;
	}

	DEBUG(10,("smb_set_file_size: file %s : setting new size to %.0f\n",
		  smb_fname_str_dbg(smb_fname), (double)size));

	if (fsp && fsp->fh->fd != -1) {
		/* Handle based call. */
		if (vfs_set_filelen(fsp, size) == -1) {
			return map_nt_error_from_unix(errno);
		}
		trigger_write_time_update_immediate(fsp);
		return NT_STATUS_OK;
	}

	status = copy_smb_filename(talloc_tos(), smb_fname, &smb_fname_tmp);
	if (!NT_STATUS_IS_OK(status)) {
		return status;
	}

	smb_fname_tmp->st = *psbuf;

        status = SMB_VFS_CREATE_FILE(
		conn,					/* conn */
		req,					/* req */
		0,					/* root_dir_fid */
		smb_fname_tmp,				/* fname */
		FILE_WRITE_DATA,			/* access_mask */
		(FILE_SHARE_READ | FILE_SHARE_WRITE |	/* share_access */
		    FILE_SHARE_DELETE),
		FILE_OPEN,				/* create_disposition*/
		0,					/* create_options */
		FILE_ATTRIBUTE_NORMAL,			/* file_attributes */
		FORCE_OPLOCK_BREAK_TO_NONE,		/* oplock_request */
		0,					/* allocation_size */
		NULL,					/* sd */
		NULL,					/* ea_list */
		&new_fsp,				/* result */
		NULL);					/* pinfo */

	TALLOC_FREE(smb_fname_tmp);

	if (!NT_STATUS_IS_OK(status)) {
		/* NB. We check for open_was_deferred in the caller. */
		return status;
	}

	/* See RAW-SFILEINFO-END-OF-FILE */
	if (fail_after_createfile) {
		close_file(req, new_fsp,NORMAL_CLOSE);
		return NT_STATUS_INVALID_LEVEL;
	}

	if (vfs_set_filelen(new_fsp, size) == -1) {
		status = map_nt_error_from_unix(errno);
		close_file(req, new_fsp,NORMAL_CLOSE);
		return status;
	}

	trigger_write_time_update_immediate(new_fsp);
	close_file(req, new_fsp,NORMAL_CLOSE);
	return NT_STATUS_OK;
}
static NTSTATUS smb_set_file_size(connection_struct *conn,
				  struct smb_request *req,
				  files_struct *fsp,
				  const struct smb_filename *smb_fname,
				  const SMB_STRUCT_STAT *psbuf,
				  SMB_OFF_T size,
				  bool fail_after_createfile)
{
	NTSTATUS status = NT_STATUS_OK;
	struct smb_filename *smb_fname_tmp = NULL;
	files_struct *new_fsp = NULL;

	if (!VALID_STAT(*psbuf)) {
		return NT_STATUS_OBJECT_NAME_NOT_FOUND;
	}

	DEBUG(6,("smb_set_file_size: size: %.0f ", (double)size));

	if (size == get_file_size_stat(psbuf)) {
		return NT_STATUS_OK;
	}

	DEBUG(10,("smb_set_file_size: file %s : setting new size to %.0f\n",
		  smb_fname_str_dbg(smb_fname), (double)size));

	if (fsp && fsp->fh->fd != -1) {
		 
		if (vfs_set_filelen(fsp, size) == -1) {
			return map_nt_error_from_unix(errno);
		}
		trigger_write_time_update_immediate(fsp);
		return NT_STATUS_OK;
	}

	status = copy_smb_filename(talloc_tos(), smb_fname, &smb_fname_tmp);
	if (!NT_STATUS_IS_OK(status)) {
		return status;
	}

	smb_fname_tmp->st = *psbuf;

        status = SMB_VFS_CREATE_FILE(
		conn,					 
		req,					 
		0,					 
		smb_fname_tmp,				 
		FILE_WRITE_DATA,			 
		(FILE_SHARE_READ | FILE_SHARE_WRITE |	 
		    FILE_SHARE_DELETE),
		FILE_OPEN,				 
		0,					 
		FILE_ATTRIBUTE_NORMAL,			 
		FORCE_OPLOCK_BREAK_TO_NONE,		 
		0,					 
		NULL,					 
		NULL,					 
		&new_fsp,				 
		NULL);					 

	TALLOC_FREE(smb_fname_tmp);

	if (!NT_STATUS_IS_OK(status)) {
		 
		return status;
	}

	 
	if (fail_after_createfile) {
		close_file(req, new_fsp,NORMAL_CLOSE);
		return NT_STATUS_INVALID_LEVEL;
	}

	if (vfs_set_filelen(new_fsp, size) == -1) {
		status = map_nt_error_from_unix(errno);
		close_file(req, new_fsp,NORMAL_CLOSE);
		return status;
	}

	trigger_write_time_update_immediate(new_fsp);
	close_file(req, new_fsp,NORMAL_CLOSE);
	return NT_STATUS_OK;
}