comparison src/fileio.c @ 272:c5d627a313b1 r21-0b34

Import from CVS: tag r21-0b34
author cvs
date Mon, 13 Aug 2007 10:28:48 +0200
parents 966663fcf606
children ca9a9ec9c1c1
comparison
equal deleted inserted replaced
271:c7b7086b0a39 272:c5d627a313b1
22 /* Synched up with: Mule 2.0, FSF 19.30. */ 22 /* Synched up with: Mule 2.0, FSF 19.30. */
23 /* More syncing: FSF Emacs 19.34.6 by Marc Paquette <marcpa@cam.org> */ 23 /* More syncing: FSF Emacs 19.34.6 by Marc Paquette <marcpa@cam.org> */
24 24
25 #include <config.h> 25 #include <config.h>
26 #include "lisp.h" 26 #include "lisp.h"
27 #include <limits.h>
27 28
28 #include "buffer.h" 29 #include "buffer.h"
29 #include "events.h" 30 #include "events.h"
30 #include "frame.h" 31 #include "frame.h"
31 #include "insdel.h" 32 #include "insdel.h"
71 filenames will sometimes compare inequal, because 72 filenames will sometimes compare inequal, because
72 `expand-file-name' doesn't always down-case the drive letter. */ 73 `expand-file-name' doesn't always down-case the drive letter. */
73 #define DRIVE_LETTER(x) (tolower (x)) 74 #define DRIVE_LETTER(x) (tolower (x))
74 #endif /* WINDOWSNT */ 75 #endif /* WINDOWSNT */
75 76
77 int lisp_to_time (Lisp_Object, time_t *);
78 Lisp_Object time_to_lisp (time_t);
79
76 /* Nonzero during writing of auto-save files */ 80 /* Nonzero during writing of auto-save files */
77 static int auto_saving; 81 static int auto_saving;
78 82
79 /* Set by auto_save_1 to mode of original file so Fwrite_region_internal 83 /* Set by auto_save_1 to mode of original file so Fwrite_region_internal
80 will create a new file with the same mode as the original */ 84 will create a new file with the same mode as the original */
288 as losing as I thought. See sys_do_signal() in sysdep.c.) 292 as losing as I thought. See sys_do_signal() in sysdep.c.)
289 293
290 Solaris include files declare the return value as ssize_t. 294 Solaris include files declare the return value as ssize_t.
291 Is that standard? */ 295 Is that standard? */
292 int 296 int
293 read_allowing_quit (int fildes, void *buf, unsigned int nbyte) 297 read_allowing_quit (int fildes, void *buf, size_t size)
294 { 298 {
295 int nread;
296 QUIT; 299 QUIT;
297 300 return sys_read_1 (fildes, buf, size, 1);
298 nread = sys_read_1 (fildes, buf, nbyte, 1);
299 return nread;
300 } 301 }
301 302
302 int 303 int
303 write_allowing_quit (int fildes, CONST void *buf, unsigned int nbyte) 304 write_allowing_quit (int fildes, CONST void *buf, size_t size)
304 { 305 {
305 int nread;
306
307 QUIT; 306 QUIT;
308 nread = sys_write_1 (fildes, buf, nbyte, 1); 307 return sys_write_1 (fildes, buf, size, 1);
309 return nread;
310 } 308 }
311 309
312 310
313 Lisp_Object Qexpand_file_name; 311 Lisp_Object Qexpand_file_name;
314 Lisp_Object Qfile_truename; 312 Lisp_Object Qfile_truename;
510 DEFUN ("unhandled-file-name-directory", Funhandled_file_name_directory, 1, 1, 0, /* 508 DEFUN ("unhandled-file-name-directory", Funhandled_file_name_directory, 1, 1, 0, /*
511 Return a directly usable directory name somehow associated with FILENAME. 509 Return a directly usable directory name somehow associated with FILENAME.
512 A `directly usable' directory name is one that may be used without the 510 A `directly usable' directory name is one that may be used without the
513 intervention of any file handler. 511 intervention of any file handler.
514 If FILENAME is a directly usable file itself, return 512 If FILENAME is a directly usable file itself, return
515 (file-name-directory FILENAME). 513 \(file-name-directory FILENAME).
516 The `call-process' and `start-process' functions use this function to 514 The `call-process' and `start-process' functions use this function to
517 get a current directory to run processes in. 515 get a current directory to run processes in.
518 */ 516 */
519 (filename)) 517 (filename))
520 { 518 {
869 } 867 }
870 return name; 868 return name;
871 #else /* not WINDOWSNT */ 869 #else /* not WINDOWSNT */
872 if (nm == XSTRING_DATA (name)) 870 if (nm == XSTRING_DATA (name))
873 return name; 871 return name;
874 return build_string (nm); 872 return build_string ((char *) nm);
875 #endif /* not WINDOWSNT */ 873 #endif /* not WINDOWSNT */
876 } 874 }
877 } 875 }
878 876
879 /* At this point, nm might or might not be an absolute file name. We 877 /* At this point, nm might or might not be an absolute file name. We
1054 1052
1055 if (newdir) 1053 if (newdir)
1056 { 1054 {
1057 /* Get rid of any slash at the end of newdir, unless newdir is 1055 /* Get rid of any slash at the end of newdir, unless newdir is
1058 just // (an incomplete UNC name). */ 1056 just // (an incomplete UNC name). */
1059 length = strlen (newdir); 1057 length = strlen ((char *) newdir);
1060 if (length > 0 && IS_DIRECTORY_SEP (newdir[length - 1]) 1058 if (length > 0 && IS_DIRECTORY_SEP (newdir[length - 1])
1061 #ifdef WINDOWSNT 1059 #ifdef WINDOWSNT
1062 && !(length == 2 && IS_DIRECTORY_SEP (newdir[0])) 1060 && !(length == 2 && IS_DIRECTORY_SEP (newdir[0]))
1063 #endif 1061 #endif
1064 ) 1062 )
1072 } 1070 }
1073 else 1071 else
1074 tlen = 0; 1072 tlen = 0;
1075 1073
1076 /* Now concatenate the directory and name to new space in the stack frame */ 1074 /* Now concatenate the directory and name to new space in the stack frame */
1077 tlen += strlen (nm) + 1; 1075 tlen += strlen ((char *) nm) + 1;
1078 #ifdef WINDOWSNT 1076 #ifdef WINDOWSNT
1079 /* Add reserved space for drive name. (The Microsoft x86 compiler 1077 /* Add reserved space for drive name. (The Microsoft x86 compiler
1080 produces incorrect code if the following two lines are combined.) */ 1078 produces incorrect code if the following two lines are combined.) */
1081 target = (Bufbyte *) alloca (tlen + 2); 1079 target = (Bufbyte *) alloca (tlen + 2);
1082 target += 2; 1080 target += 2;
1086 *target = 0; 1084 *target = 0;
1087 1085
1088 if (newdir) 1086 if (newdir)
1089 { 1087 {
1090 if (nm[0] == 0 || IS_DIRECTORY_SEP (nm[0])) 1088 if (nm[0] == 0 || IS_DIRECTORY_SEP (nm[0]))
1091 strcpy (target, newdir); 1089 strcpy ((char *) target, (char *) newdir);
1092 else 1090 else
1093 file_name_as_directory (target, newdir); 1091 file_name_as_directory ((char *) target, (char *) newdir);
1094 } 1092 }
1095 1093
1096 strcat (target, nm); 1094 strcat ((char *) target, (char *) nm);
1097 1095
1098 /* ASSERT (IS_DIRECTORY_SEP (target[0])) if not VMS */ 1096 /* ASSERT (IS_DIRECTORY_SEP (target[0])) if not VMS */
1099 1097
1100 /* Now canonicalize by removing /. and /foo/.. if they appear. */ 1098 /* Now canonicalize by removing /. and /foo/.. if they appear. */
1101 1099
1726 handler = Ffind_file_name_handler (dirname_, Qmake_directory_internal); 1724 handler = Ffind_file_name_handler (dirname_, Qmake_directory_internal);
1727 UNGCPRO; 1725 UNGCPRO;
1728 if (!NILP (handler)) 1726 if (!NILP (handler))
1729 return (call2 (handler, Qmake_directory_internal, dirname_)); 1727 return (call2 (handler, Qmake_directory_internal, dirname_));
1730 1728
1731 if (XSTRING_LENGTH (dirname_) > (sizeof (dir) - 1)) 1729 if (XSTRING_LENGTH (dirname_) > (Bytecount) (sizeof (dir) - 1))
1732 { 1730 {
1733 return Fsignal (Qfile_error, 1731 return Fsignal (Qfile_error,
1734 list3 (build_translated_string ("Creating directory"), 1732 list3 (build_translated_string ("Creating directory"),
1735 build_translated_string ("pathame too long"), 1733 build_translated_string ("pathame too long"),
1736 dirname_)); 1734 dirname_));
2061 /* This function does not GC */ 2059 /* This function does not GC */
2062 Bufbyte *ptr; 2060 Bufbyte *ptr;
2063 2061
2064 CHECK_STRING (filename); 2062 CHECK_STRING (filename);
2065 ptr = XSTRING_DATA (filename); 2063 ptr = XSTRING_DATA (filename);
2066 if (IS_DIRECTORY_SEP (*ptr) || *ptr == '~' 2064 return (IS_DIRECTORY_SEP (*ptr) || *ptr == '~'
2067 #ifdef WINDOWSNT 2065 #ifdef WINDOWSNT
2068 || (IS_DRIVE (*ptr) && ptr[1] == ':' && IS_DIRECTORY_SEP (ptr[2])) 2066 || (IS_DRIVE (*ptr) && ptr[1] == ':' && IS_DIRECTORY_SEP (ptr[2]))
2069 #endif 2067 #endif
2070 ) 2068 ) ? Qt : Qnil;
2071 return Qt;
2072 else
2073 return Qnil;
2074 } 2069 }
2075 2070
2076 /* Return nonzero if file FILENAME exists and can be executed. */ 2071 /* Return nonzero if file FILENAME exists and can be executed. */
2077 2072
2078 static int 2073 static int
2133 handler = Ffind_file_name_handler (abspath, Qfile_exists_p); 2128 handler = Ffind_file_name_handler (abspath, Qfile_exists_p);
2134 UNGCPRO; 2129 UNGCPRO;
2135 if (!NILP (handler)) 2130 if (!NILP (handler))
2136 return call2 (handler, Qfile_exists_p, abspath); 2131 return call2 (handler, Qfile_exists_p, abspath);
2137 2132
2138 if (stat ((char *) XSTRING_DATA (abspath), &statbuf) >= 0) 2133 return stat ((char *) XSTRING_DATA (abspath), &statbuf) >= 0 ? Qt : Qnil;
2139 return (Qt);
2140 else
2141 return (Qnil);
2142 } 2134 }
2143 2135
2144 DEFUN ("file-executable-p", Ffile_executable_p, 1, 1, 0, /* 2136 DEFUN ("file-executable-p", Ffile_executable_p, 1, 1, 0, /*
2145 Return t if FILENAME can be executed by you. 2137 Return t if FILENAME can be executed by you.
2146 For a directory, this means you can access files in that directory. 2138 For a directory, this means you can access files in that directory.
2162 handler = Ffind_file_name_handler (abspath, Qfile_executable_p); 2154 handler = Ffind_file_name_handler (abspath, Qfile_executable_p);
2163 UNGCPRO; 2155 UNGCPRO;
2164 if (!NILP (handler)) 2156 if (!NILP (handler))
2165 return call2 (handler, Qfile_executable_p, abspath); 2157 return call2 (handler, Qfile_executable_p, abspath);
2166 2158
2167 return (check_executable ((char *) XSTRING_DATA (abspath)) 2159 return check_executable ((char *) XSTRING_DATA (abspath)) ? Qt : Qnil;
2168 ? Qt : Qnil);
2169 } 2160 }
2170 2161
2171 DEFUN ("file-readable-p", Ffile_readable_p, 1, 1, 0, /* 2162 DEFUN ("file-readable-p", Ffile_readable_p, 1, 1, 0, /*
2172 Return t if file FILENAME exists and you can read it. 2163 Return t if file FILENAME exists and you can read it.
2173 See also `file-exists-p' and `file-attributes'. 2164 See also `file-exists-p' and `file-attributes'.
2784 curpos = st.st_size - (BUF_ZV (buf) - same_at_end); 2775 curpos = st.st_size - (BUF_ZV (buf) - same_at_end);
2785 /* If the entire file matches the buffer tail, stop the scan. */ 2776 /* If the entire file matches the buffer tail, stop the scan. */
2786 if (curpos == 0) 2777 if (curpos == 0)
2787 break; 2778 break;
2788 /* How much can we scan in the next step? */ 2779 /* How much can we scan in the next step? */
2789 trial = min (curpos, sizeof buffer); 2780 trial = min (curpos, (Bufpos) sizeof (buffer));
2790 if (lseek (fd, curpos - trial, 0) < 0) 2781 if (lseek (fd, curpos - trial, 0) < 0)
2791 report_file_error ("Setting file position", list1 (filename)); 2782 report_file_error ("Setting file position", list1 (filename));
2792 2783
2793 total_read = 0; 2784 total_read = 0;
2794 while (total_read < trial) 2785 while (total_read < trial)
2947 if (!NILP (Ffboundp (Qcompute_buffer_file_truename))) 2938 if (!NILP (Ffboundp (Qcompute_buffer_file_truename)))
2948 call1 (Qcompute_buffer_file_truename, make_buffer (buf)); 2939 call1 (Qcompute_buffer_file_truename, make_buffer (buf));
2949 } 2940 }
2950 BUF_SAVE_MODIFF (buf) = BUF_MODIFF (buf); 2941 BUF_SAVE_MODIFF (buf) = BUF_MODIFF (buf);
2951 buf->auto_save_modified = BUF_MODIFF (buf); 2942 buf->auto_save_modified = BUF_MODIFF (buf);
2952 buf->save_length = make_int (BUF_SIZE (buf)); 2943 buf->saved_size = make_int (BUF_SIZE (buf));
2953 #ifdef CLASH_DETECTION 2944 #ifdef CLASH_DETECTION
2954 if (NILP (handler)) 2945 if (NILP (handler))
2955 { 2946 {
2956 if (!NILP (buf->file_truename)) 2947 if (!NILP (buf->file_truename))
2957 unlock_file (buf->file_truename); 2948 unlock_file (buf->file_truename);
3098 Lisp_Object val = call8 (handler, Qwrite_region, start, end, 3089 Lisp_Object val = call8 (handler, Qwrite_region, start, end,
3099 filename, append, visit, lockname, codesys); 3090 filename, append, visit, lockname, codesys);
3100 if (visiting) 3091 if (visiting)
3101 { 3092 {
3102 BUF_SAVE_MODIFF (current_buffer) = BUF_MODIFF (current_buffer); 3093 BUF_SAVE_MODIFF (current_buffer) = BUF_MODIFF (current_buffer);
3103 current_buffer->save_length = 3094 current_buffer->saved_size = make_int (BUF_SIZE (current_buffer));
3104 make_int (BUF_SIZE (current_buffer));
3105 current_buffer->filename = visit_file; 3095 current_buffer->filename = visit_file;
3106 MARK_MODELINE_CHANGED; 3096 MARK_MODELINE_CHANGED;
3107 } 3097 }
3108 return val; 3098 return val;
3109 } 3099 }
3289 strerror (save_errno)); 3279 strerror (save_errno));
3290 3280
3291 if (visiting) 3281 if (visiting)
3292 { 3282 {
3293 BUF_SAVE_MODIFF (current_buffer) = BUF_MODIFF (current_buffer); 3283 BUF_SAVE_MODIFF (current_buffer) = BUF_MODIFF (current_buffer);
3294 current_buffer->save_length = make_int (BUF_SIZE (current_buffer)); 3284 current_buffer->saved_size = make_int (BUF_SIZE (current_buffer));
3295 current_buffer->filename = visit_file; 3285 current_buffer->filename = visit_file;
3296 MARK_MODELINE_CHANGED; 3286 MARK_MODELINE_CHANGED;
3297 } 3287 }
3298 else if (quietly) 3288 else if (quietly)
3299 { 3289 {
3434 int nextpos; 3424 int nextpos;
3435 unsigned char largebuf[A_WRITE_BATCH_SIZE]; 3425 unsigned char largebuf[A_WRITE_BATCH_SIZE];
3436 Lstream *instr = XLSTREAM (instream); 3426 Lstream *instr = XLSTREAM (instream);
3437 Lstream *outstr = XLSTREAM (outstream); 3427 Lstream *outstr = XLSTREAM (outstream);
3438 3428
3439 while (NILP (*annot) || CONSP (*annot)) 3429 while (LISTP (*annot))
3440 { 3430 {
3441 tem = Fcar_safe (Fcar (*annot)); 3431 tem = Fcar_safe (Fcar (*annot));
3442 if (INTP (tem)) 3432 if (INTP (tem))
3443 nextpos = XINT (tem); 3433 nextpos = XINT (tem);
3444 else 3434 else
3623 DEFUN ("set-visited-file-modtime", Fset_visited_file_modtime, 0, 1, 0, /* 3613 DEFUN ("set-visited-file-modtime", Fset_visited_file_modtime, 0, 1, 0, /*
3624 Update buffer's recorded modification time from the visited file's time. 3614 Update buffer's recorded modification time from the visited file's time.
3625 Useful if the buffer was not read from the file normally 3615 Useful if the buffer was not read from the file normally
3626 or if the file itself has been changed for some known benign reason. 3616 or if the file itself has been changed for some known benign reason.
3627 An argument specifies the modification time value to use 3617 An argument specifies the modification time value to use
3628 (instead of that of the visited file), in the form of a list 3618 \(instead of that of the visited file), in the form of a list
3629 (HIGH . LOW) or (HIGH LOW). 3619 \(HIGH . LOW) or (HIGH LOW).
3630 */ 3620 */
3631 (time_list)) 3621 (time_list))
3632 { 3622 {
3633 /* This function can call lisp */ 3623 /* This function can call lisp */
3634 if (!NILP (time_list)) 3624 if (!NILP (time_list))
3839 and file changed since last real save. */ 3829 and file changed since last real save. */
3840 if (GC_STRINGP (b->auto_save_file_name) 3830 if (GC_STRINGP (b->auto_save_file_name)
3841 && BUF_SAVE_MODIFF (b) < BUF_MODIFF (b) 3831 && BUF_SAVE_MODIFF (b) < BUF_MODIFF (b)
3842 && b->auto_save_modified < BUF_MODIFF (b) 3832 && b->auto_save_modified < BUF_MODIFF (b)
3843 /* -1 means we've turned off autosaving for a while--see below. */ 3833 /* -1 means we've turned off autosaving for a while--see below. */
3844 && XINT (b->save_length) >= 0 3834 && XINT (b->saved_size) >= 0
3845 && (do_handled_files 3835 && (do_handled_files
3846 || NILP (Ffind_file_name_handler (b->auto_save_file_name, 3836 || NILP (Ffind_file_name_handler (b->auto_save_file_name,
3847 Qwrite_region)))) 3837 Qwrite_region))))
3848 { 3838 {
3849 EMACS_TIME before_time, after_time; 3839 EMACS_TIME before_time, after_time;
3855 && (EMACS_SECS (before_time) - b->auto_save_failure_time < 3845 && (EMACS_SECS (before_time) - b->auto_save_failure_time <
3856 1200)) 3846 1200))
3857 continue; 3847 continue;
3858 3848
3859 if (!preparing_for_armageddon && 3849 if (!preparing_for_armageddon &&
3860 (XINT (b->save_length) * 10 3850 (XINT (b->saved_size) * 10
3861 > (BUF_Z (b) - BUF_BEG (b)) * 13) 3851 > (BUF_Z (b) - BUF_BEG (b)) * 13)
3862 /* A short file is likely to change a large fraction; 3852 /* A short file is likely to change a large fraction;
3863 spare the user annoying messages. */ 3853 spare the user annoying messages. */
3864 && XINT (b->save_length) > 5000 3854 && XINT (b->saved_size) > 5000
3865 /* These messages are frequent and annoying for `*mail*'. */ 3855 /* These messages are frequent and annoying for `*mail*'. */
3866 && !EQ (b->filename, Qnil) 3856 && !NILP (b->filename)
3867 && NILP (no_message) 3857 && NILP (no_message)
3868 && disable_auto_save_when_buffer_shrinks) 3858 && disable_auto_save_when_buffer_shrinks)
3869 { 3859 {
3870 /* It has shrunk too much; turn off auto-saving here. 3860 /* It has shrunk too much; turn off auto-saving here.
3871 Unless we're about to crash, in which case auto-save it 3861 Unless we're about to crash, in which case auto-save it
3874 message 3864 message
3875 ("Buffer %s has shrunk a lot; auto save turned off there", 3865 ("Buffer %s has shrunk a lot; auto save turned off there",
3876 XSTRING_DATA (b->name)); 3866 XSTRING_DATA (b->name));
3877 /* Turn off auto-saving until there's a real save, 3867 /* Turn off auto-saving until there's a real save,
3878 and prevent any more warnings. */ 3868 and prevent any more warnings. */
3879 b->save_length = make_int (-1); 3869 b->saved_size = make_int (-1);
3880 if (!gc_in_progress) 3870 if (!gc_in_progress)
3881 Fsleep_for (make_int (1)); 3871 Fsleep_for (make_int (1));
3882 continue; 3872 continue;
3883 } 3873 }
3884 set_buffer_internal (b); 3874 set_buffer_internal (b);
3963 /* Handler killed their own buffer! */ 3953 /* Handler killed their own buffer! */
3964 if (!BUFFER_LIVE_P(b)) 3954 if (!BUFFER_LIVE_P(b))
3965 continue; 3955 continue;
3966 3956
3967 b->auto_save_modified = BUF_MODIFF (b); 3957 b->auto_save_modified = BUF_MODIFF (b);
3968 b->save_length = make_int (BUF_SIZE (b)); 3958 b->saved_size = make_int (BUF_SIZE (b));
3969 EMACS_GET_TIME (after_time); 3959 EMACS_GET_TIME (after_time);
3970 /* If auto-save took more than 60 seconds, 3960 /* If auto-save took more than 60 seconds,
3971 assume it was an NFS failure that got a timeout. */ 3961 assume it was an NFS failure that got a timeout. */
3972 if (EMACS_SECS (after_time) - EMACS_SECS (before_time) > 60) 3962 if (EMACS_SECS (after_time) - EMACS_SECS (before_time) > 60)
3973 b->auto_save_failure_time = EMACS_SECS (after_time); 3963 b->auto_save_failure_time = EMACS_SECS (after_time);
4006 No auto-save file will be written until the buffer changes again. 3996 No auto-save file will be written until the buffer changes again.
4007 */ 3997 */
4008 ()) 3998 ())
4009 { 3999 {
4010 current_buffer->auto_save_modified = BUF_MODIFF (current_buffer); 4000 current_buffer->auto_save_modified = BUF_MODIFF (current_buffer);
4011 current_buffer->save_length = make_int (BUF_SIZE (current_buffer)); 4001 current_buffer->saved_size = make_int (BUF_SIZE (current_buffer));
4012 current_buffer->auto_save_failure_time = -1; 4002 current_buffer->auto_save_failure_time = -1;
4013 return Qnil; 4003 return Qnil;
4014 } 4004 }
4015 4005
4016 DEFUN ("clear-buffer-auto-save-failure", Fclear_buffer_auto_save_failure, 0, 0, 0, /* 4006 DEFUN ("clear-buffer-auto-save-failure", Fclear_buffer_auto_save_failure, 0, 0, 0, /*