Mercurial > vim
annotate src/memline.c @ 6122:18ac55444b37 v7.4.399
updated for version 7.4.399
Problem: Encryption implementation is messy. Blowfish encryption has a
weakness.
Solution: Refactor the encryption, store the state in an allocated struct
instead of using a save/restore mechanism. Introduce the
"blowfish2" method, which does not have the weakness and encrypts
the whole undo file. (largely by David Leadbeater)
author | Bram Moolenaar <bram@vim.org> |
---|---|
date | Sun, 10 Aug 2014 13:38:34 +0200 |
parents | e25a04c1c515 |
children | 86fb698a38d5 |
rev | line source |
---|---|
7 | 1 /* vi:set ts=8 sts=4 sw=4: |
2 * | |
3 * VIM - Vi IMproved by Bram Moolenaar | |
4 * | |
5 * Do ":help uganda" in Vim to read copying and usage conditions. | |
6 * Do ":help credits" in Vim to see a list of people who contributed. | |
7 * See README.txt for an overview of the Vim source code. | |
8 */ | |
9 | |
10 /* for debugging */ | |
11 /* #define CHECK(c, s) if (c) EMSG(s) */ | |
12 #define CHECK(c, s) | |
13 | |
14 /* | |
15 * memline.c: Contains the functions for appending, deleting and changing the | |
625 | 16 * text lines. The memfile functions are used to store the information in |
17 * blocks of memory, backed up by a file. The structure of the information is | |
18 * a tree. The root of the tree is a pointer block. The leaves of the tree | |
19 * are data blocks. In between may be several layers of pointer blocks, | |
20 * forming branches. | |
7 | 21 * |
22 * Three types of blocks are used: | |
23 * - Block nr 0 contains information for recovery | |
24 * - Pointer blocks contain list of pointers to other blocks. | |
25 * - Data blocks contain the actual text. | |
26 * | |
27 * Block nr 0 contains the block0 structure (see below). | |
28 * | |
29 * Block nr 1 is the first pointer block. It is the root of the tree. | |
30 * Other pointer blocks are branches. | |
31 * | |
32 * If a line is too big to fit in a single page, the block containing that | |
33 * line is made big enough to hold the line. It may span several pages. | |
34 * Otherwise all blocks are one page. | |
35 * | |
36 * A data block that was filled when starting to edit a file and was not | |
37 * changed since then, can have a negative block number. This means that it | |
38 * has not yet been assigned a place in the file. When recovering, the lines | |
39 * in this data block can be read from the original file. When the block is | |
40 * changed (lines appended/deleted/changed) or when it is flushed it gets a | |
41 * positive number. Use mf_trans_del() to get the new number, before calling | |
42 * mf_get(). | |
43 */ | |
44 | |
45 #include "vim.h" | |
46 | |
47 #ifndef UNIX /* it's in os_unix.h for Unix */ | |
48 # include <time.h> | |
49 #endif | |
50 | |
1030 | 51 #if defined(SASC) || defined(__amigaos4__) |
7 | 52 # include <proto/dos.h> /* for Open() and Close() */ |
53 #endif | |
54 | |
55 typedef struct block0 ZERO_BL; /* contents of the first block */ | |
56 typedef struct pointer_block PTR_BL; /* contents of a pointer block */ | |
57 typedef struct data_block DATA_BL; /* contents of a data block */ | |
58 typedef struct pointer_entry PTR_EN; /* block/line-count pair */ | |
59 | |
2267 | 60 #define DATA_ID (('d' << 8) + 'a') /* data block id */ |
61 #define PTR_ID (('p' << 8) + 't') /* pointer block id */ | |
62 #define BLOCK0_ID0 'b' /* block 0 id 0 */ | |
63 #define BLOCK0_ID1 '0' /* block 0 id 1 */ | |
64 #define BLOCK0_ID1_C0 'c' /* block 0 id 1 'cm' 0 */ | |
65 #define BLOCK0_ID1_C1 'C' /* block 0 id 1 'cm' 1 */ | |
6122 | 66 #define BLOCK0_ID1_C2 'd' /* block 0 id 1 'cm' 2 */ |
67 | |
68 #if defined(FEAT_CRYPT) | |
69 static int id1_codes[] = { | |
70 BLOCK0_ID1_C0, /* CRYPT_M_ZIP */ | |
71 BLOCK0_ID1_C1, /* CRYPT_M_BF */ | |
72 BLOCK0_ID1_C2, /* CRYPT_M_BF2 */ | |
73 }; | |
74 #endif | |
7 | 75 |
76 /* | |
77 * pointer to a block, used in a pointer block | |
78 */ | |
79 struct pointer_entry | |
80 { | |
81 blocknr_T pe_bnum; /* block number */ | |
82 linenr_T pe_line_count; /* number of lines in this branch */ | |
83 linenr_T pe_old_lnum; /* lnum for this block (for recovery) */ | |
84 int pe_page_count; /* number of pages in block pe_bnum */ | |
85 }; | |
86 | |
87 /* | |
88 * A pointer block contains a list of branches in the tree. | |
89 */ | |
90 struct pointer_block | |
91 { | |
92 short_u pb_id; /* ID for pointer block: PTR_ID */ | |
2240
6b4879aea261
Add test for gettabvar() and settabvar().
Bram Moolenaar <bram@vim.org>
parents:
2221
diff
changeset
|
93 short_u pb_count; /* number of pointers in this block */ |
7 | 94 short_u pb_count_max; /* maximum value for pb_count */ |
95 PTR_EN pb_pointer[1]; /* list of pointers to blocks (actually longer) | |
96 * followed by empty space until end of page */ | |
97 }; | |
98 | |
99 /* | |
100 * A data block is a leaf in the tree. | |
101 * | |
102 * The text of the lines is at the end of the block. The text of the first line | |
103 * in the block is put at the end, the text of the second line in front of it, | |
104 * etc. Thus the order of the lines is the opposite of the line number. | |
105 */ | |
106 struct data_block | |
107 { | |
108 short_u db_id; /* ID for data block: DATA_ID */ | |
109 unsigned db_free; /* free space available */ | |
110 unsigned db_txt_start; /* byte where text starts */ | |
111 unsigned db_txt_end; /* byte just after data block */ | |
112 linenr_T db_line_count; /* number of lines in this block */ | |
113 unsigned db_index[1]; /* index for start of line (actually bigger) | |
114 * followed by empty space upto db_txt_start | |
115 * followed by the text in the lines until | |
116 * end of page */ | |
117 }; | |
118 | |
119 /* | |
120 * The low bits of db_index hold the actual index. The topmost bit is | |
121 * used for the global command to be able to mark a line. | |
122 * This method is not clean, but otherwise there would be at least one extra | |
123 * byte used for each line. | |
124 * The mark has to be in this place to keep it with the correct line when other | |
125 * lines are inserted or deleted. | |
126 */ | |
127 #define DB_MARKED ((unsigned)1 << ((sizeof(unsigned) * 8) - 1)) | |
128 #define DB_INDEX_MASK (~DB_MARKED) | |
129 | |
130 #define INDEX_SIZE (sizeof(unsigned)) /* size of one db_index entry */ | |
131 #define HEADER_SIZE (sizeof(DATA_BL) - INDEX_SIZE) /* size of data block header */ | |
132 | |
39 | 133 #define B0_FNAME_SIZE_ORG 900 /* what it was in older versions */ |
2267 | 134 #define B0_FNAME_SIZE_NOCRYPT 898 /* 2 bytes used for other things */ |
135 #define B0_FNAME_SIZE_CRYPT 890 /* 10 bytes used for other things */ | |
39 | 136 #define B0_UNAME_SIZE 40 |
137 #define B0_HNAME_SIZE 40 | |
7 | 138 /* |
139 * Restrict the numbers to 32 bits, otherwise most compilers will complain. | |
140 * This won't detect a 64 bit machine that only swaps a byte in the top 32 | |
141 * bits, but that is crazy anyway. | |
142 */ | |
143 #define B0_MAGIC_LONG 0x30313233L | |
144 #define B0_MAGIC_INT 0x20212223L | |
145 #define B0_MAGIC_SHORT 0x10111213L | |
146 #define B0_MAGIC_CHAR 0x55 | |
147 | |
148 /* | |
149 * Block zero holds all info about the swap file. | |
150 * | |
151 * NOTE: DEFINITION OF BLOCK 0 SHOULD NOT CHANGE! It would make all existing | |
152 * swap files unusable! | |
153 * | |
154 * If size of block0 changes anyway, adjust MIN_SWAP_PAGE_SIZE in vim.h!! | |
155 * | |
1228 | 156 * This block is built up of single bytes, to make it portable across |
7 | 157 * different machines. b0_magic_* is used to check the byte order and size of |
158 * variables, because the rest of the swap file is not portable. | |
159 */ | |
160 struct block0 | |
161 { | |
2267 | 162 char_u b0_id[2]; /* id for block 0: BLOCK0_ID0 and BLOCK0_ID1, |
6122 | 163 * BLOCK0_ID1_C0, BLOCK0_ID1_C1, etc. */ |
7 | 164 char_u b0_version[10]; /* Vim version string */ |
165 char_u b0_page_size[4];/* number of bytes per page */ | |
166 char_u b0_mtime[4]; /* last modification time of file */ | |
167 char_u b0_ino[4]; /* inode of b0_fname */ | |
168 char_u b0_pid[4]; /* process id of creator (or 0) */ | |
169 char_u b0_uname[B0_UNAME_SIZE]; /* name of user (uid if no name) */ | |
170 char_u b0_hname[B0_HNAME_SIZE]; /* host name (if it has a name) */ | |
39 | 171 char_u b0_fname[B0_FNAME_SIZE_ORG]; /* name of file being edited */ |
7 | 172 long b0_magic_long; /* check for byte order of long */ |
173 int b0_magic_int; /* check for byte order of int */ | |
174 short b0_magic_short; /* check for byte order of short */ | |
175 char_u b0_magic_char; /* check for last char */ | |
176 }; | |
39 | 177 |
178 /* | |
625 | 179 * Note: b0_dirty and b0_flags are put at the end of the file name. For very |
39 | 180 * long file names in older versions of Vim they are invalid. |
181 * The 'fileencoding' comes before b0_flags, with a NUL in front. But only | |
182 * when there is room, for very long file names it's omitted. | |
183 */ | |
184 #define B0_DIRTY 0x55 | |
2267 | 185 #define b0_dirty b0_fname[B0_FNAME_SIZE_ORG - 1] |
39 | 186 |
187 /* | |
188 * The b0_flags field is new in Vim 7.0. | |
189 */ | |
2267 | 190 #define b0_flags b0_fname[B0_FNAME_SIZE_ORG - 2] |
191 | |
192 /* | |
193 * Crypt seed goes here, 8 bytes. New in Vim 7.3. | |
194 * Without encryption these bytes may be used for 'fenc'. | |
195 */ | |
196 #define b0_seed b0_fname[B0_FNAME_SIZE_ORG - 2 - MF_SEED_LEN] | |
39 | 197 |
198 /* The lowest two bits contain the fileformat. Zero means it's not set | |
199 * (compatible with Vim 6.x), otherwise it's EOL_UNIX + 1, EOL_DOS + 1 or | |
200 * EOL_MAC + 1. */ | |
201 #define B0_FF_MASK 3 | |
202 | |
203 /* Swap file is in directory of edited file. Used to find the file from | |
204 * different mount points. */ | |
205 #define B0_SAME_DIR 4 | |
206 | |
207 /* The 'fileencoding' is at the end of b0_fname[], with a NUL in front of it. | |
208 * When empty there is only the NUL. */ | |
209 #define B0_HAS_FENC 8 | |
7 | 210 |
211 #define STACK_INCR 5 /* nr of entries added to ml_stack at a time */ | |
212 | |
213 /* | |
214 * The line number where the first mark may be is remembered. | |
215 * If it is 0 there are no marks at all. | |
216 * (always used for the current buffer only, no buffer change possible while | |
217 * executing a global command). | |
218 */ | |
219 static linenr_T lowest_marked = 0; | |
220 | |
221 /* | |
222 * arguments for ml_find_line() | |
223 */ | |
224 #define ML_DELETE 0x11 /* delete line */ | |
225 #define ML_INSERT 0x12 /* insert line */ | |
226 #define ML_FIND 0x13 /* just find the line */ | |
227 #define ML_FLUSH 0x02 /* flush locked block */ | |
228 #define ML_SIMPLE(x) (x & 0x10) /* DEL, INS or FIND */ | |
229 | |
2267 | 230 /* argument for ml_upd_block0() */ |
231 typedef enum { | |
232 UB_FNAME = 0 /* update timestamp and filename */ | |
233 , UB_SAME_DIR /* update the B0_SAME_DIR flag */ | |
234 , UB_CRYPT /* update crypt key */ | |
235 } upd_block0_T; | |
236 | |
237 #ifdef FEAT_CRYPT | |
238 static void ml_set_b0_crypt __ARGS((buf_T *buf, ZERO_BL *b0p)); | |
239 #endif | |
240 static int ml_check_b0_id __ARGS((ZERO_BL *b0p)); | |
241 static void ml_upd_block0 __ARGS((buf_T *buf, upd_block0_T what)); | |
7 | 242 static void set_b0_fname __ARGS((ZERO_BL *, buf_T *buf)); |
39 | 243 static void set_b0_dir_flag __ARGS((ZERO_BL *b0p, buf_T *buf)); |
244 #ifdef FEAT_MBYTE | |
245 static void add_b0_fenc __ARGS((ZERO_BL *b0p, buf_T *buf)); | |
246 #endif | |
7 | 247 static time_t swapfile_info __ARGS((char_u *)); |
248 static int recov_file_names __ARGS((char_u **, char_u *, int prepend_dot)); | |
249 static int ml_append_int __ARGS((buf_T *, linenr_T, char_u *, colnr_T, int, int)); | |
250 static int ml_delete_int __ARGS((buf_T *, linenr_T, int)); | |
251 static char_u *findswapname __ARGS((buf_T *, char_u **, char_u *)); | |
252 static void ml_flush_line __ARGS((buf_T *)); | |
253 static bhdr_T *ml_new_data __ARGS((memfile_T *, int, int)); | |
254 static bhdr_T *ml_new_ptr __ARGS((memfile_T *)); | |
255 static bhdr_T *ml_find_line __ARGS((buf_T *, linenr_T, int)); | |
256 static int ml_add_stack __ARGS((buf_T *)); | |
257 static void ml_lineadd __ARGS((buf_T *, int)); | |
258 static int b0_magic_wrong __ARGS((ZERO_BL *)); | |
259 #ifdef CHECK_INODE | |
260 static int fnamecmp_ino __ARGS((char_u *, char_u *, long)); | |
261 #endif | |
262 static void long_to_char __ARGS((long, char_u *)); | |
263 static long char_to_long __ARGS((char_u *)); | |
264 #if defined(UNIX) || defined(WIN3264) | |
265 static char_u *make_percent_swname __ARGS((char_u *dir, char_u *name)); | |
266 #endif | |
2267 | 267 #ifdef FEAT_CRYPT |
6122 | 268 static cryptstate_T *ml_crypt_prepare __ARGS((memfile_T *mfp, off_t offset, int reading)); |
2267 | 269 #endif |
7 | 270 #ifdef FEAT_BYTEOFF |
271 static void ml_updatechunk __ARGS((buf_T *buf, long line, long len, int updtype)); | |
272 #endif | |
273 | |
274 /* | |
625 | 275 * Open a new memline for "buf". |
7 | 276 * |
625 | 277 * Return FAIL for failure, OK otherwise. |
7 | 278 */ |
279 int | |
625 | 280 ml_open(buf) |
281 buf_T *buf; | |
7 | 282 { |
283 memfile_T *mfp; | |
284 bhdr_T *hp = NULL; | |
285 ZERO_BL *b0p; | |
286 PTR_BL *pp; | |
287 DATA_BL *dp; | |
288 | |
625 | 289 /* |
290 * init fields in memline struct | |
291 */ | |
2267 | 292 buf->b_ml.ml_stack_size = 0; /* no stack yet */ |
625 | 293 buf->b_ml.ml_stack = NULL; /* no stack yet */ |
294 buf->b_ml.ml_stack_top = 0; /* nothing in the stack */ | |
295 buf->b_ml.ml_locked = NULL; /* no cached block */ | |
296 buf->b_ml.ml_line_lnum = 0; /* no cached line */ | |
7 | 297 #ifdef FEAT_BYTEOFF |
625 | 298 buf->b_ml.ml_chunksize = NULL; |
7 | 299 #endif |
300 | |
5737 | 301 if (cmdmod.noswapfile) |
302 buf->b_p_swf = FALSE; | |
303 | |
625 | 304 /* |
305 * When 'updatecount' is non-zero swap file may be opened later. | |
306 */ | |
307 if (p_uc && buf->b_p_swf) | |
308 buf->b_may_swap = TRUE; | |
7 | 309 else |
625 | 310 buf->b_may_swap = FALSE; |
311 | |
312 /* | |
313 * Open the memfile. No swap file is created yet. | |
314 */ | |
7 | 315 mfp = mf_open(NULL, 0); |
316 if (mfp == NULL) | |
317 goto error; | |
318 | |
625 | 319 buf->b_ml.ml_mfp = mfp; |
2267 | 320 #ifdef FEAT_CRYPT |
321 mfp->mf_buffer = buf; | |
322 #endif | |
625 | 323 buf->b_ml.ml_flags = ML_EMPTY; |
324 buf->b_ml.ml_line_count = 1; | |
13 | 325 #ifdef FEAT_LINEBREAK |
326 curwin->w_nrwidth_line_count = 0; | |
327 #endif | |
7 | 328 |
329 #if defined(MSDOS) && !defined(DJGPP) | |
330 /* for 16 bit MS-DOS create a swapfile now, because we run out of | |
331 * memory very quickly */ | |
332 if (p_uc != 0) | |
625 | 333 ml_open_file(buf); |
7 | 334 #endif |
335 | |
336 /* | |
337 * fill block0 struct and write page 0 | |
338 */ | |
339 if ((hp = mf_new(mfp, FALSE, 1)) == NULL) | |
340 goto error; | |
341 if (hp->bh_bnum != 0) | |
342 { | |
343 EMSG(_("E298: Didn't get block nr 0?")); | |
344 goto error; | |
345 } | |
346 b0p = (ZERO_BL *)(hp->bh_data); | |
347 | |
348 b0p->b0_id[0] = BLOCK0_ID0; | |
349 b0p->b0_id[1] = BLOCK0_ID1; | |
350 b0p->b0_magic_long = (long)B0_MAGIC_LONG; | |
351 b0p->b0_magic_int = (int)B0_MAGIC_INT; | |
352 b0p->b0_magic_short = (short)B0_MAGIC_SHORT; | |
353 b0p->b0_magic_char = B0_MAGIC_CHAR; | |
354 STRNCPY(b0p->b0_version, "VIM ", 4); | |
355 STRNCPY(b0p->b0_version + 4, Version, 6); | |
356 long_to_char((long)mfp->mf_page_size, b0p->b0_page_size); | |
625 | 357 |
800 | 358 #ifdef FEAT_SPELL |
359 if (!buf->b_spell) | |
360 #endif | |
625 | 361 { |
362 b0p->b0_dirty = buf->b_changed ? B0_DIRTY : 0; | |
363 b0p->b0_flags = get_fileformat(buf) + 1; | |
364 set_b0_fname(b0p, buf); | |
365 (void)get_user_name(b0p->b0_uname, B0_UNAME_SIZE); | |
366 b0p->b0_uname[B0_UNAME_SIZE - 1] = NUL; | |
367 mch_get_host_name(b0p->b0_hname, B0_HNAME_SIZE); | |
368 b0p->b0_hname[B0_HNAME_SIZE - 1] = NUL; | |
369 long_to_char(mch_get_pid(), b0p->b0_pid); | |
2267 | 370 #ifdef FEAT_CRYPT |
6122 | 371 ml_set_b0_crypt(buf, b0p); |
2267 | 372 #endif |
625 | 373 } |
7 | 374 |
375 /* | |
376 * Always sync block number 0 to disk, so we can check the file name in | |
2267 | 377 * the swap file in findswapname(). Don't do this for a help files or |
378 * a spell buffer though. | |
7 | 379 * Only works when there's a swapfile, otherwise it's done when the file |
380 * is created. | |
381 */ | |
382 mf_put(mfp, hp, TRUE, FALSE); | |
625 | 383 if (!buf->b_help && !B_SPELL(buf)) |
7 | 384 (void)mf_sync(mfp, 0); |
385 | |
625 | 386 /* |
387 * Fill in root pointer block and write page 1. | |
388 */ | |
7 | 389 if ((hp = ml_new_ptr(mfp)) == NULL) |
390 goto error; | |
391 if (hp->bh_bnum != 1) | |
392 { | |
393 EMSG(_("E298: Didn't get block nr 1?")); | |
394 goto error; | |
395 } | |
396 pp = (PTR_BL *)(hp->bh_data); | |
397 pp->pb_count = 1; | |
398 pp->pb_pointer[0].pe_bnum = 2; | |
399 pp->pb_pointer[0].pe_page_count = 1; | |
400 pp->pb_pointer[0].pe_old_lnum = 1; | |
401 pp->pb_pointer[0].pe_line_count = 1; /* line count after insertion */ | |
402 mf_put(mfp, hp, TRUE, FALSE); | |
403 | |
625 | 404 /* |
405 * Allocate first data block and create an empty line 1. | |
406 */ | |
7 | 407 if ((hp = ml_new_data(mfp, FALSE, 1)) == NULL) |
408 goto error; | |
409 if (hp->bh_bnum != 2) | |
410 { | |
411 EMSG(_("E298: Didn't get block nr 2?")); | |
412 goto error; | |
413 } | |
414 | |
415 dp = (DATA_BL *)(hp->bh_data); | |
416 dp->db_index[0] = --dp->db_txt_start; /* at end of block */ | |
417 dp->db_free -= 1 + INDEX_SIZE; | |
418 dp->db_line_count = 1; | |
2003 | 419 *((char_u *)dp + dp->db_txt_start) = NUL; /* empty line */ |
7 | 420 |
421 return OK; | |
422 | |
423 error: | |
424 if (mfp != NULL) | |
425 { | |
426 if (hp) | |
427 mf_put(mfp, hp, FALSE, FALSE); | |
428 mf_close(mfp, TRUE); /* will also free(mfp->mf_fname) */ | |
429 } | |
625 | 430 buf->b_ml.ml_mfp = NULL; |
7 | 431 return FAIL; |
432 } | |
433 | |
2267 | 434 #if defined(FEAT_CRYPT) || defined(PROTO) |
435 /* | |
436 * Prepare encryption for "buf" with block 0 "b0p". | |
437 */ | |
438 static void | |
439 ml_set_b0_crypt(buf, b0p) | |
440 buf_T *buf; | |
441 ZERO_BL *b0p; | |
442 { | |
443 if (*buf->b_p_key == NUL) | |
444 b0p->b0_id[1] = BLOCK0_ID1; | |
445 else | |
446 { | |
6122 | 447 int method_nr = crypt_get_method_nr(buf); |
448 | |
449 b0p->b0_id[1] = id1_codes[method_nr]; | |
450 if (method_nr > CRYPT_M_ZIP) | |
2267 | 451 { |
452 /* Generate a seed and store it in block 0 and in the memfile. */ | |
453 sha2_seed(&b0p->b0_seed, MF_SEED_LEN, NULL, 0); | |
454 mch_memmove(buf->b_ml.ml_mfp->mf_seed, &b0p->b0_seed, MF_SEED_LEN); | |
455 } | |
456 } | |
457 } | |
458 | |
459 /* | |
460 * Called after the crypt key or 'cryptmethod' was changed for "buf". | |
461 * Will apply this to the swapfile. | |
462 * "old_key" is the previous key. It is equal to buf->b_p_key when | |
463 * 'cryptmethod' is changed. | |
2360
d8e4b27cef80
Change 'cryptmethod' from a number to a string option. Make it global-local.
Bram Moolenaar <bram@vim.org>
parents:
2283
diff
changeset
|
464 * "old_cm" is the previous 'cryptmethod'. It is equal to the current |
d8e4b27cef80
Change 'cryptmethod' from a number to a string option. Make it global-local.
Bram Moolenaar <bram@vim.org>
parents:
2283
diff
changeset
|
465 * 'cryptmethod' when 'key' is changed. |
2267 | 466 */ |
467 void | |
468 ml_set_crypt_key(buf, old_key, old_cm) | |
469 buf_T *buf; | |
470 char_u *old_key; | |
471 int old_cm; | |
472 { | |
473 memfile_T *mfp = buf->b_ml.ml_mfp; | |
474 bhdr_T *hp; | |
475 int page_count; | |
476 int idx; | |
477 long error; | |
478 infoptr_T *ip; | |
479 PTR_BL *pp; | |
480 DATA_BL *dp; | |
481 blocknr_T bnum; | |
482 int top; | |
483 | |
2483
7901306c5a34
Fix: when setting crypt key seed was not updated when the swap file wasn't
Bram Moolenaar <bram@vim.org>
parents:
2408
diff
changeset
|
484 if (mfp == NULL) |
2267 | 485 return; /* no memfile yet, nothing to do */ |
486 | |
487 /* Set the key, method and seed to be used for reading, these must be the | |
488 * old values. */ | |
489 mfp->mf_old_key = old_key; | |
490 mfp->mf_old_cm = old_cm; | |
491 if (old_cm > 0) | |
492 mch_memmove(mfp->mf_old_seed, mfp->mf_seed, MF_SEED_LEN); | |
493 | |
494 /* Update block 0 with the crypt flag and may set a new seed. */ | |
495 ml_upd_block0(buf, UB_CRYPT); | |
496 | |
497 if (mfp->mf_infile_count > 2) | |
498 { | |
499 /* | |
500 * Need to read back all data blocks from disk, decrypt them with the | |
501 * old key/method and mark them to be written. The algorithm is | |
502 * similar to what happens in ml_recover(), but we skip negative block | |
503 * numbers. | |
504 */ | |
505 ml_flush_line(buf); /* flush buffered line */ | |
506 (void)ml_find_line(buf, (linenr_T)0, ML_FLUSH); /* flush locked block */ | |
507 | |
508 hp = NULL; | |
509 bnum = 1; /* start with block 1 */ | |
510 page_count = 1; /* which is 1 page */ | |
511 idx = 0; /* start with first index in block 1 */ | |
512 error = 0; | |
513 buf->b_ml.ml_stack_top = 0; | |
2273
7f09ce7b4126
Fix a memory leak in encryption. (Dominique Pelle)
Bram Moolenaar <bram@vim.org>
parents:
2272
diff
changeset
|
514 vim_free(buf->b_ml.ml_stack); |
2267 | 515 buf->b_ml.ml_stack = NULL; |
516 buf->b_ml.ml_stack_size = 0; /* no stack yet */ | |
517 | |
518 for ( ; !got_int; line_breakcheck()) | |
519 { | |
520 if (hp != NULL) | |
521 mf_put(mfp, hp, FALSE, FALSE); /* release previous block */ | |
522 | |
523 /* get the block (pointer or data) */ | |
524 if ((hp = mf_get(mfp, (blocknr_T)bnum, page_count)) == NULL) | |
525 { | |
526 if (bnum == 1) | |
527 break; | |
528 ++error; | |
529 } | |
530 else | |
531 { | |
532 pp = (PTR_BL *)(hp->bh_data); | |
533 if (pp->pb_id == PTR_ID) /* it is a pointer block */ | |
534 { | |
535 if (pp->pb_count == 0) | |
536 { | |
537 /* empty block? */ | |
538 ++error; | |
539 } | |
540 else if (idx < (int)pp->pb_count) /* go a block deeper */ | |
541 { | |
542 if (pp->pb_pointer[idx].pe_bnum < 0) | |
543 { | |
544 /* Skip data block with negative block number. */ | |
545 ++idx; /* get same block again for next index */ | |
546 continue; | |
547 } | |
548 | |
549 /* going one block deeper in the tree, new entry in | |
550 * stack */ | |
551 if ((top = ml_add_stack(buf)) < 0) | |
552 { | |
553 ++error; | |
554 break; /* out of memory */ | |
555 } | |
556 ip = &(buf->b_ml.ml_stack[top]); | |
557 ip->ip_bnum = bnum; | |
558 ip->ip_index = idx; | |
559 | |
560 bnum = pp->pb_pointer[idx].pe_bnum; | |
561 page_count = pp->pb_pointer[idx].pe_page_count; | |
562 continue; | |
563 } | |
564 } | |
565 else /* not a pointer block */ | |
566 { | |
567 dp = (DATA_BL *)(hp->bh_data); | |
568 if (dp->db_id != DATA_ID) /* block id wrong */ | |
569 ++error; | |
570 else | |
571 { | |
572 /* It is a data block, need to write it back to disk. */ | |
573 mf_put(mfp, hp, TRUE, FALSE); | |
574 hp = NULL; | |
575 } | |
576 } | |
577 } | |
578 | |
579 if (buf->b_ml.ml_stack_top == 0) /* finished */ | |
580 break; | |
581 | |
582 /* go one block up in the tree */ | |
583 ip = &(buf->b_ml.ml_stack[--(buf->b_ml.ml_stack_top)]); | |
584 bnum = ip->ip_bnum; | |
585 idx = ip->ip_index + 1; /* go to next index */ | |
586 page_count = 1; | |
587 } | |
2657 | 588 |
589 if (error > 0) | |
590 EMSG(_("E843: Error while updating swap file crypt")); | |
2267 | 591 } |
592 | |
593 mfp->mf_old_key = NULL; | |
594 } | |
595 #endif | |
596 | |
7 | 597 /* |
598 * ml_setname() is called when the file name of "buf" has been changed. | |
599 * It may rename the swap file. | |
600 */ | |
601 void | |
602 ml_setname(buf) | |
603 buf_T *buf; | |
604 { | |
605 int success = FALSE; | |
606 memfile_T *mfp; | |
607 char_u *fname; | |
608 char_u *dirp; | |
609 #if defined(MSDOS) || defined(MSWIN) | |
610 char_u *p; | |
611 #endif | |
612 | |
613 mfp = buf->b_ml.ml_mfp; | |
614 if (mfp->mf_fd < 0) /* there is no swap file yet */ | |
615 { | |
616 /* | |
617 * When 'updatecount' is 0 and 'noswapfile' there is no swap file. | |
618 * For help files we will make a swap file now. | |
619 */ | |
5737 | 620 if (p_uc != 0 && !cmdmod.noswapfile) |
7 | 621 ml_open_file(buf); /* create a swap file */ |
622 return; | |
623 } | |
624 | |
625 /* | |
626 * Try all directories in the 'directory' option. | |
627 */ | |
628 dirp = p_dir; | |
629 for (;;) | |
630 { | |
631 if (*dirp == NUL) /* tried all directories, fail */ | |
632 break; | |
43 | 633 fname = findswapname(buf, &dirp, mfp->mf_fname); |
634 /* alloc's fname */ | |
3158 | 635 if (dirp == NULL) /* out of memory */ |
636 break; | |
7 | 637 if (fname == NULL) /* no file name found for this dir */ |
638 continue; | |
639 | |
640 #if defined(MSDOS) || defined(MSWIN) | |
641 /* | |
642 * Set full pathname for swap file now, because a ":!cd dir" may | |
643 * change directory without us knowing it. | |
644 */ | |
645 p = FullName_save(fname, FALSE); | |
646 vim_free(fname); | |
647 fname = p; | |
648 if (fname == NULL) | |
649 continue; | |
650 #endif | |
651 /* if the file name is the same we don't have to do anything */ | |
652 if (fnamecmp(fname, mfp->mf_fname) == 0) | |
653 { | |
654 vim_free(fname); | |
655 success = TRUE; | |
656 break; | |
657 } | |
658 /* need to close the swap file before renaming */ | |
659 if (mfp->mf_fd >= 0) | |
660 { | |
661 close(mfp->mf_fd); | |
662 mfp->mf_fd = -1; | |
663 } | |
664 | |
665 /* try to rename the swap file */ | |
666 if (vim_rename(mfp->mf_fname, fname) == 0) | |
667 { | |
668 success = TRUE; | |
669 vim_free(mfp->mf_fname); | |
670 mfp->mf_fname = fname; | |
671 vim_free(mfp->mf_ffname); | |
672 #if defined(MSDOS) || defined(MSWIN) | |
673 mfp->mf_ffname = NULL; /* mf_fname is full pathname already */ | |
674 #else | |
675 mf_set_ffname(mfp); | |
676 #endif | |
2267 | 677 ml_upd_block0(buf, UB_SAME_DIR); |
7 | 678 break; |
679 } | |
680 vim_free(fname); /* this fname didn't work, try another */ | |
681 } | |
682 | |
683 if (mfp->mf_fd == -1) /* need to (re)open the swap file */ | |
684 { | |
685 mfp->mf_fd = mch_open((char *)mfp->mf_fname, O_RDWR | O_EXTRA, 0); | |
686 if (mfp->mf_fd < 0) | |
687 { | |
688 /* could not (re)open the swap file, what can we do???? */ | |
689 EMSG(_("E301: Oops, lost the swap file!!!")); | |
690 return; | |
691 } | |
2003 | 692 #ifdef HAVE_FD_CLOEXEC |
693 { | |
694 int fdflags = fcntl(mfp->mf_fd, F_GETFD); | |
695 if (fdflags >= 0 && (fdflags & FD_CLOEXEC) == 0) | |
696 fcntl(mfp->mf_fd, F_SETFD, fdflags | FD_CLOEXEC); | |
697 } | |
698 #endif | |
7 | 699 } |
700 if (!success) | |
701 EMSG(_("E302: Could not rename swap file")); | |
702 } | |
703 | |
704 /* | |
705 * Open a file for the memfile for all buffers that are not readonly or have | |
706 * been modified. | |
707 * Used when 'updatecount' changes from zero to non-zero. | |
708 */ | |
709 void | |
710 ml_open_files() | |
711 { | |
712 buf_T *buf; | |
713 | |
714 for (buf = firstbuf; buf != NULL; buf = buf->b_next) | |
715 if (!buf->b_p_ro || buf->b_changed) | |
716 ml_open_file(buf); | |
717 } | |
718 | |
719 /* | |
720 * Open a swap file for an existing memfile, if there is no swap file yet. | |
721 * If we are unable to find a file name, mf_fname will be NULL | |
722 * and the memfile will be in memory only (no recovery possible). | |
723 */ | |
724 void | |
725 ml_open_file(buf) | |
726 buf_T *buf; | |
727 { | |
728 memfile_T *mfp; | |
729 char_u *fname; | |
730 char_u *dirp; | |
731 | |
732 mfp = buf->b_ml.ml_mfp; | |
5737 | 733 if (mfp == NULL || mfp->mf_fd >= 0 || !buf->b_p_swf || cmdmod.noswapfile) |
7 | 734 return; /* nothing to do */ |
735 | |
748 | 736 #ifdef FEAT_SPELL |
625 | 737 /* For a spell buffer use a temp file name. */ |
738 if (buf->b_spell) | |
739 { | |
740 fname = vim_tempname('s'); | |
741 if (fname != NULL) | |
742 (void)mf_open_file(mfp, fname); /* consumes fname! */ | |
743 buf->b_may_swap = FALSE; | |
744 return; | |
745 } | |
746 #endif | |
747 | |
7 | 748 /* |
749 * Try all directories in 'directory' option. | |
750 */ | |
751 dirp = p_dir; | |
752 for (;;) | |
753 { | |
754 if (*dirp == NUL) | |
755 break; | |
2273
7f09ce7b4126
Fix a memory leak in encryption. (Dominique Pelle)
Bram Moolenaar <bram@vim.org>
parents:
2272
diff
changeset
|
756 /* There is a small chance that between choosing the swap file name |
7f09ce7b4126
Fix a memory leak in encryption. (Dominique Pelle)
Bram Moolenaar <bram@vim.org>
parents:
2272
diff
changeset
|
757 * and creating it, another Vim creates the file. In that case the |
7 | 758 * creation will fail and we will use another directory. */ |
43 | 759 fname = findswapname(buf, &dirp, NULL); /* allocates fname */ |
3158 | 760 if (dirp == NULL) |
761 break; /* out of memory */ | |
7 | 762 if (fname == NULL) |
763 continue; | |
764 if (mf_open_file(mfp, fname) == OK) /* consumes fname! */ | |
765 { | |
2823 | 766 #if defined(MSDOS) || defined(MSWIN) |
7 | 767 /* |
768 * set full pathname for swap file now, because a ":!cd dir" may | |
769 * change directory without us knowing it. | |
770 */ | |
771 mf_fullname(mfp); | |
772 #endif | |
2267 | 773 ml_upd_block0(buf, UB_SAME_DIR); |
39 | 774 |
7 | 775 /* Flush block zero, so others can read it */ |
776 if (mf_sync(mfp, MFS_ZERO) == OK) | |
630 | 777 { |
778 /* Mark all blocks that should be in the swapfile as dirty. | |
779 * Needed for when the 'swapfile' option was reset, so that | |
780 * the swap file was deleted, and then on again. */ | |
781 mf_set_dirty(mfp); | |
7 | 782 break; |
630 | 783 } |
7 | 784 /* Writing block 0 failed: close the file and try another dir */ |
785 mf_close_file(buf, FALSE); | |
786 } | |
787 } | |
788 | |
789 if (mfp->mf_fname == NULL) /* Failed! */ | |
790 { | |
791 need_wait_return = TRUE; /* call wait_return later */ | |
792 ++no_wait_return; | |
793 (void)EMSG2(_("E303: Unable to open swap file for \"%s\", recovery impossible"), | |
3839 | 794 buf_spname(buf) != NULL ? buf_spname(buf) : buf->b_fname); |
7 | 795 --no_wait_return; |
796 } | |
797 | |
798 /* don't try to open a swap file again */ | |
799 buf->b_may_swap = FALSE; | |
800 } | |
801 | |
802 /* | |
803 * If still need to create a swap file, and starting to edit a not-readonly | |
804 * file, or reading into an existing buffer, create a swap file now. | |
805 */ | |
806 void | |
807 check_need_swap(newfile) | |
808 int newfile; /* reading file into new buffer */ | |
809 { | |
810 if (curbuf->b_may_swap && (!curbuf->b_p_ro || !newfile)) | |
811 ml_open_file(curbuf); | |
812 } | |
813 | |
814 /* | |
815 * Close memline for buffer 'buf'. | |
816 * If 'del_file' is TRUE, delete the swap file | |
817 */ | |
818 void | |
819 ml_close(buf, del_file) | |
820 buf_T *buf; | |
821 int del_file; | |
822 { | |
823 if (buf->b_ml.ml_mfp == NULL) /* not open */ | |
824 return; | |
825 mf_close(buf->b_ml.ml_mfp, del_file); /* close the .swp file */ | |
826 if (buf->b_ml.ml_line_lnum != 0 && (buf->b_ml.ml_flags & ML_LINE_DIRTY)) | |
827 vim_free(buf->b_ml.ml_line_ptr); | |
828 vim_free(buf->b_ml.ml_stack); | |
829 #ifdef FEAT_BYTEOFF | |
830 vim_free(buf->b_ml.ml_chunksize); | |
831 buf->b_ml.ml_chunksize = NULL; | |
832 #endif | |
833 buf->b_ml.ml_mfp = NULL; | |
834 | |
835 /* Reset the "recovered" flag, give the ATTENTION prompt the next time | |
836 * this buffer is loaded. */ | |
837 buf->b_flags &= ~BF_RECOVERED; | |
838 } | |
839 | |
840 /* | |
841 * Close all existing memlines and memfiles. | |
842 * Only used when exiting. | |
843 * When 'del_file' is TRUE, delete the memfiles. | |
165 | 844 * But don't delete files that were ":preserve"d when we are POSIX compatible. |
7 | 845 */ |
846 void | |
847 ml_close_all(del_file) | |
848 int del_file; | |
849 { | |
850 buf_T *buf; | |
851 | |
852 for (buf = firstbuf; buf != NULL; buf = buf->b_next) | |
165 | 853 ml_close(buf, del_file && ((buf->b_flags & BF_PRESERVED) == 0 |
854 || vim_strchr(p_cpo, CPO_PRESERVE) == NULL)); | |
5519 | 855 #ifdef FEAT_SPELL |
856 spell_delete_wordlist(); /* delete the internal wordlist */ | |
857 #endif | |
7 | 858 #ifdef TEMPDIRNAMES |
5519 | 859 vim_deltempdir(); /* delete created temp directory */ |
7 | 860 #endif |
861 } | |
862 | |
863 /* | |
864 * Close all memfiles for not modified buffers. | |
865 * Only use just before exiting! | |
866 */ | |
867 void | |
868 ml_close_notmod() | |
869 { | |
870 buf_T *buf; | |
871 | |
872 for (buf = firstbuf; buf != NULL; buf = buf->b_next) | |
873 if (!bufIsChanged(buf)) | |
874 ml_close(buf, TRUE); /* close all not-modified buffers */ | |
875 } | |
876 | |
877 /* | |
878 * Update the timestamp in the .swp file. | |
879 * Used when the file has been written. | |
880 */ | |
881 void | |
882 ml_timestamp(buf) | |
883 buf_T *buf; | |
884 { | |
2267 | 885 ml_upd_block0(buf, UB_FNAME); |
886 } | |
887 | |
888 /* | |
889 * Return FAIL when the ID of "b0p" is wrong. | |
890 */ | |
891 static int | |
892 ml_check_b0_id(b0p) | |
893 ZERO_BL *b0p; | |
894 { | |
895 if (b0p->b0_id[0] != BLOCK0_ID0 | |
896 || (b0p->b0_id[1] != BLOCK0_ID1 | |
897 && b0p->b0_id[1] != BLOCK0_ID1_C0 | |
6122 | 898 && b0p->b0_id[1] != BLOCK0_ID1_C1 |
899 && b0p->b0_id[1] != BLOCK0_ID1_C2) | |
2267 | 900 ) |
901 return FAIL; | |
902 return OK; | |
39 | 903 } |
904 | |
905 /* | |
906 * Update the timestamp or the B0_SAME_DIR flag of the .swp file. | |
907 */ | |
908 static void | |
2267 | 909 ml_upd_block0(buf, what) |
39 | 910 buf_T *buf; |
2267 | 911 upd_block0_T what; |
39 | 912 { |
7 | 913 memfile_T *mfp; |
914 bhdr_T *hp; | |
915 ZERO_BL *b0p; | |
916 | |
917 mfp = buf->b_ml.ml_mfp; | |
918 if (mfp == NULL || (hp = mf_get(mfp, (blocknr_T)0, 1)) == NULL) | |
919 return; | |
920 b0p = (ZERO_BL *)(hp->bh_data); | |
2267 | 921 if (ml_check_b0_id(b0p) == FAIL) |
39 | 922 EMSG(_("E304: ml_upd_block0(): Didn't get block 0??")); |
7 | 923 else |
39 | 924 { |
2267 | 925 if (what == UB_FNAME) |
39 | 926 set_b0_fname(b0p, buf); |
2267 | 927 #ifdef FEAT_CRYPT |
928 else if (what == UB_CRYPT) | |
929 ml_set_b0_crypt(buf, b0p); | |
930 #endif | |
931 else /* what == UB_SAME_DIR */ | |
39 | 932 set_b0_dir_flag(b0p, buf); |
933 } | |
7 | 934 mf_put(mfp, hp, TRUE, FALSE); |
935 } | |
936 | |
937 /* | |
938 * Write file name and timestamp into block 0 of a swap file. | |
939 * Also set buf->b_mtime. | |
940 * Don't use NameBuff[]!!! | |
941 */ | |
942 static void | |
943 set_b0_fname(b0p, buf) | |
944 ZERO_BL *b0p; | |
945 buf_T *buf; | |
946 { | |
947 struct stat st; | |
948 | |
949 if (buf->b_ffname == NULL) | |
950 b0p->b0_fname[0] = NUL; | |
951 else | |
952 { | |
2823 | 953 #if defined(MSDOS) || defined(MSWIN) || defined(AMIGA) |
39 | 954 /* Systems that cannot translate "~user" back into a path: copy the |
955 * file name unmodified. Do use slashes instead of backslashes for | |
956 * portability. */ | |
2267 | 957 vim_strncpy(b0p->b0_fname, buf->b_ffname, B0_FNAME_SIZE_CRYPT - 1); |
39 | 958 # ifdef BACKSLASH_IN_FILENAME |
959 forward_slash(b0p->b0_fname); | |
960 # endif | |
7 | 961 #else |
962 size_t flen, ulen; | |
963 char_u uname[B0_UNAME_SIZE]; | |
964 | |
965 /* | |
966 * For a file under the home directory of the current user, we try to | |
967 * replace the home directory path with "~user". This helps when | |
968 * editing the same file on different machines over a network. | |
969 * First replace home dir path with "~/" with home_replace(). | |
970 * Then insert the user name to get "~user/". | |
971 */ | |
2267 | 972 home_replace(NULL, buf->b_ffname, b0p->b0_fname, |
973 B0_FNAME_SIZE_CRYPT, TRUE); | |
7 | 974 if (b0p->b0_fname[0] == '~') |
975 { | |
976 flen = STRLEN(b0p->b0_fname); | |
977 /* If there is no user name or it is too long, don't use "~/" */ | |
978 if (get_user_name(uname, B0_UNAME_SIZE) == FAIL | |
2267 | 979 || (ulen = STRLEN(uname)) + flen > B0_FNAME_SIZE_CRYPT - 1) |
980 vim_strncpy(b0p->b0_fname, buf->b_ffname, | |
981 B0_FNAME_SIZE_CRYPT - 1); | |
7 | 982 else |
983 { | |
984 mch_memmove(b0p->b0_fname + ulen + 1, b0p->b0_fname + 1, flen); | |
985 mch_memmove(b0p->b0_fname + 1, uname, ulen); | |
986 } | |
987 } | |
988 #endif | |
989 if (mch_stat((char *)buf->b_ffname, &st) >= 0) | |
990 { | |
991 long_to_char((long)st.st_mtime, b0p->b0_mtime); | |
992 #ifdef CHECK_INODE | |
993 long_to_char((long)st.st_ino, b0p->b0_ino); | |
994 #endif | |
995 buf_store_time(buf, &st, buf->b_ffname); | |
996 buf->b_mtime_read = buf->b_mtime; | |
997 } | |
998 else | |
999 { | |
1000 long_to_char(0L, b0p->b0_mtime); | |
1001 #ifdef CHECK_INODE | |
1002 long_to_char(0L, b0p->b0_ino); | |
1003 #endif | |
1004 buf->b_mtime = 0; | |
1005 buf->b_mtime_read = 0; | |
1006 buf->b_orig_size = 0; | |
1007 buf->b_orig_mode = 0; | |
1008 } | |
1009 } | |
39 | 1010 |
1011 #ifdef FEAT_MBYTE | |
1012 /* Also add the 'fileencoding' if there is room. */ | |
1013 add_b0_fenc(b0p, curbuf); | |
1014 #endif | |
7 | 1015 } |
1016 | |
1017 /* | |
39 | 1018 * Update the B0_SAME_DIR flag of the swap file. It's set if the file and the |
1019 * swapfile for "buf" are in the same directory. | |
1020 * This is fail safe: if we are not sure the directories are equal the flag is | |
1021 * not set. | |
1022 */ | |
1023 static void | |
1024 set_b0_dir_flag(b0p, buf) | |
1025 ZERO_BL *b0p; | |
1026 buf_T *buf; | |
1027 { | |
1028 if (same_directory(buf->b_ml.ml_mfp->mf_fname, buf->b_ffname)) | |
1029 b0p->b0_flags |= B0_SAME_DIR; | |
1030 else | |
1031 b0p->b0_flags &= ~B0_SAME_DIR; | |
1032 } | |
1033 | |
1034 #ifdef FEAT_MBYTE | |
1035 /* | |
1036 * When there is room, add the 'fileencoding' to block zero. | |
1037 */ | |
1038 static void | |
1039 add_b0_fenc(b0p, buf) | |
1040 ZERO_BL *b0p; | |
1041 buf_T *buf; | |
1042 { | |
1043 int n; | |
2267 | 1044 int size = B0_FNAME_SIZE_NOCRYPT; |
1045 | |
1046 # ifdef FEAT_CRYPT | |
1047 /* Without encryption use the same offset as in Vim 7.2 to be compatible. | |
1048 * With encryption it's OK to move elsewhere, the swap file is not | |
1049 * compatible anyway. */ | |
1050 if (*buf->b_p_key != NUL) | |
1051 size = B0_FNAME_SIZE_CRYPT; | |
1052 # endif | |
39 | 1053 |
835 | 1054 n = (int)STRLEN(buf->b_p_fenc); |
2267 | 1055 if ((int)STRLEN(b0p->b0_fname) + n + 1 > size) |
39 | 1056 b0p->b0_flags &= ~B0_HAS_FENC; |
1057 else | |
1058 { | |
2267 | 1059 mch_memmove((char *)b0p->b0_fname + size - n, |
39 | 1060 (char *)buf->b_p_fenc, (size_t)n); |
2267 | 1061 *(b0p->b0_fname + size - n - 1) = NUL; |
39 | 1062 b0p->b0_flags |= B0_HAS_FENC; |
1063 } | |
1064 } | |
1065 #endif | |
1066 | |
1067 | |
1068 /* | |
2267 | 1069 * Try to recover curbuf from the .swp file. |
7 | 1070 */ |
1071 void | |
1072 ml_recover() | |
1073 { | |
1074 buf_T *buf = NULL; | |
1075 memfile_T *mfp = NULL; | |
1076 char_u *fname; | |
2267 | 1077 char_u *fname_used = NULL; |
7 | 1078 bhdr_T *hp = NULL; |
1079 ZERO_BL *b0p; | |
39 | 1080 int b0_ff; |
1081 char_u *b0_fenc = NULL; | |
2267 | 1082 #ifdef FEAT_CRYPT |
1083 int b0_cm = -1; | |
1084 #endif | |
7 | 1085 PTR_BL *pp; |
1086 DATA_BL *dp; | |
1087 infoptr_T *ip; | |
1088 blocknr_T bnum; | |
1089 int page_count; | |
1090 struct stat org_stat, swp_stat; | |
1091 int len; | |
1092 int directly; | |
1093 linenr_T lnum; | |
1094 char_u *p; | |
1095 int i; | |
1096 long error; | |
1097 int cannot_open; | |
1098 linenr_T line_count; | |
1099 int has_error; | |
1100 int idx; | |
1101 int top; | |
1102 int txt_start; | |
1103 off_t size; | |
1104 int called_from_main; | |
1105 int serious_error = TRUE; | |
1106 long mtime; | |
1107 int attr; | |
2162
0527eb0f6918
After recovery check if the text changed. If it did mark the buffer as
Bram Moolenaar <bram@vim.org>
parents:
2145
diff
changeset
|
1108 int orig_file_status = NOTDONE; |
7 | 1109 |
1110 recoverymode = TRUE; | |
1111 called_from_main = (curbuf->b_ml.ml_mfp == NULL); | |
1112 attr = hl_attr(HLF_E); | |
1975 | 1113 |
1114 /* | |
1115 * If the file name ends in ".s[uvw][a-z]" we assume this is the swap file. | |
1116 * Otherwise a search is done to find the swap file(s). | |
1117 */ | |
7 | 1118 fname = curbuf->b_fname; |
1119 if (fname == NULL) /* When there is no file name */ | |
1120 fname = (char_u *)""; | |
1121 len = (int)STRLEN(fname); | |
1122 if (len >= 4 && | |
2823 | 1123 #if defined(VMS) |
1975 | 1124 STRNICMP(fname + len - 4, "_s" , 2) |
7 | 1125 #else |
1975 | 1126 STRNICMP(fname + len - 4, ".s" , 2) |
7 | 1127 #endif |
1975 | 1128 == 0 |
1129 && vim_strchr((char_u *)"UVWuvw", fname[len - 2]) != NULL | |
1130 && ASCII_ISALPHA(fname[len - 1])) | |
7 | 1131 { |
1132 directly = TRUE; | |
2267 | 1133 fname_used = vim_strsave(fname); /* make a copy for mf_open() */ |
7 | 1134 } |
1135 else | |
1136 { | |
1137 directly = FALSE; | |
1138 | |
1139 /* count the number of matching swap files */ | |
2267 | 1140 len = recover_names(fname, FALSE, 0, NULL); |
7 | 1141 if (len == 0) /* no swap files found */ |
1142 { | |
1143 EMSG2(_("E305: No swap file found for %s"), fname); | |
1144 goto theend; | |
1145 } | |
1146 if (len == 1) /* one swap file found, use it */ | |
1147 i = 1; | |
1148 else /* several swap files found, choose */ | |
1149 { | |
1150 /* list the names of the swap files */ | |
2267 | 1151 (void)recover_names(fname, TRUE, 0, NULL); |
7 | 1152 msg_putchar('\n'); |
1153 MSG_PUTS(_("Enter number of swap file to use (0 to quit): ")); | |
374 | 1154 i = get_number(FALSE, NULL); |
7 | 1155 if (i < 1 || i > len) |
1156 goto theend; | |
1157 } | |
1158 /* get the swap file name that will be used */ | |
2267 | 1159 (void)recover_names(fname, FALSE, i, &fname_used); |
7 | 1160 } |
2267 | 1161 if (fname_used == NULL) |
7 | 1162 goto theend; /* out of memory */ |
1163 | |
1164 /* When called from main() still need to initialize storage structure */ | |
625 | 1165 if (called_from_main && ml_open(curbuf) == FAIL) |
7 | 1166 getout(1); |
1167 | |
2267 | 1168 /* |
1169 * Allocate a buffer structure for the swap file that is used for recovery. | |
2407
6bc102a4bff8
Fix memory access to 'cryptmethod' during recovery. (Dominique Pelle)
Bram Moolenaar <bram@vim.org>
parents:
2394
diff
changeset
|
1170 * Only the memline and crypt information in it are really used. |
2267 | 1171 */ |
7 | 1172 buf = (buf_T *)alloc((unsigned)sizeof(buf_T)); |
1173 if (buf == NULL) | |
1174 goto theend; | |
2267 | 1175 |
1176 /* | |
1177 * init fields in memline struct | |
1178 */ | |
7 | 1179 buf->b_ml.ml_stack_size = 0; /* no stack yet */ |
1180 buf->b_ml.ml_stack = NULL; /* no stack yet */ | |
1181 buf->b_ml.ml_stack_top = 0; /* nothing in the stack */ | |
1182 buf->b_ml.ml_line_lnum = 0; /* no cached line */ | |
1183 buf->b_ml.ml_locked = NULL; /* no locked block */ | |
1184 buf->b_ml.ml_flags = 0; | |
2408
9e2e63af1641
Better fix for memory access in recovery. (Dominique Pelle)
Bram Moolenaar <bram@vim.org>
parents:
2407
diff
changeset
|
1185 #ifdef FEAT_CRYPT |
9e2e63af1641
Better fix for memory access in recovery. (Dominique Pelle)
Bram Moolenaar <bram@vim.org>
parents:
2407
diff
changeset
|
1186 buf->b_p_key = empty_option; |
9e2e63af1641
Better fix for memory access in recovery. (Dominique Pelle)
Bram Moolenaar <bram@vim.org>
parents:
2407
diff
changeset
|
1187 buf->b_p_cm = empty_option; |
9e2e63af1641
Better fix for memory access in recovery. (Dominique Pelle)
Bram Moolenaar <bram@vim.org>
parents:
2407
diff
changeset
|
1188 #endif |
7 | 1189 |
2267 | 1190 /* |
1191 * open the memfile from the old swap file | |
1192 */ | |
1193 p = vim_strsave(fname_used); /* save "fname_used" for the message: | |
1194 mf_open() will consume "fname_used"! */ | |
1195 mfp = mf_open(fname_used, O_RDONLY); | |
1196 fname_used = p; | |
7 | 1197 if (mfp == NULL || mfp->mf_fd < 0) |
1198 { | |
2267 | 1199 if (fname_used != NULL) |
1200 EMSG2(_("E306: Cannot open %s"), fname_used); | |
7 | 1201 goto theend; |
1202 } | |
1203 buf->b_ml.ml_mfp = mfp; | |
2267 | 1204 #ifdef FEAT_CRYPT |
1205 mfp->mf_buffer = buf; | |
1206 #endif | |
7 | 1207 |
1208 /* | |
1209 * The page size set in mf_open() might be different from the page size | |
1210 * used in the swap file, we must get it from block 0. But to read block | |
1211 * 0 we need a page size. Use the minimal size for block 0 here, it will | |
1212 * be set to the real value below. | |
1213 */ | |
1214 mfp->mf_page_size = MIN_SWAP_PAGE_SIZE; | |
1215 | |
2267 | 1216 /* |
1217 * try to read block 0 | |
1218 */ | |
7 | 1219 if ((hp = mf_get(mfp, (blocknr_T)0, 1)) == NULL) |
1220 { | |
1221 msg_start(); | |
1222 MSG_PUTS_ATTR(_("Unable to read block 0 from "), attr | MSG_HIST); | |
1223 msg_outtrans_attr(mfp->mf_fname, attr | MSG_HIST); | |
2267 | 1224 MSG_PUTS_ATTR(_("\nMaybe no changes were made or Vim did not update the swap file."), |
7 | 1225 attr | MSG_HIST); |
1226 msg_end(); | |
1227 goto theend; | |
1228 } | |
1229 b0p = (ZERO_BL *)(hp->bh_data); | |
1230 if (STRNCMP(b0p->b0_version, "VIM 3.0", 7) == 0) | |
1231 { | |
1232 msg_start(); | |
1233 msg_outtrans_attr(mfp->mf_fname, MSG_HIST); | |
1234 MSG_PUTS_ATTR(_(" cannot be used with this version of Vim.\n"), | |
1235 MSG_HIST); | |
1236 MSG_PUTS_ATTR(_("Use Vim version 3.0.\n"), MSG_HIST); | |
1237 msg_end(); | |
1238 goto theend; | |
1239 } | |
2267 | 1240 if (ml_check_b0_id(b0p) == FAIL) |
7 | 1241 { |
1242 EMSG2(_("E307: %s does not look like a Vim swap file"), mfp->mf_fname); | |
1243 goto theend; | |
1244 } | |
1245 if (b0_magic_wrong(b0p)) | |
1246 { | |
1247 msg_start(); | |
1248 msg_outtrans_attr(mfp->mf_fname, attr | MSG_HIST); | |
1249 #if defined(MSDOS) || defined(MSWIN) | |
1250 if (STRNCMP(b0p->b0_hname, "PC ", 3) == 0) | |
1251 MSG_PUTS_ATTR(_(" cannot be used with this version of Vim.\n"), | |
1252 attr | MSG_HIST); | |
1253 else | |
1254 #endif | |
1255 MSG_PUTS_ATTR(_(" cannot be used on this computer.\n"), | |
1256 attr | MSG_HIST); | |
1257 MSG_PUTS_ATTR(_("The file was created on "), attr | MSG_HIST); | |
2273
7f09ce7b4126
Fix a memory leak in encryption. (Dominique Pelle)
Bram Moolenaar <bram@vim.org>
parents:
2272
diff
changeset
|
1258 /* avoid going past the end of a corrupted hostname */ |
7 | 1259 b0p->b0_fname[0] = NUL; |
1260 MSG_PUTS_ATTR(b0p->b0_hname, attr | MSG_HIST); | |
1261 MSG_PUTS_ATTR(_(",\nor the file has been damaged."), attr | MSG_HIST); | |
1262 msg_end(); | |
1263 goto theend; | |
1264 } | |
1105 | 1265 |
2267 | 1266 #ifdef FEAT_CRYPT |
6122 | 1267 for (i = 0; i < (int)(sizeof(id1_codes) / sizeof(int)); ++i) |
1268 if (id1_codes[i] == b0p->b0_id[1]) | |
1269 b0_cm = i; | |
1270 if (b0_cm > 0) | |
2267 | 1271 mch_memmove(mfp->mf_seed, &b0p->b0_seed, MF_SEED_LEN); |
6122 | 1272 crypt_set_cm_option(buf, b0_cm < 0 ? 0 : b0_cm); |
2267 | 1273 #else |
1274 if (b0p->b0_id[1] != BLOCK0_ID1) | |
1275 { | |
2283
7e1bd501306d
Mainly documentation updates.
Bram Moolenaar <bram@vim.org>
parents:
2273
diff
changeset
|
1276 EMSG2(_("E833: %s is encrypted and this version of Vim does not support encryption"), mfp->mf_fname); |
2267 | 1277 goto theend; |
1278 } | |
1279 #endif | |
1280 | |
7 | 1281 /* |
1282 * If we guessed the wrong page size, we have to recalculate the | |
1283 * highest block number in the file. | |
1284 */ | |
1285 if (mfp->mf_page_size != (unsigned)char_to_long(b0p->b0_page_size)) | |
1286 { | |
1105 | 1287 unsigned previous_page_size = mfp->mf_page_size; |
1288 | |
7 | 1289 mf_new_page_size(mfp, (unsigned)char_to_long(b0p->b0_page_size)); |
1105 | 1290 if (mfp->mf_page_size < previous_page_size) |
1291 { | |
1292 msg_start(); | |
1293 msg_outtrans_attr(mfp->mf_fname, attr | MSG_HIST); | |
1294 MSG_PUTS_ATTR(_(" has been damaged (page size is smaller than minimum value).\n"), | |
1295 attr | MSG_HIST); | |
1296 msg_end(); | |
1297 goto theend; | |
1298 } | |
7 | 1299 if ((size = lseek(mfp->mf_fd, (off_t)0L, SEEK_END)) <= 0) |
1300 mfp->mf_blocknr_max = 0; /* no file or empty file */ | |
1301 else | |
1302 mfp->mf_blocknr_max = (blocknr_T)(size / mfp->mf_page_size); | |
1303 mfp->mf_infile_count = mfp->mf_blocknr_max; | |
1105 | 1304 |
1305 /* need to reallocate the memory used to store the data */ | |
1306 p = alloc(mfp->mf_page_size); | |
1307 if (p == NULL) | |
1308 goto theend; | |
1309 mch_memmove(p, hp->bh_data, previous_page_size); | |
1310 vim_free(hp->bh_data); | |
1311 hp->bh_data = p; | |
1312 b0p = (ZERO_BL *)(hp->bh_data); | |
7 | 1313 } |
1314 | |
2267 | 1315 /* |
1316 * If .swp file name given directly, use name from swap file for buffer. | |
1317 */ | |
7 | 1318 if (directly) |
1319 { | |
1320 expand_env(b0p->b0_fname, NameBuff, MAXPATHL); | |
1321 if (setfname(curbuf, NameBuff, NULL, TRUE) == FAIL) | |
1322 goto theend; | |
1323 } | |
1324 | |
1325 home_replace(NULL, mfp->mf_fname, NameBuff, MAXPATHL, TRUE); | |
274 | 1326 smsg((char_u *)_("Using swap file \"%s\""), NameBuff); |
7 | 1327 |
1328 if (buf_spname(curbuf) != NULL) | |
3839 | 1329 vim_strncpy(NameBuff, buf_spname(curbuf), MAXPATHL - 1); |
7 | 1330 else |
1331 home_replace(NULL, curbuf->b_ffname, NameBuff, MAXPATHL, TRUE); | |
274 | 1332 smsg((char_u *)_("Original file \"%s\""), NameBuff); |
7 | 1333 msg_putchar('\n'); |
1334 | |
2267 | 1335 /* |
1336 * check date of swap file and original file | |
1337 */ | |
7 | 1338 mtime = char_to_long(b0p->b0_mtime); |
1339 if (curbuf->b_ffname != NULL | |
1340 && mch_stat((char *)curbuf->b_ffname, &org_stat) != -1 | |
1341 && ((mch_stat((char *)mfp->mf_fname, &swp_stat) != -1 | |
1342 && org_stat.st_mtime > swp_stat.st_mtime) | |
1343 || org_stat.st_mtime != mtime)) | |
1344 { | |
1345 EMSG(_("E308: Warning: Original file may have been changed")); | |
1346 } | |
1347 out_flush(); | |
39 | 1348 |
1349 /* Get the 'fileformat' and 'fileencoding' from block zero. */ | |
1350 b0_ff = (b0p->b0_flags & B0_FF_MASK); | |
1351 if (b0p->b0_flags & B0_HAS_FENC) | |
1352 { | |
2271
2b33a7678e7b
Fix compiler warnings for shadowed variables. Make 'conceal' a long instead
Bram Moolenaar <bram@vim.org>
parents:
2267
diff
changeset
|
1353 int fnsize = B0_FNAME_SIZE_NOCRYPT; |
2267 | 1354 |
1355 #ifdef FEAT_CRYPT | |
1356 /* Use the same size as in add_b0_fenc(). */ | |
1357 if (b0p->b0_id[1] != BLOCK0_ID1) | |
2271
2b33a7678e7b
Fix compiler warnings for shadowed variables. Make 'conceal' a long instead
Bram Moolenaar <bram@vim.org>
parents:
2267
diff
changeset
|
1358 fnsize = B0_FNAME_SIZE_CRYPT; |
2267 | 1359 #endif |
2271
2b33a7678e7b
Fix compiler warnings for shadowed variables. Make 'conceal' a long instead
Bram Moolenaar <bram@vim.org>
parents:
2267
diff
changeset
|
1360 for (p = b0p->b0_fname + fnsize; p > b0p->b0_fname && p[-1] != NUL; --p) |
39 | 1361 ; |
2271
2b33a7678e7b
Fix compiler warnings for shadowed variables. Make 'conceal' a long instead
Bram Moolenaar <bram@vim.org>
parents:
2267
diff
changeset
|
1362 b0_fenc = vim_strnsave(p, (int)(b0p->b0_fname + fnsize - p)); |
39 | 1363 } |
1364 | |
7 | 1365 mf_put(mfp, hp, FALSE, FALSE); /* release block 0 */ |
1366 hp = NULL; | |
1367 | |
1368 /* | |
1369 * Now that we are sure that the file is going to be recovered, clear the | |
1370 * contents of the current buffer. | |
1371 */ | |
1372 while (!(curbuf->b_ml.ml_flags & ML_EMPTY)) | |
1373 ml_delete((linenr_T)1, FALSE); | |
1374 | |
1375 /* | |
1376 * Try reading the original file to obtain the values of 'fileformat', | |
1377 * 'fileencoding', etc. Ignore errors. The text itself is not used. | |
2267 | 1378 * When the file is encrypted the user is asked to enter the key. |
7 | 1379 */ |
1380 if (curbuf->b_ffname != NULL) | |
2162
0527eb0f6918
After recovery check if the text changed. If it did mark the buffer as
Bram Moolenaar <bram@vim.org>
parents:
2145
diff
changeset
|
1381 orig_file_status = readfile(curbuf->b_ffname, NULL, (linenr_T)0, |
7 | 1382 (linenr_T)0, (linenr_T)MAXLNUM, NULL, READ_NEW); |
1383 | |
2267 | 1384 #ifdef FEAT_CRYPT |
1385 if (b0_cm >= 0) | |
1386 { | |
1387 /* Need to ask the user for the crypt key. If this fails we continue | |
1388 * without a key, will probably get garbage text. */ | |
1389 if (*curbuf->b_p_key != NUL) | |
1390 { | |
1391 smsg((char_u *)_("Swap file is encrypted: \"%s\""), fname_used); | |
1392 MSG_PUTS(_("\nIf you entered a new crypt key but did not write the text file,")); | |
1393 MSG_PUTS(_("\nenter the new crypt key.")); | |
1394 MSG_PUTS(_("\nIf you wrote the text file after changing the crypt key press enter")); | |
1395 MSG_PUTS(_("\nto use the same key for text file and swap file")); | |
1396 } | |
1397 else | |
1398 smsg((char_u *)_(need_key_msg), fname_used); | |
6122 | 1399 buf->b_p_key = crypt_get_key(FALSE, FALSE); |
2267 | 1400 if (buf->b_p_key == NULL) |
1401 buf->b_p_key = curbuf->b_p_key; | |
1402 else if (*buf->b_p_key == NUL) | |
1403 { | |
1404 vim_free(buf->b_p_key); | |
1405 buf->b_p_key = curbuf->b_p_key; | |
1406 } | |
1407 if (buf->b_p_key == NULL) | |
1408 buf->b_p_key = empty_option; | |
2165
733f0dc510c3
Undo changes that are meant for the Vim 7.3 branch.
Bram Moolenaar <bram@vim.org>
parents:
2162
diff
changeset
|
1409 } |
2267 | 1410 #endif |
7 | 1411 |
39 | 1412 /* Use the 'fileformat' and 'fileencoding' as stored in the swap file. */ |
1413 if (b0_ff != 0) | |
1414 set_fileformat(b0_ff - 1, OPT_LOCAL); | |
1415 if (b0_fenc != NULL) | |
1416 { | |
1417 set_option_value((char_u *)"fenc", 0L, b0_fenc, OPT_LOCAL); | |
1418 vim_free(b0_fenc); | |
1419 } | |
1420 unchanged(curbuf, TRUE); | |
1421 | |
7 | 1422 bnum = 1; /* start with block 1 */ |
1423 page_count = 1; /* which is 1 page */ | |
1424 lnum = 0; /* append after line 0 in curbuf */ | |
1425 line_count = 0; | |
1426 idx = 0; /* start with first index in block 1 */ | |
1427 error = 0; | |
1428 buf->b_ml.ml_stack_top = 0; | |
1429 buf->b_ml.ml_stack = NULL; | |
1430 buf->b_ml.ml_stack_size = 0; /* no stack yet */ | |
1431 | |
1432 if (curbuf->b_ffname == NULL) | |
1433 cannot_open = TRUE; | |
1434 else | |
1435 cannot_open = FALSE; | |
1436 | |
1437 serious_error = FALSE; | |
1438 for ( ; !got_int; line_breakcheck()) | |
1439 { | |
1440 if (hp != NULL) | |
1441 mf_put(mfp, hp, FALSE, FALSE); /* release previous block */ | |
1442 | |
1443 /* | |
1444 * get block | |
1445 */ | |
1446 if ((hp = mf_get(mfp, (blocknr_T)bnum, page_count)) == NULL) | |
1447 { | |
1448 if (bnum == 1) | |
1449 { | |
1450 EMSG2(_("E309: Unable to read block 1 from %s"), mfp->mf_fname); | |
1451 goto theend; | |
1452 } | |
1453 ++error; | |
1454 ml_append(lnum++, (char_u *)_("???MANY LINES MISSING"), | |
1455 (colnr_T)0, TRUE); | |
1456 } | |
1457 else /* there is a block */ | |
1458 { | |
1459 pp = (PTR_BL *)(hp->bh_data); | |
1460 if (pp->pb_id == PTR_ID) /* it is a pointer block */ | |
1461 { | |
1462 /* check line count when using pointer block first time */ | |
1463 if (idx == 0 && line_count != 0) | |
1464 { | |
1465 for (i = 0; i < (int)pp->pb_count; ++i) | |
1466 line_count -= pp->pb_pointer[i].pe_line_count; | |
1467 if (line_count != 0) | |
1468 { | |
1469 ++error; | |
1470 ml_append(lnum++, (char_u *)_("???LINE COUNT WRONG"), | |
1471 (colnr_T)0, TRUE); | |
1472 } | |
1473 } | |
1474 | |
1475 if (pp->pb_count == 0) | |
1476 { | |
1477 ml_append(lnum++, (char_u *)_("???EMPTY BLOCK"), | |
1478 (colnr_T)0, TRUE); | |
1479 ++error; | |
1480 } | |
1481 else if (idx < (int)pp->pb_count) /* go a block deeper */ | |
1482 { | |
1483 if (pp->pb_pointer[idx].pe_bnum < 0) | |
1484 { | |
1485 /* | |
1486 * Data block with negative block number. | |
1487 * Try to read lines from the original file. | |
1488 * This is slow, but it works. | |
1489 */ | |
1490 if (!cannot_open) | |
1491 { | |
1492 line_count = pp->pb_pointer[idx].pe_line_count; | |
1493 if (readfile(curbuf->b_ffname, NULL, lnum, | |
1494 pp->pb_pointer[idx].pe_old_lnum - 1, | |
1495 line_count, NULL, 0) == FAIL) | |
1496 cannot_open = TRUE; | |
1497 else | |
1498 lnum += line_count; | |
1499 } | |
1500 if (cannot_open) | |
1501 { | |
1502 ++error; | |
1503 ml_append(lnum++, (char_u *)_("???LINES MISSING"), | |
1504 (colnr_T)0, TRUE); | |
1505 } | |
1506 ++idx; /* get same block again for next index */ | |
1507 continue; | |
1508 } | |
1509 | |
1510 /* | |
1511 * going one block deeper in the tree | |
1512 */ | |
1513 if ((top = ml_add_stack(buf)) < 0) /* new entry in stack */ | |
1514 { | |
1515 ++error; | |
1516 break; /* out of memory */ | |
1517 } | |
1518 ip = &(buf->b_ml.ml_stack[top]); | |
1519 ip->ip_bnum = bnum; | |
1520 ip->ip_index = idx; | |
1521 | |
1522 bnum = pp->pb_pointer[idx].pe_bnum; | |
1523 line_count = pp->pb_pointer[idx].pe_line_count; | |
1524 page_count = pp->pb_pointer[idx].pe_page_count; | |
2885 | 1525 idx = 0; |
7 | 1526 continue; |
1527 } | |
1528 } | |
1529 else /* not a pointer block */ | |
1530 { | |
1531 dp = (DATA_BL *)(hp->bh_data); | |
1532 if (dp->db_id != DATA_ID) /* block id wrong */ | |
1533 { | |
1534 if (bnum == 1) | |
1535 { | |
1536 EMSG2(_("E310: Block 1 ID wrong (%s not a .swp file?)"), | |
1537 mfp->mf_fname); | |
1538 goto theend; | |
1539 } | |
1540 ++error; | |
1541 ml_append(lnum++, (char_u *)_("???BLOCK MISSING"), | |
1542 (colnr_T)0, TRUE); | |
1543 } | |
1544 else | |
1545 { | |
1546 /* | |
1547 * it is a data block | |
1548 * Append all the lines in this block | |
1549 */ | |
1550 has_error = FALSE; | |
1551 /* | |
1552 * check length of block | |
1553 * if wrong, use length in pointer block | |
1554 */ | |
1555 if (page_count * mfp->mf_page_size != dp->db_txt_end) | |
1556 { | |
1557 ml_append(lnum++, (char_u *)_("??? from here until ???END lines may be messed up"), | |
1558 (colnr_T)0, TRUE); | |
1559 ++error; | |
1560 has_error = TRUE; | |
1561 dp->db_txt_end = page_count * mfp->mf_page_size; | |
1562 } | |
1563 | |
1564 /* make sure there is a NUL at the end of the block */ | |
1565 *((char_u *)dp + dp->db_txt_end - 1) = NUL; | |
1566 | |
1567 /* | |
1568 * check number of lines in block | |
1569 * if wrong, use count in data block | |
1570 */ | |
1571 if (line_count != dp->db_line_count) | |
1572 { | |
1573 ml_append(lnum++, (char_u *)_("??? from here until ???END lines may have been inserted/deleted"), | |
1574 (colnr_T)0, TRUE); | |
1575 ++error; | |
1576 has_error = TRUE; | |
1577 } | |
1578 | |
1579 for (i = 0; i < dp->db_line_count; ++i) | |
1580 { | |
1581 txt_start = (dp->db_index[i] & DB_INDEX_MASK); | |
1978 | 1582 if (txt_start <= (int)HEADER_SIZE |
7 | 1583 || txt_start >= (int)dp->db_txt_end) |
1584 { | |
1585 p = (char_u *)"???"; | |
1586 ++error; | |
1587 } | |
1588 else | |
1589 p = (char_u *)dp + txt_start; | |
1590 ml_append(lnum++, p, (colnr_T)0, TRUE); | |
1591 } | |
1592 if (has_error) | |
1978 | 1593 ml_append(lnum++, (char_u *)_("???END"), |
1594 (colnr_T)0, TRUE); | |
7 | 1595 } |
1596 } | |
1597 } | |
1598 | |
1599 if (buf->b_ml.ml_stack_top == 0) /* finished */ | |
1600 break; | |
1601 | |
1602 /* | |
1603 * go one block up in the tree | |
1604 */ | |
1605 ip = &(buf->b_ml.ml_stack[--(buf->b_ml.ml_stack_top)]); | |
1606 bnum = ip->ip_bnum; | |
1607 idx = ip->ip_index + 1; /* go to next index */ | |
1608 page_count = 1; | |
1609 } | |
1610 | |
1611 /* | |
2162
0527eb0f6918
After recovery check if the text changed. If it did mark the buffer as
Bram Moolenaar <bram@vim.org>
parents:
2145
diff
changeset
|
1612 * Compare the buffer contents with the original file. When they differ |
0527eb0f6918
After recovery check if the text changed. If it did mark the buffer as
Bram Moolenaar <bram@vim.org>
parents:
2145
diff
changeset
|
1613 * set the 'modified' flag. |
0527eb0f6918
After recovery check if the text changed. If it did mark the buffer as
Bram Moolenaar <bram@vim.org>
parents:
2145
diff
changeset
|
1614 * Lines 1 - lnum are the new contents. |
0527eb0f6918
After recovery check if the text changed. If it did mark the buffer as
Bram Moolenaar <bram@vim.org>
parents:
2145
diff
changeset
|
1615 * Lines lnum + 1 to ml_line_count are the original contents. |
0527eb0f6918
After recovery check if the text changed. If it did mark the buffer as
Bram Moolenaar <bram@vim.org>
parents:
2145
diff
changeset
|
1616 * Line ml_line_count + 1 in the dummy empty line. |
7 | 1617 */ |
2162
0527eb0f6918
After recovery check if the text changed. If it did mark the buffer as
Bram Moolenaar <bram@vim.org>
parents:
2145
diff
changeset
|
1618 if (orig_file_status != OK || curbuf->b_ml.ml_line_count != lnum * 2 + 1) |
0527eb0f6918
After recovery check if the text changed. If it did mark the buffer as
Bram Moolenaar <bram@vim.org>
parents:
2145
diff
changeset
|
1619 { |
0527eb0f6918
After recovery check if the text changed. If it did mark the buffer as
Bram Moolenaar <bram@vim.org>
parents:
2145
diff
changeset
|
1620 /* Recovering an empty file results in two lines and the first line is |
0527eb0f6918
After recovery check if the text changed. If it did mark the buffer as
Bram Moolenaar <bram@vim.org>
parents:
2145
diff
changeset
|
1621 * empty. Don't set the modified flag then. */ |
0527eb0f6918
After recovery check if the text changed. If it did mark the buffer as
Bram Moolenaar <bram@vim.org>
parents:
2145
diff
changeset
|
1622 if (!(curbuf->b_ml.ml_line_count == 2 && *ml_get(1) == NUL)) |
0527eb0f6918
After recovery check if the text changed. If it did mark the buffer as
Bram Moolenaar <bram@vim.org>
parents:
2145
diff
changeset
|
1623 { |
0527eb0f6918
After recovery check if the text changed. If it did mark the buffer as
Bram Moolenaar <bram@vim.org>
parents:
2145
diff
changeset
|
1624 changed_int(); |
0527eb0f6918
After recovery check if the text changed. If it did mark the buffer as
Bram Moolenaar <bram@vim.org>
parents:
2145
diff
changeset
|
1625 ++curbuf->b_changedtick; |
0527eb0f6918
After recovery check if the text changed. If it did mark the buffer as
Bram Moolenaar <bram@vim.org>
parents:
2145
diff
changeset
|
1626 } |
0527eb0f6918
After recovery check if the text changed. If it did mark the buffer as
Bram Moolenaar <bram@vim.org>
parents:
2145
diff
changeset
|
1627 } |
0527eb0f6918
After recovery check if the text changed. If it did mark the buffer as
Bram Moolenaar <bram@vim.org>
parents:
2145
diff
changeset
|
1628 else |
0527eb0f6918
After recovery check if the text changed. If it did mark the buffer as
Bram Moolenaar <bram@vim.org>
parents:
2145
diff
changeset
|
1629 { |
0527eb0f6918
After recovery check if the text changed. If it did mark the buffer as
Bram Moolenaar <bram@vim.org>
parents:
2145
diff
changeset
|
1630 for (idx = 1; idx <= lnum; ++idx) |
0527eb0f6918
After recovery check if the text changed. If it did mark the buffer as
Bram Moolenaar <bram@vim.org>
parents:
2145
diff
changeset
|
1631 { |
0527eb0f6918
After recovery check if the text changed. If it did mark the buffer as
Bram Moolenaar <bram@vim.org>
parents:
2145
diff
changeset
|
1632 /* Need to copy one line, fetching the other one may flush it. */ |
0527eb0f6918
After recovery check if the text changed. If it did mark the buffer as
Bram Moolenaar <bram@vim.org>
parents:
2145
diff
changeset
|
1633 p = vim_strsave(ml_get(idx)); |
0527eb0f6918
After recovery check if the text changed. If it did mark the buffer as
Bram Moolenaar <bram@vim.org>
parents:
2145
diff
changeset
|
1634 i = STRCMP(p, ml_get(idx + lnum)); |
0527eb0f6918
After recovery check if the text changed. If it did mark the buffer as
Bram Moolenaar <bram@vim.org>
parents:
2145
diff
changeset
|
1635 vim_free(p); |
0527eb0f6918
After recovery check if the text changed. If it did mark the buffer as
Bram Moolenaar <bram@vim.org>
parents:
2145
diff
changeset
|
1636 if (i != 0) |
0527eb0f6918
After recovery check if the text changed. If it did mark the buffer as
Bram Moolenaar <bram@vim.org>
parents:
2145
diff
changeset
|
1637 { |
0527eb0f6918
After recovery check if the text changed. If it did mark the buffer as
Bram Moolenaar <bram@vim.org>
parents:
2145
diff
changeset
|
1638 changed_int(); |
0527eb0f6918
After recovery check if the text changed. If it did mark the buffer as
Bram Moolenaar <bram@vim.org>
parents:
2145
diff
changeset
|
1639 ++curbuf->b_changedtick; |
0527eb0f6918
After recovery check if the text changed. If it did mark the buffer as
Bram Moolenaar <bram@vim.org>
parents:
2145
diff
changeset
|
1640 break; |
0527eb0f6918
After recovery check if the text changed. If it did mark the buffer as
Bram Moolenaar <bram@vim.org>
parents:
2145
diff
changeset
|
1641 } |
0527eb0f6918
After recovery check if the text changed. If it did mark the buffer as
Bram Moolenaar <bram@vim.org>
parents:
2145
diff
changeset
|
1642 } |
0527eb0f6918
After recovery check if the text changed. If it did mark the buffer as
Bram Moolenaar <bram@vim.org>
parents:
2145
diff
changeset
|
1643 } |
0527eb0f6918
After recovery check if the text changed. If it did mark the buffer as
Bram Moolenaar <bram@vim.org>
parents:
2145
diff
changeset
|
1644 |
0527eb0f6918
After recovery check if the text changed. If it did mark the buffer as
Bram Moolenaar <bram@vim.org>
parents:
2145
diff
changeset
|
1645 /* |
0527eb0f6918
After recovery check if the text changed. If it did mark the buffer as
Bram Moolenaar <bram@vim.org>
parents:
2145
diff
changeset
|
1646 * Delete the lines from the original file and the dummy line from the |
0527eb0f6918
After recovery check if the text changed. If it did mark the buffer as
Bram Moolenaar <bram@vim.org>
parents:
2145
diff
changeset
|
1647 * empty buffer. These will now be after the last line in the buffer. |
0527eb0f6918
After recovery check if the text changed. If it did mark the buffer as
Bram Moolenaar <bram@vim.org>
parents:
2145
diff
changeset
|
1648 */ |
0527eb0f6918
After recovery check if the text changed. If it did mark the buffer as
Bram Moolenaar <bram@vim.org>
parents:
2145
diff
changeset
|
1649 while (curbuf->b_ml.ml_line_count > lnum |
0527eb0f6918
After recovery check if the text changed. If it did mark the buffer as
Bram Moolenaar <bram@vim.org>
parents:
2145
diff
changeset
|
1650 && !(curbuf->b_ml.ml_flags & ML_EMPTY)) |
0527eb0f6918
After recovery check if the text changed. If it did mark the buffer as
Bram Moolenaar <bram@vim.org>
parents:
2145
diff
changeset
|
1651 ml_delete(curbuf->b_ml.ml_line_count, FALSE); |
7 | 1652 curbuf->b_flags |= BF_RECOVERED; |
1653 | |
1654 recoverymode = FALSE; | |
1655 if (got_int) | |
1656 EMSG(_("E311: Recovery Interrupted")); | |
1657 else if (error) | |
1658 { | |
1659 ++no_wait_return; | |
1660 MSG(">>>>>>>>>>>>>"); | |
1661 EMSG(_("E312: Errors detected while recovering; look for lines starting with ???")); | |
1662 --no_wait_return; | |
1663 MSG(_("See \":help E312\" for more information.")); | |
1664 MSG(">>>>>>>>>>>>>"); | |
1665 } | |
1666 else | |
1667 { | |
2162
0527eb0f6918
After recovery check if the text changed. If it did mark the buffer as
Bram Moolenaar <bram@vim.org>
parents:
2145
diff
changeset
|
1668 if (curbuf->b_changed) |
0527eb0f6918
After recovery check if the text changed. If it did mark the buffer as
Bram Moolenaar <bram@vim.org>
parents:
2145
diff
changeset
|
1669 { |
0527eb0f6918
After recovery check if the text changed. If it did mark the buffer as
Bram Moolenaar <bram@vim.org>
parents:
2145
diff
changeset
|
1670 MSG(_("Recovery completed. You should check if everything is OK.")); |
0527eb0f6918
After recovery check if the text changed. If it did mark the buffer as
Bram Moolenaar <bram@vim.org>
parents:
2145
diff
changeset
|
1671 MSG_PUTS(_("\n(You might want to write out this file under another name\n")); |
0527eb0f6918
After recovery check if the text changed. If it did mark the buffer as
Bram Moolenaar <bram@vim.org>
parents:
2145
diff
changeset
|
1672 MSG_PUTS(_("and run diff with the original file to check for changes)")); |
0527eb0f6918
After recovery check if the text changed. If it did mark the buffer as
Bram Moolenaar <bram@vim.org>
parents:
2145
diff
changeset
|
1673 } |
0527eb0f6918
After recovery check if the text changed. If it did mark the buffer as
Bram Moolenaar <bram@vim.org>
parents:
2145
diff
changeset
|
1674 else |
0527eb0f6918
After recovery check if the text changed. If it did mark the buffer as
Bram Moolenaar <bram@vim.org>
parents:
2145
diff
changeset
|
1675 MSG(_("Recovery completed. Buffer contents equals file contents.")); |
0527eb0f6918
After recovery check if the text changed. If it did mark the buffer as
Bram Moolenaar <bram@vim.org>
parents:
2145
diff
changeset
|
1676 MSG_PUTS(_("\nYou may want to delete the .swp file now.\n\n")); |
7 | 1677 cmdline_row = msg_row; |
1678 } | |
2267 | 1679 #ifdef FEAT_CRYPT |
1680 if (*buf->b_p_key != NUL && STRCMP(curbuf->b_p_key, buf->b_p_key) != 0) | |
1681 { | |
1682 MSG_PUTS(_("Using crypt key from swap file for the text file.\n")); | |
1683 set_option_value((char_u *)"key", 0L, buf->b_p_key, OPT_LOCAL); | |
1684 } | |
1685 #endif | |
7 | 1686 redraw_curbuf_later(NOT_VALID); |
1687 | |
1688 theend: | |
2267 | 1689 vim_free(fname_used); |
7 | 1690 recoverymode = FALSE; |
1691 if (mfp != NULL) | |
1692 { | |
1693 if (hp != NULL) | |
1694 mf_put(mfp, hp, FALSE, FALSE); | |
1695 mf_close(mfp, FALSE); /* will also vim_free(mfp->mf_fname) */ | |
1696 } | |
1053 | 1697 if (buf != NULL) |
1698 { | |
2267 | 1699 #ifdef FEAT_CRYPT |
1700 if (buf->b_p_key != curbuf->b_p_key) | |
1701 free_string_option(buf->b_p_key); | |
2407
6bc102a4bff8
Fix memory access to 'cryptmethod' during recovery. (Dominique Pelle)
Bram Moolenaar <bram@vim.org>
parents:
2394
diff
changeset
|
1702 free_string_option(buf->b_p_cm); |
2267 | 1703 #endif |
1053 | 1704 vim_free(buf->b_ml.ml_stack); |
1705 vim_free(buf); | |
1706 } | |
7 | 1707 if (serious_error && called_from_main) |
1708 ml_close(curbuf, TRUE); | |
1709 #ifdef FEAT_AUTOCMD | |
1710 else | |
1711 { | |
1712 apply_autocmds(EVENT_BUFREADPOST, NULL, curbuf->b_fname, FALSE, curbuf); | |
1713 apply_autocmds(EVENT_BUFWINENTER, NULL, curbuf->b_fname, FALSE, curbuf); | |
1714 } | |
1715 #endif | |
1716 return; | |
1717 } | |
1718 | |
1719 /* | |
1720 * Find the names of swap files in current directory and the directory given | |
1721 * with the 'directory' option. | |
1722 * | |
1723 * Used to: | |
1724 * - list the swap files for "vim -r" | |
1725 * - count the number of swap files when recovering | |
1726 * - list the swap files when recovering | |
1727 * - find the name of the n'th swap file when recovering | |
1728 */ | |
1729 int | |
2267 | 1730 recover_names(fname, list, nr, fname_out) |
1731 char_u *fname; /* base for swap file name */ | |
1732 int list; /* when TRUE, list the swap file names */ | |
1733 int nr; /* when non-zero, return nr'th swap file name */ | |
1734 char_u **fname_out; /* result when "nr" > 0 */ | |
7 | 1735 { |
1736 int num_names; | |
1737 char_u *(names[6]); | |
1738 char_u *tail; | |
1739 char_u *p; | |
1740 int num_files; | |
1741 int file_count = 0; | |
1742 char_u **files; | |
1743 int i; | |
1744 char_u *dirp; | |
1745 char_u *dir_name; | |
2175 | 1746 char_u *fname_res = NULL; |
2145
de0e7ca61893
updated for version 7.2.427
Bram Moolenaar <bram@zimbu.org>
parents:
2108
diff
changeset
|
1747 #ifdef HAVE_READLINK |
de0e7ca61893
updated for version 7.2.427
Bram Moolenaar <bram@zimbu.org>
parents:
2108
diff
changeset
|
1748 char_u fname_buf[MAXPATHL]; |
2175 | 1749 #endif |
1750 | |
1751 if (fname != NULL) | |
1752 { | |
1753 #ifdef HAVE_READLINK | |
2267 | 1754 /* Expand symlink in the file name, because the swap file is created |
1755 * with the actual file instead of with the symlink. */ | |
1756 if (resolve_symlink(fname, fname_buf) == OK) | |
1757 fname_res = fname_buf; | |
1758 else | |
2145
de0e7ca61893
updated for version 7.2.427
Bram Moolenaar <bram@zimbu.org>
parents:
2108
diff
changeset
|
1759 #endif |
2267 | 1760 fname_res = fname; |
2175 | 1761 } |
7 | 1762 |
1763 if (list) | |
1764 { | |
2145
de0e7ca61893
updated for version 7.2.427
Bram Moolenaar <bram@zimbu.org>
parents:
2108
diff
changeset
|
1765 /* use msg() to start the scrolling properly */ |
7 | 1766 msg((char_u *)_("Swap files found:")); |
1767 msg_putchar('\n'); | |
1768 } | |
1769 | |
1770 /* | |
1771 * Do the loop for every directory in 'directory'. | |
1772 * First allocate some memory to put the directory name in. | |
1773 */ | |
1774 dir_name = alloc((unsigned)STRLEN(p_dir) + 1); | |
1775 dirp = p_dir; | |
1776 while (dir_name != NULL && *dirp) | |
1777 { | |
1778 /* | |
1779 * Isolate a directory name from *dirp and put it in dir_name (we know | |
1780 * it is large enough, so use 31000 for length). | |
1781 * Advance dirp to next directory name. | |
1782 */ | |
1783 (void)copy_option_part(&dirp, dir_name, 31000, ","); | |
1784 | |
1785 if (dir_name[0] == '.' && dir_name[1] == NUL) /* check current dir */ | |
1786 { | |
2267 | 1787 if (fname == NULL) |
7 | 1788 { |
1789 #ifdef VMS | |
1790 names[0] = vim_strsave((char_u *)"*_sw%"); | |
1791 #else | |
1792 names[0] = vim_strsave((char_u *)"*.sw?"); | |
1793 #endif | |
1005 | 1794 #if defined(UNIX) || defined(WIN3264) |
1795 /* For Unix names starting with a dot are special. MS-Windows | |
1796 * supports this too, on some file systems. */ | |
7 | 1797 names[1] = vim_strsave((char_u *)".*.sw?"); |
1798 names[2] = vim_strsave((char_u *)".sw?"); | |
1799 num_names = 3; | |
1800 #else | |
1801 # ifdef VMS | |
1802 names[1] = vim_strsave((char_u *)".*_sw%"); | |
1803 num_names = 2; | |
1804 # else | |
1805 num_names = 1; | |
1806 # endif | |
1807 #endif | |
1808 } | |
1809 else | |
2145
de0e7ca61893
updated for version 7.2.427
Bram Moolenaar <bram@zimbu.org>
parents:
2108
diff
changeset
|
1810 num_names = recov_file_names(names, fname_res, TRUE); |
7 | 1811 } |
1812 else /* check directory dir_name */ | |
1813 { | |
2267 | 1814 if (fname == NULL) |
7 | 1815 { |
1816 #ifdef VMS | |
1817 names[0] = concat_fnames(dir_name, (char_u *)"*_sw%", TRUE); | |
1818 #else | |
1819 names[0] = concat_fnames(dir_name, (char_u *)"*.sw?", TRUE); | |
1820 #endif | |
1005 | 1821 #if defined(UNIX) || defined(WIN3264) |
1822 /* For Unix names starting with a dot are special. MS-Windows | |
1823 * supports this too, on some file systems. */ | |
7 | 1824 names[1] = concat_fnames(dir_name, (char_u *)".*.sw?", TRUE); |
1825 names[2] = concat_fnames(dir_name, (char_u *)".sw?", TRUE); | |
1826 num_names = 3; | |
1827 #else | |
1828 # ifdef VMS | |
1829 names[1] = concat_fnames(dir_name, (char_u *)".*_sw%", TRUE); | |
1830 num_names = 2; | |
1831 # else | |
1832 num_names = 1; | |
1833 # endif | |
1834 #endif | |
1835 } | |
1836 else | |
1837 { | |
1838 #if defined(UNIX) || defined(WIN3264) | |
1839 p = dir_name + STRLEN(dir_name); | |
39 | 1840 if (after_pathsep(dir_name, p) && p[-1] == p[-2]) |
7 | 1841 { |
1842 /* Ends with '//', Use Full path for swap name */ | |
2145
de0e7ca61893
updated for version 7.2.427
Bram Moolenaar <bram@zimbu.org>
parents:
2108
diff
changeset
|
1843 tail = make_percent_swname(dir_name, fname_res); |
7 | 1844 } |
1845 else | |
1846 #endif | |
1847 { | |
2145
de0e7ca61893
updated for version 7.2.427
Bram Moolenaar <bram@zimbu.org>
parents:
2108
diff
changeset
|
1848 tail = gettail(fname_res); |
7 | 1849 tail = concat_fnames(dir_name, tail, TRUE); |
1850 } | |
1851 if (tail == NULL) | |
1852 num_names = 0; | |
1853 else | |
1854 { | |
1855 num_names = recov_file_names(names, tail, FALSE); | |
1856 vim_free(tail); | |
1857 } | |
1858 } | |
1859 } | |
1860 | |
1861 /* check for out-of-memory */ | |
1862 for (i = 0; i < num_names; ++i) | |
1863 { | |
1864 if (names[i] == NULL) | |
1865 { | |
1866 for (i = 0; i < num_names; ++i) | |
1867 vim_free(names[i]); | |
1868 num_names = 0; | |
1869 } | |
1870 } | |
1871 if (num_names == 0) | |
1872 num_files = 0; | |
1873 else if (expand_wildcards(num_names, names, &num_files, &files, | |
1874 EW_KEEPALL|EW_FILE|EW_SILENT) == FAIL) | |
1875 num_files = 0; | |
1876 | |
1877 /* | |
1878 * When no swap file found, wildcard expansion might have failed (e.g. | |
1879 * not able to execute the shell). | |
1880 * Try finding a swap file by simply adding ".swp" to the file name. | |
1881 */ | |
2267 | 1882 if (*dirp == NUL && file_count + num_files == 0 && fname != NULL) |
7 | 1883 { |
1884 struct stat st; | |
1885 char_u *swapname; | |
1886 | |
2145
de0e7ca61893
updated for version 7.2.427
Bram Moolenaar <bram@zimbu.org>
parents:
2108
diff
changeset
|
1887 swapname = modname(fname_res, |
2823 | 1888 #if defined(VMS) |
2145
de0e7ca61893
updated for version 7.2.427
Bram Moolenaar <bram@zimbu.org>
parents:
2108
diff
changeset
|
1889 (char_u *)"_swp", FALSE |
7 | 1890 #else |
2145
de0e7ca61893
updated for version 7.2.427
Bram Moolenaar <bram@zimbu.org>
parents:
2108
diff
changeset
|
1891 (char_u *)".swp", TRUE |
7 | 1892 #endif |
2145
de0e7ca61893
updated for version 7.2.427
Bram Moolenaar <bram@zimbu.org>
parents:
2108
diff
changeset
|
1893 ); |
7 | 1894 if (swapname != NULL) |
1895 { | |
1896 if (mch_stat((char *)swapname, &st) != -1) /* It exists! */ | |
1897 { | |
1898 files = (char_u **)alloc((unsigned)sizeof(char_u *)); | |
1899 if (files != NULL) | |
1900 { | |
1901 files[0] = swapname; | |
1902 swapname = NULL; | |
1903 num_files = 1; | |
1904 } | |
1905 } | |
1906 vim_free(swapname); | |
1907 } | |
1908 } | |
1909 | |
1910 /* | |
1911 * remove swapfile name of the current buffer, it must be ignored | |
1912 */ | |
1913 if (curbuf->b_ml.ml_mfp != NULL | |
1914 && (p = curbuf->b_ml.ml_mfp->mf_fname) != NULL) | |
1915 { | |
1916 for (i = 0; i < num_files; ++i) | |
1917 if (fullpathcmp(p, files[i], TRUE) & FPC_SAME) | |
1918 { | |
1855 | 1919 /* Remove the name from files[i]. Move further entries |
1920 * down. When the array becomes empty free it here, since | |
1921 * FreeWild() won't be called below. */ | |
7 | 1922 vim_free(files[i]); |
1855 | 1923 if (--num_files == 0) |
1924 vim_free(files); | |
1925 else | |
1926 for ( ; i < num_files; ++i) | |
1927 files[i] = files[i + 1]; | |
7 | 1928 } |
1929 } | |
838 | 1930 if (nr > 0) |
7 | 1931 { |
1932 file_count += num_files; | |
1933 if (nr <= file_count) | |
1934 { | |
2267 | 1935 *fname_out = vim_strsave( |
1936 files[nr - 1 + num_files - file_count]); | |
7 | 1937 dirp = (char_u *)""; /* stop searching */ |
1938 } | |
1939 } | |
1940 else if (list) | |
1941 { | |
1942 if (dir_name[0] == '.' && dir_name[1] == NUL) | |
1943 { | |
2267 | 1944 if (fname == NULL) |
7 | 1945 MSG_PUTS(_(" In current directory:\n")); |
1946 else | |
1947 MSG_PUTS(_(" Using specified name:\n")); | |
1948 } | |
1949 else | |
1950 { | |
1951 MSG_PUTS(_(" In directory ")); | |
1952 msg_home_replace(dir_name); | |
1953 MSG_PUTS(":\n"); | |
1954 } | |
1955 | |
1956 if (num_files) | |
1957 { | |
1958 for (i = 0; i < num_files; ++i) | |
1959 { | |
1960 /* print the swap file name */ | |
1961 msg_outnum((long)++file_count); | |
1962 MSG_PUTS(". "); | |
1963 msg_puts(gettail(files[i])); | |
1964 msg_putchar('\n'); | |
1965 (void)swapfile_info(files[i]); | |
1966 } | |
1967 } | |
1968 else | |
1969 MSG_PUTS(_(" -- none --\n")); | |
1970 out_flush(); | |
1971 } | |
1972 else | |
1973 file_count += num_files; | |
1974 | |
1975 for (i = 0; i < num_names; ++i) | |
1976 vim_free(names[i]); | |
838 | 1977 if (num_files > 0) |
1978 FreeWild(num_files, files); | |
7 | 1979 } |
1980 vim_free(dir_name); | |
1981 return file_count; | |
1982 } | |
1983 | |
1984 #if defined(UNIX) || defined(WIN3264) /* Need _very_ long file names */ | |
1985 /* | |
1986 * Append the full path to name with path separators made into percent | |
1987 * signs, to dir. An unnamed buffer is handled as "" (<currentdir>/"") | |
1988 */ | |
1989 static char_u * | |
1990 make_percent_swname(dir, name) | |
1991 char_u *dir; | |
1992 char_u *name; | |
1993 { | |
39 | 1994 char_u *d, *s, *f; |
7 | 1995 |
1996 f = fix_fname(name != NULL ? name : (char_u *) ""); | |
1997 d = NULL; | |
1998 if (f != NULL) | |
1999 { | |
2000 s = alloc((unsigned)(STRLEN(f) + 1)); | |
2001 if (s != NULL) | |
2002 { | |
39 | 2003 STRCPY(s, f); |
2004 for (d = s; *d != NUL; mb_ptr_adv(d)) | |
2005 if (vim_ispathsep(*d)) | |
2006 *d = '%'; | |
7 | 2007 d = concat_fnames(dir, s, TRUE); |
2008 vim_free(s); | |
2009 } | |
2010 vim_free(f); | |
2011 } | |
2012 return d; | |
2013 } | |
2014 #endif | |
2015 | |
2016 #if (defined(UNIX) || defined(__EMX__) || defined(VMS)) && (defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)) | |
2017 static int process_still_running; | |
2018 #endif | |
2019 | |
2020 /* | |
580 | 2021 * Give information about an existing swap file. |
7 | 2022 * Returns timestamp (0 when unknown). |
2023 */ | |
2024 static time_t | |
2025 swapfile_info(fname) | |
2026 char_u *fname; | |
2027 { | |
2028 struct stat st; | |
2029 int fd; | |
2030 struct block0 b0; | |
2031 time_t x = (time_t)0; | |
1001 | 2032 char *p; |
7 | 2033 #ifdef UNIX |
2034 char_u uname[B0_UNAME_SIZE]; | |
2035 #endif | |
2036 | |
2037 /* print the swap file date */ | |
2038 if (mch_stat((char *)fname, &st) != -1) | |
2039 { | |
2040 #ifdef UNIX | |
2041 /* print name of owner of the file */ | |
2042 if (mch_get_uname(st.st_uid, uname, B0_UNAME_SIZE) == OK) | |
2043 { | |
2044 MSG_PUTS(_(" owned by: ")); | |
2045 msg_outtrans(uname); | |
2046 MSG_PUTS(_(" dated: ")); | |
2047 } | |
2048 else | |
2049 #endif | |
2050 MSG_PUTS(_(" dated: ")); | |
2051 x = st.st_mtime; /* Manx C can't do &st.st_mtime */ | |
1001 | 2052 p = ctime(&x); /* includes '\n' */ |
2053 if (p == NULL) | |
2054 MSG_PUTS("(invalid)\n"); | |
2055 else | |
2056 MSG_PUTS(p); | |
7 | 2057 } |
2058 | |
2059 /* | |
2060 * print the original file name | |
2061 */ | |
2062 fd = mch_open((char *)fname, O_RDONLY | O_EXTRA, 0); | |
2063 if (fd >= 0) | |
2064 { | |
2664 | 2065 if (read_eintr(fd, &b0, sizeof(b0)) == sizeof(b0)) |
7 | 2066 { |
2067 if (STRNCMP(b0.b0_version, "VIM 3.0", 7) == 0) | |
2068 { | |
2069 MSG_PUTS(_(" [from Vim version 3.0]")); | |
2070 } | |
2267 | 2071 else if (ml_check_b0_id(&b0) == FAIL) |
7 | 2072 { |
2073 MSG_PUTS(_(" [does not look like a Vim swap file]")); | |
2074 } | |
2075 else | |
2076 { | |
2077 MSG_PUTS(_(" file name: ")); | |
2078 if (b0.b0_fname[0] == NUL) | |
9 | 2079 MSG_PUTS(_("[No Name]")); |
7 | 2080 else |
2081 msg_outtrans(b0.b0_fname); | |
2082 | |
2083 MSG_PUTS(_("\n modified: ")); | |
2084 MSG_PUTS(b0.b0_dirty ? _("YES") : _("no")); | |
2085 | |
2086 if (*(b0.b0_uname) != NUL) | |
2087 { | |
2088 MSG_PUTS(_("\n user name: ")); | |
2089 msg_outtrans(b0.b0_uname); | |
2090 } | |
2091 | |
2092 if (*(b0.b0_hname) != NUL) | |
2093 { | |
2094 if (*(b0.b0_uname) != NUL) | |
2095 MSG_PUTS(_(" host name: ")); | |
2096 else | |
2097 MSG_PUTS(_("\n host name: ")); | |
2098 msg_outtrans(b0.b0_hname); | |
2099 } | |
2100 | |
2101 if (char_to_long(b0.b0_pid) != 0L) | |
2102 { | |
2103 MSG_PUTS(_("\n process ID: ")); | |
2104 msg_outnum(char_to_long(b0.b0_pid)); | |
2105 #if defined(UNIX) || defined(__EMX__) | |
2106 /* EMX kill() not working correctly, it seems */ | |
2107 if (kill((pid_t)char_to_long(b0.b0_pid), 0) == 0) | |
2108 { | |
2109 MSG_PUTS(_(" (still running)")); | |
2110 # if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) | |
2111 process_still_running = TRUE; | |
2112 # endif | |
2113 } | |
2114 #endif | |
2115 } | |
2116 | |
2117 if (b0_magic_wrong(&b0)) | |
2118 { | |
2119 #if defined(MSDOS) || defined(MSWIN) | |
2120 if (STRNCMP(b0.b0_hname, "PC ", 3) == 0) | |
2121 MSG_PUTS(_("\n [not usable with this version of Vim]")); | |
2122 else | |
2123 #endif | |
2124 MSG_PUTS(_("\n [not usable on this computer]")); | |
2125 } | |
2126 } | |
2127 } | |
2128 else | |
2129 MSG_PUTS(_(" [cannot be read]")); | |
2130 close(fd); | |
2131 } | |
2132 else | |
2133 MSG_PUTS(_(" [cannot be opened]")); | |
2134 msg_putchar('\n'); | |
2135 | |
2136 return x; | |
2137 } | |
2138 | |
2139 static int | |
2140 recov_file_names(names, path, prepend_dot) | |
2141 char_u **names; | |
2142 char_u *path; | |
2143 int prepend_dot; | |
2144 { | |
2145 int num_names; | |
2146 | |
2147 #ifdef SHORT_FNAME | |
2148 /* | |
2149 * (MS-DOS) always short names | |
2150 */ | |
2151 names[0] = modname(path, (char_u *)".sw?", FALSE); | |
2152 num_names = 1; | |
2153 #else /* !SHORT_FNAME */ | |
2154 /* | |
2155 * (Win32 and Win64) never short names, but do prepend a dot. | |
2156 * (Not MS-DOS or Win32 or Win64) maybe short name, maybe not: Try both. | |
2157 * Only use the short name if it is different. | |
2158 */ | |
2159 char_u *p; | |
2160 int i; | |
2161 # ifndef WIN3264 | |
2162 int shortname = curbuf->b_shortname; | |
2163 | |
2164 curbuf->b_shortname = FALSE; | |
2165 # endif | |
2166 | |
2167 num_names = 0; | |
2168 | |
2169 /* | |
2170 * May also add the file name with a dot prepended, for swap file in same | |
2171 * dir as original file. | |
2172 */ | |
2173 if (prepend_dot) | |
2174 { | |
2175 names[num_names] = modname(path, (char_u *)".sw?", TRUE); | |
2176 if (names[num_names] == NULL) | |
2177 goto end; | |
2178 ++num_names; | |
2179 } | |
2180 | |
2181 /* | |
2182 * Form the normal swap file name pattern by appending ".sw?". | |
2183 */ | |
2184 #ifdef VMS | |
2185 names[num_names] = concat_fnames(path, (char_u *)"_sw%", FALSE); | |
2186 #else | |
2187 names[num_names] = concat_fnames(path, (char_u *)".sw?", FALSE); | |
2188 #endif | |
2189 if (names[num_names] == NULL) | |
2190 goto end; | |
2191 if (num_names >= 1) /* check if we have the same name twice */ | |
2192 { | |
2193 p = names[num_names - 1]; | |
2194 i = (int)STRLEN(names[num_names - 1]) - (int)STRLEN(names[num_names]); | |
2195 if (i > 0) | |
2196 p += i; /* file name has been expanded to full path */ | |
2197 | |
2198 if (STRCMP(p, names[num_names]) != 0) | |
2199 ++num_names; | |
2200 else | |
2201 vim_free(names[num_names]); | |
2202 } | |
2203 else | |
2204 ++num_names; | |
2205 | |
2206 # ifndef WIN3264 | |
2207 /* | |
2208 * Also try with 'shortname' set, in case the file is on a DOS filesystem. | |
2209 */ | |
2210 curbuf->b_shortname = TRUE; | |
2211 #ifdef VMS | |
2212 names[num_names] = modname(path, (char_u *)"_sw%", FALSE); | |
2213 #else | |
2214 names[num_names] = modname(path, (char_u *)".sw?", FALSE); | |
2215 #endif | |
2216 if (names[num_names] == NULL) | |
2217 goto end; | |
2218 | |
2219 /* | |
2220 * Remove the one from 'shortname', if it's the same as with 'noshortname'. | |
2221 */ | |
2222 p = names[num_names]; | |
2223 i = STRLEN(names[num_names]) - STRLEN(names[num_names - 1]); | |
2224 if (i > 0) | |
2225 p += i; /* file name has been expanded to full path */ | |
2226 if (STRCMP(names[num_names - 1], p) == 0) | |
2227 vim_free(names[num_names]); | |
2228 else | |
2229 ++num_names; | |
2230 # endif | |
2231 | |
2232 end: | |
2233 # ifndef WIN3264 | |
2234 curbuf->b_shortname = shortname; | |
2235 # endif | |
2236 | |
2237 #endif /* !SHORT_FNAME */ | |
2238 | |
2239 return num_names; | |
2240 } | |
2241 | |
2242 /* | |
2243 * sync all memlines | |
2244 * | |
2245 * If 'check_file' is TRUE, check if original file exists and was not changed. | |
2246 * If 'check_char' is TRUE, stop syncing when character becomes available, but | |
2247 * always sync at least one block. | |
2248 */ | |
2249 void | |
2250 ml_sync_all(check_file, check_char) | |
2251 int check_file; | |
2252 int check_char; | |
2253 { | |
2254 buf_T *buf; | |
2255 struct stat st; | |
2256 | |
2257 for (buf = firstbuf; buf != NULL; buf = buf->b_next) | |
2258 { | |
2259 if (buf->b_ml.ml_mfp == NULL || buf->b_ml.ml_mfp->mf_fname == NULL) | |
2260 continue; /* no file */ | |
2261 | |
2262 ml_flush_line(buf); /* flush buffered line */ | |
2263 /* flush locked block */ | |
2264 (void)ml_find_line(buf, (linenr_T)0, ML_FLUSH); | |
2265 if (bufIsChanged(buf) && check_file && mf_need_trans(buf->b_ml.ml_mfp) | |
2266 && buf->b_ffname != NULL) | |
2267 { | |
2268 /* | |
2269 * If the original file does not exist anymore or has been changed | |
2270 * call ml_preserve() to get rid of all negative numbered blocks. | |
2271 */ | |
2272 if (mch_stat((char *)buf->b_ffname, &st) == -1 | |
2273 || st.st_mtime != buf->b_mtime_read | |
2241
60da25e3aab7
Correct use of long instead of off_t for file size. (James Vega)
Bram Moolenaar <bram@vim.org>
parents:
2240
diff
changeset
|
2274 || st.st_size != buf->b_orig_size) |
7 | 2275 { |
2276 ml_preserve(buf, FALSE); | |
2277 did_check_timestamps = FALSE; | |
2278 need_check_timestamps = TRUE; /* give message later */ | |
2279 } | |
2280 } | |
2281 if (buf->b_ml.ml_mfp->mf_dirty) | |
2282 { | |
2283 (void)mf_sync(buf->b_ml.ml_mfp, (check_char ? MFS_STOP : 0) | |
2284 | (bufIsChanged(buf) ? MFS_FLUSH : 0)); | |
2285 if (check_char && ui_char_avail()) /* character available now */ | |
2286 break; | |
2287 } | |
2288 } | |
2289 } | |
2290 | |
2291 /* | |
2292 * sync one buffer, including negative blocks | |
2293 * | |
2294 * after this all the blocks are in the swap file | |
2295 * | |
2296 * Used for the :preserve command and when the original file has been | |
2297 * changed or deleted. | |
2298 * | |
2299 * when message is TRUE the success of preserving is reported | |
2300 */ | |
2301 void | |
2302 ml_preserve(buf, message) | |
2303 buf_T *buf; | |
2304 int message; | |
2305 { | |
2306 bhdr_T *hp; | |
2307 linenr_T lnum; | |
2308 memfile_T *mfp = buf->b_ml.ml_mfp; | |
2309 int status; | |
2310 int got_int_save = got_int; | |
2311 | |
2312 if (mfp == NULL || mfp->mf_fname == NULL) | |
2313 { | |
2314 if (message) | |
2315 EMSG(_("E313: Cannot preserve, there is no swap file")); | |
2316 return; | |
2317 } | |
2318 | |
2319 /* We only want to stop when interrupted here, not when interrupted | |
2320 * before. */ | |
2321 got_int = FALSE; | |
2322 | |
2323 ml_flush_line(buf); /* flush buffered line */ | |
2324 (void)ml_find_line(buf, (linenr_T)0, ML_FLUSH); /* flush locked block */ | |
2325 status = mf_sync(mfp, MFS_ALL | MFS_FLUSH); | |
2326 | |
2327 /* stack is invalid after mf_sync(.., MFS_ALL) */ | |
2328 buf->b_ml.ml_stack_top = 0; | |
2329 | |
2330 /* | |
2331 * Some of the data blocks may have been changed from negative to | |
2332 * positive block number. In that case the pointer blocks need to be | |
2333 * updated. | |
2334 * | |
2335 * We don't know in which pointer block the references are, so we visit | |
2336 * all data blocks until there are no more translations to be done (or | |
2337 * we hit the end of the file, which can only happen in case a write fails, | |
2338 * e.g. when file system if full). | |
2339 * ml_find_line() does the work by translating the negative block numbers | |
2340 * when getting the first line of each data block. | |
2341 */ | |
2342 if (mf_need_trans(mfp) && !got_int) | |
2343 { | |
2344 lnum = 1; | |
2345 while (mf_need_trans(mfp) && lnum <= buf->b_ml.ml_line_count) | |
2346 { | |
2347 hp = ml_find_line(buf, lnum, ML_FIND); | |
2348 if (hp == NULL) | |
2349 { | |
2350 status = FAIL; | |
2351 goto theend; | |
2352 } | |
2353 CHECK(buf->b_ml.ml_locked_low != lnum, "low != lnum"); | |
2354 lnum = buf->b_ml.ml_locked_high + 1; | |
2355 } | |
2356 (void)ml_find_line(buf, (linenr_T)0, ML_FLUSH); /* flush locked block */ | |
2357 /* sync the updated pointer blocks */ | |
2358 if (mf_sync(mfp, MFS_ALL | MFS_FLUSH) == FAIL) | |
2359 status = FAIL; | |
2360 buf->b_ml.ml_stack_top = 0; /* stack is invalid now */ | |
2361 } | |
2362 theend: | |
2363 got_int |= got_int_save; | |
2364 | |
2365 if (message) | |
2366 { | |
2367 if (status == OK) | |
2368 MSG(_("File preserved")); | |
2369 else | |
2370 EMSG(_("E314: Preserve failed")); | |
2371 } | |
2372 } | |
2373 | |
2374 /* | |
2375 * NOTE: The pointer returned by the ml_get_*() functions only remains valid | |
2376 * until the next call! | |
2377 * line1 = ml_get(1); | |
2378 * line2 = ml_get(2); // line1 is now invalid! | |
2379 * Make a copy of the line if necessary. | |
2380 */ | |
2381 /* | |
2657 | 2382 * Return a pointer to a (read-only copy of a) line. |
7 | 2383 * |
2384 * On failure an error message is given and IObuff is returned (to avoid | |
2385 * having to check for error everywhere). | |
2386 */ | |
2387 char_u * | |
2388 ml_get(lnum) | |
2389 linenr_T lnum; | |
2390 { | |
2391 return ml_get_buf(curbuf, lnum, FALSE); | |
2392 } | |
2393 | |
2394 /* | |
2657 | 2395 * Return pointer to position "pos". |
7 | 2396 */ |
2397 char_u * | |
2398 ml_get_pos(pos) | |
2399 pos_T *pos; | |
2400 { | |
2401 return (ml_get_buf(curbuf, pos->lnum, FALSE) + pos->col); | |
2402 } | |
2403 | |
2404 /* | |
2657 | 2405 * Return pointer to cursor line. |
7 | 2406 */ |
2407 char_u * | |
2408 ml_get_curline() | |
2409 { | |
2410 return ml_get_buf(curbuf, curwin->w_cursor.lnum, FALSE); | |
2411 } | |
2412 | |
2413 /* | |
2657 | 2414 * Return pointer to cursor position. |
7 | 2415 */ |
2416 char_u * | |
2417 ml_get_cursor() | |
2418 { | |
2419 return (ml_get_buf(curbuf, curwin->w_cursor.lnum, FALSE) + | |
2420 curwin->w_cursor.col); | |
2421 } | |
2422 | |
2423 /* | |
2657 | 2424 * Return a pointer to a line in a specific buffer |
7 | 2425 * |
2426 * "will_change": if TRUE mark the buffer dirty (chars in the line will be | |
2427 * changed) | |
2428 */ | |
2429 char_u * | |
2430 ml_get_buf(buf, lnum, will_change) | |
2431 buf_T *buf; | |
2432 linenr_T lnum; | |
2433 int will_change; /* line will be changed */ | |
2434 { | |
1068 | 2435 bhdr_T *hp; |
2436 DATA_BL *dp; | |
2437 char_u *ptr; | |
2438 static int recursive = 0; | |
7 | 2439 |
2440 if (lnum > buf->b_ml.ml_line_count) /* invalid line number */ | |
2441 { | |
1068 | 2442 if (recursive == 0) |
2443 { | |
2444 /* Avoid giving this message for a recursive call, may happen when | |
2445 * the GUI redraws part of the text. */ | |
2446 ++recursive; | |
2447 EMSGN(_("E315: ml_get: invalid lnum: %ld"), lnum); | |
2448 --recursive; | |
2449 } | |
7 | 2450 errorret: |
2451 STRCPY(IObuff, "???"); | |
2452 return IObuff; | |
2453 } | |
2454 if (lnum <= 0) /* pretend line 0 is line 1 */ | |
2455 lnum = 1; | |
2456 | |
2457 if (buf->b_ml.ml_mfp == NULL) /* there are no lines */ | |
2458 return (char_u *)""; | |
2459 | |
2108
3cdf2a653e00
updated for version 7.2.391
Bram Moolenaar <bram@zimbu.org>
parents:
2075
diff
changeset
|
2460 /* |
3cdf2a653e00
updated for version 7.2.391
Bram Moolenaar <bram@zimbu.org>
parents:
2075
diff
changeset
|
2461 * See if it is the same line as requested last time. |
3cdf2a653e00
updated for version 7.2.391
Bram Moolenaar <bram@zimbu.org>
parents:
2075
diff
changeset
|
2462 * Otherwise may need to flush last used line. |
3cdf2a653e00
updated for version 7.2.391
Bram Moolenaar <bram@zimbu.org>
parents:
2075
diff
changeset
|
2463 * Don't use the last used line when 'swapfile' is reset, need to load all |
3cdf2a653e00
updated for version 7.2.391
Bram Moolenaar <bram@zimbu.org>
parents:
2075
diff
changeset
|
2464 * blocks. |
3cdf2a653e00
updated for version 7.2.391
Bram Moolenaar <bram@zimbu.org>
parents:
2075
diff
changeset
|
2465 */ |
1066 | 2466 if (buf->b_ml.ml_line_lnum != lnum || mf_dont_release) |
7 | 2467 { |
2468 ml_flush_line(buf); | |
2469 | |
2470 /* | |
2471 * Find the data block containing the line. | |
2472 * This also fills the stack with the blocks from the root to the data | |
2473 * block and releases any locked block. | |
2474 */ | |
2475 if ((hp = ml_find_line(buf, lnum, ML_FIND)) == NULL) | |
2476 { | |
1068 | 2477 if (recursive == 0) |
2478 { | |
2479 /* Avoid giving this message for a recursive call, may happen | |
2480 * when the GUI redraws part of the text. */ | |
2481 ++recursive; | |
2482 EMSGN(_("E316: ml_get: cannot find line %ld"), lnum); | |
2483 --recursive; | |
2484 } | |
7 | 2485 goto errorret; |
2486 } | |
2487 | |
2488 dp = (DATA_BL *)(hp->bh_data); | |
2489 | |
2490 ptr = (char_u *)dp + ((dp->db_index[lnum - buf->b_ml.ml_locked_low]) & DB_INDEX_MASK); | |
2491 buf->b_ml.ml_line_ptr = ptr; | |
2492 buf->b_ml.ml_line_lnum = lnum; | |
2493 buf->b_ml.ml_flags &= ~ML_LINE_DIRTY; | |
2494 } | |
2495 if (will_change) | |
2496 buf->b_ml.ml_flags |= (ML_LOCKED_DIRTY | ML_LOCKED_POS); | |
2497 | |
2498 return buf->b_ml.ml_line_ptr; | |
2499 } | |
2500 | |
2501 /* | |
2502 * Check if a line that was just obtained by a call to ml_get | |
2503 * is in allocated memory. | |
2504 */ | |
2505 int | |
2506 ml_line_alloced() | |
2507 { | |
2508 return (curbuf->b_ml.ml_flags & ML_LINE_DIRTY); | |
2509 } | |
2510 | |
2511 /* | |
2512 * Append a line after lnum (may be 0 to insert a line in front of the file). | |
2513 * "line" does not need to be allocated, but can't be another line in a | |
2514 * buffer, unlocking may make it invalid. | |
2515 * | |
2516 * newfile: TRUE when starting to edit a new file, meaning that pe_old_lnum | |
2517 * will be set for recovery | |
2518 * Check: The caller of this function should probably also call | |
2519 * appended_lines(). | |
2520 * | |
2521 * return FAIL for failure, OK otherwise | |
2522 */ | |
2523 int | |
2524 ml_append(lnum, line, len, newfile) | |
2525 linenr_T lnum; /* append after this line (can be 0) */ | |
2526 char_u *line; /* text of the new line */ | |
2527 colnr_T len; /* length of new line, including NUL, or 0 */ | |
2528 int newfile; /* flag, see above */ | |
2529 { | |
2530 /* When starting up, we might still need to create the memfile */ | |
2394
a3aca345aafa
Add the 'undoreload' option to be able to undo a file reload.
Bram Moolenaar <bram@vim.org>
parents:
2360
diff
changeset
|
2531 if (curbuf->b_ml.ml_mfp == NULL && open_buffer(FALSE, NULL, 0) == FAIL) |
7 | 2532 return FAIL; |
2533 | |
2534 if (curbuf->b_ml.ml_line_lnum != 0) | |
2535 ml_flush_line(curbuf); | |
2536 return ml_append_int(curbuf, lnum, line, len, newfile, FALSE); | |
2537 } | |
2538 | |
748 | 2539 #if defined(FEAT_SPELL) || defined(PROTO) |
625 | 2540 /* |
2541 * Like ml_append() but for an arbitrary buffer. The buffer must already have | |
2542 * a memline. | |
2543 */ | |
2544 int | |
2545 ml_append_buf(buf, lnum, line, len, newfile) | |
2546 buf_T *buf; | |
2547 linenr_T lnum; /* append after this line (can be 0) */ | |
2548 char_u *line; /* text of the new line */ | |
2549 colnr_T len; /* length of new line, including NUL, or 0 */ | |
2550 int newfile; /* flag, see above */ | |
2551 { | |
2552 if (buf->b_ml.ml_mfp == NULL) | |
2553 return FAIL; | |
2554 | |
2555 if (buf->b_ml.ml_line_lnum != 0) | |
2556 ml_flush_line(buf); | |
2557 return ml_append_int(buf, lnum, line, len, newfile, FALSE); | |
2558 } | |
2559 #endif | |
2560 | |
7 | 2561 static int |
2562 ml_append_int(buf, lnum, line, len, newfile, mark) | |
2563 buf_T *buf; | |
2564 linenr_T lnum; /* append after this line (can be 0) */ | |
2565 char_u *line; /* text of the new line */ | |
2566 colnr_T len; /* length of line, including NUL, or 0 */ | |
2567 int newfile; /* flag, see above */ | |
2568 int mark; /* mark the new line */ | |
2569 { | |
2570 int i; | |
2571 int line_count; /* number of indexes in current block */ | |
2572 int offset; | |
2573 int from, to; | |
2574 int space_needed; /* space needed for new line */ | |
2575 int page_size; | |
2576 int page_count; | |
2577 int db_idx; /* index for lnum in data block */ | |
2578 bhdr_T *hp; | |
2579 memfile_T *mfp; | |
2580 DATA_BL *dp; | |
2581 PTR_BL *pp; | |
2582 infoptr_T *ip; | |
2583 | |
2584 /* lnum out of range */ | |
2585 if (lnum > buf->b_ml.ml_line_count || buf->b_ml.ml_mfp == NULL) | |
2586 return FAIL; | |
2587 | |
2588 if (lowest_marked && lowest_marked > lnum) | |
2589 lowest_marked = lnum + 1; | |
2590 | |
2591 if (len == 0) | |
2592 len = (colnr_T)STRLEN(line) + 1; /* space needed for the text */ | |
2593 space_needed = len + INDEX_SIZE; /* space needed for text + index */ | |
2594 | |
2595 mfp = buf->b_ml.ml_mfp; | |
2596 page_size = mfp->mf_page_size; | |
2597 | |
2598 /* | |
2599 * find the data block containing the previous line | |
2600 * This also fills the stack with the blocks from the root to the data block | |
2601 * This also releases any locked block. | |
2602 */ | |
2603 if ((hp = ml_find_line(buf, lnum == 0 ? (linenr_T)1 : lnum, | |
2604 ML_INSERT)) == NULL) | |
2605 return FAIL; | |
2606 | |
2607 buf->b_ml.ml_flags &= ~ML_EMPTY; | |
2608 | |
2609 if (lnum == 0) /* got line one instead, correct db_idx */ | |
2610 db_idx = -1; /* careful, it is negative! */ | |
2611 else | |
2612 db_idx = lnum - buf->b_ml.ml_locked_low; | |
2613 /* get line count before the insertion */ | |
2614 line_count = buf->b_ml.ml_locked_high - buf->b_ml.ml_locked_low; | |
2615 | |
2616 dp = (DATA_BL *)(hp->bh_data); | |
2617 | |
2618 /* | |
2619 * If | |
2620 * - there is not enough room in the current block | |
2621 * - appending to the last line in the block | |
2622 * - not appending to the last line in the file | |
2623 * insert in front of the next block. | |
2624 */ | |
2625 if ((int)dp->db_free < space_needed && db_idx == line_count - 1 | |
2626 && lnum < buf->b_ml.ml_line_count) | |
2627 { | |
2628 /* | |
2629 * Now that the line is not going to be inserted in the block that we | |
2630 * expected, the line count has to be adjusted in the pointer blocks | |
2631 * by using ml_locked_lineadd. | |
2632 */ | |
2633 --(buf->b_ml.ml_locked_lineadd); | |
2634 --(buf->b_ml.ml_locked_high); | |
2635 if ((hp = ml_find_line(buf, lnum + 1, ML_INSERT)) == NULL) | |
2636 return FAIL; | |
2637 | |
2638 db_idx = -1; /* careful, it is negative! */ | |
2639 /* get line count before the insertion */ | |
2640 line_count = buf->b_ml.ml_locked_high - buf->b_ml.ml_locked_low; | |
2641 CHECK(buf->b_ml.ml_locked_low != lnum + 1, "locked_low != lnum + 1"); | |
2642 | |
2643 dp = (DATA_BL *)(hp->bh_data); | |
2644 } | |
2645 | |
2646 ++buf->b_ml.ml_line_count; | |
2647 | |
2648 if ((int)dp->db_free >= space_needed) /* enough room in data block */ | |
2649 { | |
2650 /* | |
2651 * Insert new line in existing data block, or in data block allocated above. | |
2652 */ | |
2653 dp->db_txt_start -= len; | |
2654 dp->db_free -= space_needed; | |
2655 ++(dp->db_line_count); | |
2656 | |
2657 /* | |
2658 * move the text of the lines that follow to the front | |
2659 * adjust the indexes of the lines that follow | |
2660 */ | |
2661 if (line_count > db_idx + 1) /* if there are following lines */ | |
2662 { | |
2663 /* | |
2664 * Offset is the start of the previous line. | |
2665 * This will become the character just after the new line. | |
2666 */ | |
2667 if (db_idx < 0) | |
2668 offset = dp->db_txt_end; | |
2669 else | |
2670 offset = ((dp->db_index[db_idx]) & DB_INDEX_MASK); | |
2671 mch_memmove((char *)dp + dp->db_txt_start, | |
2672 (char *)dp + dp->db_txt_start + len, | |
2673 (size_t)(offset - (dp->db_txt_start + len))); | |
2674 for (i = line_count - 1; i > db_idx; --i) | |
2675 dp->db_index[i + 1] = dp->db_index[i] - len; | |
2676 dp->db_index[db_idx + 1] = offset - len; | |
2677 } | |
2678 else /* add line at the end */ | |
2679 dp->db_index[db_idx + 1] = dp->db_txt_start; | |
2680 | |
2681 /* | |
2682 * copy the text into the block | |
2683 */ | |
2684 mch_memmove((char *)dp + dp->db_index[db_idx + 1], line, (size_t)len); | |
2685 if (mark) | |
2686 dp->db_index[db_idx + 1] |= DB_MARKED; | |
2687 | |
2688 /* | |
2689 * Mark the block dirty. | |
2690 */ | |
2691 buf->b_ml.ml_flags |= ML_LOCKED_DIRTY; | |
2692 if (!newfile) | |
2693 buf->b_ml.ml_flags |= ML_LOCKED_POS; | |
2694 } | |
2695 else /* not enough space in data block */ | |
2696 { | |
2697 /* | |
2698 * If there is not enough room we have to create a new data block and copy some | |
2699 * lines into it. | |
2700 * Then we have to insert an entry in the pointer block. | |
2701 * If this pointer block also is full, we go up another block, and so on, up | |
2702 * to the root if necessary. | |
2703 * The line counts in the pointer blocks have already been adjusted by | |
2704 * ml_find_line(). | |
2705 */ | |
2706 long line_count_left, line_count_right; | |
2707 int page_count_left, page_count_right; | |
2708 bhdr_T *hp_left; | |
2709 bhdr_T *hp_right; | |
2710 bhdr_T *hp_new; | |
2711 int lines_moved; | |
2712 int data_moved = 0; /* init to shut up gcc */ | |
2713 int total_moved = 0; /* init to shut up gcc */ | |
2714 DATA_BL *dp_right, *dp_left; | |
2715 int stack_idx; | |
2716 int in_left; | |
2717 int lineadd; | |
2718 blocknr_T bnum_left, bnum_right; | |
2719 linenr_T lnum_left, lnum_right; | |
2720 int pb_idx; | |
2721 PTR_BL *pp_new; | |
2722 | |
2723 /* | |
2724 * We are going to allocate a new data block. Depending on the | |
2725 * situation it will be put to the left or right of the existing | |
2726 * block. If possible we put the new line in the left block and move | |
2727 * the lines after it to the right block. Otherwise the new line is | |
2728 * also put in the right block. This method is more efficient when | |
2729 * inserting a lot of lines at one place. | |
2730 */ | |
2731 if (db_idx < 0) /* left block is new, right block is existing */ | |
2732 { | |
2733 lines_moved = 0; | |
2734 in_left = TRUE; | |
2735 /* space_needed does not change */ | |
2736 } | |
2737 else /* left block is existing, right block is new */ | |
2738 { | |
2739 lines_moved = line_count - db_idx - 1; | |
2740 if (lines_moved == 0) | |
2741 in_left = FALSE; /* put new line in right block */ | |
2742 /* space_needed does not change */ | |
2743 else | |
2744 { | |
2745 data_moved = ((dp->db_index[db_idx]) & DB_INDEX_MASK) - | |
2746 dp->db_txt_start; | |
2747 total_moved = data_moved + lines_moved * INDEX_SIZE; | |
2748 if ((int)dp->db_free + total_moved >= space_needed) | |
2749 { | |
2750 in_left = TRUE; /* put new line in left block */ | |
2751 space_needed = total_moved; | |
2752 } | |
2753 else | |
2754 { | |
2755 in_left = FALSE; /* put new line in right block */ | |
2756 space_needed += total_moved; | |
2757 } | |
2758 } | |
2759 } | |
2760 | |
2761 page_count = ((space_needed + HEADER_SIZE) + page_size - 1) / page_size; | |
2762 if ((hp_new = ml_new_data(mfp, newfile, page_count)) == NULL) | |
2763 { | |
2764 /* correct line counts in pointer blocks */ | |
2765 --(buf->b_ml.ml_locked_lineadd); | |
2766 --(buf->b_ml.ml_locked_high); | |
2767 return FAIL; | |
2768 } | |
2769 if (db_idx < 0) /* left block is new */ | |
2770 { | |
2771 hp_left = hp_new; | |
2772 hp_right = hp; | |
2773 line_count_left = 0; | |
2774 line_count_right = line_count; | |
2775 } | |
2776 else /* right block is new */ | |
2777 { | |
2778 hp_left = hp; | |
2779 hp_right = hp_new; | |
2780 line_count_left = line_count; | |
2781 line_count_right = 0; | |
2782 } | |
2783 dp_right = (DATA_BL *)(hp_right->bh_data); | |
2784 dp_left = (DATA_BL *)(hp_left->bh_data); | |
2785 bnum_left = hp_left->bh_bnum; | |
2786 bnum_right = hp_right->bh_bnum; | |
2787 page_count_left = hp_left->bh_page_count; | |
2788 page_count_right = hp_right->bh_page_count; | |
2789 | |
2790 /* | |
2791 * May move the new line into the right/new block. | |
2792 */ | |
2793 if (!in_left) | |
2794 { | |
2795 dp_right->db_txt_start -= len; | |
2796 dp_right->db_free -= len + INDEX_SIZE; | |
2797 dp_right->db_index[0] = dp_right->db_txt_start; | |
2798 if (mark) | |
2799 dp_right->db_index[0] |= DB_MARKED; | |
2800 | |
2801 mch_memmove((char *)dp_right + dp_right->db_txt_start, | |
2802 line, (size_t)len); | |
2803 ++line_count_right; | |
2804 } | |
2805 /* | |
2806 * may move lines from the left/old block to the right/new one. | |
2807 */ | |
2808 if (lines_moved) | |
2809 { | |
2810 /* | |
2811 */ | |
2812 dp_right->db_txt_start -= data_moved; | |
2813 dp_right->db_free -= total_moved; | |
2814 mch_memmove((char *)dp_right + dp_right->db_txt_start, | |
2815 (char *)dp_left + dp_left->db_txt_start, | |
2816 (size_t)data_moved); | |
2817 offset = dp_right->db_txt_start - dp_left->db_txt_start; | |
2818 dp_left->db_txt_start += data_moved; | |
2819 dp_left->db_free += total_moved; | |
2820 | |
2821 /* | |
2822 * update indexes in the new block | |
2823 */ | |
2824 for (to = line_count_right, from = db_idx + 1; | |
2825 from < line_count_left; ++from, ++to) | |
2826 dp_right->db_index[to] = dp->db_index[from] + offset; | |
2827 line_count_right += lines_moved; | |
2828 line_count_left -= lines_moved; | |
2829 } | |
2830 | |
2831 /* | |
2832 * May move the new line into the left (old or new) block. | |
2833 */ | |
2834 if (in_left) | |
2835 { | |
2836 dp_left->db_txt_start -= len; | |
2837 dp_left->db_free -= len + INDEX_SIZE; | |
2838 dp_left->db_index[line_count_left] = dp_left->db_txt_start; | |
2839 if (mark) | |
2840 dp_left->db_index[line_count_left] |= DB_MARKED; | |
2841 mch_memmove((char *)dp_left + dp_left->db_txt_start, | |
2842 line, (size_t)len); | |
2843 ++line_count_left; | |
2844 } | |
2845 | |
2846 if (db_idx < 0) /* left block is new */ | |
2847 { | |
2848 lnum_left = lnum + 1; | |
2849 lnum_right = 0; | |
2850 } | |
2851 else /* right block is new */ | |
2852 { | |
2853 lnum_left = 0; | |
2854 if (in_left) | |
2855 lnum_right = lnum + 2; | |
2856 else | |
2857 lnum_right = lnum + 1; | |
2858 } | |
2859 dp_left->db_line_count = line_count_left; | |
2860 dp_right->db_line_count = line_count_right; | |
2861 | |
2862 /* | |
2863 * release the two data blocks | |
2864 * The new one (hp_new) already has a correct blocknumber. | |
2865 * The old one (hp, in ml_locked) gets a positive blocknumber if | |
2866 * we changed it and we are not editing a new file. | |
2867 */ | |
2868 if (lines_moved || in_left) | |
2869 buf->b_ml.ml_flags |= ML_LOCKED_DIRTY; | |
2870 if (!newfile && db_idx >= 0 && in_left) | |
2871 buf->b_ml.ml_flags |= ML_LOCKED_POS; | |
2872 mf_put(mfp, hp_new, TRUE, FALSE); | |
2873 | |
2874 /* | |
2875 * flush the old data block | |
2876 * set ml_locked_lineadd to 0, because the updating of the | |
2877 * pointer blocks is done below | |
2878 */ | |
2879 lineadd = buf->b_ml.ml_locked_lineadd; | |
2880 buf->b_ml.ml_locked_lineadd = 0; | |
2881 ml_find_line(buf, (linenr_T)0, ML_FLUSH); /* flush data block */ | |
2882 | |
2883 /* | |
2884 * update pointer blocks for the new data block | |
2885 */ | |
2886 for (stack_idx = buf->b_ml.ml_stack_top - 1; stack_idx >= 0; | |
2887 --stack_idx) | |
2888 { | |
2889 ip = &(buf->b_ml.ml_stack[stack_idx]); | |
2890 pb_idx = ip->ip_index; | |
2891 if ((hp = mf_get(mfp, ip->ip_bnum, 1)) == NULL) | |
2892 return FAIL; | |
2893 pp = (PTR_BL *)(hp->bh_data); /* must be pointer block */ | |
2894 if (pp->pb_id != PTR_ID) | |
2895 { | |
2896 EMSG(_("E317: pointer block id wrong 3")); | |
2897 mf_put(mfp, hp, FALSE, FALSE); | |
2898 return FAIL; | |
2899 } | |
2900 /* | |
2901 * TODO: If the pointer block is full and we are adding at the end | |
2902 * try to insert in front of the next block | |
2903 */ | |
2904 /* block not full, add one entry */ | |
2905 if (pp->pb_count < pp->pb_count_max) | |
2906 { | |
2907 if (pb_idx + 1 < (int)pp->pb_count) | |
2908 mch_memmove(&pp->pb_pointer[pb_idx + 2], | |
2909 &pp->pb_pointer[pb_idx + 1], | |
2910 (size_t)(pp->pb_count - pb_idx - 1) * sizeof(PTR_EN)); | |
2911 ++pp->pb_count; | |
2912 pp->pb_pointer[pb_idx].pe_line_count = line_count_left; | |
2913 pp->pb_pointer[pb_idx].pe_bnum = bnum_left; | |
2914 pp->pb_pointer[pb_idx].pe_page_count = page_count_left; | |
2915 pp->pb_pointer[pb_idx + 1].pe_line_count = line_count_right; | |
2916 pp->pb_pointer[pb_idx + 1].pe_bnum = bnum_right; | |
2917 pp->pb_pointer[pb_idx + 1].pe_page_count = page_count_right; | |
2918 | |
2919 if (lnum_left != 0) | |
2920 pp->pb_pointer[pb_idx].pe_old_lnum = lnum_left; | |
2921 if (lnum_right != 0) | |
2922 pp->pb_pointer[pb_idx + 1].pe_old_lnum = lnum_right; | |
2923 | |
2924 mf_put(mfp, hp, TRUE, FALSE); | |
2925 buf->b_ml.ml_stack_top = stack_idx + 1; /* truncate stack */ | |
2926 | |
2927 if (lineadd) | |
2928 { | |
2929 --(buf->b_ml.ml_stack_top); | |
1167 | 2930 /* fix line count for rest of blocks in the stack */ |
7 | 2931 ml_lineadd(buf, lineadd); |
2932 /* fix stack itself */ | |
2933 buf->b_ml.ml_stack[buf->b_ml.ml_stack_top].ip_high += | |
2934 lineadd; | |
2935 ++(buf->b_ml.ml_stack_top); | |
2936 } | |
2937 | |
2938 /* | |
2939 * We are finished, break the loop here. | |
2940 */ | |
2941 break; | |
2942 } | |
2943 else /* pointer block full */ | |
2944 { | |
2945 /* | |
2946 * split the pointer block | |
2947 * allocate a new pointer block | |
2948 * move some of the pointer into the new block | |
2949 * prepare for updating the parent block | |
2950 */ | |
2951 for (;;) /* do this twice when splitting block 1 */ | |
2952 { | |
2953 hp_new = ml_new_ptr(mfp); | |
2954 if (hp_new == NULL) /* TODO: try to fix tree */ | |
2955 return FAIL; | |
2956 pp_new = (PTR_BL *)(hp_new->bh_data); | |
2957 | |
2958 if (hp->bh_bnum != 1) | |
2959 break; | |
2960 | |
2961 /* | |
2962 * if block 1 becomes full the tree is given an extra level | |
2963 * The pointers from block 1 are moved into the new block. | |
2964 * block 1 is updated to point to the new block | |
2965 * then continue to split the new block | |
2966 */ | |
2967 mch_memmove(pp_new, pp, (size_t)page_size); | |
2968 pp->pb_count = 1; | |
2969 pp->pb_pointer[0].pe_bnum = hp_new->bh_bnum; | |
2970 pp->pb_pointer[0].pe_line_count = buf->b_ml.ml_line_count; | |
2971 pp->pb_pointer[0].pe_old_lnum = 1; | |
2972 pp->pb_pointer[0].pe_page_count = 1; | |
2973 mf_put(mfp, hp, TRUE, FALSE); /* release block 1 */ | |
2974 hp = hp_new; /* new block is to be split */ | |
2975 pp = pp_new; | |
2976 CHECK(stack_idx != 0, _("stack_idx should be 0")); | |
2977 ip->ip_index = 0; | |
2978 ++stack_idx; /* do block 1 again later */ | |
2979 } | |
2980 /* | |
2981 * move the pointers after the current one to the new block | |
2982 * If there are none, the new entry will be in the new block. | |
2983 */ | |
2984 total_moved = pp->pb_count - pb_idx - 1; | |
2985 if (total_moved) | |
2986 { | |
2987 mch_memmove(&pp_new->pb_pointer[0], | |
2988 &pp->pb_pointer[pb_idx + 1], | |
2989 (size_t)(total_moved) * sizeof(PTR_EN)); | |
2990 pp_new->pb_count = total_moved; | |
2991 pp->pb_count -= total_moved - 1; | |
2992 pp->pb_pointer[pb_idx + 1].pe_bnum = bnum_right; | |
2993 pp->pb_pointer[pb_idx + 1].pe_line_count = line_count_right; | |
2994 pp->pb_pointer[pb_idx + 1].pe_page_count = page_count_right; | |
2995 if (lnum_right) | |
2996 pp->pb_pointer[pb_idx + 1].pe_old_lnum = lnum_right; | |
2997 } | |
2998 else | |
2999 { | |
3000 pp_new->pb_count = 1; | |
3001 pp_new->pb_pointer[0].pe_bnum = bnum_right; | |
3002 pp_new->pb_pointer[0].pe_line_count = line_count_right; | |
3003 pp_new->pb_pointer[0].pe_page_count = page_count_right; | |
3004 pp_new->pb_pointer[0].pe_old_lnum = lnum_right; | |
3005 } | |
3006 pp->pb_pointer[pb_idx].pe_bnum = bnum_left; | |
3007 pp->pb_pointer[pb_idx].pe_line_count = line_count_left; | |
3008 pp->pb_pointer[pb_idx].pe_page_count = page_count_left; | |
3009 if (lnum_left) | |
3010 pp->pb_pointer[pb_idx].pe_old_lnum = lnum_left; | |
3011 lnum_left = 0; | |
3012 lnum_right = 0; | |
3013 | |
3014 /* | |
3015 * recompute line counts | |
3016 */ | |
3017 line_count_right = 0; | |
3018 for (i = 0; i < (int)pp_new->pb_count; ++i) | |
3019 line_count_right += pp_new->pb_pointer[i].pe_line_count; | |
3020 line_count_left = 0; | |
3021 for (i = 0; i < (int)pp->pb_count; ++i) | |
3022 line_count_left += pp->pb_pointer[i].pe_line_count; | |
3023 | |
3024 bnum_left = hp->bh_bnum; | |
3025 bnum_right = hp_new->bh_bnum; | |
3026 page_count_left = 1; | |
3027 page_count_right = 1; | |
3028 mf_put(mfp, hp, TRUE, FALSE); | |
3029 mf_put(mfp, hp_new, TRUE, FALSE); | |
3030 } | |
3031 } | |
3032 | |
3033 /* | |
3034 * Safety check: fallen out of for loop? | |
3035 */ | |
3036 if (stack_idx < 0) | |
3037 { | |
3038 EMSG(_("E318: Updated too many blocks?")); | |
3039 buf->b_ml.ml_stack_top = 0; /* invalidate stack */ | |
3040 } | |
3041 } | |
3042 | |
3043 #ifdef FEAT_BYTEOFF | |
3044 /* The line was inserted below 'lnum' */ | |
3045 ml_updatechunk(buf, lnum + 1, (long)len, ML_CHNK_ADDLINE); | |
3046 #endif | |
3047 #ifdef FEAT_NETBEANS_INTG | |
2210 | 3048 if (netbeans_active()) |
7 | 3049 { |
3050 if (STRLEN(line) > 0) | |
835 | 3051 netbeans_inserted(buf, lnum+1, (colnr_T)0, line, (int)STRLEN(line)); |
34 | 3052 netbeans_inserted(buf, lnum+1, (colnr_T)STRLEN(line), |
7 | 3053 (char_u *)"\n", 1); |
3054 } | |
3055 #endif | |
3056 return OK; | |
3057 } | |
3058 | |
3059 /* | |
625 | 3060 * Replace line lnum, with buffering, in current buffer. |
7 | 3061 * |
720 | 3062 * If "copy" is TRUE, make a copy of the line, otherwise the line has been |
7 | 3063 * copied to allocated memory already. |
3064 * | |
3065 * Check: The caller of this function should probably also call | |
3066 * changed_lines(), unless update_screen(NOT_VALID) is used. | |
3067 * | |
3068 * return FAIL for failure, OK otherwise | |
3069 */ | |
3070 int | |
3071 ml_replace(lnum, line, copy) | |
3072 linenr_T lnum; | |
3073 char_u *line; | |
3074 int copy; | |
3075 { | |
3076 if (line == NULL) /* just checking... */ | |
3077 return FAIL; | |
3078 | |
3079 /* When starting up, we might still need to create the memfile */ | |
2394
a3aca345aafa
Add the 'undoreload' option to be able to undo a file reload.
Bram Moolenaar <bram@vim.org>
parents:
2360
diff
changeset
|
3080 if (curbuf->b_ml.ml_mfp == NULL && open_buffer(FALSE, NULL, 0) == FAIL) |
7 | 3081 return FAIL; |
3082 | |
3083 if (copy && (line = vim_strsave(line)) == NULL) /* allocate memory */ | |
3084 return FAIL; | |
3085 #ifdef FEAT_NETBEANS_INTG | |
2210 | 3086 if (netbeans_active()) |
7 | 3087 { |
3088 netbeans_removed(curbuf, lnum, 0, (long)STRLEN(ml_get(lnum))); | |
835 | 3089 netbeans_inserted(curbuf, lnum, 0, line, (int)STRLEN(line)); |
7 | 3090 } |
3091 #endif | |
3092 if (curbuf->b_ml.ml_line_lnum != lnum) /* other line buffered */ | |
3093 ml_flush_line(curbuf); /* flush it */ | |
3094 else if (curbuf->b_ml.ml_flags & ML_LINE_DIRTY) /* same line allocated */ | |
3095 vim_free(curbuf->b_ml.ml_line_ptr); /* free it */ | |
3096 curbuf->b_ml.ml_line_ptr = line; | |
3097 curbuf->b_ml.ml_line_lnum = lnum; | |
3098 curbuf->b_ml.ml_flags = (curbuf->b_ml.ml_flags | ML_LINE_DIRTY) & ~ML_EMPTY; | |
3099 | |
3100 return OK; | |
3101 } | |
3102 | |
3103 /* | |
625 | 3104 * Delete line 'lnum' in the current buffer. |
7 | 3105 * |
3106 * Check: The caller of this function should probably also call | |
3107 * deleted_lines() after this. | |
3108 * | |
3109 * return FAIL for failure, OK otherwise | |
3110 */ | |
3111 int | |
3112 ml_delete(lnum, message) | |
3113 linenr_T lnum; | |
3114 int message; | |
3115 { | |
3116 ml_flush_line(curbuf); | |
3117 return ml_delete_int(curbuf, lnum, message); | |
3118 } | |
3119 | |
3120 static int | |
3121 ml_delete_int(buf, lnum, message) | |
3122 buf_T *buf; | |
3123 linenr_T lnum; | |
3124 int message; | |
3125 { | |
3126 bhdr_T *hp; | |
3127 memfile_T *mfp; | |
3128 DATA_BL *dp; | |
3129 PTR_BL *pp; | |
3130 infoptr_T *ip; | |
3131 int count; /* number of entries in block */ | |
3132 int idx; | |
3133 int stack_idx; | |
3134 int text_start; | |
3135 int line_start; | |
3136 long line_size; | |
3137 int i; | |
3138 | |
3139 if (lnum < 1 || lnum > buf->b_ml.ml_line_count) | |
3140 return FAIL; | |
3141 | |
3142 if (lowest_marked && lowest_marked > lnum) | |
3143 lowest_marked--; | |
3144 | |
3145 /* | |
3146 * If the file becomes empty the last line is replaced by an empty line. | |
3147 */ | |
3148 if (buf->b_ml.ml_line_count == 1) /* file becomes empty */ | |
3149 { | |
3150 if (message | |
3151 #ifdef FEAT_NETBEANS_INTG | |
3152 && !netbeansSuppressNoLines | |
3153 #endif | |
3154 ) | |
680 | 3155 set_keep_msg((char_u *)_(no_lines_msg), 0); |
3156 | |
4352 | 3157 /* FEAT_BYTEOFF already handled in there, don't worry 'bout it below */ |
7 | 3158 i = ml_replace((linenr_T)1, (char_u *)"", TRUE); |
3159 buf->b_ml.ml_flags |= ML_EMPTY; | |
3160 | |
3161 return i; | |
3162 } | |
3163 | |
3164 /* | |
3165 * find the data block containing the line | |
3166 * This also fills the stack with the blocks from the root to the data block | |
3167 * This also releases any locked block. | |
3168 */ | |
3169 mfp = buf->b_ml.ml_mfp; | |
3170 if (mfp == NULL) | |
3171 return FAIL; | |
3172 | |
3173 if ((hp = ml_find_line(buf, lnum, ML_DELETE)) == NULL) | |
3174 return FAIL; | |
3175 | |
3176 dp = (DATA_BL *)(hp->bh_data); | |
3177 /* compute line count before the delete */ | |
3178 count = (long)(buf->b_ml.ml_locked_high) | |
3179 - (long)(buf->b_ml.ml_locked_low) + 2; | |
3180 idx = lnum - buf->b_ml.ml_locked_low; | |
3181 | |
3182 --buf->b_ml.ml_line_count; | |
3183 | |
3184 line_start = ((dp->db_index[idx]) & DB_INDEX_MASK); | |
3185 if (idx == 0) /* first line in block, text at the end */ | |
3186 line_size = dp->db_txt_end - line_start; | |
3187 else | |
3188 line_size = ((dp->db_index[idx - 1]) & DB_INDEX_MASK) - line_start; | |
3189 | |
3190 #ifdef FEAT_NETBEANS_INTG | |
2210 | 3191 if (netbeans_active()) |
34 | 3192 netbeans_removed(buf, lnum, 0, (long)line_size); |
7 | 3193 #endif |
3194 | |
3195 /* | |
3196 * special case: If there is only one line in the data block it becomes empty. | |
3197 * Then we have to remove the entry, pointing to this data block, from the | |
3198 * pointer block. If this pointer block also becomes empty, we go up another | |
3199 * block, and so on, up to the root if necessary. | |
3200 * The line counts in the pointer blocks have already been adjusted by | |
3201 * ml_find_line(). | |
3202 */ | |
3203 if (count == 1) | |
3204 { | |
3205 mf_free(mfp, hp); /* free the data block */ | |
3206 buf->b_ml.ml_locked = NULL; | |
3207 | |
2823 | 3208 for (stack_idx = buf->b_ml.ml_stack_top - 1; stack_idx >= 0; |
3209 --stack_idx) | |
7 | 3210 { |
3211 buf->b_ml.ml_stack_top = 0; /* stack is invalid when failing */ | |
3212 ip = &(buf->b_ml.ml_stack[stack_idx]); | |
3213 idx = ip->ip_index; | |
3214 if ((hp = mf_get(mfp, ip->ip_bnum, 1)) == NULL) | |
3215 return FAIL; | |
3216 pp = (PTR_BL *)(hp->bh_data); /* must be pointer block */ | |
3217 if (pp->pb_id != PTR_ID) | |
3218 { | |
3219 EMSG(_("E317: pointer block id wrong 4")); | |
3220 mf_put(mfp, hp, FALSE, FALSE); | |
3221 return FAIL; | |
3222 } | |
3223 count = --(pp->pb_count); | |
3224 if (count == 0) /* the pointer block becomes empty! */ | |
3225 mf_free(mfp, hp); | |
3226 else | |
3227 { | |
3228 if (count != idx) /* move entries after the deleted one */ | |
3229 mch_memmove(&pp->pb_pointer[idx], &pp->pb_pointer[idx + 1], | |
3230 (size_t)(count - idx) * sizeof(PTR_EN)); | |
3231 mf_put(mfp, hp, TRUE, FALSE); | |
3232 | |
3233 buf->b_ml.ml_stack_top = stack_idx; /* truncate stack */ | |
1167 | 3234 /* fix line count for rest of blocks in the stack */ |
3235 if (buf->b_ml.ml_locked_lineadd != 0) | |
7 | 3236 { |
3237 ml_lineadd(buf, buf->b_ml.ml_locked_lineadd); | |
3238 buf->b_ml.ml_stack[buf->b_ml.ml_stack_top].ip_high += | |
1167 | 3239 buf->b_ml.ml_locked_lineadd; |
7 | 3240 } |
3241 ++(buf->b_ml.ml_stack_top); | |
3242 | |
3243 break; | |
3244 } | |
3245 } | |
3246 CHECK(stack_idx < 0, _("deleted block 1?")); | |
3247 } | |
3248 else | |
3249 { | |
3250 /* | |
3251 * delete the text by moving the next lines forwards | |
3252 */ | |
3253 text_start = dp->db_txt_start; | |
3254 mch_memmove((char *)dp + text_start + line_size, | |
3255 (char *)dp + text_start, (size_t)(line_start - text_start)); | |
3256 | |
3257 /* | |
3258 * delete the index by moving the next indexes backwards | |
3259 * Adjust the indexes for the text movement. | |
3260 */ | |
3261 for (i = idx; i < count - 1; ++i) | |
3262 dp->db_index[i] = dp->db_index[i + 1] + line_size; | |
3263 | |
3264 dp->db_free += line_size + INDEX_SIZE; | |
3265 dp->db_txt_start += line_size; | |
3266 --(dp->db_line_count); | |
3267 | |
3268 /* | |
3269 * mark the block dirty and make sure it is in the file (for recovery) | |
3270 */ | |
3271 buf->b_ml.ml_flags |= (ML_LOCKED_DIRTY | ML_LOCKED_POS); | |
3272 } | |
3273 | |
3274 #ifdef FEAT_BYTEOFF | |
3275 ml_updatechunk(buf, lnum, line_size, ML_CHNK_DELLINE); | |
3276 #endif | |
3277 return OK; | |
3278 } | |
3279 | |
3280 /* | |
3281 * set the B_MARKED flag for line 'lnum' | |
3282 */ | |
3283 void | |
3284 ml_setmarked(lnum) | |
3285 linenr_T lnum; | |
3286 { | |
3287 bhdr_T *hp; | |
3288 DATA_BL *dp; | |
3289 /* invalid line number */ | |
3290 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count | |
3291 || curbuf->b_ml.ml_mfp == NULL) | |
3292 return; /* give error message? */ | |
3293 | |
3294 if (lowest_marked == 0 || lowest_marked > lnum) | |
3295 lowest_marked = lnum; | |
3296 | |
3297 /* | |
3298 * find the data block containing the line | |
3299 * This also fills the stack with the blocks from the root to the data block | |
3300 * This also releases any locked block. | |
3301 */ | |
3302 if ((hp = ml_find_line(curbuf, lnum, ML_FIND)) == NULL) | |
3303 return; /* give error message? */ | |
3304 | |
3305 dp = (DATA_BL *)(hp->bh_data); | |
3306 dp->db_index[lnum - curbuf->b_ml.ml_locked_low] |= DB_MARKED; | |
3307 curbuf->b_ml.ml_flags |= ML_LOCKED_DIRTY; | |
3308 } | |
3309 | |
3310 /* | |
3311 * find the first line with its B_MARKED flag set | |
3312 */ | |
3313 linenr_T | |
3314 ml_firstmarked() | |
3315 { | |
3316 bhdr_T *hp; | |
3317 DATA_BL *dp; | |
3318 linenr_T lnum; | |
3319 int i; | |
3320 | |
3321 if (curbuf->b_ml.ml_mfp == NULL) | |
3322 return (linenr_T) 0; | |
3323 | |
3324 /* | |
3325 * The search starts with lowest_marked line. This is the last line where | |
3326 * a mark was found, adjusted by inserting/deleting lines. | |
3327 */ | |
3328 for (lnum = lowest_marked; lnum <= curbuf->b_ml.ml_line_count; ) | |
3329 { | |
3330 /* | |
3331 * Find the data block containing the line. | |
3332 * This also fills the stack with the blocks from the root to the data | |
3333 * block This also releases any locked block. | |
3334 */ | |
3335 if ((hp = ml_find_line(curbuf, lnum, ML_FIND)) == NULL) | |
3336 return (linenr_T)0; /* give error message? */ | |
3337 | |
3338 dp = (DATA_BL *)(hp->bh_data); | |
3339 | |
3340 for (i = lnum - curbuf->b_ml.ml_locked_low; | |
3341 lnum <= curbuf->b_ml.ml_locked_high; ++i, ++lnum) | |
3342 if ((dp->db_index[i]) & DB_MARKED) | |
3343 { | |
3344 (dp->db_index[i]) &= DB_INDEX_MASK; | |
3345 curbuf->b_ml.ml_flags |= ML_LOCKED_DIRTY; | |
3346 lowest_marked = lnum + 1; | |
3347 return lnum; | |
3348 } | |
3349 } | |
3350 | |
3351 return (linenr_T) 0; | |
3352 } | |
3353 | |
3354 /* | |
3355 * clear all DB_MARKED flags | |
3356 */ | |
3357 void | |
3358 ml_clearmarked() | |
3359 { | |
3360 bhdr_T *hp; | |
3361 DATA_BL *dp; | |
3362 linenr_T lnum; | |
3363 int i; | |
3364 | |
3365 if (curbuf->b_ml.ml_mfp == NULL) /* nothing to do */ | |
3366 return; | |
3367 | |
3368 /* | |
3369 * The search starts with line lowest_marked. | |
3370 */ | |
3371 for (lnum = lowest_marked; lnum <= curbuf->b_ml.ml_line_count; ) | |
3372 { | |
3373 /* | |
3374 * Find the data block containing the line. | |
3375 * This also fills the stack with the blocks from the root to the data | |
3376 * block and releases any locked block. | |
3377 */ | |
3378 if ((hp = ml_find_line(curbuf, lnum, ML_FIND)) == NULL) | |
3379 return; /* give error message? */ | |
3380 | |
3381 dp = (DATA_BL *)(hp->bh_data); | |
3382 | |
3383 for (i = lnum - curbuf->b_ml.ml_locked_low; | |
3384 lnum <= curbuf->b_ml.ml_locked_high; ++i, ++lnum) | |
3385 if ((dp->db_index[i]) & DB_MARKED) | |
3386 { | |
3387 (dp->db_index[i]) &= DB_INDEX_MASK; | |
3388 curbuf->b_ml.ml_flags |= ML_LOCKED_DIRTY; | |
3389 } | |
3390 } | |
3391 | |
3392 lowest_marked = 0; | |
3393 return; | |
3394 } | |
3395 | |
3396 /* | |
3397 * flush ml_line if necessary | |
3398 */ | |
3399 static void | |
3400 ml_flush_line(buf) | |
3401 buf_T *buf; | |
3402 { | |
3403 bhdr_T *hp; | |
3404 DATA_BL *dp; | |
3405 linenr_T lnum; | |
3406 char_u *new_line; | |
3407 char_u *old_line; | |
3408 colnr_T new_len; | |
3409 int old_len; | |
3410 int extra; | |
3411 int idx; | |
3412 int start; | |
3413 int count; | |
3414 int i; | |
2075
903fcd726d90
updated for version 7.2.359
Bram Moolenaar <bram@zimbu.org>
parents:
2003
diff
changeset
|
3415 static int entered = FALSE; |
7 | 3416 |
3417 if (buf->b_ml.ml_line_lnum == 0 || buf->b_ml.ml_mfp == NULL) | |
3418 return; /* nothing to do */ | |
3419 | |
3420 if (buf->b_ml.ml_flags & ML_LINE_DIRTY) | |
3421 { | |
2075
903fcd726d90
updated for version 7.2.359
Bram Moolenaar <bram@zimbu.org>
parents:
2003
diff
changeset
|
3422 /* This code doesn't work recursively, but Netbeans may call back here |
903fcd726d90
updated for version 7.2.359
Bram Moolenaar <bram@zimbu.org>
parents:
2003
diff
changeset
|
3423 * when obtaining the cursor position. */ |
903fcd726d90
updated for version 7.2.359
Bram Moolenaar <bram@zimbu.org>
parents:
2003
diff
changeset
|
3424 if (entered) |
903fcd726d90
updated for version 7.2.359
Bram Moolenaar <bram@zimbu.org>
parents:
2003
diff
changeset
|
3425 return; |
903fcd726d90
updated for version 7.2.359
Bram Moolenaar <bram@zimbu.org>
parents:
2003
diff
changeset
|
3426 entered = TRUE; |
903fcd726d90
updated for version 7.2.359
Bram Moolenaar <bram@zimbu.org>
parents:
2003
diff
changeset
|
3427 |
7 | 3428 lnum = buf->b_ml.ml_line_lnum; |
3429 new_line = buf->b_ml.ml_line_ptr; | |
3430 | |
3431 hp = ml_find_line(buf, lnum, ML_FIND); | |
3432 if (hp == NULL) | |
3433 EMSGN(_("E320: Cannot find line %ld"), lnum); | |
3434 else | |
3435 { | |
3436 dp = (DATA_BL *)(hp->bh_data); | |
3437 idx = lnum - buf->b_ml.ml_locked_low; | |
3438 start = ((dp->db_index[idx]) & DB_INDEX_MASK); | |
3439 old_line = (char_u *)dp + start; | |
3440 if (idx == 0) /* line is last in block */ | |
3441 old_len = dp->db_txt_end - start; | |
3442 else /* text of previous line follows */ | |
3443 old_len = (dp->db_index[idx - 1] & DB_INDEX_MASK) - start; | |
3444 new_len = (colnr_T)STRLEN(new_line) + 1; | |
3445 extra = new_len - old_len; /* negative if lines gets smaller */ | |
3446 | |
3447 /* | |
3448 * if new line fits in data block, replace directly | |
3449 */ | |
3450 if ((int)dp->db_free >= extra) | |
3451 { | |
3452 /* if the length changes and there are following lines */ | |
3453 count = buf->b_ml.ml_locked_high - buf->b_ml.ml_locked_low + 1; | |
3454 if (extra != 0 && idx < count - 1) | |
3455 { | |
3456 /* move text of following lines */ | |
3457 mch_memmove((char *)dp + dp->db_txt_start - extra, | |
3458 (char *)dp + dp->db_txt_start, | |
3459 (size_t)(start - dp->db_txt_start)); | |
3460 | |
3461 /* adjust pointers of this and following lines */ | |
3462 for (i = idx + 1; i < count; ++i) | |
3463 dp->db_index[i] -= extra; | |
3464 } | |
3465 dp->db_index[idx] -= extra; | |
3466 | |
3467 /* adjust free space */ | |
3468 dp->db_free -= extra; | |
3469 dp->db_txt_start -= extra; | |
3470 | |
3471 /* copy new line into the data block */ | |
3472 mch_memmove(old_line - extra, new_line, (size_t)new_len); | |
3473 buf->b_ml.ml_flags |= (ML_LOCKED_DIRTY | ML_LOCKED_POS); | |
3474 #ifdef FEAT_BYTEOFF | |
3475 /* The else case is already covered by the insert and delete */ | |
3476 ml_updatechunk(buf, lnum, (long)extra, ML_CHNK_UPDLINE); | |
3477 #endif | |
3478 } | |
3479 else | |
3480 { | |
3481 /* | |
3482 * Cannot do it in one data block: Delete and append. | |
3483 * Append first, because ml_delete_int() cannot delete the | |
3484 * last line in a buffer, which causes trouble for a buffer | |
3485 * that has only one line. | |
3486 * Don't forget to copy the mark! | |
3487 */ | |
3488 /* How about handling errors??? */ | |
3489 (void)ml_append_int(buf, lnum, new_line, new_len, FALSE, | |
3490 (dp->db_index[idx] & DB_MARKED)); | |
3491 (void)ml_delete_int(buf, lnum, FALSE); | |
3492 } | |
3493 } | |
3494 vim_free(new_line); | |
2075
903fcd726d90
updated for version 7.2.359
Bram Moolenaar <bram@zimbu.org>
parents:
2003
diff
changeset
|
3495 |
903fcd726d90
updated for version 7.2.359
Bram Moolenaar <bram@zimbu.org>
parents:
2003
diff
changeset
|
3496 entered = FALSE; |
7 | 3497 } |
3498 | |
3499 buf->b_ml.ml_line_lnum = 0; | |
3500 } | |
3501 | |
3502 /* | |
3503 * create a new, empty, data block | |
3504 */ | |
3505 static bhdr_T * | |
3506 ml_new_data(mfp, negative, page_count) | |
3507 memfile_T *mfp; | |
3508 int negative; | |
3509 int page_count; | |
3510 { | |
3511 bhdr_T *hp; | |
3512 DATA_BL *dp; | |
3513 | |
3514 if ((hp = mf_new(mfp, negative, page_count)) == NULL) | |
3515 return NULL; | |
3516 | |
3517 dp = (DATA_BL *)(hp->bh_data); | |
3518 dp->db_id = DATA_ID; | |
3519 dp->db_txt_start = dp->db_txt_end = page_count * mfp->mf_page_size; | |
3520 dp->db_free = dp->db_txt_start - HEADER_SIZE; | |
3521 dp->db_line_count = 0; | |
3522 | |
3523 return hp; | |
3524 } | |
3525 | |
3526 /* | |
3527 * create a new, empty, pointer block | |
3528 */ | |
3529 static bhdr_T * | |
3530 ml_new_ptr(mfp) | |
3531 memfile_T *mfp; | |
3532 { | |
3533 bhdr_T *hp; | |
3534 PTR_BL *pp; | |
3535 | |
3536 if ((hp = mf_new(mfp, FALSE, 1)) == NULL) | |
3537 return NULL; | |
3538 | |
3539 pp = (PTR_BL *)(hp->bh_data); | |
3540 pp->pb_id = PTR_ID; | |
3541 pp->pb_count = 0; | |
2240
6b4879aea261
Add test for gettabvar() and settabvar().
Bram Moolenaar <bram@vim.org>
parents:
2221
diff
changeset
|
3542 pp->pb_count_max = (short_u)((mfp->mf_page_size - sizeof(PTR_BL)) |
6b4879aea261
Add test for gettabvar() and settabvar().
Bram Moolenaar <bram@vim.org>
parents:
2221
diff
changeset
|
3543 / sizeof(PTR_EN) + 1); |
7 | 3544 |
3545 return hp; | |
3546 } | |
3547 | |
3548 /* | |
3549 * lookup line 'lnum' in a memline | |
3550 * | |
3551 * action: if ML_DELETE or ML_INSERT the line count is updated while searching | |
3552 * if ML_FLUSH only flush a locked block | |
3553 * if ML_FIND just find the line | |
3554 * | |
3555 * If the block was found it is locked and put in ml_locked. | |
3556 * The stack is updated to lead to the locked block. The ip_high field in | |
3557 * the stack is updated to reflect the last line in the block AFTER the | |
3558 * insert or delete, also if the pointer block has not been updated yet. But | |
1167 | 3559 * if ml_locked != NULL ml_locked_lineadd must be added to ip_high. |
7 | 3560 * |
3561 * return: NULL for failure, pointer to block header otherwise | |
3562 */ | |
3563 static bhdr_T * | |
3564 ml_find_line(buf, lnum, action) | |
3565 buf_T *buf; | |
3566 linenr_T lnum; | |
3567 int action; | |
3568 { | |
3569 DATA_BL *dp; | |
3570 PTR_BL *pp; | |
3571 infoptr_T *ip; | |
3572 bhdr_T *hp; | |
3573 memfile_T *mfp; | |
3574 linenr_T t; | |
3575 blocknr_T bnum, bnum2; | |
3576 int dirty; | |
3577 linenr_T low, high; | |
3578 int top; | |
3579 int page_count; | |
3580 int idx; | |
3581 | |
3582 mfp = buf->b_ml.ml_mfp; | |
3583 | |
3584 /* | |
3585 * If there is a locked block check if the wanted line is in it. | |
3586 * If not, flush and release the locked block. | |
3587 * Don't do this for ML_INSERT_SAME, because the stack need to be updated. | |
3588 * Don't do this for ML_FLUSH, because we want to flush the locked block. | |
1066 | 3589 * Don't do this when 'swapfile' is reset, we want to load all the blocks. |
7 | 3590 */ |
3591 if (buf->b_ml.ml_locked) | |
3592 { | |
1066 | 3593 if (ML_SIMPLE(action) |
3594 && buf->b_ml.ml_locked_low <= lnum | |
3595 && buf->b_ml.ml_locked_high >= lnum | |
3596 && !mf_dont_release) | |
7 | 3597 { |
1066 | 3598 /* remember to update pointer blocks and stack later */ |
7 | 3599 if (action == ML_INSERT) |
3600 { | |
3601 ++(buf->b_ml.ml_locked_lineadd); | |
3602 ++(buf->b_ml.ml_locked_high); | |
3603 } | |
3604 else if (action == ML_DELETE) | |
3605 { | |
3606 --(buf->b_ml.ml_locked_lineadd); | |
3607 --(buf->b_ml.ml_locked_high); | |
3608 } | |
3609 return (buf->b_ml.ml_locked); | |
3610 } | |
3611 | |
3612 mf_put(mfp, buf->b_ml.ml_locked, buf->b_ml.ml_flags & ML_LOCKED_DIRTY, | |
3613 buf->b_ml.ml_flags & ML_LOCKED_POS); | |
3614 buf->b_ml.ml_locked = NULL; | |
3615 | |
1167 | 3616 /* |
3617 * If lines have been added or deleted in the locked block, need to | |
3618 * update the line count in pointer blocks. | |
3619 */ | |
3620 if (buf->b_ml.ml_locked_lineadd != 0) | |
7 | 3621 ml_lineadd(buf, buf->b_ml.ml_locked_lineadd); |
3622 } | |
3623 | |
3624 if (action == ML_FLUSH) /* nothing else to do */ | |
3625 return NULL; | |
3626 | |
3627 bnum = 1; /* start at the root of the tree */ | |
3628 page_count = 1; | |
3629 low = 1; | |
3630 high = buf->b_ml.ml_line_count; | |
3631 | |
3632 if (action == ML_FIND) /* first try stack entries */ | |
3633 { | |
3634 for (top = buf->b_ml.ml_stack_top - 1; top >= 0; --top) | |
3635 { | |
3636 ip = &(buf->b_ml.ml_stack[top]); | |
3637 if (ip->ip_low <= lnum && ip->ip_high >= lnum) | |
3638 { | |
3639 bnum = ip->ip_bnum; | |
3640 low = ip->ip_low; | |
3641 high = ip->ip_high; | |
3642 buf->b_ml.ml_stack_top = top; /* truncate stack at prev entry */ | |
3643 break; | |
3644 } | |
3645 } | |
3646 if (top < 0) | |
3647 buf->b_ml.ml_stack_top = 0; /* not found, start at the root */ | |
3648 } | |
3649 else /* ML_DELETE or ML_INSERT */ | |
3650 buf->b_ml.ml_stack_top = 0; /* start at the root */ | |
3651 | |
3652 /* | |
3653 * search downwards in the tree until a data block is found | |
3654 */ | |
3655 for (;;) | |
3656 { | |
3657 if ((hp = mf_get(mfp, bnum, page_count)) == NULL) | |
3658 goto error_noblock; | |
3659 | |
3660 /* | |
3661 * update high for insert/delete | |
3662 */ | |
3663 if (action == ML_INSERT) | |
3664 ++high; | |
3665 else if (action == ML_DELETE) | |
3666 --high; | |
3667 | |
3668 dp = (DATA_BL *)(hp->bh_data); | |
3669 if (dp->db_id == DATA_ID) /* data block */ | |
3670 { | |
3671 buf->b_ml.ml_locked = hp; | |
3672 buf->b_ml.ml_locked_low = low; | |
3673 buf->b_ml.ml_locked_high = high; | |
3674 buf->b_ml.ml_locked_lineadd = 0; | |
3675 buf->b_ml.ml_flags &= ~(ML_LOCKED_DIRTY | ML_LOCKED_POS); | |
3676 return hp; | |
3677 } | |
3678 | |
3679 pp = (PTR_BL *)(dp); /* must be pointer block */ | |
3680 if (pp->pb_id != PTR_ID) | |
3681 { | |
3682 EMSG(_("E317: pointer block id wrong")); | |
3683 goto error_block; | |
3684 } | |
3685 | |
3686 if ((top = ml_add_stack(buf)) < 0) /* add new entry to stack */ | |
3687 goto error_block; | |
3688 ip = &(buf->b_ml.ml_stack[top]); | |
3689 ip->ip_bnum = bnum; | |
3690 ip->ip_low = low; | |
3691 ip->ip_high = high; | |
3692 ip->ip_index = -1; /* index not known yet */ | |
3693 | |
3694 dirty = FALSE; | |
3695 for (idx = 0; idx < (int)pp->pb_count; ++idx) | |
3696 { | |
3697 t = pp->pb_pointer[idx].pe_line_count; | |
3698 CHECK(t == 0, _("pe_line_count is zero")); | |
3699 if ((low += t) > lnum) | |
3700 { | |
3701 ip->ip_index = idx; | |
3702 bnum = pp->pb_pointer[idx].pe_bnum; | |
3703 page_count = pp->pb_pointer[idx].pe_page_count; | |
3704 high = low - 1; | |
3705 low -= t; | |
3706 | |
3707 /* | |
3708 * a negative block number may have been changed | |
3709 */ | |
3710 if (bnum < 0) | |
3711 { | |
3712 bnum2 = mf_trans_del(mfp, bnum); | |
3713 if (bnum != bnum2) | |
3714 { | |
3715 bnum = bnum2; | |
3716 pp->pb_pointer[idx].pe_bnum = bnum; | |
3717 dirty = TRUE; | |
3718 } | |
3719 } | |
3720 | |
3721 break; | |
3722 } | |
3723 } | |
3724 if (idx >= (int)pp->pb_count) /* past the end: something wrong! */ | |
3725 { | |
3726 if (lnum > buf->b_ml.ml_line_count) | |
3727 EMSGN(_("E322: line number out of range: %ld past the end"), | |
3728 lnum - buf->b_ml.ml_line_count); | |
3729 | |
3730 else | |
3731 EMSGN(_("E323: line count wrong in block %ld"), bnum); | |
3732 goto error_block; | |
3733 } | |
3734 if (action == ML_DELETE) | |
3735 { | |
3736 pp->pb_pointer[idx].pe_line_count--; | |
3737 dirty = TRUE; | |
3738 } | |
3739 else if (action == ML_INSERT) | |
3740 { | |
3741 pp->pb_pointer[idx].pe_line_count++; | |
3742 dirty = TRUE; | |
3743 } | |
3744 mf_put(mfp, hp, dirty, FALSE); | |
3745 } | |
3746 | |
3747 error_block: | |
3748 mf_put(mfp, hp, FALSE, FALSE); | |
3749 error_noblock: | |
2267 | 3750 /* |
3751 * If action is ML_DELETE or ML_INSERT we have to correct the tree for | |
3752 * the incremented/decremented line counts, because there won't be a line | |
3753 * inserted/deleted after all. | |
3754 */ | |
7 | 3755 if (action == ML_DELETE) |
3756 ml_lineadd(buf, 1); | |
3757 else if (action == ML_INSERT) | |
3758 ml_lineadd(buf, -1); | |
3759 buf->b_ml.ml_stack_top = 0; | |
3760 return NULL; | |
3761 } | |
3762 | |
3763 /* | |
3764 * add an entry to the info pointer stack | |
3765 * | |
3766 * return -1 for failure, number of the new entry otherwise | |
3767 */ | |
3768 static int | |
3769 ml_add_stack(buf) | |
3770 buf_T *buf; | |
3771 { | |
3772 int top; | |
3773 infoptr_T *newstack; | |
3774 | |
3775 top = buf->b_ml.ml_stack_top; | |
3776 | |
2267 | 3777 /* may have to increase the stack size */ |
7 | 3778 if (top == buf->b_ml.ml_stack_size) |
3779 { | |
2267 | 3780 CHECK(top > 0, _("Stack size increases")); /* more than 5 levels??? */ |
7 | 3781 |
3782 newstack = (infoptr_T *)alloc((unsigned)sizeof(infoptr_T) * | |
3783 (buf->b_ml.ml_stack_size + STACK_INCR)); | |
3784 if (newstack == NULL) | |
3785 return -1; | |
1624 | 3786 mch_memmove(newstack, buf->b_ml.ml_stack, |
3787 (size_t)top * sizeof(infoptr_T)); | |
7 | 3788 vim_free(buf->b_ml.ml_stack); |
3789 buf->b_ml.ml_stack = newstack; | |
3790 buf->b_ml.ml_stack_size += STACK_INCR; | |
3791 } | |
3792 | |
3793 buf->b_ml.ml_stack_top++; | |
3794 return top; | |
3795 } | |
3796 | |
3797 /* | |
3798 * Update the pointer blocks on the stack for inserted/deleted lines. | |
3799 * The stack itself is also updated. | |
3800 * | |
3801 * When a insert/delete line action fails, the line is not inserted/deleted, | |
3802 * but the pointer blocks have already been updated. That is fixed here by | |
3803 * walking through the stack. | |
3804 * | |
3805 * Count is the number of lines added, negative if lines have been deleted. | |
3806 */ | |
3807 static void | |
3808 ml_lineadd(buf, count) | |
3809 buf_T *buf; | |
3810 int count; | |
3811 { | |
3812 int idx; | |
3813 infoptr_T *ip; | |
3814 PTR_BL *pp; | |
3815 memfile_T *mfp = buf->b_ml.ml_mfp; | |
3816 bhdr_T *hp; | |
3817 | |
3818 for (idx = buf->b_ml.ml_stack_top - 1; idx >= 0; --idx) | |
3819 { | |
3820 ip = &(buf->b_ml.ml_stack[idx]); | |
3821 if ((hp = mf_get(mfp, ip->ip_bnum, 1)) == NULL) | |
3822 break; | |
3823 pp = (PTR_BL *)(hp->bh_data); /* must be pointer block */ | |
3824 if (pp->pb_id != PTR_ID) | |
3825 { | |
3826 mf_put(mfp, hp, FALSE, FALSE); | |
3827 EMSG(_("E317: pointer block id wrong 2")); | |
3828 break; | |
3829 } | |
3830 pp->pb_pointer[ip->ip_index].pe_line_count += count; | |
3831 ip->ip_high += count; | |
3832 mf_put(mfp, hp, TRUE, FALSE); | |
3833 } | |
3834 } | |
3835 | |
2214
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
3836 #if defined(HAVE_READLINK) || defined(PROTO) |
594 | 3837 /* |
3838 * Resolve a symlink in the last component of a file name. | |
3839 * Note that f_resolve() does it for every part of the path, we don't do that | |
3840 * here. | |
3841 * If it worked returns OK and the resolved link in "buf[MAXPATHL]". | |
3842 * Otherwise returns FAIL. | |
3843 */ | |
2214
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
3844 int |
594 | 3845 resolve_symlink(fname, buf) |
3846 char_u *fname; | |
3847 char_u *buf; | |
3848 { | |
3849 char_u tmp[MAXPATHL]; | |
3850 int ret; | |
3851 int depth = 0; | |
3852 | |
3853 if (fname == NULL) | |
3854 return FAIL; | |
3855 | |
3856 /* Put the result so far in tmp[], starting with the original name. */ | |
3857 vim_strncpy(tmp, fname, MAXPATHL - 1); | |
3858 | |
3859 for (;;) | |
3860 { | |
3861 /* Limit symlink depth to 100, catch recursive loops. */ | |
3862 if (++depth == 100) | |
3863 { | |
3864 EMSG2(_("E773: Symlink loop for \"%s\""), fname); | |
3865 return FAIL; | |
3866 } | |
3867 | |
3868 ret = readlink((char *)tmp, (char *)buf, MAXPATHL - 1); | |
3869 if (ret <= 0) | |
3870 { | |
619 | 3871 if (errno == EINVAL || errno == ENOENT) |
594 | 3872 { |
619 | 3873 /* Found non-symlink or not existing file, stop here. |
1855 | 3874 * When at the first level use the unmodified name, skip the |
594 | 3875 * call to vim_FullName(). */ |
3876 if (depth == 1) | |
3877 return FAIL; | |
3878 | |
3879 /* Use the resolved name in tmp[]. */ | |
3880 break; | |
3881 } | |
3882 | |
3883 /* There must be some error reading links, use original name. */ | |
3884 return FAIL; | |
3885 } | |
3886 buf[ret] = NUL; | |
3887 | |
3888 /* | |
3889 * Check whether the symlink is relative or absolute. | |
3890 * If it's relative, build a new path based on the directory | |
3891 * portion of the filename (if any) and the path the symlink | |
3892 * points to. | |
3893 */ | |
3894 if (mch_isFullName(buf)) | |
3895 STRCPY(tmp, buf); | |
3896 else | |
3897 { | |
3898 char_u *tail; | |
3899 | |
3900 tail = gettail(tmp); | |
3901 if (STRLEN(tail) + STRLEN(buf) >= MAXPATHL) | |
3902 return FAIL; | |
3903 STRCPY(tail, buf); | |
3904 } | |
3905 } | |
3906 | |
3907 /* | |
3908 * Try to resolve the full name of the file so that the swapfile name will | |
3909 * be consistent even when opening a relative symlink from different | |
3910 * working directories. | |
3911 */ | |
3912 return vim_FullName(tmp, buf, MAXPATHL, TRUE); | |
3913 } | |
3914 #endif | |
3915 | |
7 | 3916 /* |
460 | 3917 * Make swap file name out of the file name and a directory name. |
3918 * Returns pointer to allocated memory or NULL. | |
7 | 3919 */ |
460 | 3920 char_u * |
3921 makeswapname(fname, ffname, buf, dir_name) | |
3922 char_u *fname; | |
1978 | 3923 char_u *ffname UNUSED; |
7 | 3924 buf_T *buf; |
3925 char_u *dir_name; | |
3926 { | |
3927 char_u *r, *s; | |
2145
de0e7ca61893
updated for version 7.2.427
Bram Moolenaar <bram@zimbu.org>
parents:
2108
diff
changeset
|
3928 char_u *fname_res = fname; |
594 | 3929 #ifdef HAVE_READLINK |
3930 char_u fname_buf[MAXPATHL]; | |
3931 #endif | |
7 | 3932 |
3933 #if defined(UNIX) || defined(WIN3264) /* Need _very_ long file names */ | |
3934 s = dir_name + STRLEN(dir_name); | |
39 | 3935 if (after_pathsep(dir_name, s) && s[-1] == s[-2]) |
7 | 3936 { /* Ends with '//', Use Full path */ |
3937 r = NULL; | |
460 | 3938 if ((s = make_percent_swname(dir_name, fname)) != NULL) |
7 | 3939 { |
3940 r = modname(s, (char_u *)".swp", FALSE); | |
3941 vim_free(s); | |
3942 } | |
3943 return r; | |
3944 } | |
3945 #endif | |
3946 | |
594 | 3947 #ifdef HAVE_READLINK |
3948 /* Expand symlink in the file name, so that we put the swap file with the | |
3949 * actual file instead of with the symlink. */ | |
3950 if (resolve_symlink(fname, fname_buf) == OK) | |
3951 fname_res = fname_buf; | |
3952 #endif | |
3953 | |
7 | 3954 r = buf_modname( |
3955 #ifdef SHORT_FNAME | |
3956 TRUE, | |
3957 #else | |
3958 (buf->b_p_sn || buf->b_shortname), | |
3959 #endif | |
594 | 3960 fname_res, |
7 | 3961 (char_u *) |
2823 | 3962 #if defined(VMS) |
7 | 3963 "_swp", |
3964 #else | |
3965 ".swp", | |
3966 #endif | |
3967 #ifdef SHORT_FNAME /* always 8.3 file name */ | |
3968 FALSE | |
3969 #else | |
3970 /* Prepend a '.' to the swap file name for the current directory. */ | |
3971 dir_name[0] == '.' && dir_name[1] == NUL | |
3972 #endif | |
3973 ); | |
3974 if (r == NULL) /* out of memory */ | |
3975 return NULL; | |
3976 | |
3977 s = get_file_in_dir(r, dir_name); | |
3978 vim_free(r); | |
3979 return s; | |
3980 } | |
3981 | |
3982 /* | |
3983 * Get file name to use for swap file or backup file. | |
3984 * Use the name of the edited file "fname" and an entry in the 'dir' or 'bdir' | |
3985 * option "dname". | |
3986 * - If "dname" is ".", return "fname" (swap file in dir of file). | |
3987 * - If "dname" starts with "./", insert "dname" in "fname" (swap file | |
3988 * relative to dir of file). | |
3989 * - Otherwise, prepend "dname" to the tail of "fname" (swap file in specific | |
3990 * dir). | |
3991 * | |
3992 * The return value is an allocated string and can be NULL. | |
3993 */ | |
3994 char_u * | |
3995 get_file_in_dir(fname, dname) | |
3996 char_u *fname; | |
3997 char_u *dname; /* don't use "dirname", it is a global for Alpha */ | |
3998 { | |
3999 char_u *t; | |
4000 char_u *tail; | |
4001 char_u *retval; | |
4002 int save_char; | |
4003 | |
4004 tail = gettail(fname); | |
4005 | |
4006 if (dname[0] == '.' && dname[1] == NUL) | |
4007 retval = vim_strsave(fname); | |
4008 else if (dname[0] == '.' && vim_ispathsep(dname[1])) | |
4009 { | |
4010 if (tail == fname) /* no path before file name */ | |
4011 retval = concat_fnames(dname + 2, tail, TRUE); | |
4012 else | |
4013 { | |
4014 save_char = *tail; | |
4015 *tail = NUL; | |
4016 t = concat_fnames(fname, dname + 2, TRUE); | |
4017 *tail = save_char; | |
4018 if (t == NULL) /* out of memory */ | |
4019 retval = NULL; | |
4020 else | |
4021 { | |
4022 retval = concat_fnames(t, tail, TRUE); | |
4023 vim_free(t); | |
4024 } | |
4025 } | |
4026 } | |
4027 else | |
4028 retval = concat_fnames(dname, tail, TRUE); | |
4029 | |
5432 | 4030 #ifdef WIN3264 |
4031 if (retval != NULL) | |
4032 for (t = gettail(retval); *t != NUL; mb_ptr_adv(t)) | |
4033 if (*t == ':') | |
4034 *t = '%'; | |
4035 #endif | |
4036 | |
7 | 4037 return retval; |
4038 } | |
4039 | |
580 | 4040 static void attention_message __ARGS((buf_T *buf, char_u *fname)); |
4041 | |
4042 /* | |
4043 * Print the ATTENTION message: info about an existing swap file. | |
4044 */ | |
4045 static void | |
4046 attention_message(buf, fname) | |
4047 buf_T *buf; /* buffer being edited */ | |
4048 char_u *fname; /* swap file name */ | |
4049 { | |
4050 struct stat st; | |
4051 time_t x, sx; | |
1001 | 4052 char *p; |
580 | 4053 |
4054 ++no_wait_return; | |
4055 (void)EMSG(_("E325: ATTENTION")); | |
4056 MSG_PUTS(_("\nFound a swap file by the name \"")); | |
4057 msg_home_replace(fname); | |
4058 MSG_PUTS("\"\n"); | |
4059 sx = swapfile_info(fname); | |
4060 MSG_PUTS(_("While opening file \"")); | |
4061 msg_outtrans(buf->b_fname); | |
4062 MSG_PUTS("\"\n"); | |
4063 if (mch_stat((char *)buf->b_fname, &st) != -1) | |
4064 { | |
4065 MSG_PUTS(_(" dated: ")); | |
4066 x = st.st_mtime; /* Manx C can't do &st.st_mtime */ | |
1001 | 4067 p = ctime(&x); /* includes '\n' */ |
4068 if (p == NULL) | |
4069 MSG_PUTS("(invalid)\n"); | |
4070 else | |
4071 MSG_PUTS(p); | |
580 | 4072 if (sx != 0 && x > sx) |
4073 MSG_PUTS(_(" NEWER than swap file!\n")); | |
4074 } | |
4075 /* Some of these messages are long to allow translation to | |
4076 * other languages. */ | |
2703 | 4077 MSG_PUTS(_("\n(1) Another program may be editing the same file. If this is the case,\n be careful not to end up with two different instances of the same\n file when making changes.")); |
4078 MSG_PUTS(_(" Quit, or continue with caution.\n")); | |
4079 MSG_PUTS(_("(2) An edit session for this file crashed.\n")); | |
580 | 4080 MSG_PUTS(_(" If this is the case, use \":recover\" or \"vim -r ")); |
4081 msg_outtrans(buf->b_fname); | |
4082 MSG_PUTS(_("\"\n to recover the changes (see \":help recovery\").\n")); | |
4083 MSG_PUTS(_(" If you did this already, delete the swap file \"")); | |
4084 msg_outtrans(fname); | |
4085 MSG_PUTS(_("\"\n to avoid this message.\n")); | |
4086 cmdline_row = msg_row; | |
4087 --no_wait_return; | |
4088 } | |
4089 | |
4090 #ifdef FEAT_AUTOCMD | |
4091 static int do_swapexists __ARGS((buf_T *buf, char_u *fname)); | |
4092 | |
4093 /* | |
4094 * Trigger the SwapExists autocommands. | |
4095 * Returns a value for equivalent to do_dialog() (see below): | |
4096 * 0: still need to ask for a choice | |
4097 * 1: open read-only | |
4098 * 2: edit anyway | |
4099 * 3: recover | |
4100 * 4: delete it | |
4101 * 5: quit | |
4102 * 6: abort | |
4103 */ | |
4104 static int | |
4105 do_swapexists(buf, fname) | |
4106 buf_T *buf; | |
4107 char_u *fname; | |
4108 { | |
4109 set_vim_var_string(VV_SWAPNAME, fname, -1); | |
4110 set_vim_var_string(VV_SWAPCHOICE, NULL, -1); | |
4111 | |
4112 /* Trigger SwapExists autocommands with <afile> set to the file being | |
1856 | 4113 * edited. Disallow changing directory here. */ |
4114 ++allbuf_lock; | |
580 | 4115 apply_autocmds(EVENT_SWAPEXISTS, buf->b_fname, NULL, FALSE, NULL); |
1856 | 4116 --allbuf_lock; |
580 | 4117 |
4118 set_vim_var_string(VV_SWAPNAME, NULL, -1); | |
4119 | |
4120 switch (*get_vim_var_str(VV_SWAPCHOICE)) | |
4121 { | |
4122 case 'o': return 1; | |
4123 case 'e': return 2; | |
4124 case 'r': return 3; | |
4125 case 'd': return 4; | |
4126 case 'q': return 5; | |
4127 case 'a': return 6; | |
4128 } | |
4129 | |
4130 return 0; | |
4131 } | |
4132 #endif | |
4133 | |
7 | 4134 /* |
4135 * Find out what name to use for the swap file for buffer 'buf'. | |
4136 * | |
4137 * Several names are tried to find one that does not exist | |
460 | 4138 * Returns the name in allocated memory or NULL. |
3158 | 4139 * When out of memory "dirp" is set to NULL. |
7 | 4140 * |
4141 * Note: If BASENAMELEN is not correct, you will get error messages for | |
2214
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
4142 * not being able to open the swap or undo file |
1856 | 4143 * Note: May trigger SwapExists autocmd, pointers may change! |
7 | 4144 */ |
4145 static char_u * | |
4146 findswapname(buf, dirp, old_fname) | |
4147 buf_T *buf; | |
4148 char_u **dirp; /* pointer to list of directories */ | |
4149 char_u *old_fname; /* don't give warning for this file name */ | |
4150 { | |
4151 char_u *fname; | |
4152 int n; | |
4153 char_u *dir_name; | |
4154 #ifdef AMIGA | |
4155 BPTR fh; | |
4156 #endif | |
4157 #ifndef SHORT_FNAME | |
4158 int r; | |
4159 #endif | |
5432 | 4160 char_u *buf_fname = buf->b_fname; |
7 | 4161 |
4162 #if !defined(SHORT_FNAME) \ | |
5432 | 4163 && ((!defined(UNIX) && !defined(OS2)) || defined(ARCHIE)) |
7 | 4164 # define CREATE_DUMMY_FILE |
4165 FILE *dummyfd = NULL; | |
4166 | |
5432 | 4167 # ifdef WIN3264 |
4168 if (buf_fname != NULL && !mch_isFullName(buf_fname) | |
4169 && vim_strchr(gettail(buf_fname), ':')) | |
4170 { | |
4171 char_u *t; | |
4172 | |
4173 buf_fname = vim_strsave(buf_fname); | |
4174 if (buf_fname == NULL) | |
4175 buf_fname = buf->b_fname; | |
4176 else | |
4177 for (t = gettail(buf_fname); *t != NUL; mb_ptr_adv(t)) | |
4178 if (*t == ':') | |
4179 *t = '%'; | |
4180 } | |
4181 # endif | |
4182 | |
2214
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
4183 /* |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
4184 * If we start editing a new file, e.g. "test.doc", which resides on an |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
4185 * MSDOS compatible filesystem, it is possible that the file |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
4186 * "test.doc.swp" which we create will be exactly the same file. To avoid |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
4187 * this problem we temporarily create "test.doc". Don't do this when the |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
4188 * check below for a 8.3 file name is used. |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
4189 */ |
5432 | 4190 if (!(buf->b_p_sn || buf->b_shortname) && buf_fname != NULL |
4191 && mch_getperm(buf_fname) < 0) | |
4192 dummyfd = mch_fopen((char *)buf_fname, "w"); | |
7 | 4193 #endif |
4194 | |
2214
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
4195 /* |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
4196 * Isolate a directory name from *dirp and put it in dir_name. |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
4197 * First allocate some memory to put the directory name in. |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
4198 */ |
7 | 4199 dir_name = alloc((unsigned)STRLEN(*dirp) + 1); |
3158 | 4200 if (dir_name == NULL) |
4201 *dirp = NULL; | |
4202 else | |
7 | 4203 (void)copy_option_part(dirp, dir_name, 31000, ","); |
4204 | |
2214
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
4205 /* |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
4206 * we try different names until we find one that does not exist yet |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
4207 */ |
7 | 4208 if (dir_name == NULL) /* out of memory */ |
4209 fname = NULL; | |
4210 else | |
5432 | 4211 fname = makeswapname(buf_fname, buf->b_ffname, buf, dir_name); |
7 | 4212 |
4213 for (;;) | |
4214 { | |
4215 if (fname == NULL) /* must be out of memory */ | |
4216 break; | |
4217 if ((n = (int)STRLEN(fname)) == 0) /* safety check */ | |
4218 { | |
4219 vim_free(fname); | |
4220 fname = NULL; | |
4221 break; | |
4222 } | |
4223 #if (defined(UNIX) || defined(OS2)) && !defined(ARCHIE) && !defined(SHORT_FNAME) | |
4224 /* | |
4225 * Some systems have a MS-DOS compatible filesystem that use 8.3 character | |
4226 * file names. If this is the first try and the swap file name does not fit in | |
4227 * 8.3, detect if this is the case, set shortname and try again. | |
4228 */ | |
4229 if (fname[n - 2] == 'w' && fname[n - 1] == 'p' | |
4230 && !(buf->b_p_sn || buf->b_shortname)) | |
4231 { | |
4232 char_u *tail; | |
4233 char_u *fname2; | |
4234 struct stat s1, s2; | |
4235 int f1, f2; | |
4236 int created1 = FALSE, created2 = FALSE; | |
4237 int same = FALSE; | |
4238 | |
4239 /* | |
4240 * Check if swapfile name does not fit in 8.3: | |
4241 * It either contains two dots, is longer than 8 chars, or starts | |
4242 * with a dot. | |
4243 */ | |
5432 | 4244 tail = gettail(buf_fname); |
7 | 4245 if ( vim_strchr(tail, '.') != NULL |
4246 || STRLEN(tail) > (size_t)8 | |
4247 || *gettail(fname) == '.') | |
4248 { | |
4249 fname2 = alloc(n + 2); | |
4250 if (fname2 != NULL) | |
4251 { | |
4252 STRCPY(fname2, fname); | |
4253 /* if fname == "xx.xx.swp", fname2 = "xx.xx.swx" | |
4254 * if fname == ".xx.swp", fname2 = ".xx.swpx" | |
4255 * if fname == "123456789.swp", fname2 = "12345678x.swp" | |
4256 */ | |
4257 if (vim_strchr(tail, '.') != NULL) | |
4258 fname2[n - 1] = 'x'; | |
4259 else if (*gettail(fname) == '.') | |
4260 { | |
4261 fname2[n] = 'x'; | |
4262 fname2[n + 1] = NUL; | |
4263 } | |
4264 else | |
4265 fname2[n - 5] += 1; | |
4266 /* | |
4267 * may need to create the files to be able to use mch_stat() | |
4268 */ | |
4269 f1 = mch_open((char *)fname, O_RDONLY | O_EXTRA, 0); | |
4270 if (f1 < 0) | |
4271 { | |
4272 f1 = mch_open_rw((char *)fname, | |
4273 O_RDWR|O_CREAT|O_EXCL|O_EXTRA); | |
4274 #if defined(OS2) | |
4275 if (f1 < 0 && errno == ENOENT) | |
4276 same = TRUE; | |
4277 #endif | |
4278 created1 = TRUE; | |
4279 } | |
4280 if (f1 >= 0) | |
4281 { | |
4282 f2 = mch_open((char *)fname2, O_RDONLY | O_EXTRA, 0); | |
4283 if (f2 < 0) | |
4284 { | |
4285 f2 = mch_open_rw((char *)fname2, | |
4286 O_RDWR|O_CREAT|O_EXCL|O_EXTRA); | |
4287 created2 = TRUE; | |
4288 } | |
4289 if (f2 >= 0) | |
4290 { | |
4291 /* | |
4292 * Both files exist now. If mch_stat() returns the | |
4293 * same device and inode they are the same file. | |
4294 */ | |
4295 if (mch_fstat(f1, &s1) != -1 | |
4296 && mch_fstat(f2, &s2) != -1 | |
4297 && s1.st_dev == s2.st_dev | |
4298 && s1.st_ino == s2.st_ino) | |
4299 same = TRUE; | |
4300 close(f2); | |
4301 if (created2) | |
4302 mch_remove(fname2); | |
4303 } | |
4304 close(f1); | |
4305 if (created1) | |
4306 mch_remove(fname); | |
4307 } | |
4308 vim_free(fname2); | |
4309 if (same) | |
4310 { | |
4311 buf->b_shortname = TRUE; | |
4312 vim_free(fname); | |
5432 | 4313 fname = makeswapname(buf_fname, buf->b_ffname, |
460 | 4314 buf, dir_name); |
7 | 4315 continue; /* try again with b_shortname set */ |
4316 } | |
4317 } | |
4318 } | |
4319 } | |
4320 #endif | |
4321 /* | |
4322 * check if the swapfile already exists | |
4323 */ | |
4324 if (mch_getperm(fname) < 0) /* it does not exist */ | |
4325 { | |
4326 #ifdef HAVE_LSTAT | |
4327 struct stat sb; | |
4328 | |
4329 /* | |
4330 * Extra security check: When a swap file is a symbolic link, this | |
4331 * is most likely a symlink attack. | |
4332 */ | |
4333 if (mch_lstat((char *)fname, &sb) < 0) | |
4334 #else | |
4335 # ifdef AMIGA | |
4336 fh = Open((UBYTE *)fname, (long)MODE_NEWFILE); | |
4337 /* | |
4338 * on the Amiga mch_getperm() will return -1 when the file exists | |
4339 * but is being used by another program. This happens if you edit | |
4340 * a file twice. | |
4341 */ | |
4342 if (fh != (BPTR)NULL) /* can open file, OK */ | |
4343 { | |
4344 Close(fh); | |
4345 mch_remove(fname); | |
4346 break; | |
4347 } | |
4348 if (IoErr() != ERROR_OBJECT_IN_USE | |
4349 && IoErr() != ERROR_OBJECT_EXISTS) | |
4350 # endif | |
4351 #endif | |
4352 break; | |
4353 } | |
4354 | |
4355 /* | |
4356 * A file name equal to old_fname is OK to use. | |
4357 */ | |
4358 if (old_fname != NULL && fnamecmp(fname, old_fname) == 0) | |
4359 break; | |
4360 | |
4361 /* | |
4362 * get here when file already exists | |
4363 */ | |
4364 if (fname[n - 2] == 'w' && fname[n - 1] == 'p') /* first try */ | |
4365 { | |
4366 #ifndef SHORT_FNAME | |
4367 /* | |
4368 * on MS-DOS compatible filesystems (e.g. messydos) file.doc.swp | |
4369 * and file.doc are the same file. To guess if this problem is | |
4370 * present try if file.doc.swx exists. If it does, we set | |
4371 * buf->b_shortname and try file_doc.swp (dots replaced by | |
4372 * underscores for this file), and try again. If it doesn't we | |
4373 * assume that "file.doc.swp" already exists. | |
4374 */ | |
4375 if (!(buf->b_p_sn || buf->b_shortname)) /* not tried yet */ | |
4376 { | |
4377 fname[n - 1] = 'x'; | |
4378 r = mch_getperm(fname); /* try "file.swx" */ | |
4379 fname[n - 1] = 'p'; | |
4380 if (r >= 0) /* "file.swx" seems to exist */ | |
4381 { | |
4382 buf->b_shortname = TRUE; | |
4383 vim_free(fname); | |
5432 | 4384 fname = makeswapname(buf_fname, buf->b_ffname, |
460 | 4385 buf, dir_name); |
7 | 4386 continue; /* try again with '.' replaced with '_' */ |
4387 } | |
4388 } | |
4389 #endif | |
4390 /* | |
4391 * If we get here the ".swp" file really exists. | |
4392 * Give an error message, unless recovering, no file name, we are | |
4393 * viewing a help file or when the path of the file is different | |
4394 * (happens when all .swp files are in one directory). | |
4395 */ | |
5432 | 4396 if (!recoverymode && buf_fname != NULL |
43 | 4397 && !buf->b_help && !(buf->b_flags & BF_DUMMY)) |
7 | 4398 { |
4399 int fd; | |
4400 struct block0 b0; | |
4401 int differ = FALSE; | |
4402 | |
4403 /* | |
4404 * Try to read block 0 from the swap file to get the original | |
4405 * file name (and inode number). | |
4406 */ | |
4407 fd = mch_open((char *)fname, O_RDONLY | O_EXTRA, 0); | |
4408 if (fd >= 0) | |
4409 { | |
2664 | 4410 if (read_eintr(fd, &b0, sizeof(b0)) == sizeof(b0)) |
7 | 4411 { |
4412 /* | |
39 | 4413 * If the swapfile has the same directory as the |
4414 * buffer don't compare the directory names, they can | |
4415 * have a different mountpoint. | |
7 | 4416 */ |
39 | 4417 if (b0.b0_flags & B0_SAME_DIR) |
4418 { | |
4419 if (fnamecmp(gettail(buf->b_ffname), | |
4420 gettail(b0.b0_fname)) != 0 | |
4421 || !same_directory(fname, buf->b_ffname)) | |
594 | 4422 { |
4423 #ifdef CHECK_INODE | |
4424 /* Symlinks may point to the same file even | |
4425 * when the name differs, need to check the | |
4426 * inode too. */ | |
4427 expand_env(b0.b0_fname, NameBuff, MAXPATHL); | |
4428 if (fnamecmp_ino(buf->b_ffname, NameBuff, | |
4429 char_to_long(b0.b0_ino))) | |
4430 #endif | |
4431 differ = TRUE; | |
4432 } | |
39 | 4433 } |
4434 else | |
4435 { | |
4436 /* | |
4437 * The name in the swap file may be | |
4438 * "~user/path/file". Expand it first. | |
4439 */ | |
4440 expand_env(b0.b0_fname, NameBuff, MAXPATHL); | |
7 | 4441 #ifdef CHECK_INODE |
39 | 4442 if (fnamecmp_ino(buf->b_ffname, NameBuff, |
594 | 4443 char_to_long(b0.b0_ino))) |
39 | 4444 differ = TRUE; |
7 | 4445 #else |
39 | 4446 if (fnamecmp(NameBuff, buf->b_ffname) != 0) |
4447 differ = TRUE; | |
7 | 4448 #endif |
39 | 4449 } |
7 | 4450 } |
4451 close(fd); | |
4452 } | |
4453 | |
4454 /* give the ATTENTION message when there is an old swap file | |
4455 * for the current file, and the buffer was not recovered. */ | |
4456 if (differ == FALSE && !(curbuf->b_flags & BF_RECOVERED) | |
4457 && vim_strchr(p_shm, SHM_ATTENTION) == NULL) | |
4458 { | |
580 | 4459 #if defined(HAS_SWAP_EXISTS_ACTION) |
4460 int choice = 0; | |
4461 #endif | |
7 | 4462 #ifdef CREATE_DUMMY_FILE |
4463 int did_use_dummy = FALSE; | |
4464 | |
4465 /* Avoid getting a warning for the file being created | |
4466 * outside of Vim, it was created at the start of this | |
4467 * function. Delete the file now, because Vim might exit | |
4468 * here if the window is closed. */ | |
4469 if (dummyfd != NULL) | |
4470 { | |
4471 fclose(dummyfd); | |
4472 dummyfd = NULL; | |
5432 | 4473 mch_remove(buf_fname); |
7 | 4474 did_use_dummy = TRUE; |
4475 } | |
4476 #endif | |
4477 | |
4478 #if (defined(UNIX) || defined(__EMX__) || defined(VMS)) && (defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)) | |
4479 process_still_running = FALSE; | |
4480 #endif | |
580 | 4481 #ifdef FEAT_AUTOCMD |
4482 /* | |
4483 * If there is an SwapExists autocommand and we can handle | |
4484 * the response, trigger it. It may return 0 to ask the | |
4485 * user anyway. | |
4486 */ | |
4487 if (swap_exists_action != SEA_NONE | |
5432 | 4488 && has_autocmd(EVENT_SWAPEXISTS, buf_fname, buf)) |
580 | 4489 choice = do_swapexists(buf, fname); |
4490 | |
4491 if (choice == 0) | |
4492 #endif | |
7 | 4493 { |
580 | 4494 #ifdef FEAT_GUI |
4495 /* If we are supposed to start the GUI but it wasn't | |
4496 * completely started yet, start it now. This makes | |
4497 * the messages displayed in the Vim window when | |
4498 * loading a session from the .gvimrc file. */ | |
4499 if (gui.starting && !gui.in_use) | |
4500 gui_start(); | |
4501 #endif | |
4502 /* Show info about the existing swap file. */ | |
4503 attention_message(buf, fname); | |
4504 | |
4505 /* We don't want a 'q' typed at the more-prompt | |
4506 * interrupt loading a file. */ | |
4507 got_int = FALSE; | |
7 | 4508 } |
4509 | |
4510 #if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) | |
580 | 4511 if (swap_exists_action != SEA_NONE && choice == 0) |
7 | 4512 { |
4513 char_u *name; | |
4514 | |
4515 name = alloc((unsigned)(STRLEN(fname) | |
4516 + STRLEN(_("Swap file \"")) | |
4517 + STRLEN(_("\" already exists!")) + 5)); | |
4518 if (name != NULL) | |
4519 { | |
4520 STRCPY(name, _("Swap file \"")); | |
4521 home_replace(NULL, fname, name + STRLEN(name), | |
4522 1000, TRUE); | |
4523 STRCAT(name, _("\" already exists!")); | |
4524 } | |
580 | 4525 choice = do_dialog(VIM_WARNING, |
7 | 4526 (char_u *)_("VIM - ATTENTION"), |
4527 name == NULL | |
4528 ? (char_u *)_("Swap file already exists!") | |
4529 : name, | |
4530 # if defined(UNIX) || defined(__EMX__) || defined(VMS) | |
4531 process_still_running | |
4532 ? (char_u *)_("&Open Read-Only\n&Edit anyway\n&Recover\n&Quit\n&Abort") : | |
4533 # endif | |
2684 | 4534 (char_u *)_("&Open Read-Only\n&Edit anyway\n&Recover\n&Delete it\n&Quit\n&Abort"), 1, NULL, FALSE); |
580 | 4535 |
4536 # if defined(UNIX) || defined(__EMX__) || defined(VMS) | |
4537 if (process_still_running && choice >= 4) | |
4538 choice++; /* Skip missing "Delete it" button */ | |
4539 # endif | |
4540 vim_free(name); | |
4541 | |
4542 /* pretend screen didn't scroll, need redraw anyway */ | |
4543 msg_scrolled = 0; | |
4544 redraw_all_later(NOT_VALID); | |
4545 } | |
4546 #endif | |
4547 | |
4548 #if defined(HAS_SWAP_EXISTS_ACTION) | |
4549 if (choice > 0) | |
4550 { | |
4551 switch (choice) | |
7 | 4552 { |
4553 case 1: | |
4554 buf->b_p_ro = TRUE; | |
4555 break; | |
4556 case 2: | |
4557 break; | |
4558 case 3: | |
4559 swap_exists_action = SEA_RECOVER; | |
4560 break; | |
4561 case 4: | |
580 | 4562 mch_remove(fname); |
4563 break; | |
4564 case 5: | |
7 | 4565 swap_exists_action = SEA_QUIT; |
4566 break; | |
580 | 4567 case 6: |
7 | 4568 swap_exists_action = SEA_QUIT; |
4569 got_int = TRUE; | |
4570 break; | |
4571 } | |
4572 | |
4573 /* If the file was deleted this fname can be used. */ | |
4574 if (mch_getperm(fname) < 0) | |
4575 break; | |
4576 } | |
4577 else | |
4578 #endif | |
4579 { | |
4580 MSG_PUTS("\n"); | |
625 | 4581 if (msg_silent == 0) |
4582 /* call wait_return() later */ | |
4583 need_wait_return = TRUE; | |
7 | 4584 } |
4585 | |
4586 #ifdef CREATE_DUMMY_FILE | |
4587 /* Going to try another name, need the dummy file again. */ | |
4588 if (did_use_dummy) | |
5432 | 4589 dummyfd = mch_fopen((char *)buf_fname, "w"); |
7 | 4590 #endif |
4591 } | |
4592 } | |
4593 } | |
4594 | |
4595 /* | |
4596 * Change the ".swp" extension to find another file that can be used. | |
4597 * First decrement the last char: ".swo", ".swn", etc. | |
4598 * If that still isn't enough decrement the last but one char: ".svz" | |
9 | 4599 * Can happen when editing many "No Name" buffers. |
7 | 4600 */ |
4601 if (fname[n - 1] == 'a') /* ".s?a" */ | |
4602 { | |
4603 if (fname[n - 2] == 'a') /* ".saa": tried enough, give up */ | |
4604 { | |
4605 EMSG(_("E326: Too many swap files found")); | |
4606 vim_free(fname); | |
4607 fname = NULL; | |
4608 break; | |
4609 } | |
4610 --fname[n - 2]; /* ".svz", ".suz", etc. */ | |
4611 fname[n - 1] = 'z' + 1; | |
4612 } | |
4613 --fname[n - 1]; /* ".swo", ".swn", etc. */ | |
4614 } | |
4615 | |
4616 vim_free(dir_name); | |
4617 #ifdef CREATE_DUMMY_FILE | |
4618 if (dummyfd != NULL) /* file has been created temporarily */ | |
4619 { | |
4620 fclose(dummyfd); | |
5432 | 4621 mch_remove(buf_fname); |
7 | 4622 } |
4623 #endif | |
5432 | 4624 #ifdef WIN3264 |
4625 if (buf_fname != buf->b_fname) | |
4626 vim_free(buf_fname); | |
4627 #endif | |
7 | 4628 return fname; |
4629 } | |
4630 | |
4631 static int | |
4632 b0_magic_wrong(b0p) | |
4633 ZERO_BL *b0p; | |
4634 { | |
4635 return (b0p->b0_magic_long != (long)B0_MAGIC_LONG | |
4636 || b0p->b0_magic_int != (int)B0_MAGIC_INT | |
4637 || b0p->b0_magic_short != (short)B0_MAGIC_SHORT | |
4638 || b0p->b0_magic_char != B0_MAGIC_CHAR); | |
4639 } | |
4640 | |
4641 #ifdef CHECK_INODE | |
4642 /* | |
4643 * Compare current file name with file name from swap file. | |
4644 * Try to use inode numbers when possible. | |
4645 * Return non-zero when files are different. | |
4646 * | |
4647 * When comparing file names a few things have to be taken into consideration: | |
4648 * - When working over a network the full path of a file depends on the host. | |
4649 * We check the inode number if possible. It is not 100% reliable though, | |
4650 * because the device number cannot be used over a network. | |
4651 * - When a file does not exist yet (editing a new file) there is no inode | |
4652 * number. | |
4653 * - The file name in a swap file may not be valid on the current host. The | |
4654 * "~user" form is used whenever possible to avoid this. | |
4655 * | |
4656 * This is getting complicated, let's make a table: | |
4657 * | |
4658 * ino_c ino_s fname_c fname_s differ = | |
4659 * | |
4660 * both files exist -> compare inode numbers: | |
4661 * != 0 != 0 X X ino_c != ino_s | |
4662 * | |
4663 * inode number(s) unknown, file names available -> compare file names | |
4664 * == 0 X OK OK fname_c != fname_s | |
4665 * X == 0 OK OK fname_c != fname_s | |
4666 * | |
4667 * current file doesn't exist, file for swap file exist, file name(s) not | |
4668 * available -> probably different | |
4669 * == 0 != 0 FAIL X TRUE | |
4670 * == 0 != 0 X FAIL TRUE | |
4671 * | |
4672 * current file exists, inode for swap unknown, file name(s) not | |
4673 * available -> probably different | |
4674 * != 0 == 0 FAIL X TRUE | |
4675 * != 0 == 0 X FAIL TRUE | |
4676 * | |
4677 * current file doesn't exist, inode for swap unknown, one file name not | |
4678 * available -> probably different | |
4679 * == 0 == 0 FAIL OK TRUE | |
4680 * == 0 == 0 OK FAIL TRUE | |
4681 * | |
4682 * current file doesn't exist, inode for swap unknown, both file names not | |
4683 * available -> probably same file | |
4684 * == 0 == 0 FAIL FAIL FALSE | |
4685 * | |
4686 * Note that when the ino_t is 64 bits, only the last 32 will be used. This | |
4687 * can't be changed without making the block 0 incompatible with 32 bit | |
4688 * versions. | |
4689 */ | |
4690 | |
4691 static int | |
4692 fnamecmp_ino(fname_c, fname_s, ino_block0) | |
4693 char_u *fname_c; /* current file name */ | |
4694 char_u *fname_s; /* file name from swap file */ | |
4695 long ino_block0; | |
4696 { | |
4697 struct stat st; | |
4698 ino_t ino_c = 0; /* ino of current file */ | |
4699 ino_t ino_s; /* ino of file from swap file */ | |
4700 char_u buf_c[MAXPATHL]; /* full path of fname_c */ | |
4701 char_u buf_s[MAXPATHL]; /* full path of fname_s */ | |
4702 int retval_c; /* flag: buf_c valid */ | |
4703 int retval_s; /* flag: buf_s valid */ | |
4704 | |
4705 if (mch_stat((char *)fname_c, &st) == 0) | |
4706 ino_c = (ino_t)st.st_ino; | |
4707 | |
4708 /* | |
4709 * First we try to get the inode from the file name, because the inode in | |
4710 * the swap file may be outdated. If that fails (e.g. this path is not | |
4711 * valid on this machine), use the inode from block 0. | |
4712 */ | |
4713 if (mch_stat((char *)fname_s, &st) == 0) | |
4714 ino_s = (ino_t)st.st_ino; | |
4715 else | |
4716 ino_s = (ino_t)ino_block0; | |
4717 | |
4718 if (ino_c && ino_s) | |
4719 return (ino_c != ino_s); | |
4720 | |
4721 /* | |
4722 * One of the inode numbers is unknown, try a forced vim_FullName() and | |
4723 * compare the file names. | |
4724 */ | |
4725 retval_c = vim_FullName(fname_c, buf_c, MAXPATHL, TRUE); | |
4726 retval_s = vim_FullName(fname_s, buf_s, MAXPATHL, TRUE); | |
4727 if (retval_c == OK && retval_s == OK) | |
4728 return (STRCMP(buf_c, buf_s) != 0); | |
4729 | |
4730 /* | |
4731 * Can't compare inodes or file names, guess that the files are different, | |
4732 * unless both appear not to exist at all. | |
4733 */ | |
4734 if (ino_s == 0 && ino_c == 0 && retval_c == FAIL && retval_s == FAIL) | |
4735 return FALSE; | |
4736 return TRUE; | |
4737 } | |
4738 #endif /* CHECK_INODE */ | |
4739 | |
4740 /* | |
4741 * Move a long integer into a four byte character array. | |
4742 * Used for machine independency in block zero. | |
4743 */ | |
4744 static void | |
4745 long_to_char(n, s) | |
4746 long n; | |
4747 char_u *s; | |
4748 { | |
4749 s[0] = (char_u)(n & 0xff); | |
4750 n = (unsigned)n >> 8; | |
4751 s[1] = (char_u)(n & 0xff); | |
4752 n = (unsigned)n >> 8; | |
4753 s[2] = (char_u)(n & 0xff); | |
4754 n = (unsigned)n >> 8; | |
4755 s[3] = (char_u)(n & 0xff); | |
4756 } | |
4757 | |
4758 static long | |
4759 char_to_long(s) | |
4760 char_u *s; | |
4761 { | |
4762 long retval; | |
4763 | |
4764 retval = s[3]; | |
4765 retval <<= 8; | |
4766 retval |= s[2]; | |
4767 retval <<= 8; | |
4768 retval |= s[1]; | |
4769 retval <<= 8; | |
4770 retval |= s[0]; | |
4771 | |
4772 return retval; | |
4773 } | |
4774 | |
39 | 4775 /* |
4776 * Set the flags in the first block of the swap file: | |
4777 * - file is modified or not: buf->b_changed | |
4778 * - 'fileformat' | |
4779 * - 'fileencoding' | |
4780 */ | |
7 | 4781 void |
39 | 4782 ml_setflags(buf) |
7 | 4783 buf_T *buf; |
4784 { | |
4785 bhdr_T *hp; | |
4786 ZERO_BL *b0p; | |
4787 | |
4788 if (!buf->b_ml.ml_mfp) | |
4789 return; | |
4790 for (hp = buf->b_ml.ml_mfp->mf_used_last; hp != NULL; hp = hp->bh_prev) | |
4791 { | |
4792 if (hp->bh_bnum == 0) | |
4793 { | |
4794 b0p = (ZERO_BL *)(hp->bh_data); | |
39 | 4795 b0p->b0_dirty = buf->b_changed ? B0_DIRTY : 0; |
4796 b0p->b0_flags = (b0p->b0_flags & ~B0_FF_MASK) | |
4797 | (get_fileformat(buf) + 1); | |
4798 #ifdef FEAT_MBYTE | |
4799 add_b0_fenc(b0p, buf); | |
4800 #endif | |
7 | 4801 hp->bh_flags |= BH_DIRTY; |
4802 mf_sync(buf->b_ml.ml_mfp, MFS_ZERO); | |
4803 break; | |
4804 } | |
4805 } | |
4806 } | |
4807 | |
2267 | 4808 #if defined(FEAT_CRYPT) || defined(PROTO) |
4809 /* | |
4810 * If "data" points to a data block encrypt the text in it and return a copy | |
4811 * in allocated memory. Return NULL when out of memory. | |
4812 * Otherwise return "data". | |
4813 */ | |
4814 char_u * | |
4815 ml_encrypt_data(mfp, data, offset, size) | |
4816 memfile_T *mfp; | |
4817 char_u *data; | |
4818 off_t offset; | |
4819 unsigned size; | |
4820 { | |
4821 DATA_BL *dp = (DATA_BL *)data; | |
4822 char_u *head_end; | |
4823 char_u *text_start; | |
4824 char_u *new_data; | |
4825 int text_len; | |
6122 | 4826 cryptstate_T *state; |
2267 | 4827 |
4828 if (dp->db_id != DATA_ID) | |
4829 return data; | |
4830 | |
4831 new_data = (char_u *)alloc(size); | |
4832 if (new_data == NULL) | |
4833 return NULL; | |
4834 head_end = (char_u *)(&dp->db_index[dp->db_line_count]); | |
4835 text_start = (char_u *)dp + dp->db_txt_start; | |
4836 text_len = size - dp->db_txt_start; | |
4837 | |
4838 /* Copy the header and the text. */ | |
4839 mch_memmove(new_data, dp, head_end - (char_u *)dp); | |
4840 | |
4841 /* Encrypt the text. */ | |
6122 | 4842 state = ml_crypt_prepare(mfp, offset, FALSE); |
4843 crypt_encode(state, text_start, text_len, new_data + dp->db_txt_start); | |
4844 crypt_free_state(state); | |
2267 | 4845 |
4846 /* Clear the gap. */ | |
4847 if (head_end < text_start) | |
4848 vim_memset(new_data + (head_end - data), 0, text_start - head_end); | |
4849 | |
4850 return new_data; | |
4851 } | |
4852 | |
4853 /* | |
4854 * Decrypt the text in "data" if it points to a data block. | |
4855 */ | |
4856 void | |
4857 ml_decrypt_data(mfp, data, offset, size) | |
4858 memfile_T *mfp; | |
4859 char_u *data; | |
4860 off_t offset; | |
4861 unsigned size; | |
4862 { | |
4863 DATA_BL *dp = (DATA_BL *)data; | |
4864 char_u *head_end; | |
4865 char_u *text_start; | |
4866 int text_len; | |
6122 | 4867 cryptstate_T *state; |
2267 | 4868 |
4869 if (dp->db_id == DATA_ID) | |
4870 { | |
4871 head_end = (char_u *)(&dp->db_index[dp->db_line_count]); | |
4872 text_start = (char_u *)dp + dp->db_txt_start; | |
4873 text_len = dp->db_txt_end - dp->db_txt_start; | |
4874 | |
4875 if (head_end > text_start || dp->db_txt_start > size | |
4876 || dp->db_txt_end > size) | |
4877 return; /* data was messed up */ | |
4878 | |
4879 /* Decrypt the text in place. */ | |
6122 | 4880 state = ml_crypt_prepare(mfp, offset, TRUE); |
4881 crypt_decode_inplace(state, text_start, text_len); | |
4882 crypt_free_state(state); | |
2267 | 4883 } |
4884 } | |
4885 | |
4886 /* | |
4887 * Prepare for encryption/decryption, using the key, seed and offset. | |
6122 | 4888 * Return an allocated cryptstate_T *. |
2267 | 4889 */ |
6122 | 4890 static cryptstate_T * |
2267 | 4891 ml_crypt_prepare(mfp, offset, reading) |
4892 memfile_T *mfp; | |
4893 off_t offset; | |
4894 int reading; | |
4895 { | |
4896 buf_T *buf = mfp->mf_buffer; | |
4897 char_u salt[50]; | |
6122 | 4898 int method_nr; |
2267 | 4899 char_u *key; |
4900 char_u *seed; | |
4901 | |
4902 if (reading && mfp->mf_old_key != NULL) | |
4903 { | |
4904 /* Reading back blocks with the previous key/method/seed. */ | |
6122 | 4905 method_nr = mfp->mf_old_cm; |
2267 | 4906 key = mfp->mf_old_key; |
4907 seed = mfp->mf_old_seed; | |
4908 } | |
4909 else | |
4910 { | |
6122 | 4911 method_nr = crypt_get_method_nr(buf); |
2267 | 4912 key = buf->b_p_key; |
4913 seed = mfp->mf_seed; | |
4914 } | |
4915 | |
6122 | 4916 if (method_nr == CRYPT_M_ZIP) |
2267 | 4917 { |
6122 | 4918 /* For PKzip: Append the offset to the key, so that we use a different |
4919 * key for every block. */ | |
2267 | 4920 vim_snprintf((char *)salt, sizeof(salt), "%s%ld", key, (long)offset); |
6122 | 4921 return crypt_create(method_nr, salt, NULL, 0, NULL, 0); |
2267 | 4922 } |
6122 | 4923 |
4924 /* Using blowfish or better: add salt and seed. We use the byte offset | |
4925 * of the block for the salt. */ | |
4926 vim_snprintf((char *)salt, sizeof(salt), "%ld", (long)offset); | |
4927 return crypt_create(method_nr, key, salt, (int)STRLEN(salt), | |
4928 seed, MF_SEED_LEN); | |
2267 | 4929 } |
4930 | |
4931 #endif | |
4932 | |
4933 | |
7 | 4934 #if defined(FEAT_BYTEOFF) || defined(PROTO) |
4935 | |
4936 #define MLCS_MAXL 800 /* max no of lines in chunk */ | |
4937 #define MLCS_MINL 400 /* should be half of MLCS_MAXL */ | |
4938 | |
4939 /* | |
2407
6bc102a4bff8
Fix memory access to 'cryptmethod' during recovery. (Dominique Pelle)
Bram Moolenaar <bram@vim.org>
parents:
2394
diff
changeset
|
4940 * Keep information for finding byte offset of a line, updtype may be one of: |
7 | 4941 * ML_CHNK_ADDLINE: Add len to parent chunk, possibly splitting it |
4942 * Careful: ML_CHNK_ADDLINE may cause ml_find_line() to be called. | |
4943 * ML_CHNK_DELLINE: Subtract len from parent chunk, possibly deleting it | |
4944 * ML_CHNK_UPDLINE: Add len to parent chunk, as a signed entity. | |
4945 */ | |
4946 static void | |
4947 ml_updatechunk(buf, line, len, updtype) | |
4948 buf_T *buf; | |
4949 linenr_T line; | |
4950 long len; | |
4951 int updtype; | |
4952 { | |
4953 static buf_T *ml_upd_lastbuf = NULL; | |
4954 static linenr_T ml_upd_lastline; | |
4955 static linenr_T ml_upd_lastcurline; | |
4956 static int ml_upd_lastcurix; | |
4957 | |
4958 linenr_T curline = ml_upd_lastcurline; | |
4959 int curix = ml_upd_lastcurix; | |
4960 long size; | |
4961 chunksize_T *curchnk; | |
4962 int rest; | |
4963 bhdr_T *hp; | |
4964 DATA_BL *dp; | |
4965 | |
4966 if (buf->b_ml.ml_usedchunks == -1 || len == 0) | |
4967 return; | |
4968 if (buf->b_ml.ml_chunksize == NULL) | |
4969 { | |
4970 buf->b_ml.ml_chunksize = (chunksize_T *) | |
4971 alloc((unsigned)sizeof(chunksize_T) * 100); | |
4972 if (buf->b_ml.ml_chunksize == NULL) | |
4973 { | |
4974 buf->b_ml.ml_usedchunks = -1; | |
4975 return; | |
4976 } | |
4977 buf->b_ml.ml_numchunks = 100; | |
4978 buf->b_ml.ml_usedchunks = 1; | |
4979 buf->b_ml.ml_chunksize[0].mlcs_numlines = 1; | |
4980 buf->b_ml.ml_chunksize[0].mlcs_totalsize = 1; | |
4981 } | |
4982 | |
4983 if (updtype == ML_CHNK_UPDLINE && buf->b_ml.ml_line_count == 1) | |
4984 { | |
4985 /* | |
4986 * First line in empty buffer from ml_flush_line() -- reset | |
4987 */ | |
4988 buf->b_ml.ml_usedchunks = 1; | |
4989 buf->b_ml.ml_chunksize[0].mlcs_numlines = 1; | |
4990 buf->b_ml.ml_chunksize[0].mlcs_totalsize = | |
4991 (long)STRLEN(buf->b_ml.ml_line_ptr) + 1; | |
4992 return; | |
4993 } | |
4994 | |
4995 /* | |
4996 * Find chunk that our line belongs to, curline will be at start of the | |
4997 * chunk. | |
4998 */ | |
4999 if (buf != ml_upd_lastbuf || line != ml_upd_lastline + 1 | |
5000 || updtype != ML_CHNK_ADDLINE) | |
5001 { | |
5002 for (curline = 1, curix = 0; | |
5003 curix < buf->b_ml.ml_usedchunks - 1 | |
5004 && line >= curline + buf->b_ml.ml_chunksize[curix].mlcs_numlines; | |
5005 curix++) | |
5006 { | |
5007 curline += buf->b_ml.ml_chunksize[curix].mlcs_numlines; | |
5008 } | |
5009 } | |
5010 else if (line >= curline + buf->b_ml.ml_chunksize[curix].mlcs_numlines | |
5011 && curix < buf->b_ml.ml_usedchunks - 1) | |
5012 { | |
5013 /* Adjust cached curix & curline */ | |
5014 curline += buf->b_ml.ml_chunksize[curix].mlcs_numlines; | |
5015 curix++; | |
5016 } | |
5017 curchnk = buf->b_ml.ml_chunksize + curix; | |
5018 | |
5019 if (updtype == ML_CHNK_DELLINE) | |
1030 | 5020 len = -len; |
7 | 5021 curchnk->mlcs_totalsize += len; |
5022 if (updtype == ML_CHNK_ADDLINE) | |
5023 { | |
5024 curchnk->mlcs_numlines++; | |
5025 | |
5026 /* May resize here so we don't have to do it in both cases below */ | |
5027 if (buf->b_ml.ml_usedchunks + 1 >= buf->b_ml.ml_numchunks) | |
5028 { | |
5029 buf->b_ml.ml_numchunks = buf->b_ml.ml_numchunks * 3 / 2; | |
5030 buf->b_ml.ml_chunksize = (chunksize_T *) | |
5031 vim_realloc(buf->b_ml.ml_chunksize, | |
5032 sizeof(chunksize_T) * buf->b_ml.ml_numchunks); | |
5033 if (buf->b_ml.ml_chunksize == NULL) | |
5034 { | |
5035 /* Hmmmm, Give up on offset for this buffer */ | |
5036 buf->b_ml.ml_usedchunks = -1; | |
5037 return; | |
5038 } | |
5039 } | |
5040 | |
5041 if (buf->b_ml.ml_chunksize[curix].mlcs_numlines >= MLCS_MAXL) | |
5042 { | |
5043 int count; /* number of entries in block */ | |
5044 int idx; | |
5045 int text_end; | |
5046 int linecnt; | |
5047 | |
5048 mch_memmove(buf->b_ml.ml_chunksize + curix + 1, | |
5049 buf->b_ml.ml_chunksize + curix, | |
5050 (buf->b_ml.ml_usedchunks - curix) * | |
5051 sizeof(chunksize_T)); | |
1855 | 5052 /* Compute length of first half of lines in the split chunk */ |
7 | 5053 size = 0; |
5054 linecnt = 0; | |
5055 while (curline < buf->b_ml.ml_line_count | |
5056 && linecnt < MLCS_MINL) | |
5057 { | |
5058 if ((hp = ml_find_line(buf, curline, ML_FIND)) == NULL) | |
5059 { | |
5060 buf->b_ml.ml_usedchunks = -1; | |
5061 return; | |
5062 } | |
5063 dp = (DATA_BL *)(hp->bh_data); | |
5064 count = (long)(buf->b_ml.ml_locked_high) - | |
5065 (long)(buf->b_ml.ml_locked_low) + 1; | |
5066 idx = curline - buf->b_ml.ml_locked_low; | |
5067 curline = buf->b_ml.ml_locked_high + 1; | |
5068 if (idx == 0)/* first line in block, text at the end */ | |
5069 text_end = dp->db_txt_end; | |
5070 else | |
5071 text_end = ((dp->db_index[idx - 1]) & DB_INDEX_MASK); | |
5072 /* Compute index of last line to use in this MEMLINE */ | |
5073 rest = count - idx; | |
5074 if (linecnt + rest > MLCS_MINL) | |
5075 { | |
5076 idx += MLCS_MINL - linecnt - 1; | |
5077 linecnt = MLCS_MINL; | |
5078 } | |
5079 else | |
5080 { | |
5081 idx = count - 1; | |
5082 linecnt += rest; | |
5083 } | |
5084 size += text_end - ((dp->db_index[idx]) & DB_INDEX_MASK); | |
5085 } | |
5086 buf->b_ml.ml_chunksize[curix].mlcs_numlines = linecnt; | |
5087 buf->b_ml.ml_chunksize[curix + 1].mlcs_numlines -= linecnt; | |
5088 buf->b_ml.ml_chunksize[curix].mlcs_totalsize = size; | |
5089 buf->b_ml.ml_chunksize[curix + 1].mlcs_totalsize -= size; | |
5090 buf->b_ml.ml_usedchunks++; | |
5091 ml_upd_lastbuf = NULL; /* Force recalc of curix & curline */ | |
5092 return; | |
5093 } | |
5094 else if (buf->b_ml.ml_chunksize[curix].mlcs_numlines >= MLCS_MINL | |
5095 && curix == buf->b_ml.ml_usedchunks - 1 | |
5096 && buf->b_ml.ml_line_count - line <= 1) | |
5097 { | |
5098 /* | |
5099 * We are in the last chunk and it is cheap to crate a new one | |
5100 * after this. Do it now to avoid the loop above later on | |
5101 */ | |
5102 curchnk = buf->b_ml.ml_chunksize + curix + 1; | |
5103 buf->b_ml.ml_usedchunks++; | |
5104 if (line == buf->b_ml.ml_line_count) | |
5105 { | |
5106 curchnk->mlcs_numlines = 0; | |
5107 curchnk->mlcs_totalsize = 0; | |
5108 } | |
5109 else | |
5110 { | |
5111 /* | |
5112 * Line is just prior to last, move count for last | |
5113 * This is the common case when loading a new file | |
5114 */ | |
5115 hp = ml_find_line(buf, buf->b_ml.ml_line_count, ML_FIND); | |
5116 if (hp == NULL) | |
5117 { | |
5118 buf->b_ml.ml_usedchunks = -1; | |
5119 return; | |
5120 } | |
5121 dp = (DATA_BL *)(hp->bh_data); | |
5122 if (dp->db_line_count == 1) | |
5123 rest = dp->db_txt_end - dp->db_txt_start; | |
5124 else | |
5125 rest = | |
5126 ((dp->db_index[dp->db_line_count - 2]) & DB_INDEX_MASK) | |
5127 - dp->db_txt_start; | |
5128 curchnk->mlcs_totalsize = rest; | |
5129 curchnk->mlcs_numlines = 1; | |
5130 curchnk[-1].mlcs_totalsize -= rest; | |
5131 curchnk[-1].mlcs_numlines -= 1; | |
5132 } | |
5133 } | |
5134 } | |
5135 else if (updtype == ML_CHNK_DELLINE) | |
5136 { | |
5137 curchnk->mlcs_numlines--; | |
5138 ml_upd_lastbuf = NULL; /* Force recalc of curix & curline */ | |
5139 if (curix < (buf->b_ml.ml_usedchunks - 1) | |
5140 && (curchnk->mlcs_numlines + curchnk[1].mlcs_numlines) | |
5141 <= MLCS_MINL) | |
5142 { | |
5143 curix++; | |
5144 curchnk = buf->b_ml.ml_chunksize + curix; | |
5145 } | |
5146 else if (curix == 0 && curchnk->mlcs_numlines <= 0) | |
5147 { | |
5148 buf->b_ml.ml_usedchunks--; | |
5149 mch_memmove(buf->b_ml.ml_chunksize, buf->b_ml.ml_chunksize + 1, | |
5150 buf->b_ml.ml_usedchunks * sizeof(chunksize_T)); | |
5151 return; | |
5152 } | |
5153 else if (curix == 0 || (curchnk->mlcs_numlines > 10 | |
5154 && (curchnk->mlcs_numlines + curchnk[-1].mlcs_numlines) | |
5155 > MLCS_MINL)) | |
5156 { | |
5157 return; | |
5158 } | |
5159 | |
5160 /* Collapse chunks */ | |
5161 curchnk[-1].mlcs_numlines += curchnk->mlcs_numlines; | |
5162 curchnk[-1].mlcs_totalsize += curchnk->mlcs_totalsize; | |
5163 buf->b_ml.ml_usedchunks--; | |
5164 if (curix < buf->b_ml.ml_usedchunks) | |
5165 { | |
5166 mch_memmove(buf->b_ml.ml_chunksize + curix, | |
5167 buf->b_ml.ml_chunksize + curix + 1, | |
5168 (buf->b_ml.ml_usedchunks - curix) * | |
5169 sizeof(chunksize_T)); | |
5170 } | |
5171 return; | |
5172 } | |
5173 ml_upd_lastbuf = buf; | |
5174 ml_upd_lastline = line; | |
5175 ml_upd_lastcurline = curline; | |
5176 ml_upd_lastcurix = curix; | |
5177 } | |
5178 | |
5179 /* | |
5180 * Find offset for line or line with offset. | |
169 | 5181 * Find line with offset if "lnum" is 0; return remaining offset in offp |
5182 * Find offset of line if "lnum" > 0 | |
7 | 5183 * return -1 if information is not available |
5184 */ | |
5185 long | |
169 | 5186 ml_find_line_or_offset(buf, lnum, offp) |
7 | 5187 buf_T *buf; |
169 | 5188 linenr_T lnum; |
7 | 5189 long *offp; |
5190 { | |
5191 linenr_T curline; | |
5192 int curix; | |
5193 long size; | |
5194 bhdr_T *hp; | |
5195 DATA_BL *dp; | |
5196 int count; /* number of entries in block */ | |
5197 int idx; | |
5198 int start_idx; | |
5199 int text_end; | |
5200 long offset; | |
5201 int len; | |
5202 int ffdos = (get_fileformat(buf) == EOL_DOS); | |
5203 int extra = 0; | |
5204 | |
169 | 5205 /* take care of cached line first */ |
5206 ml_flush_line(curbuf); | |
5207 | |
7 | 5208 if (buf->b_ml.ml_usedchunks == -1 |
5209 || buf->b_ml.ml_chunksize == NULL | |
169 | 5210 || lnum < 0) |
7 | 5211 return -1; |
5212 | |
5213 if (offp == NULL) | |
5214 offset = 0; | |
5215 else | |
5216 offset = *offp; | |
169 | 5217 if (lnum == 0 && offset <= 0) |
7 | 5218 return 1; /* Not a "find offset" and offset 0 _must_ be in line 1 */ |
5219 /* | |
5220 * Find the last chunk before the one containing our line. Last chunk is | |
5221 * special because it will never qualify | |
5222 */ | |
5223 curline = 1; | |
5224 curix = size = 0; | |
5225 while (curix < buf->b_ml.ml_usedchunks - 1 | |
169 | 5226 && ((lnum != 0 |
5227 && lnum >= curline + buf->b_ml.ml_chunksize[curix].mlcs_numlines) | |
7 | 5228 || (offset != 0 |
5229 && offset > size + buf->b_ml.ml_chunksize[curix].mlcs_totalsize | |
5230 + ffdos * buf->b_ml.ml_chunksize[curix].mlcs_numlines))) | |
5231 { | |
5232 curline += buf->b_ml.ml_chunksize[curix].mlcs_numlines; | |
5233 size += buf->b_ml.ml_chunksize[curix].mlcs_totalsize; | |
5234 if (offset && ffdos) | |
5235 size += buf->b_ml.ml_chunksize[curix].mlcs_numlines; | |
5236 curix++; | |
5237 } | |
5238 | |
169 | 5239 while ((lnum != 0 && curline < lnum) || (offset != 0 && size < offset)) |
7 | 5240 { |
5241 if (curline > buf->b_ml.ml_line_count | |
5242 || (hp = ml_find_line(buf, curline, ML_FIND)) == NULL) | |
5243 return -1; | |
5244 dp = (DATA_BL *)(hp->bh_data); | |
5245 count = (long)(buf->b_ml.ml_locked_high) - | |
5246 (long)(buf->b_ml.ml_locked_low) + 1; | |
5247 start_idx = idx = curline - buf->b_ml.ml_locked_low; | |
5248 if (idx == 0)/* first line in block, text at the end */ | |
5249 text_end = dp->db_txt_end; | |
5250 else | |
5251 text_end = ((dp->db_index[idx - 1]) & DB_INDEX_MASK); | |
5252 /* Compute index of last line to use in this MEMLINE */ | |
169 | 5253 if (lnum != 0) |
7 | 5254 { |
169 | 5255 if (curline + (count - idx) >= lnum) |
5256 idx += lnum - curline - 1; | |
7 | 5257 else |
5258 idx = count - 1; | |
5259 } | |
5260 else | |
5261 { | |
5262 extra = 0; | |
5263 while (offset >= size | |
5264 + text_end - (int)((dp->db_index[idx]) & DB_INDEX_MASK) | |
5265 + ffdos) | |
5266 { | |
5267 if (ffdos) | |
5268 size++; | |
5269 if (idx == count - 1) | |
5270 { | |
5271 extra = 1; | |
5272 break; | |
5273 } | |
5274 idx++; | |
5275 } | |
5276 } | |
5277 len = text_end - ((dp->db_index[idx]) & DB_INDEX_MASK); | |
5278 size += len; | |
5279 if (offset != 0 && size >= offset) | |
5280 { | |
5281 if (size + ffdos == offset) | |
5282 *offp = 0; | |
5283 else if (idx == start_idx) | |
5284 *offp = offset - size + len; | |
5285 else | |
5286 *offp = offset - size + len | |
5287 - (text_end - ((dp->db_index[idx - 1]) & DB_INDEX_MASK)); | |
5288 curline += idx - start_idx + extra; | |
5289 if (curline > buf->b_ml.ml_line_count) | |
5290 return -1; /* exactly one byte beyond the end */ | |
5291 return curline; | |
5292 } | |
5293 curline = buf->b_ml.ml_locked_high + 1; | |
5294 } | |
5295 | |
169 | 5296 if (lnum != 0) |
20 | 5297 { |
5298 /* Count extra CR characters. */ | |
5299 if (ffdos) | |
169 | 5300 size += lnum - 1; |
20 | 5301 |
5302 /* Don't count the last line break if 'bin' and 'noeol'. */ | |
5303 if (buf->b_p_bin && !buf->b_p_eol) | |
5304 size -= ffdos + 1; | |
5305 } | |
5306 | |
7 | 5307 return size; |
5308 } | |
5309 | |
5310 /* | |
5311 * Goto byte in buffer with offset 'cnt'. | |
5312 */ | |
5313 void | |
5314 goto_byte(cnt) | |
5315 long cnt; | |
5316 { | |
5317 long boff = cnt; | |
5318 linenr_T lnum; | |
5319 | |
5320 ml_flush_line(curbuf); /* cached line may be dirty */ | |
5321 setpcmark(); | |
5322 if (boff) | |
5323 --boff; | |
5324 lnum = ml_find_line_or_offset(curbuf, (linenr_T)0, &boff); | |
5325 if (lnum < 1) /* past the end */ | |
5326 { | |
5327 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count; | |
5328 curwin->w_curswant = MAXCOL; | |
5329 coladvance((colnr_T)MAXCOL); | |
5330 } | |
5331 else | |
5332 { | |
5333 curwin->w_cursor.lnum = lnum; | |
5334 curwin->w_cursor.col = (colnr_T)boff; | |
572 | 5335 # ifdef FEAT_VIRTUALEDIT |
5336 curwin->w_cursor.coladd = 0; | |
5337 # endif | |
7 | 5338 curwin->w_set_curswant = TRUE; |
5339 } | |
5340 check_cursor(); | |
5341 | |
5342 # ifdef FEAT_MBYTE | |
5343 /* Make sure the cursor is on the first byte of a multi-byte char. */ | |
5344 if (has_mbyte) | |
5345 mb_adjust_cursor(); | |
5346 # endif | |
5347 } | |
5348 #endif |