name.ml (2061B)
1 (* Unison file synchronizer: src/name.ml *) 2 (* Copyright 1999-2020, Benjamin C. Pierce 3 4 This program is free software: you can redistribute it and/or modify 5 it under the terms of the GNU General Public License as published by 6 the Free Software Foundation, either version 3 of the License, or 7 (at your option) any later version. 8 9 This program is distributed in the hope that it will be useful, 10 but WITHOUT ANY WARRANTY; without even the implied warranty of 11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 GNU General Public License for more details. 13 14 You should have received a copy of the GNU General Public License 15 along with this program. If not, see <http://www.gnu.org/licenses/>. 16 *) 17 18 19 (* NOTE: IF YOU CHANGE TYPE "NAME", THE ARCHIVE FORMAT CHANGES; 20 INCREMENT "UPDATE.ARCHIVEFORMAT" *) 21 type t = string 22 23 let m = Umarshal.string 24 25 let compare n1 n2 = (Case.ops())#compare n1 n2 26 27 let eq a b = (0 = (compare a b)) 28 29 let toString n = n 30 31 let fromString s = 32 if String.length s = 0 then 33 raise(Invalid_argument "Name.fromString(empty string)"); 34 (* Make sure there are no slashes in the s *) 35 begin try 36 ignore(String.index s '/'); 37 raise (Util.Transient (Printf.sprintf "Filename '%s' contains a '/'" s)) 38 with Not_found -> () end; 39 (* We ought to consider further checks, e.g., in Windows, no colons *) 40 s 41 42 let hash n = (Case.ops())#hash n 43 44 let normalize n = (Case.ops())#normalizeFilename n 45 46 (****) 47 48 let badEncoding s = (Case.ops())#badEncoding s 49 50 (* Windows file naming conventions are descripted here: 51 <http://msdn.microsoft.com/en-us/library/aa365247(printer).aspx> *) 52 let badWindowsFilenameRx = 53 Rx.case_insensitive 54 (Rx.rx 55 "(.*[\000-\031<>:\"/\\|?*].*)|\ 56 ((con|prn|aux|nul|com[1-9]|lpt[1-9])(\\..*)?)|\ 57 (.*[. ])") 58 59 let badWindowsFilenameRelaxedRx = 60 Rx.case_insensitive (Rx.rx "(con|prn|aux|nul|com[1-9]|lpt[1-9])(\\..*)?") 61 62 (* FIX: should also check for a max filename length, not sure how much *) 63 let badFile s = Rx.match_string badWindowsFilenameRx s