-
Notifications
You must be signed in to change notification settings - Fork 0
/
fswrite.ml
80 lines (59 loc) · 2.19 KB
/
fswrite.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
(*
This source code is part of the program 'fntool'
Copyright: Oliver Bandel
Copyleft: GNU GPL v3 or higher/later version
Use this software at your own risk.
*)
type oldname = string
type newname = string
type dirname = string
module type Provider =
sig
val move_or_print: oldname -> newname -> dirname -> unit
val rename_or_print: oldname -> newname -> unit
end
module Action = functor (IN: Provider) ->
struct
(* helper to create directories *)
(* ---------------------------- *)
let create_dir_if_not_exists dirname =
if Sys.file_exists dirname then () else Unix.mkdir dirname 0o700
(* file-mover function *)
(* ------------------- *)
let move_files_to_newdir namemapping =
List.iter ( fun (fname, dir_name) ->
create_dir_if_not_exists dir_name;
let newname = Filename.concat dir_name fname in
IN.move_or_print fname newname dir_name
) namemapping
(* file renamer function *)
(* --------------------- *)
let do_rename (fileinfo : Fileinfo.fileinfo) newname =
if Sys.file_exists newname then (Printf.eprintf "target-filename exists already: could not rename %s to %s\n" fileinfo.fni.filename newname )
else
begin
IN.rename_or_print fileinfo.fni.filename newname
end
end
module Renamer : Provider =
struct
let move_or_print oldfn newfn dirname =
Unix.rename oldfn newfn;
Printf.printf "file moved to dir: %12s <--- %s\n%!" dirname oldfn
let rename_or_print oldfn newfn =
Printf.printf "renaming %s" oldfn;
Printf.printf " to %s\n" newfn;
flush stdout;
Sys.rename oldfn newfn
end
module GitPrinter : Provider =
struct
let move_or_print oldname newname dirname = Printf.printf "git mv %s %s\n" oldname dirname
let rename_or_print oldname newname = Printf.printf "git mv %s %s\n" oldname newname
end
module A = Action(Renamer)
module B = Action(GitPrinter)
let move_files_to_newdir = A.move_files_to_newdir
let do_rename = A.do_rename
let gitcmd_move_files_to_newdir = B.move_files_to_newdir
let gitcmd_do_rename = B.do_rename