comparison src/fileio.c @ 70960:d876c40c06dd

* fileio.c (Fcopy_file): Delete argument MUSTBENEW. Incorporate the exclusive file-opening functionality into the behavior when OK-IF-ALREADY-EXISTS is nil. (Frename_file): Call Fcopy_file without MUSTBENEW argument.
author Chong Yidong <cyd@stupidchicken.com>
date Fri, 26 May 2006 15:18:28 +0000
parents e963ced8a1ca
children 2414d21c77fa 46b1096093f5 a8190f7e546e
comparison
equal deleted inserted replaced
70959:23bc72ff5e31 70960:d876c40c06dd
2402 statptr->st_mode = 0; 2402 statptr->st_mode = 0;
2403 } 2403 }
2404 return; 2404 return;
2405 } 2405 }
2406 2406
2407 DEFUN ("copy-file", Fcopy_file, Scopy_file, 2, 6, 2407 DEFUN ("copy-file", Fcopy_file, Scopy_file, 2, 5,
2408 "fCopy file: \nGCopy %s to file: \np\nP", 2408 "fCopy file: \nGCopy %s to file: \np\nP",
2409 doc: /* Copy FILE to NEWNAME. Both args must be strings. 2409 doc: /* Copy FILE to NEWNAME. Both args must be strings.
2410 If NEWNAME names a directory, copy FILE there. 2410 If NEWNAME names a directory, copy FILE there.
2411 Signals a `file-already-exists' error if file NEWNAME already exists, 2411
2412 unless a third argument OK-IF-ALREADY-EXISTS is supplied and non-nil. 2412 This function always sets the file modes of the output file to match
2413 A number as third arg means request confirmation if NEWNAME already exists. 2413 the input file.
2414 This is what happens in interactive use with M-x. 2414
2415 Always sets the file modes of the output file to match the input file. 2415 The optional third argument OK-IF-ALREADY-EXISTS specifies what to do
2416 if file NEWNAME already exists. If OK-IF-ALREADY-EXISTS is nil, we
2417 signal a `file-already-exists' error without overwriting. If
2418 OK-IF-ALREADY-EXISTS is a number, we request confirmation from the user
2419 about overwriting; this is what happens in interactive use with M-x.
2420 Any other value for OK-IF-ALREADY-EXISTS means to overwrite the
2421 existing file.
2416 2422
2417 Fourth arg KEEP-TIME non-nil means give the output file the same 2423 Fourth arg KEEP-TIME non-nil means give the output file the same
2418 last-modified time as the old one. (This works on only some systems.) 2424 last-modified time as the old one. (This works on only some systems.)
2419 2425
2420 A prefix arg makes KEEP-TIME non-nil. 2426 A prefix arg makes KEEP-TIME non-nil.
2421 2427
2422 The optional fifth arg MUSTBENEW, if non-nil, insists on a check
2423 for an existing file with the same name. If MUSTBENEW is `excl',
2424 that means to get an error if the file already exists; never overwrite.
2425 If MUSTBENEW is neither nil nor `excl', that means ask for
2426 confirmation before overwriting, but do go ahead and overwrite the file
2427 if the user confirms.
2428
2429 If PRESERVE-UID-GID is non-nil, we try to transfer the 2428 If PRESERVE-UID-GID is non-nil, we try to transfer the
2430 uid and gid of FILE to NEWNAME. */) 2429 uid and gid of FILE to NEWNAME. */)
2431 (file, newname, ok_if_already_exists, keep_time, mustbenew, preserve_uid_gid) 2430 (file, newname, ok_if_already_exists, keep_time, preserve_uid_gid)
2432 Lisp_Object file, newname, ok_if_already_exists, keep_time, mustbenew; 2431 Lisp_Object file, newname, ok_if_already_exists, keep_time;
2433 Lisp_Object preserve_uid_gid; 2432 Lisp_Object preserve_uid_gid;
2434 { 2433 {
2435 int ifd, ofd, n; 2434 int ifd, ofd, n;
2436 char buf[16 * 1024]; 2435 char buf[16 * 1024];
2437 struct stat st, out_st; 2436 struct stat st, out_st;
2443 2442
2444 encoded_file = encoded_newname = Qnil; 2443 encoded_file = encoded_newname = Qnil;
2445 GCPRO4 (file, newname, encoded_file, encoded_newname); 2444 GCPRO4 (file, newname, encoded_file, encoded_newname);
2446 CHECK_STRING (file); 2445 CHECK_STRING (file);
2447 CHECK_STRING (newname); 2446 CHECK_STRING (newname);
2448
2449 if (!NILP (mustbenew) && !EQ (mustbenew, Qexcl))
2450 barf_or_query_if_file_exists (newname, "overwrite", 1, 0, 1);
2451 2447
2452 if (!NILP (Ffile_directory_p (newname))) 2448 if (!NILP (Ffile_directory_p (newname)))
2453 newname = Fexpand_file_name (Ffile_name_nondirectory (file), newname); 2449 newname = Fexpand_file_name (Ffile_name_nondirectory (file), newname);
2454 else 2450 else
2455 newname = Fexpand_file_name (newname, Qnil); 2451 newname = Fexpand_file_name (newname, Qnil);
2549 #else 2545 #else
2550 #ifdef MSDOS 2546 #ifdef MSDOS
2551 /* System's default file type was set to binary by _fmode in emacs.c. */ 2547 /* System's default file type was set to binary by _fmode in emacs.c. */
2552 ofd = emacs_open (SDATA (encoded_newname), 2548 ofd = emacs_open (SDATA (encoded_newname),
2553 O_WRONLY | O_TRUNC | O_CREAT 2549 O_WRONLY | O_TRUNC | O_CREAT
2554 | (EQ (mustbenew, Qexcl) ? O_EXCL : 0), 2550 | (NILP (ok_if_already_exists) ? O_EXCL : 0),
2555 S_IREAD | S_IWRITE); 2551 S_IREAD | S_IWRITE);
2556 #else /* not MSDOS */ 2552 #else /* not MSDOS */
2557 ofd = emacs_open (SDATA (encoded_newname), 2553 ofd = emacs_open (SDATA (encoded_newname),
2558 O_WRONLY | O_TRUNC | O_CREAT 2554 O_WRONLY | O_TRUNC | O_CREAT
2559 | (EQ (mustbenew, Qexcl) ? O_EXCL : 0), 2555 | (NILP (ok_if_already_exists) ? O_EXCL : 0),
2560 0666); 2556 0666);
2561 #endif /* not MSDOS */ 2557 #endif /* not MSDOS */
2562 #endif /* VMS */ 2558 #endif /* VMS */
2563 if (ofd < 0) 2559 if (ofd < 0)
2564 report_file_error ("Opening output file", Fcons (newname, Qnil)); 2560 report_file_error ("Opening output file", Fcons (newname, Qnil));
2799 #endif 2795 #endif
2800 Fcopy_file (file, newname, 2796 Fcopy_file (file, newname,
2801 /* We have already prompted if it was an integer, 2797 /* We have already prompted if it was an integer,
2802 so don't have copy-file prompt again. */ 2798 so don't have copy-file prompt again. */
2803 NILP (ok_if_already_exists) ? Qnil : Qt, 2799 NILP (ok_if_already_exists) ? Qnil : Qt,
2804 Qt, Qnil, Qt); 2800 Qt, Qt);
2805 2801
2806 Fdelete_file (file); 2802 Fdelete_file (file);
2807 } 2803 }
2808 else 2804 else
2809 #ifdef NO_ARG_ARRAY 2805 #ifdef NO_ARG_ARRAY