comparison src/fileio.c @ 107845:688679bd79f5

Try to detect file modification within the same second. * buffer.h (struct buffer): New field modtime_size. * buffer.c (reset_buffer): Initialize it. * fileio.c (Finsert_file_contents, Fwrite_region): Set it. (Fverify_visited_file_modtime): Check it. (Fclear_visited_file_modtime, Fset_visited_file_modtime): Clear it. (Fset_visited_file_modtime): Set (or clear) it.
author Stefan Monnier <monnier@iro.umontreal.ca>
date Mon, 12 Apr 2010 21:47:40 -0400
parents 912a8c0c9a8a
children 12062a99ca8d
comparison
equal deleted inserted replaced
107844:17333bba44b4 107845:688679bd79f5
4090 current_buffer->undo_list = Qnil; 4090 current_buffer->undo_list = Qnil;
4091 4091
4092 if (NILP (handler)) 4092 if (NILP (handler))
4093 { 4093 {
4094 current_buffer->modtime = st.st_mtime; 4094 current_buffer->modtime = st.st_mtime;
4095 current_buffer->modtime_size = st.st_size;
4095 current_buffer->filename = orig_filename; 4096 current_buffer->filename = orig_filename;
4096 } 4097 }
4097 4098
4098 SAVE_MODIFF = MODIFF; 4099 SAVE_MODIFF = MODIFF;
4099 BUF_AUTOSAVE_MODIFF (current_buffer) = MODIFF; 4100 BUF_AUTOSAVE_MODIFF (current_buffer) = MODIFF;
4693 4694
4694 /* Do this before reporting IO error 4695 /* Do this before reporting IO error
4695 to avoid a "file has changed on disk" warning on 4696 to avoid a "file has changed on disk" warning on
4696 next attempt to save. */ 4697 next attempt to save. */
4697 if (visiting) 4698 if (visiting)
4698 current_buffer->modtime = st.st_mtime; 4699 {
4700 current_buffer->modtime = st.st_mtime;
4701 current_buffer->modtime_size = st.st_size;
4702 }
4699 4703
4700 if (failure) 4704 if (failure)
4701 error ("IO error writing %s: %s", SDATA (filename), 4705 error ("IO error writing %s: %s", SDATA (filename),
4702 emacs_strerror (save_errno)); 4706 emacs_strerror (save_errno));
4703 4707
5002 if (errno == ENOENT || errno == EACCES || errno == ENOTDIR) 5006 if (errno == ENOENT || errno == EACCES || errno == ENOTDIR)
5003 st.st_mtime = -1; 5007 st.st_mtime = -1;
5004 else 5008 else
5005 st.st_mtime = 0; 5009 st.st_mtime = 0;
5006 } 5010 }
5007 if (st.st_mtime == b->modtime 5011 if ((st.st_mtime == b->modtime
5008 /* If both are positive, accept them if they are off by one second. */ 5012 /* If both are positive, accept them if they are off by one second. */
5009 || (st.st_mtime > 0 && b->modtime > 0 5013 || (st.st_mtime > 0 && b->modtime > 0
5010 && (st.st_mtime == b->modtime + 1 5014 && (st.st_mtime == b->modtime + 1
5011 || st.st_mtime == b->modtime - 1))) 5015 || st.st_mtime == b->modtime - 1)))
5016 && (st.st_size == b->modtime_size
5017 || b->modtime_size < 0))
5012 return Qt; 5018 return Qt;
5013 return Qnil; 5019 return Qnil;
5014 } 5020 }
5015 5021
5016 DEFUN ("clear-visited-file-modtime", Fclear_visited_file_modtime, 5022 DEFUN ("clear-visited-file-modtime", Fclear_visited_file_modtime,
5018 doc: /* Clear out records of last mod time of visited file. 5024 doc: /* Clear out records of last mod time of visited file.
5019 Next attempt to save will certainly not complain of a discrepancy. */) 5025 Next attempt to save will certainly not complain of a discrepancy. */)
5020 () 5026 ()
5021 { 5027 {
5022 current_buffer->modtime = 0; 5028 current_buffer->modtime = 0;
5029 current_buffer->modtime_size = -1;
5023 return Qnil; 5030 return Qnil;
5024 } 5031 }
5025 5032
5026 DEFUN ("visited-file-modtime", Fvisited_file_modtime, 5033 DEFUN ("visited-file-modtime", Fvisited_file_modtime,
5027 Svisited_file_modtime, 0, 0, 0, 5034 Svisited_file_modtime, 0, 0, 0,
5047 \(HIGH . LOW) or (HIGH LOW). */) 5054 \(HIGH . LOW) or (HIGH LOW). */)
5048 (time_list) 5055 (time_list)
5049 Lisp_Object time_list; 5056 Lisp_Object time_list;
5050 { 5057 {
5051 if (!NILP (time_list)) 5058 if (!NILP (time_list))
5052 current_buffer->modtime = cons_to_long (time_list); 5059 {
5060 current_buffer->modtime = cons_to_long (time_list);
5061 current_buffer->modtime_size = -1;
5062 }
5053 else 5063 else
5054 { 5064 {
5055 register Lisp_Object filename; 5065 register Lisp_Object filename;
5056 struct stat st; 5066 struct stat st;
5057 Lisp_Object handler; 5067 Lisp_Object handler;
5066 return call2 (handler, Qset_visited_file_modtime, Qnil); 5076 return call2 (handler, Qset_visited_file_modtime, Qnil);
5067 5077
5068 filename = ENCODE_FILE (filename); 5078 filename = ENCODE_FILE (filename);
5069 5079
5070 if (stat (SDATA (filename), &st) >= 0) 5080 if (stat (SDATA (filename), &st) >= 0)
5071 current_buffer->modtime = st.st_mtime; 5081 {
5082 current_buffer->modtime = st.st_mtime;
5083 current_buffer->modtime_size = st.st_size;
5084 }
5072 } 5085 }
5073 5086
5074 return Qnil; 5087 return Qnil;
5075 } 5088 }
5076 5089