caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] portable truncate
@ 2013-07-26 10:35 Florent Monnier
  2013-07-26 10:46 ` Mihamina Rakotomandimby
                   ` (3 more replies)
  0 siblings, 4 replies; 19+ messages in thread
From: Florent Monnier @ 2013-07-26 10:35 UTC (permalink / raw)
  To: caml-list

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 <sys/types.h>
#include <fail.h>
#include <mlvalues.h>
#include <io.h>
#include "unixsupport.h"
#include <unistd.h>

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

^ permalink raw reply	[flat|nested] 19+ messages in thread

end of thread, other threads:[~2013-07-27 20:03 UTC | newest]

Thread overview: 19+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-07-26 10:35 [Caml-list] portable truncate Florent Monnier
2013-07-26 10:46 ` Mihamina Rakotomandimby
2013-07-26 10:48   ` Török Edwin
2013-07-26 10:52 ` Daniel Bünzli
2013-07-26 12:02 ` [Caml-list] " Florent Monnier
2013-07-27  7:05   ` David Allsopp
2013-07-27  8:54     ` Florent Monnier
2013-07-27  8:38   ` Adrien Nader
2013-07-27  9:05     ` Adrien Nader
2013-07-27 14:11     ` Florent Monnier
2013-07-27 14:23       ` Adrien Nader
2013-07-27 15:34         ` Florent Monnier
2013-07-27 15:43           ` Adrien Nader
2013-07-27 19:45             ` Florent Monnier
2013-07-27 19:53               ` Adrien Nader
2013-07-27 20:03                 ` Török Edwin
     [not found] ` <20130727181711.65d8456f@kiwi.local.tld>
2013-07-27 14:41   ` [Caml-list] " Florent Monnier
2013-07-27 15:26     ` Adrien Nader
2013-07-27 15:40       ` Florent Monnier

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).