+ let title = if string_is_whitespace title then `Null else `String title in
+
+ (* Check if something with the same name already exists. If replace=1
+ * then we can replace it, otherwise we must present an error message.
+ *)
+ let replace = q#param_true "replace" in
+ let sth = dbh#prepare_cached "select 1 from files
+ where hostid = ? and name = ?" in
+ sth#execute [`Int hostid; `String name];
+
+ let exists = try sth#fetch1int () = 1 with Not_found -> false in
+
+ if exists then (
+ if not replace then (
+ error ~title:"File already exists" ~back_button:true
+ q ("An file with the same name already exists.");
+ return ()
+ ) else (
+ let sth = dbh#prepare_cached "update files
+ set name_deleted = name, name = null
+ where hostid = ? and name = ?" in
+ sth#execute [`Int hostid; `String name];
+ )
+ );