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 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376
|
(* Unison file synchronizer: src/os.ml *)
(* Copyright 1999-2014, Benjamin C. Pierce
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*)
(* This file attempts to isolate operating system specific details from the *)
(* rest of the program. *)
let debug = Util.debug "os"
(* Assumption: Prefs are not loaded on server, so clientHostName is always *)
(* set to myCanonicalHostName. *)
let localCanonicalHostName =
try System.getenv "UNISONLOCALHOSTNAME"
with Not_found -> Unix.gethostname()
let clientHostName : string Prefs.t =
Prefs.createString "clientHostName" localCanonicalHostName
"!set host name of client"
("When specified, the host name of the client will not be guessed" ^
"and the provided host name will be used to find the archive.")
let serverHostName = localCanonicalHostName
let myCanonicalHostName () =
if !Trace.runningasserver then serverHostName else Prefs.read clientHostName
let tempFilePrefix = ".unison."
let tempFileSuffixFixed = ".unison.tmp"
let tempFileSuffix = ref tempFileSuffixFixed
let includeInTempNames s =
(* BCP: Added this in Jan 08. If (as I believe) it never fails, then this tricky
stuff can be deleted. *)
assert (s<>"");
tempFileSuffix :=
if s = "" then tempFileSuffixFixed
else "." ^ s ^ tempFileSuffixFixed
let isTempFile file =
Util.endswith file tempFileSuffixFixed &&
Util.startswith file tempFilePrefix
(*****************************************************************************)
(* QUERYING THE FILESYSTEM *)
(*****************************************************************************)
let exists fspath path =
(Fileinfo.get false fspath path).Fileinfo.typ <> `ABSENT
let readLink fspath path =
Util.convertUnixErrorsToTransient
"reading symbolic link"
(fun () ->
let abspath = Fspath.concat fspath path in
Fs.readlink abspath)
let rec isAppleDoubleFile file =
Prefs.read Osx.rsrc &&
String.length file > 2 && file.[0] = '.' && file.[1] = '_'
(* Assumes that (fspath, path) is a directory, and returns the list of *)
(* children, except for '.' and '..'. *)
let allChildrenOf fspath path =
Util.convertUnixErrorsToTransient
"scanning directory"
(fun () ->
let rec loop children directory =
let newFile = try directory.Fs.readdir () with End_of_file -> "" in
if newFile = "" then children else
let newChildren =
if newFile = "." || newFile = ".." then
children
else
Name.fromString newFile :: children in
loop newChildren directory
in
let absolutePath = Fspath.concat fspath path in
let directory =
try
Some (Fs.opendir absolutePath)
with Unix.Unix_error (Unix.ENOENT, _, _) ->
(* FIX (in Ocaml): under Windows, when a directory is empty
(not even "." and ".."), FindFirstFile fails with
ERROR_FILE_NOT_FOUND while ocaml expects the error
ERROR_NO_MORE_FILES *)
None
in
match directory with
Some directory ->
begin try
let result = loop [] directory in
directory.Fs.closedir ();
result
with Unix.Unix_error _ as e ->
begin try
directory.Fs.closedir ()
with Unix.Unix_error _ -> () end;
raise e
end
| None ->
[])
(* Assumes that (fspath, path) is a directory, and returns the list of *)
(* children, except for temporary files and AppleDouble files. *)
let rec childrenOf fspath path =
List.filter
(fun filename ->
let file = Name.toString filename in
if isAppleDoubleFile file then
false
(* does it belong to here ? *)
(* else if Util.endswith file backupFileSuffix then begin *)
(* let newPath = Path.child path filename in *)
(* removeBackupIfUnwanted fspath newPath; *)
(* false *)
(* end *)
else if isTempFile file then begin
if Util.endswith file !tempFileSuffix then begin
let p = Path.child path filename in
let i = Fileinfo.get false fspath p in
let secondsinthirtydays = 2592000.0 in
if Props.time i.Fileinfo.desc +. secondsinthirtydays < Util.time()
then begin
debug (fun()-> Util.msg "deleting old temp file %s\n"
(Fspath.toDebugString (Fspath.concat fspath p)));
delete fspath p
end else
debug (fun()-> Util.msg
"keeping temp file %s since it is less than 30 days old\n"
(Fspath.toDebugString (Fspath.concat fspath p)));
end;
false
end else
true)
(allChildrenOf fspath path)
(*****************************************************************************)
(* ACTIONS ON FILESYSTEM *)
(*****************************************************************************)
(* Deletes a file or a directory, but checks before if there is something *)
and delete fspath path =
Util.convertUnixErrorsToTransient
"deleting"
(fun () ->
let absolutePath = Fspath.concat fspath path in
match (Fileinfo.get false fspath path).Fileinfo.typ with
`DIRECTORY ->
begin try
Fs.chmod absolutePath 0o700
with Unix.Unix_error _ -> () end;
Safelist.iter
(fun child -> delete fspath (Path.child path child))
(allChildrenOf fspath path);
Fs.rmdir absolutePath
| `FILE ->
if Util.osType <> `Unix then begin
try
Fs.chmod absolutePath 0o600;
with Unix.Unix_error _ -> ()
end;
Fs.unlink absolutePath;
if Prefs.read Osx.rsrc then begin
let pathDouble = Fspath.appleDouble absolutePath in
if Fs.file_exists pathDouble then
Fs.unlink pathDouble
end
| `SYMLINK ->
(* Note that chmod would not do the right thing on links *)
Fs.unlink absolutePath
| `ABSENT ->
())
let rename fname sourcefspath sourcepath targetfspath targetpath =
let source = Fspath.concat sourcefspath sourcepath in
let source' = Fspath.toPrintString source in
let target = Fspath.concat targetfspath targetpath in
let target' = Fspath.toPrintString target in
if source = target then
raise (Util.Transient ("Rename ("^fname^"): identical source and target " ^ source'));
Util.convertUnixErrorsToTransient ("renaming " ^ source' ^ " to " ^ target')
(fun () ->
debug (fun() -> Util.msg "rename %s to %s\n" source' target');
Fs.rename source target;
if Prefs.read Osx.rsrc then begin
let sourceDouble = Fspath.appleDouble source in
let targetDouble = Fspath.appleDouble target in
if Fs.file_exists sourceDouble then
Fs.rename sourceDouble targetDouble
else if Fs.file_exists targetDouble then
Fs.unlink targetDouble
end)
let symlink =
if Util.isCygwin || (Util.osType != `Win32) then
fun fspath path l ->
Util.convertUnixErrorsToTransient
"writing symbolic link"
(fun () ->
let abspath = Fspath.concat fspath path in
Fs.symlink l abspath)
else
fun fspath path l ->
raise (Util.Transient
(Format.sprintf
"Cannot create symlink \"%s\": \
symlinks are not supported under Windows"
(Fspath.toPrintString (Fspath.concat fspath path))))
(* Create a new directory, using the permissions from the given props *)
let createDir fspath path props =
Util.convertUnixErrorsToTransient
"creating directory"
(fun () ->
let absolutePath = Fspath.concat fspath path in
Fs.mkdir absolutePath (Props.perms props))
(*****************************************************************************)
(* FINGERPRINTS *)
(*****************************************************************************)
type fullfingerprint = Fingerprint.t * Fingerprint.t
let fingerprint fspath path info =
(Fingerprint.file fspath path,
Osx.ressFingerprint fspath path info.Fileinfo.osX)
let pseudoFingerprint path size =
(Fingerprint.pseudo path size, Fingerprint.dummy)
let isPseudoFingerprint (fp,rfp) =
Fingerprint.ispseudo fp
(* FIX: not completely safe under Unix *)
(* (with networked file system such as NFS) *)
let safeFingerprint fspath path info optFp =
let rec retryLoop count info optFp optRessFp =
if count = 0 then
raise (Util.Transient
(Printf.sprintf
"Failed to fingerprint file \"%s\": \
the file keeps on changing"
(Fspath.toPrintString (Fspath.concat fspath path))))
else
let fp =
match optFp with
None -> Fingerprint.file fspath path
| Some fp -> fp
in
let ressFp =
match optRessFp with
None -> Osx.ressFingerprint fspath path info.Fileinfo.osX
| Some ress -> ress
in
let (info', dataUnchanged, ressUnchanged) =
Fileinfo.unchanged fspath path info in
if dataUnchanged && ressUnchanged then
(info', (fp, ressFp))
else
retryLoop (count - 1) info'
(if dataUnchanged then Some fp else None)
(if ressUnchanged then Some ressFp else None)
in
retryLoop 10 info (* Maximum retries: 10 times *)
(match optFp with None -> None | Some (d, _) -> Some d)
None
let fullfingerprint_to_string (fp,rfp) =
Printf.sprintf "(%s,%s)" (Fingerprint.toString fp) (Fingerprint.toString rfp)
let reasonForFingerprintMismatch (fpdata,fpress) (fpdata',fpress') =
if fpdata = fpdata' then "resource fork"
else if fpress = fpress' then "file contents"
else "both file contents and resource fork"
let fullfingerprint_dummy = (Fingerprint.dummy,Fingerprint.dummy)
let fullfingerprintHash (fp, rfp) =
Fingerprint.hash fp + 31 * Fingerprint.hash rfp
let fullfingerprintEqual (fp, rfp) (fp', rfp') =
Fingerprint.equal fp fp' && Fingerprint.equal rfp rfp'
(*****************************************************************************)
(* UNISON DIRECTORY *)
(*****************************************************************************)
(* Gives the fspath of the archive directory on the machine, depending on *)
(* which OS we use *)
let unisonDir =
try
System.fspathFromString (System.getenv "UNISON")
with Not_found ->
let genericName =
Util.fileInHomeDir (Printf.sprintf ".%s" Uutil.myName) in
if Osx.isMacOSX && not (System.file_exists genericName) then
Util.fileInHomeDir "Library/Application Support/Unison"
else
genericName
(* build a fspath representing an archive child path whose name is given *)
let fileInUnisonDir str = System.fspathConcat unisonDir str
(* Make sure archive directory exists *)
let createUnisonDir() =
try ignore (System.stat unisonDir)
with Unix.Unix_error(_) ->
Util.convertUnixErrorsToFatal
(Printf.sprintf "creating unison directory %s"
(System.fspathToPrintString unisonDir))
(fun () ->
ignore (System.mkdir unisonDir 0o700))
(*****************************************************************************)
(* TEMPORARY FILES *)
(*****************************************************************************)
(* Truncate a filename to at most [l] bytes, making sure of not
truncating an UTF-8 character. Assumption: [String.length s > l] *)
let rec truncate_filename s l =
if l > 0 && Char.code s.[l] land 0xC0 = 0x80 then
truncate_filename s (l - 1)
else
String.sub s 0 l
(* We need to be careful not to use longer temp-file names than the
file system permits. eCryptfs has the lowest file name length
limit we know of, at 143 bytes. *)
let maxFileNameLength = 143
(* Generates an unused fspath for a temporary file. *)
let genTempPath fresh fspath path prefix suffix =
let rec f i =
let s =
if i=0 then suffix
else Printf.sprintf "..%03d.%s" i suffix in
let tempPath =
match Path.deconstructRev path with
None ->
assert false
| Some (name, parentPath) ->
let name = Name.toString name in
let nameLen = String.length name in
let prefixLen = String.length prefix in
let suffixLen = String.length s in
let maxLen = maxFileNameLength - prefixLen - suffixLen in
let name =
if nameLen <= maxLen then name else
let nameDigest = Digest.to_hex (Digest.string name) in
let nameDigestLen = String.length nameDigest in
let maxLen = maxLen - nameDigestLen in
assert (maxLen>0);
(truncate_filename name maxLen ^ nameDigest)
in
Path.child parentPath (Name.fromString (prefix ^ name ^ s))
in
if fresh && exists fspath tempPath then f (i + 1) else tempPath
in f 0
let tempPath ?(fresh=true) fspath path =
genTempPath fresh fspath path tempFilePrefix !tempFileSuffix
|