From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Original-To: caml-list@sympa.inria.fr Delivered-To: caml-list@sympa.inria.fr Received: from mail2-relais-roc.national.inria.fr (mail2-relais-roc.national.inria.fr [192.134.164.83]) by sympa.inria.fr (Postfix) with ESMTPS id 4052D81799 for ; Fri, 26 Jul 2013 12:35:14 +0200 (CEST) Received-SPF: None (mail2-smtp-roc.national.inria.fr: no sender authenticity information available from domain of monnier.florent@gmail.com) identity=pra; client-ip=74.125.82.47; receiver=mail2-smtp-roc.national.inria.fr; envelope-from="monnier.florent@gmail.com"; x-sender="monnier.florent@gmail.com"; x-conformance=sidf_compatible Received-SPF: Pass (mail2-smtp-roc.national.inria.fr: domain of monnier.florent@gmail.com designates 74.125.82.47 as permitted sender) identity=mailfrom; client-ip=74.125.82.47; receiver=mail2-smtp-roc.national.inria.fr; envelope-from="monnier.florent@gmail.com"; x-sender="monnier.florent@gmail.com"; x-conformance=sidf_compatible; x-record-type="v=spf1" Received-SPF: None (mail2-smtp-roc.national.inria.fr: no sender authenticity information available from domain of postmaster@mail-wg0-f47.google.com) identity=helo; client-ip=74.125.82.47; receiver=mail2-smtp-roc.national.inria.fr; envelope-from="monnier.florent@gmail.com"; x-sender="postmaster@mail-wg0-f47.google.com"; x-conformance=sidf_compatible X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AgECAFxQ8lFKfVIvk2dsb2JhbABagztQvlIIFg4BAQEBBwsLCRQEJIJSGQEbHgMSEF0BEQEFARaIHAEDD5g9gn6MT4J/hFQKGScNZId0AQUMk30Dl1+BKY4+FimEPDo X-IPAS-Result: AgECAFxQ8lFKfVIvk2dsb2JhbABagztQvlIIFg4BAQEBBwsLCRQEJIJSGQEbHgMSEF0BEQEFARaIHAEDD5g9gn6MT4J/hFQKGScNZId0AQUMk30Dl1+BKY4+FimEPDo X-IronPort-AV: E=Sophos;i="4.89,750,1367964000"; d="scan'208";a="27409397" Received: from mail-wg0-f47.google.com ([74.125.82.47]) by mail2-smtp-roc.national.inria.fr with ESMTP/TLS/RC4-SHA; 26 Jul 2013 12:35:07 +0200 Received: by mail-wg0-f47.google.com with SMTP id j13so2615209wgh.2 for ; Fri, 26 Jul 2013 03:35:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:date:message-id:subject:from:to:content-type; bh=DpZw0XrDHRag2wE/FD8+ZrLgA6pDZHQ7f6TLTM0nZds=; b=ZDxAfV6Aa9ZuMAABT/1uYhaTDHrk2Dz/4TSmahjS4PFo1tL/N147kzKSRf2v5oPTy7 ZYrR1py3dxuWj//V6ttIvjNwSx1VU0nV1+NjHOPufRbaiD2UEi8JPppj+EkJz/Gxbi+8 IX/yelbrYoW+foYj3rSXmi1qwnRJT+Hy7CW0EpIBUdyda0X+0ye3bb/fdbxqrp7mWuxD SiYHNHcY+ZPyrGORV5F9Zz6cvvj82D9twwvxkwBRAXIdqt+8tvhtPFKYj2+/uVR/f0WF LgwlF8NDDCDwEF3a8jaBpPlspW534lJjYI8LpdNkSJ6cr119CCbzKIAHxqNklcghJLEc Mciw== MIME-Version: 1.0 X-Received: by 10.194.242.99 with SMTP id wp3mr33813461wjc.78.1374834903492; Fri, 26 Jul 2013 03:35:03 -0700 (PDT) Received: by 10.194.80.36 with HTTP; Fri, 26 Jul 2013 03:35:03 -0700 (PDT) Date: Fri, 26 Jul 2013 12:35:03 +0200 Message-ID: From: Florent Monnier To: caml-list@inria.fr Content-Type: text/plain; charset=ISO-8859-1 X-Validation-by: monnier.florent@gmail.com Subject: [Caml-list] portable truncate Hi, I would like to know how to truncate a file under MS/Windows. When I use any of the functions Unix.(LargeFile.)[f]truncate I get these exceptions: Exception: Invalid_argument "Unix.truncate not implemented". Exception: Invalid_argument "Unix.LargeFile.ftruncate not implemented". 1) I thought that the module Unix only contains portable things for Windows, so it's not true? Or maybe this is only an issue with the protz's OCaml, not OCaml itself that we can install or compile in another way ? 2) what other ways can I use to truncate my files in a portable way? The files that I want to truncate are opened with Unix.openfile, and so are of type Unix.file_descr. I tryed to replicate these stubs without the macro that removes these functions but I still get the exception from MyUnix.ftruncate: Failure "ftruncate" But if I use MyUnix.truncate instead (the input is then the filename, not the Unix.file_descr as I would like), then it works. So if I have to use this as a workaround I guess I have to close the file_descr before and open it again after ? Or is it safe to do it at the same time ? I would still prefer a workarround that doesn't require to use anything else than ocaml and its stdlib. /* Xavier Leroy, projet Cristal, INRIA Rocquencourt Copyright 1996 Institut National de Recherche en Informatique et en Automatique. All rights reserved. This file is distributed under the terms of the GNU Library General Public License, with the special exception on linking described in the file LICENSE of OCaml. */ #include #include #include #include #include "unixsupport.h" #include CAMLprim value myunix_ftruncate(value fd, value len) { if (ftruncate(Int_val(fd), Long_val(len)) == -1) caml_failwith("ftruncate"); return Val_unit; } CAMLprim value myunix_ftruncate_64(value fd, value len) { if (ftruncate(Int_val(fd), Int64_val(len)) == -1) caml_failwith("ftruncate64"); return Val_unit; } CAMLprim value myunix_truncate(value path, value len) { if (truncate(String_val(path), Long_val(len)) == -1) caml_failwith("truncate"); return Val_unit; } CAMLprim value myunix_truncate_64(value path, value len) { if (truncate(String_val(path), Int64_val(len)) == -1) caml_failwith("truncate64"); return Val_unit; } $ cat myUnix.ml (* Xavier Leroy, projet Cristal, INRIA Rocquencourt Copyright 1996 Institut National de Recherche en Informatique et en Automatique. All rights reserved. This file is distributed under the terms of the GNU Library General Public License, with the special exception on linking described the file LICENSE of OCaml. *) type file_descr = Unix.file_descr external ftruncate : file_descr -> int -> unit = "myunix_ftruncate" external truncate : string -> int -> unit = "myunix_truncate" module LargeFile = struct external ftruncate : file_descr -> int64 -> unit = "myunix_ftruncate_64" external truncate : string -> int64 -> unit = "myunix_truncate_64" end $ export CC='/bin/i686-w64-mingw32-gcc' $ $CC -c -I c:/OCaml/lib/caml ftruncate.c $ ocamlc -c myUnix.mli $ ocamlc -c myUnix.ml $ ocamlmklib -o myunix myUnix.cmo ftruncate.o -- Cheers