<commit_before>(*
 * Copyright (C) 2006-2009 Citrix Systems Inc.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published
 * by the Free Software Foundation; version 2.1 only. with the special
 * exception on linking described in file LICENSE.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *)
(** HTTP handler for importing a raw VDI.
 * @group Import and Export
 *)

module D=Debug.Make(struct let name="import" end)
open D

open Http
open Importexport
open Sparse_encoding
open Unixext
open Pervasiveext
open Client

let vhd_tool = "/usr/sbin/vhd-tool"

let receive protocol (s: Unix.file_descr) (path: string) =
  let s' = Uuidm.to_string (Uuidm.create `V4) in
  let args = [ "serve";
               "--direct";
               "--source-protocol"; protocol;
               "--source-fd"; s';
               "--destination"; "file://" ^ path;
               "--destination-format"; "raw" ] in
  info "Executing %s %s" vhd_tool (String.concat " " args);
  let pid = Forkhelpers.safe_close_and_exec None None None [ s', s ] vhd_tool args in
  let (_, status) = Forkhelpers.waitpid pid in
  if status <> Unix.WEXITED 0 then begin
   error "vhd-tool failed, returning VDI_IO_ERROR";
   raise (Api_errors.Server_error (Api_errors.vdi_io_error, ["Device I/O errors"])) 
  end

let localhost_handler rpc session_id vdi (req: Request.t) (s: Unix.file_descr) =
  req.Request.close <- true;
  Xapi_http.with_context "Importing raw VDI" req s
    (fun __context ->
	let all = req.Request.query @ req.Request.cookie in
      let chunked = List.mem_assoc "chunked" all in
      let task_id = Context.get_task_id __context in
	 debug "import_raw_vdi task_id = %s vdi = %s; chunked = %b" (Ref.string_of task_id) (Ref.string_of vdi) chunked;
	 try
	match req.Request.transfer_encoding with
	| Some x ->
	    error "Chunked encoding not yet implemented in the import code";
	    Http_svr.headers s (http_403_forbidden ());
	    raise (Failure (Printf.sprintf "import code cannot handle encoding: %s" x))<commit_msg>import_raw_vdi: log output from vhd-tool<commit_after>(*
 * Copyright (C) 2006-2009 Citrix Systems Inc.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published
 * by the Free Software Foundation; version 2.1 only. with the special
 * exception on linking described in file LICENSE.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *)
(** HTTP handler for importing a raw VDI.
 * @group Import and Export
 *)

module D=Debug.Make(struct let name="import" end)
open D

open Http
open Importexport
open Sparse_encoding
open Unixext
open Pervasiveext
open Client

let vhd_tool = "/usr/sbin/vhd-tool"

let receive protocol (s: Unix.file_descr) (path: string) =
  let s' = Uuidm.to_string (Uuidm.create `V4) in
  let args = [ "serve";
               "--direct";
               "--source-protocol"; protocol;
               "--source-fd"; s';
               "--destination"; "file://" ^ path;
               "--destination-format"; "raw" ] in
  info "Executing %s %s" vhd_tool (String.concat " " args);
  let open Forkhelpers in
  match with_logfile_fd "vhd-tool"
    (fun log_fd ->
      let pid = safe_close_and_exec None (Some log_fd) (Some log_fd) [ s', s ] vhd_tool args in
      let (_, status) = waitpid pid in
      if status <> Unix.WEXITED 0 then begin
        error "vhd-tool failed, returning VDI_IO_ERROR";
        raise (Api_errors.Server_error (Api_errors.vdi_io_error, ["Device I/O errors"])) 
      end
    ) with
  | Success(out, _) -> debug "%s" out
  | Failure(out, e) -> error "vhd-tool output: %s" out; raise e


let localhost_handler rpc session_id vdi (req: Request.t) (s: Unix.file_descr) =
  req.Request.close <- true;
  Xapi_http.with_context "Importing raw VDI" req s
    (fun __context ->
	let all = req.Request.query @ req.Request.cookie in
      let chunked = List.mem_assoc "chunked" all in
      let task_id = Context.get_task_id __context in
	 debug "import_raw_vdi task_id = %s vdi = %s; chunked = %b" (Ref.string_of task_id) (Ref.string_of vdi) chunked;
	 try
	match req.Request.transfer_encoding with
	| Some x ->
	    error "Chunked encoding not yet implemented in the import code";
	    Http_svr.headers s (http_403_forbidden ());
	    raise (Failure (Printf.sprintf "import code cannot handle encoding: %s" x))
{'fraction_non_alphanumeric': 0.10018552875695733, 'fraction_numerical': 0.006184291898577613, 'mean_word_length': 3.2711267605633805, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 12, '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': '2947243', 'n_tokens_mistral': 1553, 'n_tokens_neox': 1501, 'n_words': 562}