blob: 705bc4bcaaa8b2ce6319d270918d57c80f34abd6 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
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 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011/* #define CHECK(c, s) do { if (c) emsg((s)); } while (0) */
Bram Moolenaar6f470022018-04-10 18:47:20 +020012#define CHECK(c, s) do { /**/ } while (0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013
14/*
15 * memline.c: Contains the functions for appending, deleting and changing the
Bram Moolenaar4770d092006-01-12 23:22:24 +000016 * 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.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021 *
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
Bram Moolenaar071d4272004-06-13 20:20:40 +000045#include "vim.h"
46
Bram Moolenaar071d4272004-06-13 20:20:40 +000047#ifndef UNIX /* it's in os_unix.h for Unix */
48# include <time.h>
49#endif
50
Bram Moolenaar5a6404c2006-11-01 17:12:57 +000051#if defined(SASC) || defined(__amigaos4__)
Bram Moolenaar071d4272004-06-13 20:20:40 +000052# include <proto/dos.h> /* for Open() and Close() */
53#endif
54
55typedef struct block0 ZERO_BL; /* contents of the first block */
56typedef struct pointer_block PTR_BL; /* contents of a pointer block */
57typedef struct data_block DATA_BL; /* contents of a data block */
58typedef struct pointer_entry PTR_EN; /* block/line-count pair */
59
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +020060#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 */
Bram Moolenaar8f4ac012014-08-10 13:38:34 +020066#define BLOCK0_ID1_C2 'd' /* block 0 id 1 'cm' 2 */
67
68#if defined(FEAT_CRYPT)
69static 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
Bram Moolenaar071d4272004-06-13 20:20:40 +000075
76/*
77 * pointer to a block, used in a pointer block
78 */
79struct 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 */
90struct pointer_block
91{
92 short_u pb_id; /* ID for pointer block: PTR_ID */
Bram Moolenaar20a825a2010-05-31 21:27:30 +020093 short_u pb_count; /* number of pointers in this block */
Bram Moolenaar071d4272004-06-13 20:20:40 +000094 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 */
106struct 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
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000133#define B0_FNAME_SIZE_ORG 900 /* what it was in older versions */
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +0200134#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 */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000136#define B0_UNAME_SIZE 40
137#define B0_HNAME_SIZE 40
Bram Moolenaar071d4272004-06-13 20:20:40 +0000138/*
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 *
Bram Moolenaarbae0c162007-05-10 19:30:25 +0000156 * This block is built up of single bytes, to make it portable across
Bram Moolenaar071d4272004-06-13 20:20:40 +0000157 * 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 */
160struct block0
161{
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +0200162 char_u b0_id[2]; /* id for block 0: BLOCK0_ID0 and BLOCK0_ID1,
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200163 * BLOCK0_ID1_C0, BLOCK0_ID1_C1, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000164 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) */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000171 char_u b0_fname[B0_FNAME_SIZE_ORG]; /* name of file being edited */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000172 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};
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000177
178/*
Bram Moolenaar4770d092006-01-12 23:22:24 +0000179 * Note: b0_dirty and b0_flags are put at the end of the file name. For very
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000180 * 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
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +0200185#define b0_dirty b0_fname[B0_FNAME_SIZE_ORG - 1]
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000186
187/*
188 * The b0_flags field is new in Vim 7.0.
189 */
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +0200190#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]
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000197
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
Bram Moolenaar071d4272004-06-13 20:20:40 +0000210
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 */
219static 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
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +0200230/* argument for ml_upd_block0() */
231typedef 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
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100238static void ml_set_b0_crypt(buf_T *buf, ZERO_BL *b0p);
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +0200239#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100240static void ml_upd_block0(buf_T *buf, upd_block0_T what);
241static void set_b0_fname(ZERO_BL *, buf_T *buf);
242static void set_b0_dir_flag(ZERO_BL *b0p, buf_T *buf);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100243static void add_b0_fenc(ZERO_BL *b0p, buf_T *buf);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100244static time_t swapfile_info(char_u *);
245static int recov_file_names(char_u **, char_u *, int prepend_dot);
246static int ml_append_int(buf_T *, linenr_T, char_u *, colnr_T, int, int);
247static int ml_delete_int(buf_T *, linenr_T, int);
248static char_u *findswapname(buf_T *, char_u **, char_u *);
249static void ml_flush_line(buf_T *);
250static bhdr_T *ml_new_data(memfile_T *, int, int);
251static bhdr_T *ml_new_ptr(memfile_T *);
252static bhdr_T *ml_find_line(buf_T *, linenr_T, int);
253static int ml_add_stack(buf_T *);
254static void ml_lineadd(buf_T *, int);
255static int b0_magic_wrong(ZERO_BL *);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000256#ifdef CHECK_INODE
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100257static int fnamecmp_ino(char_u *, char_u *, long);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000258#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100259static void long_to_char(long, char_u *);
260static long char_to_long(char_u *);
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +0200261#ifdef FEAT_CRYPT
Bram Moolenaar8767f522016-07-01 17:17:39 +0200262static cryptstate_T *ml_crypt_prepare(memfile_T *mfp, off_T offset, int reading);
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +0200263#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000264#ifdef FEAT_BYTEOFF
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100265static void ml_updatechunk(buf_T *buf, long line, long len, int updtype);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000266#endif
267
268/*
Bram Moolenaar4770d092006-01-12 23:22:24 +0000269 * Open a new memline for "buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000270 *
Bram Moolenaar4770d092006-01-12 23:22:24 +0000271 * Return FAIL for failure, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000272 */
273 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100274ml_open(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000275{
276 memfile_T *mfp;
277 bhdr_T *hp = NULL;
278 ZERO_BL *b0p;
279 PTR_BL *pp;
280 DATA_BL *dp;
281
Bram Moolenaar4770d092006-01-12 23:22:24 +0000282 /*
283 * init fields in memline struct
284 */
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +0200285 buf->b_ml.ml_stack_size = 0; /* no stack yet */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000286 buf->b_ml.ml_stack = NULL; /* no stack yet */
287 buf->b_ml.ml_stack_top = 0; /* nothing in the stack */
288 buf->b_ml.ml_locked = NULL; /* no cached block */
289 buf->b_ml.ml_line_lnum = 0; /* no cached line */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000290#ifdef FEAT_BYTEOFF
Bram Moolenaar4770d092006-01-12 23:22:24 +0000291 buf->b_ml.ml_chunksize = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000292#endif
293
Bram Moolenaar5803ae62014-03-23 16:04:02 +0100294 if (cmdmod.noswapfile)
295 buf->b_p_swf = FALSE;
296
Bram Moolenaar4770d092006-01-12 23:22:24 +0000297 /*
298 * When 'updatecount' is non-zero swap file may be opened later.
299 */
300 if (p_uc && buf->b_p_swf)
301 buf->b_may_swap = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000302 else
Bram Moolenaar4770d092006-01-12 23:22:24 +0000303 buf->b_may_swap = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000304
Bram Moolenaar4770d092006-01-12 23:22:24 +0000305 /*
306 * Open the memfile. No swap file is created yet.
307 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000308 mfp = mf_open(NULL, 0);
309 if (mfp == NULL)
310 goto error;
311
Bram Moolenaar4770d092006-01-12 23:22:24 +0000312 buf->b_ml.ml_mfp = mfp;
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +0200313#ifdef FEAT_CRYPT
314 mfp->mf_buffer = buf;
315#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +0000316 buf->b_ml.ml_flags = ML_EMPTY;
317 buf->b_ml.ml_line_count = 1;
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000318#ifdef FEAT_LINEBREAK
319 curwin->w_nrwidth_line_count = 0;
320#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000321
Bram Moolenaar071d4272004-06-13 20:20:40 +0000322/*
323 * fill block0 struct and write page 0
324 */
325 if ((hp = mf_new(mfp, FALSE, 1)) == NULL)
326 goto error;
327 if (hp->bh_bnum != 0)
328 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100329 iemsg(_("E298: Didn't get block nr 0?"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000330 goto error;
331 }
332 b0p = (ZERO_BL *)(hp->bh_data);
333
334 b0p->b0_id[0] = BLOCK0_ID0;
335 b0p->b0_id[1] = BLOCK0_ID1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000336 b0p->b0_magic_long = (long)B0_MAGIC_LONG;
337 b0p->b0_magic_int = (int)B0_MAGIC_INT;
338 b0p->b0_magic_short = (short)B0_MAGIC_SHORT;
339 b0p->b0_magic_char = B0_MAGIC_CHAR;
Bram Moolenaar22c10562018-05-26 17:35:27 +0200340 mch_memmove(b0p->b0_version, "VIM ", 4);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000341 STRNCPY(b0p->b0_version + 4, Version, 6);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000342 long_to_char((long)mfp->mf_page_size, b0p->b0_page_size);
Bram Moolenaar4770d092006-01-12 23:22:24 +0000343
Bram Moolenaar76b92b22006-03-24 22:46:53 +0000344#ifdef FEAT_SPELL
345 if (!buf->b_spell)
346#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +0000347 {
348 b0p->b0_dirty = buf->b_changed ? B0_DIRTY : 0;
349 b0p->b0_flags = get_fileformat(buf) + 1;
350 set_b0_fname(b0p, buf);
351 (void)get_user_name(b0p->b0_uname, B0_UNAME_SIZE);
352 b0p->b0_uname[B0_UNAME_SIZE - 1] = NUL;
353 mch_get_host_name(b0p->b0_hname, B0_HNAME_SIZE);
354 b0p->b0_hname[B0_HNAME_SIZE - 1] = NUL;
355 long_to_char(mch_get_pid(), b0p->b0_pid);
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +0200356#ifdef FEAT_CRYPT
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200357 ml_set_b0_crypt(buf, b0p);
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +0200358#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +0000359 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000360
361 /*
362 * Always sync block number 0 to disk, so we can check the file name in
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +0200363 * the swap file in findswapname(). Don't do this for a help files or
364 * a spell buffer though.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000365 * Only works when there's a swapfile, otherwise it's done when the file
366 * is created.
367 */
368 mf_put(mfp, hp, TRUE, FALSE);
Bram Moolenaar4770d092006-01-12 23:22:24 +0000369 if (!buf->b_help && !B_SPELL(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000370 (void)mf_sync(mfp, 0);
371
Bram Moolenaar4770d092006-01-12 23:22:24 +0000372 /*
373 * Fill in root pointer block and write page 1.
374 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000375 if ((hp = ml_new_ptr(mfp)) == NULL)
376 goto error;
377 if (hp->bh_bnum != 1)
378 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100379 iemsg(_("E298: Didn't get block nr 1?"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000380 goto error;
381 }
382 pp = (PTR_BL *)(hp->bh_data);
383 pp->pb_count = 1;
384 pp->pb_pointer[0].pe_bnum = 2;
385 pp->pb_pointer[0].pe_page_count = 1;
386 pp->pb_pointer[0].pe_old_lnum = 1;
387 pp->pb_pointer[0].pe_line_count = 1; /* line count after insertion */
388 mf_put(mfp, hp, TRUE, FALSE);
389
Bram Moolenaar4770d092006-01-12 23:22:24 +0000390 /*
391 * Allocate first data block and create an empty line 1.
392 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000393 if ((hp = ml_new_data(mfp, FALSE, 1)) == NULL)
394 goto error;
395 if (hp->bh_bnum != 2)
396 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100397 iemsg(_("E298: Didn't get block nr 2?"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000398 goto error;
399 }
400
401 dp = (DATA_BL *)(hp->bh_data);
402 dp->db_index[0] = --dp->db_txt_start; /* at end of block */
403 dp->db_free -= 1 + INDEX_SIZE;
404 dp->db_line_count = 1;
Bram Moolenaarf05da212009-11-17 16:13:15 +0000405 *((char_u *)dp + dp->db_txt_start) = NUL; /* empty line */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000406
407 return OK;
408
409error:
410 if (mfp != NULL)
411 {
412 if (hp)
413 mf_put(mfp, hp, FALSE, FALSE);
414 mf_close(mfp, TRUE); /* will also free(mfp->mf_fname) */
415 }
Bram Moolenaar4770d092006-01-12 23:22:24 +0000416 buf->b_ml.ml_mfp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000417 return FAIL;
418}
419
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +0200420#if defined(FEAT_CRYPT) || defined(PROTO)
421/*
Bram Moolenaar2be79502014-08-13 21:58:28 +0200422 * Prepare encryption for "buf" for the current key and method.
423 */
424 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100425ml_set_mfp_crypt(buf_T *buf)
Bram Moolenaar2be79502014-08-13 21:58:28 +0200426{
427 if (*buf->b_p_key != NUL)
428 {
429 int method_nr = crypt_get_method_nr(buf);
430
431 if (method_nr > CRYPT_M_ZIP)
432 {
433 /* Generate a seed and store it in the memfile. */
434 sha2_seed(buf->b_ml.ml_mfp->mf_seed, MF_SEED_LEN, NULL, 0);
435 }
436 }
437}
438
439/*
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +0200440 * Prepare encryption for "buf" with block 0 "b0p".
441 */
442 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100443ml_set_b0_crypt(buf_T *buf, ZERO_BL *b0p)
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +0200444{
445 if (*buf->b_p_key == NUL)
446 b0p->b0_id[1] = BLOCK0_ID1;
447 else
448 {
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200449 int method_nr = crypt_get_method_nr(buf);
450
451 b0p->b0_id[1] = id1_codes[method_nr];
452 if (method_nr > CRYPT_M_ZIP)
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +0200453 {
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +0200454 /* Generate a seed and store it in block 0 and in the memfile. */
455 sha2_seed(&b0p->b0_seed, MF_SEED_LEN, NULL, 0);
456 mch_memmove(buf->b_ml.ml_mfp->mf_seed, &b0p->b0_seed, MF_SEED_LEN);
457 }
458 }
459}
460
461/*
462 * Called after the crypt key or 'cryptmethod' was changed for "buf".
463 * Will apply this to the swapfile.
464 * "old_key" is the previous key. It is equal to buf->b_p_key when
465 * 'cryptmethod' is changed.
Bram Moolenaar49771f42010-07-20 17:32:38 +0200466 * "old_cm" is the previous 'cryptmethod'. It is equal to the current
467 * 'cryptmethod' when 'key' is changed.
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +0200468 */
469 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100470ml_set_crypt_key(
471 buf_T *buf,
472 char_u *old_key,
473 char_u *old_cm)
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +0200474{
475 memfile_T *mfp = buf->b_ml.ml_mfp;
476 bhdr_T *hp;
477 int page_count;
478 int idx;
479 long error;
480 infoptr_T *ip;
481 PTR_BL *pp;
482 DATA_BL *dp;
483 blocknr_T bnum;
484 int top;
Bram Moolenaarbc563362015-06-09 18:35:25 +0200485 int old_method;
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +0200486
Bram Moolenaar3832c462010-08-04 15:32:46 +0200487 if (mfp == NULL)
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +0200488 return; /* no memfile yet, nothing to do */
Bram Moolenaarbc563362015-06-09 18:35:25 +0200489 old_method = crypt_method_nr_from_name(old_cm);
490
491 /* First make sure the swapfile is in a consistent state, using the old
492 * key and method. */
493 {
494 char_u *new_key = buf->b_p_key;
495 char_u *new_buf_cm = buf->b_p_cm;
496
497 buf->b_p_key = old_key;
498 buf->b_p_cm = old_cm;
499 ml_preserve(buf, FALSE);
500 buf->b_p_key = new_key;
501 buf->b_p_cm = new_buf_cm;
502 }
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +0200503
504 /* Set the key, method and seed to be used for reading, these must be the
505 * old values. */
506 mfp->mf_old_key = old_key;
Bram Moolenaarbc563362015-06-09 18:35:25 +0200507 mfp->mf_old_cm = old_method;
508 if (old_method > 0 && *old_key != NUL)
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +0200509 mch_memmove(mfp->mf_old_seed, mfp->mf_seed, MF_SEED_LEN);
510
511 /* Update block 0 with the crypt flag and may set a new seed. */
512 ml_upd_block0(buf, UB_CRYPT);
513
514 if (mfp->mf_infile_count > 2)
515 {
516 /*
517 * Need to read back all data blocks from disk, decrypt them with the
518 * old key/method and mark them to be written. The algorithm is
519 * similar to what happens in ml_recover(), but we skip negative block
520 * numbers.
521 */
522 ml_flush_line(buf); /* flush buffered line */
523 (void)ml_find_line(buf, (linenr_T)0, ML_FLUSH); /* flush locked block */
524
525 hp = NULL;
526 bnum = 1; /* start with block 1 */
527 page_count = 1; /* which is 1 page */
528 idx = 0; /* start with first index in block 1 */
529 error = 0;
530 buf->b_ml.ml_stack_top = 0;
Bram Moolenaard23a8232018-02-10 18:45:26 +0100531 VIM_CLEAR(buf->b_ml.ml_stack);
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +0200532 buf->b_ml.ml_stack_size = 0; /* no stack yet */
533
534 for ( ; !got_int; line_breakcheck())
535 {
536 if (hp != NULL)
537 mf_put(mfp, hp, FALSE, FALSE); /* release previous block */
538
539 /* get the block (pointer or data) */
540 if ((hp = mf_get(mfp, (blocknr_T)bnum, page_count)) == NULL)
541 {
542 if (bnum == 1)
543 break;
544 ++error;
545 }
546 else
547 {
548 pp = (PTR_BL *)(hp->bh_data);
549 if (pp->pb_id == PTR_ID) /* it is a pointer block */
550 {
551 if (pp->pb_count == 0)
552 {
553 /* empty block? */
554 ++error;
555 }
556 else if (idx < (int)pp->pb_count) /* go a block deeper */
557 {
558 if (pp->pb_pointer[idx].pe_bnum < 0)
559 {
Bram Moolenaarbc563362015-06-09 18:35:25 +0200560 /* Skip data block with negative block number.
561 * Should not happen, because of the ml_preserve()
562 * above. Get same block again for next index. */
Bram Moolenaar4b7214e2019-01-03 21:55:32 +0100563 ++idx;
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +0200564 continue;
565 }
566
567 /* going one block deeper in the tree, new entry in
568 * stack */
569 if ((top = ml_add_stack(buf)) < 0)
570 {
571 ++error;
572 break; /* out of memory */
573 }
574 ip = &(buf->b_ml.ml_stack[top]);
575 ip->ip_bnum = bnum;
576 ip->ip_index = idx;
577
578 bnum = pp->pb_pointer[idx].pe_bnum;
579 page_count = pp->pb_pointer[idx].pe_page_count;
Bram Moolenaarbc563362015-06-09 18:35:25 +0200580 idx = 0;
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +0200581 continue;
582 }
583 }
584 else /* not a pointer block */
585 {
586 dp = (DATA_BL *)(hp->bh_data);
587 if (dp->db_id != DATA_ID) /* block id wrong */
588 ++error;
589 else
590 {
591 /* It is a data block, need to write it back to disk. */
592 mf_put(mfp, hp, TRUE, FALSE);
593 hp = NULL;
594 }
595 }
596 }
597
598 if (buf->b_ml.ml_stack_top == 0) /* finished */
599 break;
600
601 /* go one block up in the tree */
602 ip = &(buf->b_ml.ml_stack[--(buf->b_ml.ml_stack_top)]);
603 bnum = ip->ip_bnum;
604 idx = ip->ip_index + 1; /* go to next index */
605 page_count = 1;
606 }
Bram Moolenaarbc563362015-06-09 18:35:25 +0200607 if (hp != NULL)
608 mf_put(mfp, hp, FALSE, FALSE); /* release previous block */
Bram Moolenaar2e2e13c2010-12-08 13:17:03 +0100609
610 if (error > 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100611 emsg(_("E843: Error while updating swap file crypt"));
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +0200612 }
613
614 mfp->mf_old_key = NULL;
615}
616#endif
617
Bram Moolenaar071d4272004-06-13 20:20:40 +0000618/*
619 * ml_setname() is called when the file name of "buf" has been changed.
620 * It may rename the swap file.
621 */
622 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100623ml_setname(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000624{
625 int success = FALSE;
626 memfile_T *mfp;
627 char_u *fname;
628 char_u *dirp;
Bram Moolenaar48e330a2016-02-23 14:53:34 +0100629#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000630 char_u *p;
631#endif
632
633 mfp = buf->b_ml.ml_mfp;
634 if (mfp->mf_fd < 0) /* there is no swap file yet */
635 {
636 /*
637 * When 'updatecount' is 0 and 'noswapfile' there is no swap file.
638 * For help files we will make a swap file now.
639 */
Bram Moolenaar5803ae62014-03-23 16:04:02 +0100640 if (p_uc != 0 && !cmdmod.noswapfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000641 ml_open_file(buf); /* create a swap file */
642 return;
643 }
644
645 /*
646 * Try all directories in the 'directory' option.
647 */
648 dirp = p_dir;
649 for (;;)
650 {
651 if (*dirp == NUL) /* tried all directories, fail */
652 break;
Bram Moolenaar8fc061c2004-12-29 21:03:02 +0000653 fname = findswapname(buf, &dirp, mfp->mf_fname);
654 /* alloc's fname */
Bram Moolenaarf541c362011-10-26 11:44:18 +0200655 if (dirp == NULL) /* out of memory */
656 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000657 if (fname == NULL) /* no file name found for this dir */
658 continue;
659
Bram Moolenaar48e330a2016-02-23 14:53:34 +0100660#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000661 /*
662 * Set full pathname for swap file now, because a ":!cd dir" may
663 * change directory without us knowing it.
664 */
665 p = FullName_save(fname, FALSE);
666 vim_free(fname);
667 fname = p;
668 if (fname == NULL)
669 continue;
670#endif
671 /* if the file name is the same we don't have to do anything */
672 if (fnamecmp(fname, mfp->mf_fname) == 0)
673 {
674 vim_free(fname);
675 success = TRUE;
676 break;
677 }
678 /* need to close the swap file before renaming */
679 if (mfp->mf_fd >= 0)
680 {
681 close(mfp->mf_fd);
682 mfp->mf_fd = -1;
683 }
684
685 /* try to rename the swap file */
686 if (vim_rename(mfp->mf_fname, fname) == 0)
687 {
688 success = TRUE;
689 vim_free(mfp->mf_fname);
690 mfp->mf_fname = fname;
691 vim_free(mfp->mf_ffname);
Bram Moolenaar48e330a2016-02-23 14:53:34 +0100692#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000693 mfp->mf_ffname = NULL; /* mf_fname is full pathname already */
694#else
695 mf_set_ffname(mfp);
696#endif
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +0200697 ml_upd_block0(buf, UB_SAME_DIR);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000698 break;
699 }
700 vim_free(fname); /* this fname didn't work, try another */
701 }
702
703 if (mfp->mf_fd == -1) /* need to (re)open the swap file */
704 {
705 mfp->mf_fd = mch_open((char *)mfp->mf_fname, O_RDWR | O_EXTRA, 0);
706 if (mfp->mf_fd < 0)
707 {
708 /* could not (re)open the swap file, what can we do???? */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100709 emsg(_("E301: Oops, lost the swap file!!!"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000710 return;
711 }
Bram Moolenaarf05da212009-11-17 16:13:15 +0000712#ifdef HAVE_FD_CLOEXEC
713 {
714 int fdflags = fcntl(mfp->mf_fd, F_GETFD);
715 if (fdflags >= 0 && (fdflags & FD_CLOEXEC) == 0)
Bram Moolenaarfbc4b4d2016-02-07 15:14:01 +0100716 (void)fcntl(mfp->mf_fd, F_SETFD, fdflags | FD_CLOEXEC);
Bram Moolenaarf05da212009-11-17 16:13:15 +0000717 }
718#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000719 }
720 if (!success)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100721 emsg(_("E302: Could not rename swap file"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000722}
723
724/*
725 * Open a file for the memfile for all buffers that are not readonly or have
726 * been modified.
727 * Used when 'updatecount' changes from zero to non-zero.
728 */
729 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100730ml_open_files(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000731{
732 buf_T *buf;
733
Bram Moolenaar29323592016-07-24 22:04:11 +0200734 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000735 if (!buf->b_p_ro || buf->b_changed)
736 ml_open_file(buf);
737}
738
739/*
740 * Open a swap file for an existing memfile, if there is no swap file yet.
741 * If we are unable to find a file name, mf_fname will be NULL
742 * and the memfile will be in memory only (no recovery possible).
743 */
744 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100745ml_open_file(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000746{
747 memfile_T *mfp;
748 char_u *fname;
749 char_u *dirp;
750
751 mfp = buf->b_ml.ml_mfp;
Bram Moolenaar5803ae62014-03-23 16:04:02 +0100752 if (mfp == NULL || mfp->mf_fd >= 0 || !buf->b_p_swf || cmdmod.noswapfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000753 return; /* nothing to do */
754
Bram Moolenaara1956f62006-03-12 22:18:00 +0000755#ifdef FEAT_SPELL
Bram Moolenaar4770d092006-01-12 23:22:24 +0000756 /* For a spell buffer use a temp file name. */
757 if (buf->b_spell)
758 {
Bram Moolenaare5c421c2015-03-31 13:33:08 +0200759 fname = vim_tempname('s', FALSE);
Bram Moolenaar4770d092006-01-12 23:22:24 +0000760 if (fname != NULL)
761 (void)mf_open_file(mfp, fname); /* consumes fname! */
762 buf->b_may_swap = FALSE;
763 return;
764 }
765#endif
766
Bram Moolenaar071d4272004-06-13 20:20:40 +0000767 /*
768 * Try all directories in 'directory' option.
769 */
770 dirp = p_dir;
771 for (;;)
772 {
773 if (*dirp == NUL)
774 break;
Bram Moolenaare242b832010-06-24 05:39:03 +0200775 /* There is a small chance that between choosing the swap file name
776 * and creating it, another Vim creates the file. In that case the
Bram Moolenaar071d4272004-06-13 20:20:40 +0000777 * creation will fail and we will use another directory. */
Bram Moolenaar8fc061c2004-12-29 21:03:02 +0000778 fname = findswapname(buf, &dirp, NULL); /* allocates fname */
Bram Moolenaarf541c362011-10-26 11:44:18 +0200779 if (dirp == NULL)
780 break; /* out of memory */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000781 if (fname == NULL)
782 continue;
783 if (mf_open_file(mfp, fname) == OK) /* consumes fname! */
784 {
Bram Moolenaar48e330a2016-02-23 14:53:34 +0100785#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000786 /*
787 * set full pathname for swap file now, because a ":!cd dir" may
788 * change directory without us knowing it.
789 */
790 mf_fullname(mfp);
791#endif
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +0200792 ml_upd_block0(buf, UB_SAME_DIR);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000793
Bram Moolenaar071d4272004-06-13 20:20:40 +0000794 /* Flush block zero, so others can read it */
795 if (mf_sync(mfp, MFS_ZERO) == OK)
Bram Moolenaarc32840f2006-01-14 21:23:38 +0000796 {
797 /* Mark all blocks that should be in the swapfile as dirty.
798 * Needed for when the 'swapfile' option was reset, so that
799 * the swap file was deleted, and then on again. */
800 mf_set_dirty(mfp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000801 break;
Bram Moolenaarc32840f2006-01-14 21:23:38 +0000802 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000803 /* Writing block 0 failed: close the file and try another dir */
804 mf_close_file(buf, FALSE);
805 }
806 }
807
808 if (mfp->mf_fname == NULL) /* Failed! */
809 {
810 need_wait_return = TRUE; /* call wait_return later */
811 ++no_wait_return;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100812 (void)semsg(_("E303: Unable to open swap file for \"%s\", recovery impossible"),
Bram Moolenaare1704ba2012-10-03 18:25:00 +0200813 buf_spname(buf) != NULL ? buf_spname(buf) : buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000814 --no_wait_return;
815 }
816
817 /* don't try to open a swap file again */
818 buf->b_may_swap = FALSE;
819}
820
821/*
822 * If still need to create a swap file, and starting to edit a not-readonly
823 * file, or reading into an existing buffer, create a swap file now.
824 */
825 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100826check_need_swap(
Bram Moolenaar2f0f8712018-08-21 18:50:18 +0200827 int newfile) // reading file into new buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +0000828{
Bram Moolenaar2f0f8712018-08-21 18:50:18 +0200829 int old_msg_silent = msg_silent; // might be reset by an E325 message
830
Bram Moolenaar071d4272004-06-13 20:20:40 +0000831 if (curbuf->b_may_swap && (!curbuf->b_p_ro || !newfile))
832 ml_open_file(curbuf);
Bram Moolenaar2f0f8712018-08-21 18:50:18 +0200833 msg_silent = old_msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000834}
835
836/*
837 * Close memline for buffer 'buf'.
838 * If 'del_file' is TRUE, delete the swap file
839 */
840 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100841ml_close(buf_T *buf, int del_file)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000842{
843 if (buf->b_ml.ml_mfp == NULL) /* not open */
844 return;
845 mf_close(buf->b_ml.ml_mfp, del_file); /* close the .swp file */
846 if (buf->b_ml.ml_line_lnum != 0 && (buf->b_ml.ml_flags & ML_LINE_DIRTY))
847 vim_free(buf->b_ml.ml_line_ptr);
848 vim_free(buf->b_ml.ml_stack);
849#ifdef FEAT_BYTEOFF
Bram Moolenaard23a8232018-02-10 18:45:26 +0100850 VIM_CLEAR(buf->b_ml.ml_chunksize);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000851#endif
852 buf->b_ml.ml_mfp = NULL;
853
854 /* Reset the "recovered" flag, give the ATTENTION prompt the next time
855 * this buffer is loaded. */
856 buf->b_flags &= ~BF_RECOVERED;
857}
858
859/*
860 * Close all existing memlines and memfiles.
861 * Only used when exiting.
862 * When 'del_file' is TRUE, delete the memfiles.
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000863 * But don't delete files that were ":preserve"d when we are POSIX compatible.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000864 */
865 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100866ml_close_all(int del_file)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000867{
868 buf_T *buf;
869
Bram Moolenaar29323592016-07-24 22:04:11 +0200870 FOR_ALL_BUFFERS(buf)
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000871 ml_close(buf, del_file && ((buf->b_flags & BF_PRESERVED) == 0
872 || vim_strchr(p_cpo, CPO_PRESERVE) == NULL));
Bram Moolenaar34b466e2013-11-28 17:41:46 +0100873#ifdef FEAT_SPELL
874 spell_delete_wordlist(); /* delete the internal wordlist */
875#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000876#ifdef TEMPDIRNAMES
Bram Moolenaar34b466e2013-11-28 17:41:46 +0100877 vim_deltempdir(); /* delete created temp directory */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000878#endif
879}
880
881/*
882 * Close all memfiles for not modified buffers.
883 * Only use just before exiting!
884 */
885 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100886ml_close_notmod(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000887{
888 buf_T *buf;
889
Bram Moolenaar29323592016-07-24 22:04:11 +0200890 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000891 if (!bufIsChanged(buf))
892 ml_close(buf, TRUE); /* close all not-modified buffers */
893}
894
895/*
896 * Update the timestamp in the .swp file.
897 * Used when the file has been written.
898 */
899 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100900ml_timestamp(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000901{
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +0200902 ml_upd_block0(buf, UB_FNAME);
903}
904
905/*
906 * Return FAIL when the ID of "b0p" is wrong.
907 */
908 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100909ml_check_b0_id(ZERO_BL *b0p)
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +0200910{
911 if (b0p->b0_id[0] != BLOCK0_ID0
912 || (b0p->b0_id[1] != BLOCK0_ID1
913 && b0p->b0_id[1] != BLOCK0_ID1_C0
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200914 && b0p->b0_id[1] != BLOCK0_ID1_C1
915 && b0p->b0_id[1] != BLOCK0_ID1_C2)
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +0200916 )
917 return FAIL;
918 return OK;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000919}
920
921/*
922 * Update the timestamp or the B0_SAME_DIR flag of the .swp file.
923 */
924 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100925ml_upd_block0(buf_T *buf, upd_block0_T what)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000926{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000927 memfile_T *mfp;
928 bhdr_T *hp;
929 ZERO_BL *b0p;
930
931 mfp = buf->b_ml.ml_mfp;
Bram Moolenaar2be79502014-08-13 21:58:28 +0200932 if (mfp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000933 return;
Bram Moolenaar2be79502014-08-13 21:58:28 +0200934 hp = mf_get(mfp, (blocknr_T)0, 1);
935 if (hp == NULL)
936 {
937#ifdef FEAT_CRYPT
938 /* Possibly update the seed in the memfile before there is a block0. */
939 if (what == UB_CRYPT)
940 ml_set_mfp_crypt(buf);
941#endif
942 return;
943 }
944
Bram Moolenaar071d4272004-06-13 20:20:40 +0000945 b0p = (ZERO_BL *)(hp->bh_data);
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +0200946 if (ml_check_b0_id(b0p) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100947 iemsg(_("E304: ml_upd_block0(): Didn't get block 0??"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000948 else
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000949 {
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +0200950 if (what == UB_FNAME)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000951 set_b0_fname(b0p, buf);
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +0200952#ifdef FEAT_CRYPT
953 else if (what == UB_CRYPT)
954 ml_set_b0_crypt(buf, b0p);
955#endif
956 else /* what == UB_SAME_DIR */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000957 set_b0_dir_flag(b0p, buf);
958 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000959 mf_put(mfp, hp, TRUE, FALSE);
960}
961
962/*
963 * Write file name and timestamp into block 0 of a swap file.
964 * Also set buf->b_mtime.
965 * Don't use NameBuff[]!!!
966 */
967 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100968set_b0_fname(ZERO_BL *b0p, buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000969{
Bram Moolenaar8767f522016-07-01 17:17:39 +0200970 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000971
972 if (buf->b_ffname == NULL)
973 b0p->b0_fname[0] = NUL;
974 else
975 {
Bram Moolenaar48e330a2016-02-23 14:53:34 +0100976#if defined(MSWIN) || defined(AMIGA)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000977 /* Systems that cannot translate "~user" back into a path: copy the
978 * file name unmodified. Do use slashes instead of backslashes for
979 * portability. */
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +0200980 vim_strncpy(b0p->b0_fname, buf->b_ffname, B0_FNAME_SIZE_CRYPT - 1);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000981# ifdef BACKSLASH_IN_FILENAME
982 forward_slash(b0p->b0_fname);
983# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000984#else
985 size_t flen, ulen;
986 char_u uname[B0_UNAME_SIZE];
987
988 /*
989 * For a file under the home directory of the current user, we try to
990 * replace the home directory path with "~user". This helps when
991 * editing the same file on different machines over a network.
992 * First replace home dir path with "~/" with home_replace().
993 * Then insert the user name to get "~user/".
994 */
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +0200995 home_replace(NULL, buf->b_ffname, b0p->b0_fname,
996 B0_FNAME_SIZE_CRYPT, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000997 if (b0p->b0_fname[0] == '~')
998 {
999 flen = STRLEN(b0p->b0_fname);
1000 /* If there is no user name or it is too long, don't use "~/" */
1001 if (get_user_name(uname, B0_UNAME_SIZE) == FAIL
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02001002 || (ulen = STRLEN(uname)) + flen > B0_FNAME_SIZE_CRYPT - 1)
1003 vim_strncpy(b0p->b0_fname, buf->b_ffname,
1004 B0_FNAME_SIZE_CRYPT - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001005 else
1006 {
1007 mch_memmove(b0p->b0_fname + ulen + 1, b0p->b0_fname + 1, flen);
1008 mch_memmove(b0p->b0_fname + 1, uname, ulen);
1009 }
1010 }
1011#endif
1012 if (mch_stat((char *)buf->b_ffname, &st) >= 0)
1013 {
1014 long_to_char((long)st.st_mtime, b0p->b0_mtime);
1015#ifdef CHECK_INODE
1016 long_to_char((long)st.st_ino, b0p->b0_ino);
1017#endif
1018 buf_store_time(buf, &st, buf->b_ffname);
1019 buf->b_mtime_read = buf->b_mtime;
1020 }
1021 else
1022 {
1023 long_to_char(0L, b0p->b0_mtime);
1024#ifdef CHECK_INODE
1025 long_to_char(0L, b0p->b0_ino);
1026#endif
1027 buf->b_mtime = 0;
1028 buf->b_mtime_read = 0;
1029 buf->b_orig_size = 0;
1030 buf->b_orig_mode = 0;
1031 }
1032 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001033
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001034 /* Also add the 'fileencoding' if there is room. */
1035 add_b0_fenc(b0p, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001036}
1037
1038/*
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001039 * Update the B0_SAME_DIR flag of the swap file. It's set if the file and the
1040 * swapfile for "buf" are in the same directory.
1041 * This is fail safe: if we are not sure the directories are equal the flag is
1042 * not set.
1043 */
1044 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001045set_b0_dir_flag(ZERO_BL *b0p, buf_T *buf)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001046{
1047 if (same_directory(buf->b_ml.ml_mfp->mf_fname, buf->b_ffname))
1048 b0p->b0_flags |= B0_SAME_DIR;
1049 else
1050 b0p->b0_flags &= ~B0_SAME_DIR;
1051}
1052
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001053/*
1054 * When there is room, add the 'fileencoding' to block zero.
1055 */
1056 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001057add_b0_fenc(
1058 ZERO_BL *b0p,
1059 buf_T *buf)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001060{
1061 int n;
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02001062 int size = B0_FNAME_SIZE_NOCRYPT;
1063
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01001064#ifdef FEAT_CRYPT
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02001065 /* Without encryption use the same offset as in Vim 7.2 to be compatible.
1066 * With encryption it's OK to move elsewhere, the swap file is not
1067 * compatible anyway. */
1068 if (*buf->b_p_key != NUL)
1069 size = B0_FNAME_SIZE_CRYPT;
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01001070#endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001071
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001072 n = (int)STRLEN(buf->b_p_fenc);
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02001073 if ((int)STRLEN(b0p->b0_fname) + n + 1 > size)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001074 b0p->b0_flags &= ~B0_HAS_FENC;
1075 else
1076 {
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02001077 mch_memmove((char *)b0p->b0_fname + size - n,
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001078 (char *)buf->b_p_fenc, (size_t)n);
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02001079 *(b0p->b0_fname + size - n - 1) = NUL;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001080 b0p->b0_flags |= B0_HAS_FENC;
1081 }
1082}
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001083
1084
1085/*
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02001086 * Try to recover curbuf from the .swp file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001087 */
1088 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001089ml_recover(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001090{
1091 buf_T *buf = NULL;
1092 memfile_T *mfp = NULL;
1093 char_u *fname;
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02001094 char_u *fname_used = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001095 bhdr_T *hp = NULL;
1096 ZERO_BL *b0p;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001097 int b0_ff;
1098 char_u *b0_fenc = NULL;
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02001099#ifdef FEAT_CRYPT
1100 int b0_cm = -1;
1101#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001102 PTR_BL *pp;
1103 DATA_BL *dp;
1104 infoptr_T *ip;
1105 blocknr_T bnum;
1106 int page_count;
Bram Moolenaar8767f522016-07-01 17:17:39 +02001107 stat_T org_stat, swp_stat;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001108 int len;
1109 int directly;
1110 linenr_T lnum;
1111 char_u *p;
1112 int i;
1113 long error;
1114 int cannot_open;
1115 linenr_T line_count;
1116 int has_error;
1117 int idx;
1118 int top;
1119 int txt_start;
Bram Moolenaar8767f522016-07-01 17:17:39 +02001120 off_T size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001121 int called_from_main;
1122 int serious_error = TRUE;
1123 long mtime;
1124 int attr;
Bram Moolenaarfc2d5bd2010-05-15 17:06:53 +02001125 int orig_file_status = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001126
1127 recoverymode = TRUE;
1128 called_from_main = (curbuf->b_ml.ml_mfp == NULL);
Bram Moolenaar8820b482017-03-16 17:23:31 +01001129 attr = HL_ATTR(HLF_E);
Bram Moolenaard0ba34a2009-11-03 12:06:23 +00001130
1131 /*
Bram Moolenaaraf903e52017-12-02 15:11:22 +01001132 * If the file name ends in ".s[a-w][a-z]" we assume this is the swap file.
Bram Moolenaard0ba34a2009-11-03 12:06:23 +00001133 * Otherwise a search is done to find the swap file(s).
1134 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001135 fname = curbuf->b_fname;
1136 if (fname == NULL) /* When there is no file name */
1137 fname = (char_u *)"";
1138 len = (int)STRLEN(fname);
1139 if (len >= 4 &&
Bram Moolenaare60acc12011-05-10 16:41:25 +02001140#if defined(VMS)
Bram Moolenaar79518e22017-02-17 16:31:35 +01001141 STRNICMP(fname + len - 4, "_s", 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001142#else
Bram Moolenaar79518e22017-02-17 16:31:35 +01001143 STRNICMP(fname + len - 4, ".s", 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001144#endif
Bram Moolenaar79518e22017-02-17 16:31:35 +01001145 == 0
Bram Moolenaaraf903e52017-12-02 15:11:22 +01001146 && vim_strchr((char_u *)"abcdefghijklmnopqrstuvw",
1147 TOLOWER_ASC(fname[len - 2])) != NULL
Bram Moolenaard0ba34a2009-11-03 12:06:23 +00001148 && ASCII_ISALPHA(fname[len - 1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001149 {
1150 directly = TRUE;
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02001151 fname_used = vim_strsave(fname); /* make a copy for mf_open() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001152 }
1153 else
1154 {
1155 directly = FALSE;
1156
1157 /* count the number of matching swap files */
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02001158 len = recover_names(fname, FALSE, 0, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001159 if (len == 0) /* no swap files found */
1160 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001161 semsg(_("E305: No swap file found for %s"), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001162 goto theend;
1163 }
1164 if (len == 1) /* one swap file found, use it */
1165 i = 1;
1166 else /* several swap files found, choose */
1167 {
1168 /* list the names of the swap files */
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02001169 (void)recover_names(fname, TRUE, 0, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001170 msg_putchar('\n');
Bram Moolenaar32526b32019-01-19 17:43:09 +01001171 msg_puts(_("Enter number of swap file to use (0 to quit): "));
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001172 i = get_number(FALSE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001173 if (i < 1 || i > len)
1174 goto theend;
1175 }
1176 /* get the swap file name that will be used */
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02001177 (void)recover_names(fname, FALSE, i, &fname_used);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001178 }
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02001179 if (fname_used == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001180 goto theend; /* out of memory */
1181
1182 /* When called from main() still need to initialize storage structure */
Bram Moolenaar4770d092006-01-12 23:22:24 +00001183 if (called_from_main && ml_open(curbuf) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001184 getout(1);
1185
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02001186 /*
1187 * Allocate a buffer structure for the swap file that is used for recovery.
Bram Moolenaar0ad014c2010-07-25 14:00:46 +02001188 * Only the memline and crypt information in it are really used.
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02001189 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001190 buf = (buf_T *)alloc((unsigned)sizeof(buf_T));
1191 if (buf == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001192 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001193
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02001194 /*
1195 * init fields in memline struct
1196 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001197 buf->b_ml.ml_stack_size = 0; /* no stack yet */
1198 buf->b_ml.ml_stack = NULL; /* no stack yet */
1199 buf->b_ml.ml_stack_top = 0; /* nothing in the stack */
1200 buf->b_ml.ml_line_lnum = 0; /* no cached line */
1201 buf->b_ml.ml_locked = NULL; /* no locked block */
1202 buf->b_ml.ml_flags = 0;
Bram Moolenaar0fe849a2010-07-25 15:11:11 +02001203#ifdef FEAT_CRYPT
1204 buf->b_p_key = empty_option;
1205 buf->b_p_cm = empty_option;
1206#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001207
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02001208 /*
1209 * open the memfile from the old swap file
1210 */
1211 p = vim_strsave(fname_used); /* save "fname_used" for the message:
1212 mf_open() will consume "fname_used"! */
1213 mfp = mf_open(fname_used, O_RDONLY);
1214 fname_used = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001215 if (mfp == NULL || mfp->mf_fd < 0)
1216 {
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02001217 if (fname_used != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001218 semsg(_("E306: Cannot open %s"), fname_used);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001219 goto theend;
1220 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001221 buf->b_ml.ml_mfp = mfp;
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02001222#ifdef FEAT_CRYPT
1223 mfp->mf_buffer = buf;
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02001224#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001225
1226 /*
1227 * The page size set in mf_open() might be different from the page size
1228 * used in the swap file, we must get it from block 0. But to read block
1229 * 0 we need a page size. Use the minimal size for block 0 here, it will
1230 * be set to the real value below.
1231 */
1232 mfp->mf_page_size = MIN_SWAP_PAGE_SIZE;
1233
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02001234 /*
1235 * try to read block 0
1236 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001237 if ((hp = mf_get(mfp, (blocknr_T)0, 1)) == NULL)
1238 {
1239 msg_start();
Bram Moolenaar32526b32019-01-19 17:43:09 +01001240 msg_puts_attr(_("Unable to read block 0 from "), attr | MSG_HIST);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001241 msg_outtrans_attr(mfp->mf_fname, attr | MSG_HIST);
Bram Moolenaar32526b32019-01-19 17:43:09 +01001242 msg_puts_attr(_("\nMaybe no changes were made or Vim did not update the swap file."),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001243 attr | MSG_HIST);
1244 msg_end();
1245 goto theend;
1246 }
1247 b0p = (ZERO_BL *)(hp->bh_data);
1248 if (STRNCMP(b0p->b0_version, "VIM 3.0", 7) == 0)
1249 {
1250 msg_start();
1251 msg_outtrans_attr(mfp->mf_fname, MSG_HIST);
Bram Moolenaar32526b32019-01-19 17:43:09 +01001252 msg_puts_attr(_(" cannot be used with this version of Vim.\n"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001253 MSG_HIST);
Bram Moolenaar32526b32019-01-19 17:43:09 +01001254 msg_puts_attr(_("Use Vim version 3.0.\n"), MSG_HIST);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001255 msg_end();
1256 goto theend;
1257 }
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02001258 if (ml_check_b0_id(b0p) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001259 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001260 semsg(_("E307: %s does not look like a Vim swap file"), mfp->mf_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001261 goto theend;
1262 }
1263 if (b0_magic_wrong(b0p))
1264 {
1265 msg_start();
1266 msg_outtrans_attr(mfp->mf_fname, attr | MSG_HIST);
Bram Moolenaar48e330a2016-02-23 14:53:34 +01001267#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001268 if (STRNCMP(b0p->b0_hname, "PC ", 3) == 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001269 msg_puts_attr(_(" cannot be used with this version of Vim.\n"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001270 attr | MSG_HIST);
1271 else
1272#endif
Bram Moolenaar32526b32019-01-19 17:43:09 +01001273 msg_puts_attr(_(" cannot be used on this computer.\n"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001274 attr | MSG_HIST);
Bram Moolenaar32526b32019-01-19 17:43:09 +01001275 msg_puts_attr(_("The file was created on "), attr | MSG_HIST);
Bram Moolenaare242b832010-06-24 05:39:03 +02001276 /* avoid going past the end of a corrupted hostname */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001277 b0p->b0_fname[0] = NUL;
Bram Moolenaar32526b32019-01-19 17:43:09 +01001278 msg_puts_attr((char *)b0p->b0_hname, attr | MSG_HIST);
1279 msg_puts_attr(_(",\nor the file has been damaged."), attr | MSG_HIST);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001280 msg_end();
1281 goto theend;
1282 }
Bram Moolenaar1c536282007-04-26 15:21:56 +00001283
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02001284#ifdef FEAT_CRYPT
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001285 for (i = 0; i < (int)(sizeof(id1_codes) / sizeof(int)); ++i)
1286 if (id1_codes[i] == b0p->b0_id[1])
1287 b0_cm = i;
1288 if (b0_cm > 0)
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02001289 mch_memmove(mfp->mf_seed, &b0p->b0_seed, MF_SEED_LEN);
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001290 crypt_set_cm_option(buf, b0_cm < 0 ? 0 : b0_cm);
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02001291#else
1292 if (b0p->b0_id[1] != BLOCK0_ID1)
1293 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001294 semsg(_("E833: %s is encrypted and this version of Vim does not support encryption"), mfp->mf_fname);
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02001295 goto theend;
1296 }
1297#endif
1298
Bram Moolenaar071d4272004-06-13 20:20:40 +00001299 /*
1300 * If we guessed the wrong page size, we have to recalculate the
1301 * highest block number in the file.
1302 */
1303 if (mfp->mf_page_size != (unsigned)char_to_long(b0p->b0_page_size))
1304 {
Bram Moolenaar1c536282007-04-26 15:21:56 +00001305 unsigned previous_page_size = mfp->mf_page_size;
1306
Bram Moolenaar071d4272004-06-13 20:20:40 +00001307 mf_new_page_size(mfp, (unsigned)char_to_long(b0p->b0_page_size));
Bram Moolenaar1c536282007-04-26 15:21:56 +00001308 if (mfp->mf_page_size < previous_page_size)
1309 {
1310 msg_start();
1311 msg_outtrans_attr(mfp->mf_fname, attr | MSG_HIST);
Bram Moolenaar32526b32019-01-19 17:43:09 +01001312 msg_puts_attr(_(" has been damaged (page size is smaller than minimum value).\n"),
Bram Moolenaar1c536282007-04-26 15:21:56 +00001313 attr | MSG_HIST);
1314 msg_end();
1315 goto theend;
1316 }
Bram Moolenaar8767f522016-07-01 17:17:39 +02001317 if ((size = vim_lseek(mfp->mf_fd, (off_T)0L, SEEK_END)) <= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001318 mfp->mf_blocknr_max = 0; /* no file or empty file */
1319 else
1320 mfp->mf_blocknr_max = (blocknr_T)(size / mfp->mf_page_size);
1321 mfp->mf_infile_count = mfp->mf_blocknr_max;
Bram Moolenaar1c536282007-04-26 15:21:56 +00001322
1323 /* need to reallocate the memory used to store the data */
1324 p = alloc(mfp->mf_page_size);
1325 if (p == NULL)
1326 goto theend;
1327 mch_memmove(p, hp->bh_data, previous_page_size);
1328 vim_free(hp->bh_data);
1329 hp->bh_data = p;
1330 b0p = (ZERO_BL *)(hp->bh_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001331 }
1332
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02001333 /*
1334 * If .swp file name given directly, use name from swap file for buffer.
1335 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001336 if (directly)
1337 {
1338 expand_env(b0p->b0_fname, NameBuff, MAXPATHL);
1339 if (setfname(curbuf, NameBuff, NULL, TRUE) == FAIL)
1340 goto theend;
1341 }
1342
1343 home_replace(NULL, mfp->mf_fname, NameBuff, MAXPATHL, TRUE);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001344 smsg(_("Using swap file \"%s\""), NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001345
1346 if (buf_spname(curbuf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02001347 vim_strncpy(NameBuff, buf_spname(curbuf), MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001348 else
1349 home_replace(NULL, curbuf->b_ffname, NameBuff, MAXPATHL, TRUE);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001350 smsg(_("Original file \"%s\""), NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001351 msg_putchar('\n');
1352
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02001353 /*
1354 * check date of swap file and original file
1355 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001356 mtime = char_to_long(b0p->b0_mtime);
1357 if (curbuf->b_ffname != NULL
1358 && mch_stat((char *)curbuf->b_ffname, &org_stat) != -1
1359 && ((mch_stat((char *)mfp->mf_fname, &swp_stat) != -1
1360 && org_stat.st_mtime > swp_stat.st_mtime)
1361 || org_stat.st_mtime != mtime))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001362 emsg(_("E308: Warning: Original file may have been changed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001363 out_flush();
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001364
1365 /* Get the 'fileformat' and 'fileencoding' from block zero. */
1366 b0_ff = (b0p->b0_flags & B0_FF_MASK);
1367 if (b0p->b0_flags & B0_HAS_FENC)
1368 {
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02001369 int fnsize = B0_FNAME_SIZE_NOCRYPT;
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02001370
1371#ifdef FEAT_CRYPT
1372 /* Use the same size as in add_b0_fenc(). */
1373 if (b0p->b0_id[1] != BLOCK0_ID1)
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02001374 fnsize = B0_FNAME_SIZE_CRYPT;
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02001375#endif
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02001376 for (p = b0p->b0_fname + fnsize; p > b0p->b0_fname && p[-1] != NUL; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001377 ;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02001378 b0_fenc = vim_strnsave(p, (int)(b0p->b0_fname + fnsize - p));
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001379 }
1380
Bram Moolenaar071d4272004-06-13 20:20:40 +00001381 mf_put(mfp, hp, FALSE, FALSE); /* release block 0 */
1382 hp = NULL;
1383
1384 /*
1385 * Now that we are sure that the file is going to be recovered, clear the
1386 * contents of the current buffer.
1387 */
1388 while (!(curbuf->b_ml.ml_flags & ML_EMPTY))
1389 ml_delete((linenr_T)1, FALSE);
1390
1391 /*
1392 * Try reading the original file to obtain the values of 'fileformat',
1393 * 'fileencoding', etc. Ignore errors. The text itself is not used.
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02001394 * When the file is encrypted the user is asked to enter the key.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001395 */
1396 if (curbuf->b_ffname != NULL)
Bram Moolenaarfc2d5bd2010-05-15 17:06:53 +02001397 orig_file_status = readfile(curbuf->b_ffname, NULL, (linenr_T)0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001398 (linenr_T)0, (linenr_T)MAXLNUM, NULL, READ_NEW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001399
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02001400#ifdef FEAT_CRYPT
1401 if (b0_cm >= 0)
1402 {
1403 /* Need to ask the user for the crypt key. If this fails we continue
1404 * without a key, will probably get garbage text. */
1405 if (*curbuf->b_p_key != NUL)
1406 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001407 smsg(_("Swap file is encrypted: \"%s\""), fname_used);
Bram Moolenaar32526b32019-01-19 17:43:09 +01001408 msg_puts(_("\nIf you entered a new crypt key but did not write the text file,"));
1409 msg_puts(_("\nenter the new crypt key."));
1410 msg_puts(_("\nIf you wrote the text file after changing the crypt key press enter"));
1411 msg_puts(_("\nto use the same key for text file and swap file"));
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02001412 }
1413 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001414 smsg(_(need_key_msg), fname_used);
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001415 buf->b_p_key = crypt_get_key(FALSE, FALSE);
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02001416 if (buf->b_p_key == NULL)
1417 buf->b_p_key = curbuf->b_p_key;
1418 else if (*buf->b_p_key == NUL)
1419 {
1420 vim_free(buf->b_p_key);
1421 buf->b_p_key = curbuf->b_p_key;
1422 }
1423 if (buf->b_p_key == NULL)
1424 buf->b_p_key = empty_option;
1425 }
1426#endif
1427
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001428 /* Use the 'fileformat' and 'fileencoding' as stored in the swap file. */
1429 if (b0_ff != 0)
1430 set_fileformat(b0_ff - 1, OPT_LOCAL);
1431 if (b0_fenc != NULL)
1432 {
1433 set_option_value((char_u *)"fenc", 0L, b0_fenc, OPT_LOCAL);
1434 vim_free(b0_fenc);
1435 }
1436 unchanged(curbuf, TRUE);
1437
Bram Moolenaar071d4272004-06-13 20:20:40 +00001438 bnum = 1; /* start with block 1 */
1439 page_count = 1; /* which is 1 page */
1440 lnum = 0; /* append after line 0 in curbuf */
1441 line_count = 0;
1442 idx = 0; /* start with first index in block 1 */
1443 error = 0;
1444 buf->b_ml.ml_stack_top = 0;
1445 buf->b_ml.ml_stack = NULL;
1446 buf->b_ml.ml_stack_size = 0; /* no stack yet */
1447
1448 if (curbuf->b_ffname == NULL)
1449 cannot_open = TRUE;
1450 else
1451 cannot_open = FALSE;
1452
1453 serious_error = FALSE;
1454 for ( ; !got_int; line_breakcheck())
1455 {
1456 if (hp != NULL)
1457 mf_put(mfp, hp, FALSE, FALSE); /* release previous block */
1458
1459 /*
1460 * get block
1461 */
1462 if ((hp = mf_get(mfp, (blocknr_T)bnum, page_count)) == NULL)
1463 {
1464 if (bnum == 1)
1465 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001466 semsg(_("E309: Unable to read block 1 from %s"), mfp->mf_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001467 goto theend;
1468 }
1469 ++error;
1470 ml_append(lnum++, (char_u *)_("???MANY LINES MISSING"),
1471 (colnr_T)0, TRUE);
1472 }
1473 else /* there is a block */
1474 {
1475 pp = (PTR_BL *)(hp->bh_data);
1476 if (pp->pb_id == PTR_ID) /* it is a pointer block */
1477 {
1478 /* check line count when using pointer block first time */
1479 if (idx == 0 && line_count != 0)
1480 {
1481 for (i = 0; i < (int)pp->pb_count; ++i)
1482 line_count -= pp->pb_pointer[i].pe_line_count;
1483 if (line_count != 0)
1484 {
1485 ++error;
1486 ml_append(lnum++, (char_u *)_("???LINE COUNT WRONG"),
1487 (colnr_T)0, TRUE);
1488 }
1489 }
1490
1491 if (pp->pb_count == 0)
1492 {
1493 ml_append(lnum++, (char_u *)_("???EMPTY BLOCK"),
1494 (colnr_T)0, TRUE);
1495 ++error;
1496 }
1497 else if (idx < (int)pp->pb_count) /* go a block deeper */
1498 {
1499 if (pp->pb_pointer[idx].pe_bnum < 0)
1500 {
1501 /*
1502 * Data block with negative block number.
1503 * Try to read lines from the original file.
1504 * This is slow, but it works.
1505 */
1506 if (!cannot_open)
1507 {
1508 line_count = pp->pb_pointer[idx].pe_line_count;
1509 if (readfile(curbuf->b_ffname, NULL, lnum,
1510 pp->pb_pointer[idx].pe_old_lnum - 1,
Bram Moolenaare13b9af2017-01-13 22:01:02 +01001511 line_count, NULL, 0) != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001512 cannot_open = TRUE;
1513 else
1514 lnum += line_count;
1515 }
1516 if (cannot_open)
1517 {
1518 ++error;
1519 ml_append(lnum++, (char_u *)_("???LINES MISSING"),
1520 (colnr_T)0, TRUE);
1521 }
1522 ++idx; /* get same block again for next index */
1523 continue;
1524 }
1525
1526 /*
1527 * going one block deeper in the tree
1528 */
1529 if ((top = ml_add_stack(buf)) < 0) /* new entry in stack */
1530 {
1531 ++error;
1532 break; /* out of memory */
1533 }
1534 ip = &(buf->b_ml.ml_stack[top]);
1535 ip->ip_bnum = bnum;
1536 ip->ip_index = idx;
1537
1538 bnum = pp->pb_pointer[idx].pe_bnum;
1539 line_count = pp->pb_pointer[idx].pe_line_count;
1540 page_count = pp->pb_pointer[idx].pe_page_count;
Bram Moolenaar986a0032011-06-13 01:07:27 +02001541 idx = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001542 continue;
1543 }
1544 }
1545 else /* not a pointer block */
1546 {
1547 dp = (DATA_BL *)(hp->bh_data);
1548 if (dp->db_id != DATA_ID) /* block id wrong */
1549 {
1550 if (bnum == 1)
1551 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001552 semsg(_("E310: Block 1 ID wrong (%s not a .swp file?)"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001553 mfp->mf_fname);
1554 goto theend;
1555 }
1556 ++error;
1557 ml_append(lnum++, (char_u *)_("???BLOCK MISSING"),
1558 (colnr_T)0, TRUE);
1559 }
1560 else
1561 {
1562 /*
1563 * it is a data block
1564 * Append all the lines in this block
1565 */
1566 has_error = FALSE;
1567 /*
1568 * check length of block
1569 * if wrong, use length in pointer block
1570 */
1571 if (page_count * mfp->mf_page_size != dp->db_txt_end)
1572 {
1573 ml_append(lnum++, (char_u *)_("??? from here until ???END lines may be messed up"),
1574 (colnr_T)0, TRUE);
1575 ++error;
1576 has_error = TRUE;
1577 dp->db_txt_end = page_count * mfp->mf_page_size;
1578 }
1579
1580 /* make sure there is a NUL at the end of the block */
1581 *((char_u *)dp + dp->db_txt_end - 1) = NUL;
1582
1583 /*
1584 * check number of lines in block
1585 * if wrong, use count in data block
1586 */
1587 if (line_count != dp->db_line_count)
1588 {
1589 ml_append(lnum++, (char_u *)_("??? from here until ???END lines may have been inserted/deleted"),
1590 (colnr_T)0, TRUE);
1591 ++error;
1592 has_error = TRUE;
1593 }
1594
1595 for (i = 0; i < dp->db_line_count; ++i)
1596 {
1597 txt_start = (dp->db_index[i] & DB_INDEX_MASK);
Bram Moolenaar740885b2009-11-03 14:33:17 +00001598 if (txt_start <= (int)HEADER_SIZE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001599 || txt_start >= (int)dp->db_txt_end)
1600 {
1601 p = (char_u *)"???";
1602 ++error;
1603 }
1604 else
1605 p = (char_u *)dp + txt_start;
1606 ml_append(lnum++, p, (colnr_T)0, TRUE);
1607 }
1608 if (has_error)
Bram Moolenaar740885b2009-11-03 14:33:17 +00001609 ml_append(lnum++, (char_u *)_("???END"),
1610 (colnr_T)0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001611 }
1612 }
1613 }
1614
1615 if (buf->b_ml.ml_stack_top == 0) /* finished */
1616 break;
1617
1618 /*
1619 * go one block up in the tree
1620 */
1621 ip = &(buf->b_ml.ml_stack[--(buf->b_ml.ml_stack_top)]);
1622 bnum = ip->ip_bnum;
1623 idx = ip->ip_index + 1; /* go to next index */
1624 page_count = 1;
1625 }
1626
1627 /*
Bram Moolenaarfc2d5bd2010-05-15 17:06:53 +02001628 * Compare the buffer contents with the original file. When they differ
1629 * set the 'modified' flag.
1630 * Lines 1 - lnum are the new contents.
1631 * Lines lnum + 1 to ml_line_count are the original contents.
1632 * Line ml_line_count + 1 in the dummy empty line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001633 */
Bram Moolenaarfc2d5bd2010-05-15 17:06:53 +02001634 if (orig_file_status != OK || curbuf->b_ml.ml_line_count != lnum * 2 + 1)
1635 {
1636 /* Recovering an empty file results in two lines and the first line is
1637 * empty. Don't set the modified flag then. */
1638 if (!(curbuf->b_ml.ml_line_count == 2 && *ml_get(1) == NUL))
1639 {
1640 changed_int();
Bram Moolenaar95c526e2017-02-25 14:59:34 +01001641 ++CHANGEDTICK(curbuf);
Bram Moolenaarfc2d5bd2010-05-15 17:06:53 +02001642 }
1643 }
1644 else
1645 {
1646 for (idx = 1; idx <= lnum; ++idx)
1647 {
1648 /* Need to copy one line, fetching the other one may flush it. */
1649 p = vim_strsave(ml_get(idx));
1650 i = STRCMP(p, ml_get(idx + lnum));
1651 vim_free(p);
1652 if (i != 0)
1653 {
1654 changed_int();
Bram Moolenaar95c526e2017-02-25 14:59:34 +01001655 ++CHANGEDTICK(curbuf);
Bram Moolenaarfc2d5bd2010-05-15 17:06:53 +02001656 break;
1657 }
1658 }
1659 }
1660
1661 /*
1662 * Delete the lines from the original file and the dummy line from the
1663 * empty buffer. These will now be after the last line in the buffer.
1664 */
1665 while (curbuf->b_ml.ml_line_count > lnum
1666 && !(curbuf->b_ml.ml_flags & ML_EMPTY))
1667 ml_delete(curbuf->b_ml.ml_line_count, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001668 curbuf->b_flags |= BF_RECOVERED;
1669
1670 recoverymode = FALSE;
1671 if (got_int)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001672 emsg(_("E311: Recovery Interrupted"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001673 else if (error)
1674 {
1675 ++no_wait_return;
Bram Moolenaar32526b32019-01-19 17:43:09 +01001676 msg(">>>>>>>>>>>>>");
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001677 emsg(_("E312: Errors detected while recovering; look for lines starting with ???"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001678 --no_wait_return;
Bram Moolenaar32526b32019-01-19 17:43:09 +01001679 msg(_("See \":help E312\" for more information."));
1680 msg(">>>>>>>>>>>>>");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001681 }
1682 else
1683 {
Bram Moolenaarfc2d5bd2010-05-15 17:06:53 +02001684 if (curbuf->b_changed)
1685 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01001686 msg(_("Recovery completed. You should check if everything is OK."));
1687 msg_puts(_("\n(You might want to write out this file under another name\n"));
1688 msg_puts(_("and run diff with the original file to check for changes)"));
Bram Moolenaarfc2d5bd2010-05-15 17:06:53 +02001689 }
1690 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01001691 msg(_("Recovery completed. Buffer contents equals file contents."));
1692 msg_puts(_("\nYou may want to delete the .swp file now.\n\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001693 cmdline_row = msg_row;
1694 }
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02001695#ifdef FEAT_CRYPT
1696 if (*buf->b_p_key != NUL && STRCMP(curbuf->b_p_key, buf->b_p_key) != 0)
1697 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01001698 msg_puts(_("Using crypt key from swap file for the text file.\n"));
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02001699 set_option_value((char_u *)"key", 0L, buf->b_p_key, OPT_LOCAL);
1700 }
1701#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001702 redraw_curbuf_later(NOT_VALID);
1703
1704theend:
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02001705 vim_free(fname_used);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001706 recoverymode = FALSE;
1707 if (mfp != NULL)
1708 {
1709 if (hp != NULL)
1710 mf_put(mfp, hp, FALSE, FALSE);
1711 mf_close(mfp, FALSE); /* will also vim_free(mfp->mf_fname) */
1712 }
Bram Moolenaardf88dda2007-01-09 13:34:50 +00001713 if (buf != NULL)
1714 {
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02001715#ifdef FEAT_CRYPT
1716 if (buf->b_p_key != curbuf->b_p_key)
1717 free_string_option(buf->b_p_key);
Bram Moolenaar0ad014c2010-07-25 14:00:46 +02001718 free_string_option(buf->b_p_cm);
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02001719#endif
Bram Moolenaardf88dda2007-01-09 13:34:50 +00001720 vim_free(buf->b_ml.ml_stack);
1721 vim_free(buf);
1722 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001723 if (serious_error && called_from_main)
1724 ml_close(curbuf, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001725 else
1726 {
1727 apply_autocmds(EVENT_BUFREADPOST, NULL, curbuf->b_fname, FALSE, curbuf);
1728 apply_autocmds(EVENT_BUFWINENTER, NULL, curbuf->b_fname, FALSE, curbuf);
1729 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001730 return;
1731}
1732
1733/*
1734 * Find the names of swap files in current directory and the directory given
1735 * with the 'directory' option.
1736 *
1737 * Used to:
1738 * - list the swap files for "vim -r"
1739 * - count the number of swap files when recovering
1740 * - list the swap files when recovering
1741 * - find the name of the n'th swap file when recovering
1742 */
1743 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001744recover_names(
1745 char_u *fname, /* base for swap file name */
1746 int list, /* when TRUE, list the swap file names */
1747 int nr, /* when non-zero, return nr'th swap file name */
1748 char_u **fname_out) /* result when "nr" > 0 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001749{
1750 int num_names;
1751 char_u *(names[6]);
1752 char_u *tail;
1753 char_u *p;
1754 int num_files;
1755 int file_count = 0;
1756 char_u **files;
1757 int i;
1758 char_u *dirp;
1759 char_u *dir_name;
Bram Moolenaar64354da2010-05-25 21:37:17 +02001760 char_u *fname_res = NULL;
Bram Moolenaar9dbe4752010-05-14 17:52:42 +02001761#ifdef HAVE_READLINK
1762 char_u fname_buf[MAXPATHL];
Bram Moolenaar64354da2010-05-25 21:37:17 +02001763#endif
Bram Moolenaar9dbe4752010-05-14 17:52:42 +02001764
Bram Moolenaar64354da2010-05-25 21:37:17 +02001765 if (fname != NULL)
1766 {
1767#ifdef HAVE_READLINK
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02001768 /* Expand symlink in the file name, because the swap file is created
1769 * with the actual file instead of with the symlink. */
1770 if (resolve_symlink(fname, fname_buf) == OK)
1771 fname_res = fname_buf;
1772 else
Bram Moolenaar9dbe4752010-05-14 17:52:42 +02001773#endif
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02001774 fname_res = fname;
Bram Moolenaar64354da2010-05-25 21:37:17 +02001775 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001776
1777 if (list)
1778 {
Bram Moolenaar9dbe4752010-05-14 17:52:42 +02001779 /* use msg() to start the scrolling properly */
Bram Moolenaar32526b32019-01-19 17:43:09 +01001780 msg(_("Swap files found:"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001781 msg_putchar('\n');
1782 }
1783
1784 /*
1785 * Do the loop for every directory in 'directory'.
1786 * First allocate some memory to put the directory name in.
1787 */
1788 dir_name = alloc((unsigned)STRLEN(p_dir) + 1);
1789 dirp = p_dir;
1790 while (dir_name != NULL && *dirp)
1791 {
1792 /*
1793 * Isolate a directory name from *dirp and put it in dir_name (we know
1794 * it is large enough, so use 31000 for length).
1795 * Advance dirp to next directory name.
1796 */
1797 (void)copy_option_part(&dirp, dir_name, 31000, ",");
1798
1799 if (dir_name[0] == '.' && dir_name[1] == NUL) /* check current dir */
1800 {
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02001801 if (fname == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001802 {
1803#ifdef VMS
1804 names[0] = vim_strsave((char_u *)"*_sw%");
1805#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001806 names[0] = vim_strsave((char_u *)"*.sw?");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001807#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01001808#if defined(UNIX) || defined(MSWIN)
Bram Moolenaar2cc93182006-10-10 19:56:03 +00001809 /* For Unix names starting with a dot are special. MS-Windows
1810 * supports this too, on some file systems. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001811 names[1] = vim_strsave((char_u *)".*.sw?");
1812 names[2] = vim_strsave((char_u *)".sw?");
1813 num_names = 3;
1814#else
1815# ifdef VMS
1816 names[1] = vim_strsave((char_u *)".*_sw%");
1817 num_names = 2;
1818# else
1819 num_names = 1;
1820# endif
1821#endif
1822 }
1823 else
Bram Moolenaar9dbe4752010-05-14 17:52:42 +02001824 num_names = recov_file_names(names, fname_res, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001825 }
1826 else /* check directory dir_name */
1827 {
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02001828 if (fname == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001829 {
1830#ifdef VMS
1831 names[0] = concat_fnames(dir_name, (char_u *)"*_sw%", TRUE);
1832#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001833 names[0] = concat_fnames(dir_name, (char_u *)"*.sw?", TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001834#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01001835#if defined(UNIX) || defined(MSWIN)
Bram Moolenaar2cc93182006-10-10 19:56:03 +00001836 /* For Unix names starting with a dot are special. MS-Windows
1837 * supports this too, on some file systems. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001838 names[1] = concat_fnames(dir_name, (char_u *)".*.sw?", TRUE);
1839 names[2] = concat_fnames(dir_name, (char_u *)".sw?", TRUE);
1840 num_names = 3;
1841#else
1842# ifdef VMS
1843 names[1] = concat_fnames(dir_name, (char_u *)".*_sw%", TRUE);
1844 num_names = 2;
1845# else
1846 num_names = 1;
1847# endif
1848#endif
1849 }
1850 else
1851 {
Bram Moolenaar4f974752019-02-17 17:44:42 +01001852#if defined(UNIX) || defined(MSWIN)
Bram Moolenaarb113c3a2017-02-28 21:26:17 +01001853 int len = (int)STRLEN(dir_name);
Bram Moolenaarc525e3a2017-02-18 16:59:02 +01001854
1855 p = dir_name + len;
1856 if (after_pathsep(dir_name, p) && len > 1 && p[-1] == p[-2])
Bram Moolenaar071d4272004-06-13 20:20:40 +00001857 {
1858 /* Ends with '//', Use Full path for swap name */
Bram Moolenaar9dbe4752010-05-14 17:52:42 +02001859 tail = make_percent_swname(dir_name, fname_res);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001860 }
1861 else
1862#endif
1863 {
Bram Moolenaar9dbe4752010-05-14 17:52:42 +02001864 tail = gettail(fname_res);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001865 tail = concat_fnames(dir_name, tail, TRUE);
1866 }
1867 if (tail == NULL)
1868 num_names = 0;
1869 else
1870 {
1871 num_names = recov_file_names(names, tail, FALSE);
1872 vim_free(tail);
1873 }
1874 }
1875 }
1876
1877 /* check for out-of-memory */
1878 for (i = 0; i < num_names; ++i)
1879 {
1880 if (names[i] == NULL)
1881 {
1882 for (i = 0; i < num_names; ++i)
1883 vim_free(names[i]);
1884 num_names = 0;
1885 }
1886 }
1887 if (num_names == 0)
1888 num_files = 0;
1889 else if (expand_wildcards(num_names, names, &num_files, &files,
1890 EW_KEEPALL|EW_FILE|EW_SILENT) == FAIL)
1891 num_files = 0;
1892
1893 /*
1894 * When no swap file found, wildcard expansion might have failed (e.g.
1895 * not able to execute the shell).
1896 * Try finding a swap file by simply adding ".swp" to the file name.
1897 */
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02001898 if (*dirp == NUL && file_count + num_files == 0 && fname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001899 {
Bram Moolenaar8767f522016-07-01 17:17:39 +02001900 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001901 char_u *swapname;
1902
Bram Moolenaar9dbe4752010-05-14 17:52:42 +02001903 swapname = modname(fname_res,
Bram Moolenaare60acc12011-05-10 16:41:25 +02001904#if defined(VMS)
Bram Moolenaar9dbe4752010-05-14 17:52:42 +02001905 (char_u *)"_swp", FALSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001906#else
Bram Moolenaar9dbe4752010-05-14 17:52:42 +02001907 (char_u *)".swp", TRUE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001908#endif
Bram Moolenaar9dbe4752010-05-14 17:52:42 +02001909 );
Bram Moolenaar071d4272004-06-13 20:20:40 +00001910 if (swapname != NULL)
1911 {
1912 if (mch_stat((char *)swapname, &st) != -1) /* It exists! */
1913 {
1914 files = (char_u **)alloc((unsigned)sizeof(char_u *));
1915 if (files != NULL)
1916 {
1917 files[0] = swapname;
1918 swapname = NULL;
1919 num_files = 1;
1920 }
1921 }
1922 vim_free(swapname);
1923 }
1924 }
1925
1926 /*
1927 * remove swapfile name of the current buffer, it must be ignored
1928 */
1929 if (curbuf->b_ml.ml_mfp != NULL
1930 && (p = curbuf->b_ml.ml_mfp->mf_fname) != NULL)
1931 {
1932 for (i = 0; i < num_files; ++i)
1933 if (fullpathcmp(p, files[i], TRUE) & FPC_SAME)
1934 {
Bram Moolenaar9439cdd2009-04-22 13:39:36 +00001935 /* Remove the name from files[i]. Move further entries
1936 * down. When the array becomes empty free it here, since
1937 * FreeWild() won't be called below. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001938 vim_free(files[i]);
Bram Moolenaar9439cdd2009-04-22 13:39:36 +00001939 if (--num_files == 0)
1940 vim_free(files);
1941 else
1942 for ( ; i < num_files; ++i)
1943 files[i] = files[i + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001944 }
1945 }
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00001946 if (nr > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001947 {
1948 file_count += num_files;
1949 if (nr <= file_count)
1950 {
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02001951 *fname_out = vim_strsave(
1952 files[nr - 1 + num_files - file_count]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001953 dirp = (char_u *)""; /* stop searching */
1954 }
1955 }
1956 else if (list)
1957 {
1958 if (dir_name[0] == '.' && dir_name[1] == NUL)
1959 {
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02001960 if (fname == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001961 msg_puts(_(" In current directory:\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001962 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01001963 msg_puts(_(" Using specified name:\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001964 }
1965 else
1966 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01001967 msg_puts(_(" In directory "));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001968 msg_home_replace(dir_name);
Bram Moolenaar32526b32019-01-19 17:43:09 +01001969 msg_puts(":\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001970 }
1971
1972 if (num_files)
1973 {
1974 for (i = 0; i < num_files; ++i)
1975 {
1976 /* print the swap file name */
1977 msg_outnum((long)++file_count);
Bram Moolenaar32526b32019-01-19 17:43:09 +01001978 msg_puts(". ");
1979 msg_puts((char *)gettail(files[i]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001980 msg_putchar('\n');
1981 (void)swapfile_info(files[i]);
1982 }
1983 }
1984 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01001985 msg_puts(_(" -- none --\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001986 out_flush();
1987 }
1988 else
1989 file_count += num_files;
1990
1991 for (i = 0; i < num_names; ++i)
1992 vim_free(names[i]);
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00001993 if (num_files > 0)
1994 FreeWild(num_files, files);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001995 }
1996 vim_free(dir_name);
1997 return file_count;
1998}
1999
Bram Moolenaar4f974752019-02-17 17:44:42 +01002000#if defined(UNIX) || defined(MSWIN) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002001/*
Bram Moolenaarb782ba42018-08-07 21:39:28 +02002002 * Need _very_ long file names.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002003 * Append the full path to name with path separators made into percent
2004 * signs, to dir. An unnamed buffer is handled as "" (<currentdir>/"")
2005 */
Bram Moolenaarb782ba42018-08-07 21:39:28 +02002006 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002007make_percent_swname(char_u *dir, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002008{
Bram Moolenaarb782ba42018-08-07 21:39:28 +02002009 char_u *d = NULL, *s, *f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002010
Bram Moolenaarb782ba42018-08-07 21:39:28 +02002011 f = fix_fname(name != NULL ? name : (char_u *)"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002012 if (f != NULL)
2013 {
2014 s = alloc((unsigned)(STRLEN(f) + 1));
2015 if (s != NULL)
2016 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002017 STRCPY(s, f);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002018 for (d = s; *d != NUL; MB_PTR_ADV(d))
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002019 if (vim_ispathsep(*d))
2020 *d = '%';
Bram Moolenaar071d4272004-06-13 20:20:40 +00002021 d = concat_fnames(dir, s, TRUE);
2022 vim_free(s);
2023 }
2024 vim_free(f);
2025 }
2026 return d;
2027}
2028#endif
2029
Bram Moolenaar1b243ea2019-04-28 22:50:40 +02002030#if (defined(UNIX) || defined(VMS) || defined(MSWIN)) \
2031 && (defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG))
2032# define HAVE_PROCESS_STILL_RUNNING
Bram Moolenaar071d4272004-06-13 20:20:40 +00002033static int process_still_running;
2034#endif
2035
Bram Moolenaar47ad5652018-08-21 21:09:07 +02002036#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002037/*
Bram Moolenaar00f123a2018-08-21 20:28:54 +02002038 * Return information found in swapfile "fname" in dictionary "d".
2039 * This is used by the swapinfo() function.
2040 */
2041 void
2042get_b0_dict(char_u *fname, dict_T *d)
2043{
2044 int fd;
2045 struct block0 b0;
2046
2047 if ((fd = mch_open((char *)fname, O_RDONLY | O_EXTRA, 0)) >= 0)
2048 {
2049 if (read_eintr(fd, &b0, sizeof(b0)) == sizeof(b0))
2050 {
Bram Moolenaar47ad5652018-08-21 21:09:07 +02002051 if (ml_check_b0_id(&b0) == FAIL)
Bram Moolenaare6fdf792018-12-26 22:57:42 +01002052 dict_add_string(d, "error", (char_u *)"Not a swap file");
Bram Moolenaar47ad5652018-08-21 21:09:07 +02002053 else if (b0_magic_wrong(&b0))
Bram Moolenaare6fdf792018-12-26 22:57:42 +01002054 dict_add_string(d, "error", (char_u *)"Magic number mismatch");
Bram Moolenaar00f123a2018-08-21 20:28:54 +02002055 else
2056 {
2057 /* we have swap information */
Bram Moolenaare6fdf792018-12-26 22:57:42 +01002058 dict_add_string_len(d, "version", b0.b0_version, 10);
2059 dict_add_string_len(d, "user", b0.b0_uname, B0_UNAME_SIZE);
2060 dict_add_string_len(d, "host", b0.b0_hname, B0_HNAME_SIZE);
2061 dict_add_string_len(d, "fname", b0.b0_fname, B0_FNAME_SIZE_ORG);
Bram Moolenaar00f123a2018-08-21 20:28:54 +02002062
2063 dict_add_number(d, "pid", char_to_long(b0.b0_pid));
2064 dict_add_number(d, "mtime", char_to_long(b0.b0_mtime));
Bram Moolenaar47ad5652018-08-21 21:09:07 +02002065 dict_add_number(d, "dirty", b0.b0_dirty ? 1 : 0);
2066# ifdef CHECK_INODE
Bram Moolenaar00f123a2018-08-21 20:28:54 +02002067 dict_add_number(d, "inode", char_to_long(b0.b0_ino));
Bram Moolenaar47ad5652018-08-21 21:09:07 +02002068# endif
Bram Moolenaar00f123a2018-08-21 20:28:54 +02002069 }
2070 }
2071 else
Bram Moolenaare6fdf792018-12-26 22:57:42 +01002072 dict_add_string(d, "error", (char_u *)"Cannot read file");
Bram Moolenaar00f123a2018-08-21 20:28:54 +02002073 close(fd);
2074 }
2075 else
Bram Moolenaare6fdf792018-12-26 22:57:42 +01002076 dict_add_string(d, "error", (char_u *)"Cannot open file");
Bram Moolenaar00f123a2018-08-21 20:28:54 +02002077}
Bram Moolenaar47ad5652018-08-21 21:09:07 +02002078#endif
Bram Moolenaar00f123a2018-08-21 20:28:54 +02002079
2080/*
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002081 * Give information about an existing swap file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002082 * Returns timestamp (0 when unknown).
2083 */
2084 static time_t
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002085swapfile_info(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002086{
Bram Moolenaar8767f522016-07-01 17:17:39 +02002087 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002088 int fd;
2089 struct block0 b0;
2090 time_t x = (time_t)0;
Bram Moolenaar31e97bf2006-10-10 14:20:13 +00002091 char *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002092#ifdef UNIX
2093 char_u uname[B0_UNAME_SIZE];
2094#endif
2095
2096 /* print the swap file date */
2097 if (mch_stat((char *)fname, &st) != -1)
2098 {
2099#ifdef UNIX
2100 /* print name of owner of the file */
2101 if (mch_get_uname(st.st_uid, uname, B0_UNAME_SIZE) == OK)
2102 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01002103 msg_puts(_(" owned by: "));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002104 msg_outtrans(uname);
Bram Moolenaar32526b32019-01-19 17:43:09 +01002105 msg_puts(_(" dated: "));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002106 }
2107 else
2108#endif
Bram Moolenaar32526b32019-01-19 17:43:09 +01002109 msg_puts(_(" dated: "));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002110 x = st.st_mtime; /* Manx C can't do &st.st_mtime */
Bram Moolenaar31e97bf2006-10-10 14:20:13 +00002111 p = ctime(&x); /* includes '\n' */
2112 if (p == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002113 msg_puts("(invalid)\n");
Bram Moolenaar31e97bf2006-10-10 14:20:13 +00002114 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01002115 msg_puts(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002116 }
2117
2118 /*
2119 * print the original file name
2120 */
2121 fd = mch_open((char *)fname, O_RDONLY | O_EXTRA, 0);
2122 if (fd >= 0)
2123 {
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01002124 if (read_eintr(fd, &b0, sizeof(b0)) == sizeof(b0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002125 {
2126 if (STRNCMP(b0.b0_version, "VIM 3.0", 7) == 0)
2127 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01002128 msg_puts(_(" [from Vim version 3.0]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002129 }
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02002130 else if (ml_check_b0_id(&b0) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002131 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01002132 msg_puts(_(" [does not look like a Vim swap file]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002133 }
2134 else
2135 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01002136 msg_puts(_(" file name: "));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002137 if (b0.b0_fname[0] == NUL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002138 msg_puts(_("[No Name]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002139 else
2140 msg_outtrans(b0.b0_fname);
2141
Bram Moolenaar32526b32019-01-19 17:43:09 +01002142 msg_puts(_("\n modified: "));
2143 msg_puts(b0.b0_dirty ? _("YES") : _("no"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002144
2145 if (*(b0.b0_uname) != NUL)
2146 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01002147 msg_puts(_("\n user name: "));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002148 msg_outtrans(b0.b0_uname);
2149 }
2150
2151 if (*(b0.b0_hname) != NUL)
2152 {
2153 if (*(b0.b0_uname) != NUL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002154 msg_puts(_(" host name: "));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002155 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01002156 msg_puts(_("\n host name: "));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002157 msg_outtrans(b0.b0_hname);
2158 }
2159
2160 if (char_to_long(b0.b0_pid) != 0L)
2161 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01002162 msg_puts(_("\n process ID: "));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002163 msg_outnum(char_to_long(b0.b0_pid));
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02002164#if defined(UNIX) || defined(MSWIN)
Bram Moolenaar1b243ea2019-04-28 22:50:40 +02002165 if (mch_process_running(char_to_long(b0.b0_pid)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002166 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01002167 msg_puts(_(" (STILL RUNNING)"));
Bram Moolenaar1b243ea2019-04-28 22:50:40 +02002168# ifdef HAVE_PROCESS_STILL_RUNNING
Bram Moolenaar071d4272004-06-13 20:20:40 +00002169 process_still_running = TRUE;
2170# endif
2171 }
2172#endif
2173 }
2174
2175 if (b0_magic_wrong(&b0))
2176 {
Bram Moolenaar48e330a2016-02-23 14:53:34 +01002177#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002178 if (STRNCMP(b0.b0_hname, "PC ", 3) == 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002179 msg_puts(_("\n [not usable with this version of Vim]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002180 else
2181#endif
Bram Moolenaar32526b32019-01-19 17:43:09 +01002182 msg_puts(_("\n [not usable on this computer]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002183 }
2184 }
2185 }
2186 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01002187 msg_puts(_(" [cannot be read]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002188 close(fd);
2189 }
2190 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01002191 msg_puts(_(" [cannot be opened]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002192 msg_putchar('\n');
2193
2194 return x;
2195}
2196
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02002197/*
2198 * Return TRUE if the swap file looks OK and there are no changes, thus it can
2199 * be safely deleted.
2200 */
2201 static time_t
2202swapfile_unchanged(char_u *fname)
2203{
2204 stat_T st;
2205 int fd;
2206 struct block0 b0;
2207 int ret = TRUE;
Bram Moolenaar1b243ea2019-04-28 22:50:40 +02002208#if defined(UNIX) || defined(MSWIN)
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02002209 long pid;
2210#endif
2211
2212 // must be able to stat the swap file
2213 if (mch_stat((char *)fname, &st) == -1)
2214 return FALSE;
2215
2216 // must be able to read the first block
2217 fd = mch_open((char *)fname, O_RDONLY | O_EXTRA, 0);
2218 if (fd < 0)
2219 return FALSE;
2220 if (read_eintr(fd, &b0, sizeof(b0)) != sizeof(b0))
2221 {
2222 close(fd);
2223 return FALSE;
2224 }
2225
2226 // the ID and magic number must be correct
2227 if (ml_check_b0_id(&b0) == FAIL|| b0_magic_wrong(&b0))
2228 ret = FALSE;
2229
2230 // must be unchanged
2231 if (b0.b0_dirty)
2232 ret = FALSE;
2233
2234#if defined(UNIX) || defined(MSWIN)
2235 // process must known and not be running
2236 pid = char_to_long(b0.b0_pid);
Bram Moolenaar1b243ea2019-04-28 22:50:40 +02002237 if (pid == 0L || mch_process_running(pid))
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02002238 ret = FALSE;
2239#endif
2240
2241 // TODO: Should we check if the swap file was created on the current
2242 // system? And the current user?
2243
2244 close(fd);
2245 return ret;
2246}
2247
Bram Moolenaar071d4272004-06-13 20:20:40 +00002248 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002249recov_file_names(char_u **names, char_u *path, int prepend_dot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002250{
2251 int num_names;
2252
Bram Moolenaar071d4272004-06-13 20:20:40 +00002253 /*
2254 * (Win32 and Win64) never short names, but do prepend a dot.
2255 * (Not MS-DOS or Win32 or Win64) maybe short name, maybe not: Try both.
2256 * Only use the short name if it is different.
2257 */
2258 char_u *p;
2259 int i;
Bram Moolenaar4f974752019-02-17 17:44:42 +01002260# ifndef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00002261 int shortname = curbuf->b_shortname;
2262
2263 curbuf->b_shortname = FALSE;
2264# endif
2265
2266 num_names = 0;
2267
2268 /*
2269 * May also add the file name with a dot prepended, for swap file in same
2270 * dir as original file.
2271 */
2272 if (prepend_dot)
2273 {
2274 names[num_names] = modname(path, (char_u *)".sw?", TRUE);
2275 if (names[num_names] == NULL)
2276 goto end;
2277 ++num_names;
2278 }
2279
2280 /*
2281 * Form the normal swap file name pattern by appending ".sw?".
2282 */
2283#ifdef VMS
2284 names[num_names] = concat_fnames(path, (char_u *)"_sw%", FALSE);
2285#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002286 names[num_names] = concat_fnames(path, (char_u *)".sw?", FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002287#endif
2288 if (names[num_names] == NULL)
2289 goto end;
2290 if (num_names >= 1) /* check if we have the same name twice */
2291 {
2292 p = names[num_names - 1];
2293 i = (int)STRLEN(names[num_names - 1]) - (int)STRLEN(names[num_names]);
2294 if (i > 0)
2295 p += i; /* file name has been expanded to full path */
2296
2297 if (STRCMP(p, names[num_names]) != 0)
2298 ++num_names;
2299 else
2300 vim_free(names[num_names]);
2301 }
2302 else
2303 ++num_names;
2304
Bram Moolenaar4f974752019-02-17 17:44:42 +01002305# ifndef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00002306 /*
2307 * Also try with 'shortname' set, in case the file is on a DOS filesystem.
2308 */
2309 curbuf->b_shortname = TRUE;
2310#ifdef VMS
2311 names[num_names] = modname(path, (char_u *)"_sw%", FALSE);
2312#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002313 names[num_names] = modname(path, (char_u *)".sw?", FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002314#endif
2315 if (names[num_names] == NULL)
2316 goto end;
2317
2318 /*
2319 * Remove the one from 'shortname', if it's the same as with 'noshortname'.
2320 */
2321 p = names[num_names];
2322 i = STRLEN(names[num_names]) - STRLEN(names[num_names - 1]);
2323 if (i > 0)
2324 p += i; /* file name has been expanded to full path */
2325 if (STRCMP(names[num_names - 1], p) == 0)
2326 vim_free(names[num_names]);
2327 else
2328 ++num_names;
2329# endif
2330
2331end:
Bram Moolenaar4f974752019-02-17 17:44:42 +01002332# ifndef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00002333 curbuf->b_shortname = shortname;
2334# endif
2335
Bram Moolenaar071d4272004-06-13 20:20:40 +00002336 return num_names;
2337}
2338
2339/*
2340 * sync all memlines
2341 *
2342 * If 'check_file' is TRUE, check if original file exists and was not changed.
2343 * If 'check_char' is TRUE, stop syncing when character becomes available, but
2344 * always sync at least one block.
2345 */
2346 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002347ml_sync_all(int check_file, int check_char)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002348{
2349 buf_T *buf;
Bram Moolenaar8767f522016-07-01 17:17:39 +02002350 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002351
Bram Moolenaar29323592016-07-24 22:04:11 +02002352 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002353 {
2354 if (buf->b_ml.ml_mfp == NULL || buf->b_ml.ml_mfp->mf_fname == NULL)
2355 continue; /* no file */
2356
2357 ml_flush_line(buf); /* flush buffered line */
2358 /* flush locked block */
2359 (void)ml_find_line(buf, (linenr_T)0, ML_FLUSH);
2360 if (bufIsChanged(buf) && check_file && mf_need_trans(buf->b_ml.ml_mfp)
2361 && buf->b_ffname != NULL)
2362 {
2363 /*
2364 * If the original file does not exist anymore or has been changed
2365 * call ml_preserve() to get rid of all negative numbered blocks.
2366 */
2367 if (mch_stat((char *)buf->b_ffname, &st) == -1
2368 || st.st_mtime != buf->b_mtime_read
Bram Moolenaar914703b2010-05-31 21:59:46 +02002369 || st.st_size != buf->b_orig_size)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002370 {
2371 ml_preserve(buf, FALSE);
2372 did_check_timestamps = FALSE;
2373 need_check_timestamps = TRUE; /* give message later */
2374 }
2375 }
2376 if (buf->b_ml.ml_mfp->mf_dirty)
2377 {
2378 (void)mf_sync(buf->b_ml.ml_mfp, (check_char ? MFS_STOP : 0)
2379 | (bufIsChanged(buf) ? MFS_FLUSH : 0));
2380 if (check_char && ui_char_avail()) /* character available now */
2381 break;
2382 }
2383 }
2384}
2385
2386/*
2387 * sync one buffer, including negative blocks
2388 *
2389 * after this all the blocks are in the swap file
2390 *
2391 * Used for the :preserve command and when the original file has been
2392 * changed or deleted.
2393 *
2394 * when message is TRUE the success of preserving is reported
2395 */
2396 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002397ml_preserve(buf_T *buf, int message)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002398{
2399 bhdr_T *hp;
2400 linenr_T lnum;
2401 memfile_T *mfp = buf->b_ml.ml_mfp;
2402 int status;
2403 int got_int_save = got_int;
2404
2405 if (mfp == NULL || mfp->mf_fname == NULL)
2406 {
2407 if (message)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002408 emsg(_("E313: Cannot preserve, there is no swap file"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002409 return;
2410 }
2411
2412 /* We only want to stop when interrupted here, not when interrupted
2413 * before. */
2414 got_int = FALSE;
2415
2416 ml_flush_line(buf); /* flush buffered line */
2417 (void)ml_find_line(buf, (linenr_T)0, ML_FLUSH); /* flush locked block */
2418 status = mf_sync(mfp, MFS_ALL | MFS_FLUSH);
2419
2420 /* stack is invalid after mf_sync(.., MFS_ALL) */
2421 buf->b_ml.ml_stack_top = 0;
2422
2423 /*
2424 * Some of the data blocks may have been changed from negative to
2425 * positive block number. In that case the pointer blocks need to be
2426 * updated.
2427 *
2428 * We don't know in which pointer block the references are, so we visit
2429 * all data blocks until there are no more translations to be done (or
2430 * we hit the end of the file, which can only happen in case a write fails,
2431 * e.g. when file system if full).
2432 * ml_find_line() does the work by translating the negative block numbers
2433 * when getting the first line of each data block.
2434 */
2435 if (mf_need_trans(mfp) && !got_int)
2436 {
2437 lnum = 1;
2438 while (mf_need_trans(mfp) && lnum <= buf->b_ml.ml_line_count)
2439 {
2440 hp = ml_find_line(buf, lnum, ML_FIND);
2441 if (hp == NULL)
2442 {
2443 status = FAIL;
2444 goto theend;
2445 }
2446 CHECK(buf->b_ml.ml_locked_low != lnum, "low != lnum");
2447 lnum = buf->b_ml.ml_locked_high + 1;
2448 }
2449 (void)ml_find_line(buf, (linenr_T)0, ML_FLUSH); /* flush locked block */
2450 /* sync the updated pointer blocks */
2451 if (mf_sync(mfp, MFS_ALL | MFS_FLUSH) == FAIL)
2452 status = FAIL;
2453 buf->b_ml.ml_stack_top = 0; /* stack is invalid now */
2454 }
2455theend:
2456 got_int |= got_int_save;
2457
2458 if (message)
2459 {
2460 if (status == OK)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002461 msg(_("File preserved"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002462 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002463 emsg(_("E314: Preserve failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002464 }
2465}
2466
2467/*
2468 * NOTE: The pointer returned by the ml_get_*() functions only remains valid
2469 * until the next call!
2470 * line1 = ml_get(1);
2471 * line2 = ml_get(2); // line1 is now invalid!
2472 * Make a copy of the line if necessary.
2473 */
2474/*
Bram Moolenaar2e2e13c2010-12-08 13:17:03 +01002475 * Return a pointer to a (read-only copy of a) line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002476 *
2477 * On failure an error message is given and IObuff is returned (to avoid
2478 * having to check for error everywhere).
2479 */
2480 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002481ml_get(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002482{
2483 return ml_get_buf(curbuf, lnum, FALSE);
2484}
2485
2486/*
Bram Moolenaar2e2e13c2010-12-08 13:17:03 +01002487 * Return pointer to position "pos".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002488 */
2489 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002490ml_get_pos(pos_T *pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002491{
2492 return (ml_get_buf(curbuf, pos->lnum, FALSE) + pos->col);
2493}
2494
2495/*
Bram Moolenaar2e2e13c2010-12-08 13:17:03 +01002496 * Return pointer to cursor line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002497 */
2498 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002499ml_get_curline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002500{
2501 return ml_get_buf(curbuf, curwin->w_cursor.lnum, FALSE);
2502}
2503
2504/*
Bram Moolenaar2e2e13c2010-12-08 13:17:03 +01002505 * Return pointer to cursor position.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002506 */
2507 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002508ml_get_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002509{
2510 return (ml_get_buf(curbuf, curwin->w_cursor.lnum, FALSE) +
2511 curwin->w_cursor.col);
2512}
2513
2514/*
Bram Moolenaar2e2e13c2010-12-08 13:17:03 +01002515 * Return a pointer to a line in a specific buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00002516 *
2517 * "will_change": if TRUE mark the buffer dirty (chars in the line will be
2518 * changed)
2519 */
2520 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002521ml_get_buf(
2522 buf_T *buf,
2523 linenr_T lnum,
2524 int will_change) /* line will be changed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002525{
Bram Moolenaarad40f022007-02-13 03:01:39 +00002526 bhdr_T *hp;
2527 DATA_BL *dp;
Bram Moolenaarad40f022007-02-13 03:01:39 +00002528 static int recursive = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002529
2530 if (lnum > buf->b_ml.ml_line_count) /* invalid line number */
2531 {
Bram Moolenaarad40f022007-02-13 03:01:39 +00002532 if (recursive == 0)
2533 {
2534 /* Avoid giving this message for a recursive call, may happen when
2535 * the GUI redraws part of the text. */
2536 ++recursive;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002537 siemsg(_("E315: ml_get: invalid lnum: %ld"), lnum);
Bram Moolenaarad40f022007-02-13 03:01:39 +00002538 --recursive;
2539 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002540errorret:
2541 STRCPY(IObuff, "???");
2542 return IObuff;
2543 }
2544 if (lnum <= 0) /* pretend line 0 is line 1 */
2545 lnum = 1;
2546
2547 if (buf->b_ml.ml_mfp == NULL) /* there are no lines */
2548 return (char_u *)"";
2549
Bram Moolenaar37d619f2010-03-10 14:46:26 +01002550 /*
2551 * See if it is the same line as requested last time.
2552 * Otherwise may need to flush last used line.
2553 * Don't use the last used line when 'swapfile' is reset, need to load all
2554 * blocks.
2555 */
Bram Moolenaar47b8b152007-02-07 02:41:57 +00002556 if (buf->b_ml.ml_line_lnum != lnum || mf_dont_release)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002557 {
Bram Moolenaar98aefe72018-12-13 22:20:09 +01002558 unsigned start, end;
2559 colnr_T len;
2560 int idx;
2561
Bram Moolenaar071d4272004-06-13 20:20:40 +00002562 ml_flush_line(buf);
2563
2564 /*
2565 * Find the data block containing the line.
2566 * This also fills the stack with the blocks from the root to the data
2567 * block and releases any locked block.
2568 */
2569 if ((hp = ml_find_line(buf, lnum, ML_FIND)) == NULL)
2570 {
Bram Moolenaarad40f022007-02-13 03:01:39 +00002571 if (recursive == 0)
2572 {
2573 /* Avoid giving this message for a recursive call, may happen
2574 * when the GUI redraws part of the text. */
2575 ++recursive;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002576 siemsg(_("E316: ml_get: cannot find line %ld"), lnum);
Bram Moolenaarad40f022007-02-13 03:01:39 +00002577 --recursive;
2578 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002579 goto errorret;
2580 }
2581
2582 dp = (DATA_BL *)(hp->bh_data);
2583
Bram Moolenaar98aefe72018-12-13 22:20:09 +01002584 idx = lnum - buf->b_ml.ml_locked_low;
2585 start = ((dp->db_index[idx]) & DB_INDEX_MASK);
2586 // The text ends where the previous line starts. The first line ends
2587 // at the end of the block.
2588 if (idx == 0)
2589 end = dp->db_txt_end;
2590 else
2591 end = ((dp->db_index[idx - 1]) & DB_INDEX_MASK);
2592 len = end - start;
2593
2594 buf->b_ml.ml_line_ptr = (char_u *)dp + start;
2595 buf->b_ml.ml_line_len = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002596 buf->b_ml.ml_line_lnum = lnum;
2597 buf->b_ml.ml_flags &= ~ML_LINE_DIRTY;
2598 }
2599 if (will_change)
2600 buf->b_ml.ml_flags |= (ML_LOCKED_DIRTY | ML_LOCKED_POS);
2601
2602 return buf->b_ml.ml_line_ptr;
2603}
2604
2605/*
2606 * Check if a line that was just obtained by a call to ml_get
2607 * is in allocated memory.
2608 */
2609 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002610ml_line_alloced(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002611{
2612 return (curbuf->b_ml.ml_flags & ML_LINE_DIRTY);
2613}
2614
Bram Moolenaarb56ac042018-12-28 23:22:40 +01002615#ifdef FEAT_TEXT_PROP
2616 static void
2617add_text_props_for_append(
2618 buf_T *buf,
2619 linenr_T lnum,
2620 char_u **line,
2621 int *len,
2622 char_u **tofree)
2623{
2624 int round;
2625 int new_prop_count = 0;
2626 int count;
2627 int n;
2628 char_u *props;
2629 int new_len;
2630 char_u *new_line;
2631 textprop_T prop;
2632
2633 // Make two rounds:
2634 // 1. calculate the extra space needed
2635 // 2. allocate the space and fill it
2636 for (round = 1; round <= 2; ++round)
2637 {
2638 if (round == 2)
2639 {
2640 if (new_prop_count == 0)
2641 return; // nothing to do
2642 new_len = *len + new_prop_count * sizeof(textprop_T);
2643 new_line = alloc((unsigned)new_len);
2644 if (new_line == NULL)
2645 return;
2646 mch_memmove(new_line, *line, *len);
2647 new_prop_count = 0;
2648 }
2649
2650 // Get the line above to find any props that continue in the next
2651 // line.
2652 count = get_text_props(buf, lnum, &props, FALSE);
2653 for (n = 0; n < count; ++n)
2654 {
2655 mch_memmove(&prop, props + n * sizeof(textprop_T), sizeof(textprop_T));
2656 if (prop.tp_flags & TP_FLAG_CONT_NEXT)
2657 {
2658 if (round == 2)
2659 {
2660 prop.tp_flags |= TP_FLAG_CONT_PREV;
2661 prop.tp_col = 1;
2662 prop.tp_len = *len;
2663 mch_memmove(new_line + *len + new_prop_count * sizeof(textprop_T), &prop, sizeof(textprop_T));
2664 }
2665 ++new_prop_count;
2666 }
2667 }
2668 }
2669 *line = new_line;
2670 *tofree = new_line;
2671 *len = new_len;
2672}
2673#endif
2674
Bram Moolenaar071d4272004-06-13 20:20:40 +00002675/*
2676 * Append a line after lnum (may be 0 to insert a line in front of the file).
2677 * "line" does not need to be allocated, but can't be another line in a
2678 * buffer, unlocking may make it invalid.
2679 *
2680 * newfile: TRUE when starting to edit a new file, meaning that pe_old_lnum
2681 * will be set for recovery
2682 * Check: The caller of this function should probably also call
2683 * appended_lines().
2684 *
2685 * return FAIL for failure, OK otherwise
2686 */
2687 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002688ml_append(
2689 linenr_T lnum, /* append after this line (can be 0) */
2690 char_u *line, /* text of the new line */
2691 colnr_T len, /* length of new line, including NUL, or 0 */
2692 int newfile) /* flag, see above */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002693{
2694 /* When starting up, we might still need to create the memfile */
Bram Moolenaar59f931e2010-07-24 20:27:03 +02002695 if (curbuf->b_ml.ml_mfp == NULL && open_buffer(FALSE, NULL, 0) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002696 return FAIL;
2697
2698 if (curbuf->b_ml.ml_line_lnum != 0)
2699 ml_flush_line(curbuf);
2700 return ml_append_int(curbuf, lnum, line, len, newfile, FALSE);
2701}
2702
Bram Moolenaar4033c552017-09-16 20:54:51 +02002703#if defined(FEAT_SPELL) || defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar4770d092006-01-12 23:22:24 +00002704/*
2705 * Like ml_append() but for an arbitrary buffer. The buffer must already have
2706 * a memline.
2707 */
2708 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002709ml_append_buf(
2710 buf_T *buf,
2711 linenr_T lnum, /* append after this line (can be 0) */
2712 char_u *line, /* text of the new line */
2713 colnr_T len, /* length of new line, including NUL, or 0 */
2714 int newfile) /* flag, see above */
Bram Moolenaar4770d092006-01-12 23:22:24 +00002715{
2716 if (buf->b_ml.ml_mfp == NULL)
2717 return FAIL;
2718
2719 if (buf->b_ml.ml_line_lnum != 0)
2720 ml_flush_line(buf);
2721 return ml_append_int(buf, lnum, line, len, newfile, FALSE);
2722}
2723#endif
2724
Bram Moolenaar071d4272004-06-13 20:20:40 +00002725 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002726ml_append_int(
2727 buf_T *buf,
Bram Moolenaar98aefe72018-12-13 22:20:09 +01002728 linenr_T lnum, // append after this line (can be 0)
Bram Moolenaarb56ac042018-12-28 23:22:40 +01002729 char_u *line_arg, // text of the new line
Bram Moolenaar98aefe72018-12-13 22:20:09 +01002730 colnr_T len_arg, // length of line, including NUL, or 0
2731 int newfile, // flag, see above
2732 int mark) // mark the new line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002733{
Bram Moolenaarb56ac042018-12-28 23:22:40 +01002734 char_u *line = line_arg;
2735 colnr_T len = len_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002736 int i;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01002737 int line_count; // number of indexes in current block
Bram Moolenaar071d4272004-06-13 20:20:40 +00002738 int offset;
2739 int from, to;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01002740 int space_needed; // space needed for new line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002741 int page_size;
2742 int page_count;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01002743 int db_idx; // index for lnum in data block
Bram Moolenaar071d4272004-06-13 20:20:40 +00002744 bhdr_T *hp;
2745 memfile_T *mfp;
2746 DATA_BL *dp;
2747 PTR_BL *pp;
2748 infoptr_T *ip;
Bram Moolenaarb56ac042018-12-28 23:22:40 +01002749#ifdef FEAT_TEXT_PROP
2750 char_u *tofree = NULL;
2751#endif
2752 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002753
Bram Moolenaar071d4272004-06-13 20:20:40 +00002754 if (lnum > buf->b_ml.ml_line_count || buf->b_ml.ml_mfp == NULL)
Bram Moolenaarb56ac042018-12-28 23:22:40 +01002755 return FAIL; // lnum out of range
Bram Moolenaar071d4272004-06-13 20:20:40 +00002756
2757 if (lowest_marked && lowest_marked > lnum)
2758 lowest_marked = lnum + 1;
2759
2760 if (len == 0)
Bram Moolenaar98aefe72018-12-13 22:20:09 +01002761 len = (colnr_T)STRLEN(line) + 1; // space needed for the text
Bram Moolenaarb56ac042018-12-28 23:22:40 +01002762
2763#ifdef FEAT_TEXT_PROP
2764 if (curbuf->b_has_textprop && lnum > 0)
2765 // Add text properties that continue from the previous line.
2766 add_text_props_for_append(buf, lnum, &line, &len, &tofree);
2767#endif
2768
Bram Moolenaar98aefe72018-12-13 22:20:09 +01002769 space_needed = len + INDEX_SIZE; // space needed for text + index
Bram Moolenaar071d4272004-06-13 20:20:40 +00002770
2771 mfp = buf->b_ml.ml_mfp;
2772 page_size = mfp->mf_page_size;
2773
2774/*
2775 * find the data block containing the previous line
2776 * This also fills the stack with the blocks from the root to the data block
2777 * This also releases any locked block.
2778 */
2779 if ((hp = ml_find_line(buf, lnum == 0 ? (linenr_T)1 : lnum,
2780 ML_INSERT)) == NULL)
Bram Moolenaarb56ac042018-12-28 23:22:40 +01002781 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002782
2783 buf->b_ml.ml_flags &= ~ML_EMPTY;
2784
2785 if (lnum == 0) /* got line one instead, correct db_idx */
2786 db_idx = -1; /* careful, it is negative! */
2787 else
2788 db_idx = lnum - buf->b_ml.ml_locked_low;
2789 /* get line count before the insertion */
2790 line_count = buf->b_ml.ml_locked_high - buf->b_ml.ml_locked_low;
2791
2792 dp = (DATA_BL *)(hp->bh_data);
2793
2794/*
2795 * If
2796 * - there is not enough room in the current block
2797 * - appending to the last line in the block
2798 * - not appending to the last line in the file
2799 * insert in front of the next block.
2800 */
2801 if ((int)dp->db_free < space_needed && db_idx == line_count - 1
2802 && lnum < buf->b_ml.ml_line_count)
2803 {
2804 /*
2805 * Now that the line is not going to be inserted in the block that we
2806 * expected, the line count has to be adjusted in the pointer blocks
2807 * by using ml_locked_lineadd.
2808 */
2809 --(buf->b_ml.ml_locked_lineadd);
2810 --(buf->b_ml.ml_locked_high);
2811 if ((hp = ml_find_line(buf, lnum + 1, ML_INSERT)) == NULL)
Bram Moolenaarb56ac042018-12-28 23:22:40 +01002812 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002813
2814 db_idx = -1; /* careful, it is negative! */
2815 /* get line count before the insertion */
2816 line_count = buf->b_ml.ml_locked_high - buf->b_ml.ml_locked_low;
2817 CHECK(buf->b_ml.ml_locked_low != lnum + 1, "locked_low != lnum + 1");
2818
2819 dp = (DATA_BL *)(hp->bh_data);
2820 }
2821
2822 ++buf->b_ml.ml_line_count;
2823
2824 if ((int)dp->db_free >= space_needed) /* enough room in data block */
2825 {
Bram Moolenaarb56ac042018-12-28 23:22:40 +01002826 /*
2827 * Insert the new line in an existing data block, or in the data block
2828 * allocated above.
2829 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002830 dp->db_txt_start -= len;
2831 dp->db_free -= space_needed;
2832 ++(dp->db_line_count);
2833
2834 /*
2835 * move the text of the lines that follow to the front
2836 * adjust the indexes of the lines that follow
2837 */
2838 if (line_count > db_idx + 1) /* if there are following lines */
2839 {
2840 /*
2841 * Offset is the start of the previous line.
2842 * This will become the character just after the new line.
2843 */
2844 if (db_idx < 0)
2845 offset = dp->db_txt_end;
2846 else
2847 offset = ((dp->db_index[db_idx]) & DB_INDEX_MASK);
2848 mch_memmove((char *)dp + dp->db_txt_start,
2849 (char *)dp + dp->db_txt_start + len,
2850 (size_t)(offset - (dp->db_txt_start + len)));
2851 for (i = line_count - 1; i > db_idx; --i)
2852 dp->db_index[i + 1] = dp->db_index[i] - len;
2853 dp->db_index[db_idx + 1] = offset - len;
2854 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01002855 else
2856 // add line at the end (which is the start of the text)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002857 dp->db_index[db_idx + 1] = dp->db_txt_start;
2858
2859 /*
2860 * copy the text into the block
2861 */
2862 mch_memmove((char *)dp + dp->db_index[db_idx + 1], line, (size_t)len);
2863 if (mark)
2864 dp->db_index[db_idx + 1] |= DB_MARKED;
2865
2866 /*
2867 * Mark the block dirty.
2868 */
2869 buf->b_ml.ml_flags |= ML_LOCKED_DIRTY;
2870 if (!newfile)
2871 buf->b_ml.ml_flags |= ML_LOCKED_POS;
2872 }
2873 else /* not enough space in data block */
2874 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002875 long line_count_left, line_count_right;
2876 int page_count_left, page_count_right;
2877 bhdr_T *hp_left;
2878 bhdr_T *hp_right;
2879 bhdr_T *hp_new;
2880 int lines_moved;
2881 int data_moved = 0; /* init to shut up gcc */
2882 int total_moved = 0; /* init to shut up gcc */
2883 DATA_BL *dp_right, *dp_left;
2884 int stack_idx;
2885 int in_left;
2886 int lineadd;
2887 blocknr_T bnum_left, bnum_right;
2888 linenr_T lnum_left, lnum_right;
2889 int pb_idx;
2890 PTR_BL *pp_new;
2891
2892 /*
Bram Moolenaarb56ac042018-12-28 23:22:40 +01002893 * There is not enough room, we have to create a new data block and
2894 * copy some lines into it.
2895 * Then we have to insert an entry in the pointer block.
2896 * If this pointer block also is full, we go up another block, and so
2897 * on, up to the root if necessary.
2898 * The line counts in the pointer blocks have already been adjusted by
2899 * ml_find_line().
2900 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00002901 * We are going to allocate a new data block. Depending on the
2902 * situation it will be put to the left or right of the existing
2903 * block. If possible we put the new line in the left block and move
2904 * the lines after it to the right block. Otherwise the new line is
2905 * also put in the right block. This method is more efficient when
2906 * inserting a lot of lines at one place.
2907 */
2908 if (db_idx < 0) /* left block is new, right block is existing */
2909 {
2910 lines_moved = 0;
2911 in_left = TRUE;
2912 /* space_needed does not change */
2913 }
2914 else /* left block is existing, right block is new */
2915 {
2916 lines_moved = line_count - db_idx - 1;
2917 if (lines_moved == 0)
2918 in_left = FALSE; /* put new line in right block */
2919 /* space_needed does not change */
2920 else
2921 {
2922 data_moved = ((dp->db_index[db_idx]) & DB_INDEX_MASK) -
2923 dp->db_txt_start;
2924 total_moved = data_moved + lines_moved * INDEX_SIZE;
2925 if ((int)dp->db_free + total_moved >= space_needed)
2926 {
2927 in_left = TRUE; /* put new line in left block */
2928 space_needed = total_moved;
2929 }
2930 else
2931 {
2932 in_left = FALSE; /* put new line in right block */
2933 space_needed += total_moved;
2934 }
2935 }
2936 }
2937
2938 page_count = ((space_needed + HEADER_SIZE) + page_size - 1) / page_size;
2939 if ((hp_new = ml_new_data(mfp, newfile, page_count)) == NULL)
2940 {
2941 /* correct line counts in pointer blocks */
2942 --(buf->b_ml.ml_locked_lineadd);
2943 --(buf->b_ml.ml_locked_high);
Bram Moolenaarb56ac042018-12-28 23:22:40 +01002944 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002945 }
2946 if (db_idx < 0) /* left block is new */
2947 {
2948 hp_left = hp_new;
2949 hp_right = hp;
2950 line_count_left = 0;
2951 line_count_right = line_count;
2952 }
2953 else /* right block is new */
2954 {
2955 hp_left = hp;
2956 hp_right = hp_new;
2957 line_count_left = line_count;
2958 line_count_right = 0;
2959 }
2960 dp_right = (DATA_BL *)(hp_right->bh_data);
2961 dp_left = (DATA_BL *)(hp_left->bh_data);
2962 bnum_left = hp_left->bh_bnum;
2963 bnum_right = hp_right->bh_bnum;
2964 page_count_left = hp_left->bh_page_count;
2965 page_count_right = hp_right->bh_page_count;
2966
2967 /*
2968 * May move the new line into the right/new block.
2969 */
2970 if (!in_left)
2971 {
2972 dp_right->db_txt_start -= len;
2973 dp_right->db_free -= len + INDEX_SIZE;
2974 dp_right->db_index[0] = dp_right->db_txt_start;
2975 if (mark)
2976 dp_right->db_index[0] |= DB_MARKED;
2977
2978 mch_memmove((char *)dp_right + dp_right->db_txt_start,
2979 line, (size_t)len);
2980 ++line_count_right;
2981 }
2982 /*
2983 * may move lines from the left/old block to the right/new one.
2984 */
2985 if (lines_moved)
2986 {
2987 /*
2988 */
2989 dp_right->db_txt_start -= data_moved;
2990 dp_right->db_free -= total_moved;
2991 mch_memmove((char *)dp_right + dp_right->db_txt_start,
2992 (char *)dp_left + dp_left->db_txt_start,
2993 (size_t)data_moved);
2994 offset = dp_right->db_txt_start - dp_left->db_txt_start;
2995 dp_left->db_txt_start += data_moved;
2996 dp_left->db_free += total_moved;
2997
2998 /*
2999 * update indexes in the new block
3000 */
3001 for (to = line_count_right, from = db_idx + 1;
3002 from < line_count_left; ++from, ++to)
3003 dp_right->db_index[to] = dp->db_index[from] + offset;
3004 line_count_right += lines_moved;
3005 line_count_left -= lines_moved;
3006 }
3007
3008 /*
3009 * May move the new line into the left (old or new) block.
3010 */
3011 if (in_left)
3012 {
3013 dp_left->db_txt_start -= len;
3014 dp_left->db_free -= len + INDEX_SIZE;
3015 dp_left->db_index[line_count_left] = dp_left->db_txt_start;
3016 if (mark)
3017 dp_left->db_index[line_count_left] |= DB_MARKED;
3018 mch_memmove((char *)dp_left + dp_left->db_txt_start,
3019 line, (size_t)len);
3020 ++line_count_left;
3021 }
3022
3023 if (db_idx < 0) /* left block is new */
3024 {
3025 lnum_left = lnum + 1;
3026 lnum_right = 0;
3027 }
3028 else /* right block is new */
3029 {
3030 lnum_left = 0;
3031 if (in_left)
3032 lnum_right = lnum + 2;
3033 else
3034 lnum_right = lnum + 1;
3035 }
3036 dp_left->db_line_count = line_count_left;
3037 dp_right->db_line_count = line_count_right;
3038
3039 /*
3040 * release the two data blocks
3041 * The new one (hp_new) already has a correct blocknumber.
3042 * The old one (hp, in ml_locked) gets a positive blocknumber if
3043 * we changed it and we are not editing a new file.
3044 */
3045 if (lines_moved || in_left)
3046 buf->b_ml.ml_flags |= ML_LOCKED_DIRTY;
3047 if (!newfile && db_idx >= 0 && in_left)
3048 buf->b_ml.ml_flags |= ML_LOCKED_POS;
3049 mf_put(mfp, hp_new, TRUE, FALSE);
3050
3051 /*
3052 * flush the old data block
3053 * set ml_locked_lineadd to 0, because the updating of the
3054 * pointer blocks is done below
3055 */
3056 lineadd = buf->b_ml.ml_locked_lineadd;
3057 buf->b_ml.ml_locked_lineadd = 0;
3058 ml_find_line(buf, (linenr_T)0, ML_FLUSH); /* flush data block */
3059
3060 /*
3061 * update pointer blocks for the new data block
3062 */
3063 for (stack_idx = buf->b_ml.ml_stack_top - 1; stack_idx >= 0;
3064 --stack_idx)
3065 {
3066 ip = &(buf->b_ml.ml_stack[stack_idx]);
3067 pb_idx = ip->ip_index;
3068 if ((hp = mf_get(mfp, ip->ip_bnum, 1)) == NULL)
Bram Moolenaarb56ac042018-12-28 23:22:40 +01003069 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003070 pp = (PTR_BL *)(hp->bh_data); /* must be pointer block */
3071 if (pp->pb_id != PTR_ID)
3072 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003073 iemsg(_("E317: pointer block id wrong 3"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003074 mf_put(mfp, hp, FALSE, FALSE);
Bram Moolenaarb56ac042018-12-28 23:22:40 +01003075 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003076 }
3077 /*
3078 * TODO: If the pointer block is full and we are adding at the end
3079 * try to insert in front of the next block
3080 */
3081 /* block not full, add one entry */
3082 if (pp->pb_count < pp->pb_count_max)
3083 {
3084 if (pb_idx + 1 < (int)pp->pb_count)
3085 mch_memmove(&pp->pb_pointer[pb_idx + 2],
3086 &pp->pb_pointer[pb_idx + 1],
3087 (size_t)(pp->pb_count - pb_idx - 1) * sizeof(PTR_EN));
3088 ++pp->pb_count;
3089 pp->pb_pointer[pb_idx].pe_line_count = line_count_left;
3090 pp->pb_pointer[pb_idx].pe_bnum = bnum_left;
3091 pp->pb_pointer[pb_idx].pe_page_count = page_count_left;
3092 pp->pb_pointer[pb_idx + 1].pe_line_count = line_count_right;
3093 pp->pb_pointer[pb_idx + 1].pe_bnum = bnum_right;
3094 pp->pb_pointer[pb_idx + 1].pe_page_count = page_count_right;
3095
3096 if (lnum_left != 0)
3097 pp->pb_pointer[pb_idx].pe_old_lnum = lnum_left;
3098 if (lnum_right != 0)
3099 pp->pb_pointer[pb_idx + 1].pe_old_lnum = lnum_right;
3100
3101 mf_put(mfp, hp, TRUE, FALSE);
3102 buf->b_ml.ml_stack_top = stack_idx + 1; /* truncate stack */
3103
3104 if (lineadd)
3105 {
3106 --(buf->b_ml.ml_stack_top);
Bram Moolenaar6b803a72007-05-06 14:25:46 +00003107 /* fix line count for rest of blocks in the stack */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003108 ml_lineadd(buf, lineadd);
3109 /* fix stack itself */
3110 buf->b_ml.ml_stack[buf->b_ml.ml_stack_top].ip_high +=
3111 lineadd;
3112 ++(buf->b_ml.ml_stack_top);
3113 }
3114
3115 /*
3116 * We are finished, break the loop here.
3117 */
3118 break;
3119 }
3120 else /* pointer block full */
3121 {
3122 /*
3123 * split the pointer block
3124 * allocate a new pointer block
3125 * move some of the pointer into the new block
3126 * prepare for updating the parent block
3127 */
3128 for (;;) /* do this twice when splitting block 1 */
3129 {
3130 hp_new = ml_new_ptr(mfp);
3131 if (hp_new == NULL) /* TODO: try to fix tree */
Bram Moolenaarb56ac042018-12-28 23:22:40 +01003132 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003133 pp_new = (PTR_BL *)(hp_new->bh_data);
3134
3135 if (hp->bh_bnum != 1)
3136 break;
3137
3138 /*
3139 * if block 1 becomes full the tree is given an extra level
3140 * The pointers from block 1 are moved into the new block.
3141 * block 1 is updated to point to the new block
3142 * then continue to split the new block
3143 */
3144 mch_memmove(pp_new, pp, (size_t)page_size);
3145 pp->pb_count = 1;
3146 pp->pb_pointer[0].pe_bnum = hp_new->bh_bnum;
3147 pp->pb_pointer[0].pe_line_count = buf->b_ml.ml_line_count;
3148 pp->pb_pointer[0].pe_old_lnum = 1;
3149 pp->pb_pointer[0].pe_page_count = 1;
3150 mf_put(mfp, hp, TRUE, FALSE); /* release block 1 */
3151 hp = hp_new; /* new block is to be split */
3152 pp = pp_new;
3153 CHECK(stack_idx != 0, _("stack_idx should be 0"));
3154 ip->ip_index = 0;
3155 ++stack_idx; /* do block 1 again later */
3156 }
3157 /*
3158 * move the pointers after the current one to the new block
3159 * If there are none, the new entry will be in the new block.
3160 */
3161 total_moved = pp->pb_count - pb_idx - 1;
3162 if (total_moved)
3163 {
3164 mch_memmove(&pp_new->pb_pointer[0],
3165 &pp->pb_pointer[pb_idx + 1],
3166 (size_t)(total_moved) * sizeof(PTR_EN));
3167 pp_new->pb_count = total_moved;
3168 pp->pb_count -= total_moved - 1;
3169 pp->pb_pointer[pb_idx + 1].pe_bnum = bnum_right;
3170 pp->pb_pointer[pb_idx + 1].pe_line_count = line_count_right;
3171 pp->pb_pointer[pb_idx + 1].pe_page_count = page_count_right;
3172 if (lnum_right)
3173 pp->pb_pointer[pb_idx + 1].pe_old_lnum = lnum_right;
3174 }
3175 else
3176 {
3177 pp_new->pb_count = 1;
3178 pp_new->pb_pointer[0].pe_bnum = bnum_right;
3179 pp_new->pb_pointer[0].pe_line_count = line_count_right;
3180 pp_new->pb_pointer[0].pe_page_count = page_count_right;
3181 pp_new->pb_pointer[0].pe_old_lnum = lnum_right;
3182 }
3183 pp->pb_pointer[pb_idx].pe_bnum = bnum_left;
3184 pp->pb_pointer[pb_idx].pe_line_count = line_count_left;
3185 pp->pb_pointer[pb_idx].pe_page_count = page_count_left;
3186 if (lnum_left)
3187 pp->pb_pointer[pb_idx].pe_old_lnum = lnum_left;
3188 lnum_left = 0;
3189 lnum_right = 0;
3190
3191 /*
3192 * recompute line counts
3193 */
3194 line_count_right = 0;
3195 for (i = 0; i < (int)pp_new->pb_count; ++i)
3196 line_count_right += pp_new->pb_pointer[i].pe_line_count;
3197 line_count_left = 0;
3198 for (i = 0; i < (int)pp->pb_count; ++i)
3199 line_count_left += pp->pb_pointer[i].pe_line_count;
3200
3201 bnum_left = hp->bh_bnum;
3202 bnum_right = hp_new->bh_bnum;
3203 page_count_left = 1;
3204 page_count_right = 1;
3205 mf_put(mfp, hp, TRUE, FALSE);
3206 mf_put(mfp, hp_new, TRUE, FALSE);
3207 }
3208 }
3209
3210 /*
3211 * Safety check: fallen out of for loop?
3212 */
3213 if (stack_idx < 0)
3214 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003215 iemsg(_("E318: Updated too many blocks?"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003216 buf->b_ml.ml_stack_top = 0; /* invalidate stack */
3217 }
3218 }
3219
3220#ifdef FEAT_BYTEOFF
3221 /* The line was inserted below 'lnum' */
3222 ml_updatechunk(buf, lnum + 1, (long)len, ML_CHNK_ADDLINE);
3223#endif
3224#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003225 if (netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003226 {
3227 if (STRLEN(line) > 0)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003228 netbeans_inserted(buf, lnum+1, (colnr_T)0, line, (int)STRLEN(line));
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +00003229 netbeans_inserted(buf, lnum+1, (colnr_T)STRLEN(line),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003230 (char_u *)"\n", 1);
3231 }
3232#endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01003233#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar99ef0622016-03-06 20:22:25 +01003234 if (buf->b_write_to_channel)
3235 channel_write_new_lines(buf);
3236#endif
Bram Moolenaarb56ac042018-12-28 23:22:40 +01003237 ret = OK;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01003238
Bram Moolenaarb56ac042018-12-28 23:22:40 +01003239theend:
3240#ifdef FEAT_TEXT_PROP
3241 vim_free(tofree);
3242#endif
3243 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003244}
3245
3246/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00003247 * Replace line lnum, with buffering, in current buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003248 *
Bram Moolenaar1056d982006-03-09 22:37:52 +00003249 * If "copy" is TRUE, make a copy of the line, otherwise the line has been
Bram Moolenaar071d4272004-06-13 20:20:40 +00003250 * copied to allocated memory already.
3251 *
3252 * Check: The caller of this function should probably also call
3253 * changed_lines(), unless update_screen(NOT_VALID) is used.
3254 *
3255 * return FAIL for failure, OK otherwise
3256 */
3257 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003258ml_replace(linenr_T lnum, char_u *line, int copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003259{
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003260 colnr_T len = -1;
3261
3262 if (line != NULL)
Bram Moolenaar4efe73b2018-12-16 14:37:39 +01003263 len = (colnr_T)STRLEN(line);
Bram Moolenaarccae4672019-01-04 15:09:57 +01003264 return ml_replace_len(lnum, line, len, FALSE, copy);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003265}
3266
Bram Moolenaarccae4672019-01-04 15:09:57 +01003267/*
3268 * Replace a line for the current buffer. Like ml_replace() with:
3269 * "len_arg" is the length of the text, excluding NUL.
3270 * If "has_props" is TRUE then "line_arg" includes the text properties and
3271 * "len_arg" includes the NUL of the text.
3272 */
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003273 int
Bram Moolenaarccae4672019-01-04 15:09:57 +01003274ml_replace_len(
3275 linenr_T lnum,
3276 char_u *line_arg,
3277 colnr_T len_arg,
3278 int has_props,
3279 int copy)
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003280{
3281 char_u *line = line_arg;
3282 colnr_T len = len_arg;
3283
Bram Moolenaar071d4272004-06-13 20:20:40 +00003284 if (line == NULL) /* just checking... */
3285 return FAIL;
3286
3287 /* When starting up, we might still need to create the memfile */
Bram Moolenaar59f931e2010-07-24 20:27:03 +02003288 if (curbuf->b_ml.ml_mfp == NULL && open_buffer(FALSE, NULL, 0) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003289 return FAIL;
3290
Bram Moolenaarccae4672019-01-04 15:09:57 +01003291 if (!has_props)
3292 ++len; // include the NUL after the text
3293 if (copy)
3294 {
3295 // copy the line to allocated memory
3296#ifdef FEAT_TEXT_PROP
3297 if (has_props)
3298 line = vim_memsave(line, len);
3299 else
3300#endif
3301 line = vim_strnsave(line, len - 1);
3302 if (line == NULL)
3303 return FAIL;
3304 }
3305
Bram Moolenaar071d4272004-06-13 20:20:40 +00003306#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003307 if (netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003308 {
3309 netbeans_removed(curbuf, lnum, 0, (long)STRLEN(ml_get(lnum)));
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003310 netbeans_inserted(curbuf, lnum, 0, line, (int)STRLEN(line));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003311 }
3312#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003313 if (curbuf->b_ml.ml_line_lnum != lnum)
3314 {
3315 // another line is buffered, flush it
3316 ml_flush_line(curbuf);
Bram Moolenaarca79a5f2018-12-13 23:16:36 +01003317 curbuf->b_ml.ml_flags &= ~ML_LINE_DIRTY;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003318
3319#ifdef FEAT_TEXT_PROP
Bram Moolenaarccae4672019-01-04 15:09:57 +01003320 if (curbuf->b_has_textprop && !has_props)
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003321 // Need to fetch the old line to copy over any text properties.
3322 ml_get_buf(curbuf, lnum, TRUE);
3323#endif
3324 }
3325
3326#ifdef FEAT_TEXT_PROP
Bram Moolenaarccae4672019-01-04 15:09:57 +01003327 if (curbuf->b_has_textprop && !has_props)
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003328 {
3329 size_t oldtextlen = STRLEN(curbuf->b_ml.ml_line_ptr) + 1;
3330
3331 if (oldtextlen < (size_t)curbuf->b_ml.ml_line_len)
3332 {
3333 char_u *newline;
3334 size_t textproplen = curbuf->b_ml.ml_line_len - oldtextlen;
3335
3336 // Need to copy over text properties, stored after the text.
Bram Moolenaarccae4672019-01-04 15:09:57 +01003337 newline = alloc(len + (int)textproplen);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003338 if (newline != NULL)
3339 {
Bram Moolenaarccae4672019-01-04 15:09:57 +01003340 mch_memmove(newline, line, len);
3341 mch_memmove(newline + len, curbuf->b_ml.ml_line_ptr + oldtextlen, textproplen);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003342 vim_free(line);
3343 line = newline;
Bram Moolenaar4efe73b2018-12-16 14:37:39 +01003344 len += (colnr_T)textproplen;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003345 }
3346 }
3347 }
3348#endif
3349
Bram Moolenaarccae4672019-01-04 15:09:57 +01003350 if (curbuf->b_ml.ml_flags & ML_LINE_DIRTY) // same line allocated
3351 vim_free(curbuf->b_ml.ml_line_ptr); // free it
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003352
Bram Moolenaar071d4272004-06-13 20:20:40 +00003353 curbuf->b_ml.ml_line_ptr = line;
Bram Moolenaarccae4672019-01-04 15:09:57 +01003354 curbuf->b_ml.ml_line_len = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003355 curbuf->b_ml.ml_line_lnum = lnum;
3356 curbuf->b_ml.ml_flags = (curbuf->b_ml.ml_flags | ML_LINE_DIRTY) & ~ML_EMPTY;
3357
3358 return OK;
3359}
3360
Bram Moolenaarc1a9bc12018-12-28 21:59:29 +01003361#ifdef FEAT_TEXT_PROP
3362/*
3363 * Adjust text properties in line "lnum" for a deleted line.
3364 * When "above" is true this is the line above the deleted line.
3365 * "del_props" are the properties of the deleted line.
3366 */
3367 static void
3368adjust_text_props_for_delete(
3369 buf_T *buf,
3370 linenr_T lnum,
3371 char_u *del_props,
3372 int del_props_len,
3373 int above)
3374{
3375 int did_get_line = FALSE;
3376 int done_del;
3377 int done_this;
3378 textprop_T prop_del;
3379 textprop_T prop_this;
3380 bhdr_T *hp;
3381 DATA_BL *dp;
3382 int idx;
3383 int line_start;
3384 long line_size;
3385 int this_props_len;
3386 char_u *text;
3387 size_t textlen;
3388 int found;
3389
3390 for (done_del = 0; done_del < del_props_len; done_del += sizeof(textprop_T))
3391 {
3392 mch_memmove(&prop_del, del_props + done_del, sizeof(textprop_T));
3393 if ((above && (prop_del.tp_flags & TP_FLAG_CONT_PREV)
3394 && !(prop_del.tp_flags & TP_FLAG_CONT_NEXT))
3395 || (!above && (prop_del.tp_flags & TP_FLAG_CONT_NEXT)
3396 && !(prop_del.tp_flags & TP_FLAG_CONT_PREV)))
3397 {
3398 if (!did_get_line)
3399 {
3400 did_get_line = TRUE;
3401 if ((hp = ml_find_line(buf, lnum, ML_FIND)) == NULL)
3402 return;
3403
3404 dp = (DATA_BL *)(hp->bh_data);
3405 idx = lnum - buf->b_ml.ml_locked_low;
3406 line_start = ((dp->db_index[idx]) & DB_INDEX_MASK);
3407 if (idx == 0) // first line in block, text at the end
3408 line_size = dp->db_txt_end - line_start;
3409 else
3410 line_size = ((dp->db_index[idx - 1]) & DB_INDEX_MASK) - line_start;
3411 text = (char_u *)dp + line_start;
3412 textlen = STRLEN(text) + 1;
3413 if ((long)textlen >= line_size)
3414 {
3415 if (above)
3416 internal_error("no text property above deleted line");
3417 else
3418 internal_error("no text property below deleted line");
3419 return;
3420 }
Bram Moolenaar4b7214e2019-01-03 21:55:32 +01003421 this_props_len = line_size - (int)textlen;
Bram Moolenaarc1a9bc12018-12-28 21:59:29 +01003422 }
3423
3424 found = FALSE;
3425 for (done_this = 0; done_this < this_props_len; done_this += sizeof(textprop_T))
3426 {
3427 mch_memmove(&prop_this, text + textlen + done_del, sizeof(textprop_T));
3428 if (prop_del.tp_id == prop_this.tp_id
3429 && prop_del.tp_type == prop_this.tp_type)
3430 {
3431 int flag = above ? TP_FLAG_CONT_NEXT : TP_FLAG_CONT_PREV;
3432
3433 found = TRUE;
3434 if (prop_this.tp_flags & flag)
3435 {
3436 prop_this.tp_flags &= ~flag;
3437 mch_memmove(text + textlen + done_del, &prop_this, sizeof(textprop_T));
3438 }
3439 else if (above)
3440 internal_error("text property above deleted line does not continue");
3441 else
3442 internal_error("text property below deleted line does not continue");
3443 }
3444 }
3445 if (!found)
3446 {
3447 if (above)
3448 internal_error("text property above deleted line not found");
3449 else
3450 internal_error("text property below deleted line not found");
3451 }
3452
3453 buf->b_ml.ml_flags |= (ML_LOCKED_DIRTY | ML_LOCKED_POS);
3454 }
3455 }
3456}
3457#endif
3458
Bram Moolenaar071d4272004-06-13 20:20:40 +00003459/*
Bram Moolenaar4033c552017-09-16 20:54:51 +02003460 * Delete line "lnum" in the current buffer.
3461 * When "message" is TRUE may give a "No lines in buffer" message.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003462 *
3463 * Check: The caller of this function should probably also call
3464 * deleted_lines() after this.
3465 *
3466 * return FAIL for failure, OK otherwise
3467 */
3468 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003469ml_delete(linenr_T lnum, int message)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003470{
3471 ml_flush_line(curbuf);
3472 return ml_delete_int(curbuf, lnum, message);
3473}
3474
3475 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003476ml_delete_int(buf_T *buf, linenr_T lnum, int message)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003477{
3478 bhdr_T *hp;
3479 memfile_T *mfp;
3480 DATA_BL *dp;
3481 PTR_BL *pp;
3482 infoptr_T *ip;
3483 int count; /* number of entries in block */
3484 int idx;
3485 int stack_idx;
3486 int text_start;
3487 int line_start;
3488 long line_size;
3489 int i;
Bram Moolenaarc1a9bc12018-12-28 21:59:29 +01003490 int ret = FAIL;
3491#ifdef FEAT_TEXT_PROP
3492 char_u *textprop_save = NULL;
3493 int textprop_save_len;
3494#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003495
3496 if (lnum < 1 || lnum > buf->b_ml.ml_line_count)
3497 return FAIL;
3498
3499 if (lowest_marked && lowest_marked > lnum)
3500 lowest_marked--;
3501
3502/*
3503 * If the file becomes empty the last line is replaced by an empty line.
3504 */
3505 if (buf->b_ml.ml_line_count == 1) /* file becomes empty */
3506 {
3507 if (message
3508#ifdef FEAT_NETBEANS_INTG
3509 && !netbeansSuppressNoLines
3510#endif
3511 )
Bram Moolenaar238a5642006-02-21 22:12:05 +00003512 set_keep_msg((char_u *)_(no_lines_msg), 0);
3513
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02003514 /* FEAT_BYTEOFF already handled in there, don't worry 'bout it below */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003515 i = ml_replace((linenr_T)1, (char_u *)"", TRUE);
3516 buf->b_ml.ml_flags |= ML_EMPTY;
3517
3518 return i;
3519 }
3520
3521/*
Bram Moolenaarc1a9bc12018-12-28 21:59:29 +01003522 * Find the data block containing the line.
3523 * This also fills the stack with the blocks from the root to the data block.
3524 * This also releases any locked block..
Bram Moolenaar071d4272004-06-13 20:20:40 +00003525 */
3526 mfp = buf->b_ml.ml_mfp;
3527 if (mfp == NULL)
3528 return FAIL;
3529
3530 if ((hp = ml_find_line(buf, lnum, ML_DELETE)) == NULL)
3531 return FAIL;
3532
3533 dp = (DATA_BL *)(hp->bh_data);
3534 /* compute line count before the delete */
3535 count = (long)(buf->b_ml.ml_locked_high)
3536 - (long)(buf->b_ml.ml_locked_low) + 2;
3537 idx = lnum - buf->b_ml.ml_locked_low;
3538
3539 --buf->b_ml.ml_line_count;
3540
3541 line_start = ((dp->db_index[idx]) & DB_INDEX_MASK);
3542 if (idx == 0) /* first line in block, text at the end */
3543 line_size = dp->db_txt_end - line_start;
3544 else
3545 line_size = ((dp->db_index[idx - 1]) & DB_INDEX_MASK) - line_start;
3546
3547#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003548 if (netbeans_active())
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +00003549 netbeans_removed(buf, lnum, 0, (long)line_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003550#endif
Bram Moolenaarc1a9bc12018-12-28 21:59:29 +01003551#ifdef FEAT_TEXT_PROP
3552 // If there are text properties, make a copy, so that we can update
3553 // properties in preceding and following lines.
3554 if (buf->b_has_textprop)
3555 {
3556 size_t textlen = STRLEN((char_u *)dp + line_start) + 1;
3557
3558 if ((long)textlen < line_size)
3559 {
Bram Moolenaar4b7214e2019-01-03 21:55:32 +01003560 textprop_save_len = line_size - (int)textlen;
Bram Moolenaarc1a9bc12018-12-28 21:59:29 +01003561 textprop_save = vim_memsave((char_u *)dp + line_start + textlen,
3562 textprop_save_len);
3563 }
3564 }
3565#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003566
3567/*
3568 * special case: If there is only one line in the data block it becomes empty.
3569 * Then we have to remove the entry, pointing to this data block, from the
3570 * pointer block. If this pointer block also becomes empty, we go up another
3571 * block, and so on, up to the root if necessary.
3572 * The line counts in the pointer blocks have already been adjusted by
3573 * ml_find_line().
3574 */
3575 if (count == 1)
3576 {
3577 mf_free(mfp, hp); /* free the data block */
3578 buf->b_ml.ml_locked = NULL;
3579
Bram Moolenaare60acc12011-05-10 16:41:25 +02003580 for (stack_idx = buf->b_ml.ml_stack_top - 1; stack_idx >= 0;
3581 --stack_idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003582 {
3583 buf->b_ml.ml_stack_top = 0; /* stack is invalid when failing */
3584 ip = &(buf->b_ml.ml_stack[stack_idx]);
3585 idx = ip->ip_index;
3586 if ((hp = mf_get(mfp, ip->ip_bnum, 1)) == NULL)
Bram Moolenaarc1a9bc12018-12-28 21:59:29 +01003587 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003588 pp = (PTR_BL *)(hp->bh_data); /* must be pointer block */
3589 if (pp->pb_id != PTR_ID)
3590 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003591 iemsg(_("E317: pointer block id wrong 4"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003592 mf_put(mfp, hp, FALSE, FALSE);
Bram Moolenaarc1a9bc12018-12-28 21:59:29 +01003593 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003594 }
3595 count = --(pp->pb_count);
3596 if (count == 0) /* the pointer block becomes empty! */
3597 mf_free(mfp, hp);
3598 else
3599 {
3600 if (count != idx) /* move entries after the deleted one */
3601 mch_memmove(&pp->pb_pointer[idx], &pp->pb_pointer[idx + 1],
3602 (size_t)(count - idx) * sizeof(PTR_EN));
3603 mf_put(mfp, hp, TRUE, FALSE);
3604
3605 buf->b_ml.ml_stack_top = stack_idx; /* truncate stack */
Bram Moolenaar6b803a72007-05-06 14:25:46 +00003606 /* fix line count for rest of blocks in the stack */
3607 if (buf->b_ml.ml_locked_lineadd != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003608 {
3609 ml_lineadd(buf, buf->b_ml.ml_locked_lineadd);
3610 buf->b_ml.ml_stack[buf->b_ml.ml_stack_top].ip_high +=
Bram Moolenaar6b803a72007-05-06 14:25:46 +00003611 buf->b_ml.ml_locked_lineadd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003612 }
3613 ++(buf->b_ml.ml_stack_top);
3614
3615 break;
3616 }
3617 }
3618 CHECK(stack_idx < 0, _("deleted block 1?"));
3619 }
3620 else
3621 {
3622 /*
3623 * delete the text by moving the next lines forwards
3624 */
3625 text_start = dp->db_txt_start;
3626 mch_memmove((char *)dp + text_start + line_size,
3627 (char *)dp + text_start, (size_t)(line_start - text_start));
3628
3629 /*
3630 * delete the index by moving the next indexes backwards
3631 * Adjust the indexes for the text movement.
3632 */
3633 for (i = idx; i < count - 1; ++i)
3634 dp->db_index[i] = dp->db_index[i + 1] + line_size;
3635
3636 dp->db_free += line_size + INDEX_SIZE;
3637 dp->db_txt_start += line_size;
3638 --(dp->db_line_count);
3639
3640 /*
3641 * mark the block dirty and make sure it is in the file (for recovery)
3642 */
3643 buf->b_ml.ml_flags |= (ML_LOCKED_DIRTY | ML_LOCKED_POS);
3644 }
3645
3646#ifdef FEAT_BYTEOFF
3647 ml_updatechunk(buf, lnum, line_size, ML_CHNK_DELLINE);
3648#endif
Bram Moolenaarc1a9bc12018-12-28 21:59:29 +01003649 ret = OK;
3650
3651theend:
3652#ifdef FEAT_TEXT_PROP
3653 if (textprop_save != NULL)
3654 {
3655 // Adjust text properties in the line above and below.
3656 if (lnum > 1)
3657 adjust_text_props_for_delete(buf, lnum - 1, textprop_save, textprop_save_len, TRUE);
3658 if (lnum <= buf->b_ml.ml_line_count)
3659 adjust_text_props_for_delete(buf, lnum, textprop_save, textprop_save_len, FALSE);
3660 }
3661 vim_free(textprop_save);
3662#endif
3663 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003664}
3665
3666/*
Bram Moolenaarc1a9bc12018-12-28 21:59:29 +01003667 * set the DB_MARKED flag for line 'lnum'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003668 */
3669 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003670ml_setmarked(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003671{
3672 bhdr_T *hp;
3673 DATA_BL *dp;
3674 /* invalid line number */
3675 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count
3676 || curbuf->b_ml.ml_mfp == NULL)
3677 return; /* give error message? */
3678
3679 if (lowest_marked == 0 || lowest_marked > lnum)
3680 lowest_marked = lnum;
3681
3682 /*
3683 * find the data block containing the line
3684 * This also fills the stack with the blocks from the root to the data block
3685 * This also releases any locked block.
3686 */
3687 if ((hp = ml_find_line(curbuf, lnum, ML_FIND)) == NULL)
3688 return; /* give error message? */
3689
3690 dp = (DATA_BL *)(hp->bh_data);
3691 dp->db_index[lnum - curbuf->b_ml.ml_locked_low] |= DB_MARKED;
3692 curbuf->b_ml.ml_flags |= ML_LOCKED_DIRTY;
3693}
3694
3695/*
Bram Moolenaarc1a9bc12018-12-28 21:59:29 +01003696 * find the first line with its DB_MARKED flag set
Bram Moolenaar071d4272004-06-13 20:20:40 +00003697 */
3698 linenr_T
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003699ml_firstmarked(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003700{
3701 bhdr_T *hp;
3702 DATA_BL *dp;
3703 linenr_T lnum;
3704 int i;
3705
3706 if (curbuf->b_ml.ml_mfp == NULL)
3707 return (linenr_T) 0;
3708
3709 /*
3710 * The search starts with lowest_marked line. This is the last line where
3711 * a mark was found, adjusted by inserting/deleting lines.
3712 */
3713 for (lnum = lowest_marked; lnum <= curbuf->b_ml.ml_line_count; )
3714 {
3715 /*
3716 * Find the data block containing the line.
3717 * This also fills the stack with the blocks from the root to the data
3718 * block This also releases any locked block.
3719 */
3720 if ((hp = ml_find_line(curbuf, lnum, ML_FIND)) == NULL)
3721 return (linenr_T)0; /* give error message? */
3722
3723 dp = (DATA_BL *)(hp->bh_data);
3724
3725 for (i = lnum - curbuf->b_ml.ml_locked_low;
3726 lnum <= curbuf->b_ml.ml_locked_high; ++i, ++lnum)
3727 if ((dp->db_index[i]) & DB_MARKED)
3728 {
3729 (dp->db_index[i]) &= DB_INDEX_MASK;
3730 curbuf->b_ml.ml_flags |= ML_LOCKED_DIRTY;
3731 lowest_marked = lnum + 1;
3732 return lnum;
3733 }
3734 }
3735
3736 return (linenr_T) 0;
3737}
3738
Bram Moolenaar071d4272004-06-13 20:20:40 +00003739/*
3740 * clear all DB_MARKED flags
3741 */
3742 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003743ml_clearmarked(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003744{
3745 bhdr_T *hp;
3746 DATA_BL *dp;
3747 linenr_T lnum;
3748 int i;
3749
3750 if (curbuf->b_ml.ml_mfp == NULL) /* nothing to do */
3751 return;
3752
3753 /*
3754 * The search starts with line lowest_marked.
3755 */
3756 for (lnum = lowest_marked; lnum <= curbuf->b_ml.ml_line_count; )
3757 {
3758 /*
3759 * Find the data block containing the line.
3760 * This also fills the stack with the blocks from the root to the data
3761 * block and releases any locked block.
3762 */
3763 if ((hp = ml_find_line(curbuf, lnum, ML_FIND)) == NULL)
3764 return; /* give error message? */
3765
3766 dp = (DATA_BL *)(hp->bh_data);
3767
3768 for (i = lnum - curbuf->b_ml.ml_locked_low;
3769 lnum <= curbuf->b_ml.ml_locked_high; ++i, ++lnum)
3770 if ((dp->db_index[i]) & DB_MARKED)
3771 {
3772 (dp->db_index[i]) &= DB_INDEX_MASK;
3773 curbuf->b_ml.ml_flags |= ML_LOCKED_DIRTY;
3774 }
3775 }
3776
3777 lowest_marked = 0;
3778 return;
3779}
3780
3781/*
3782 * flush ml_line if necessary
3783 */
3784 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003785ml_flush_line(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003786{
3787 bhdr_T *hp;
3788 DATA_BL *dp;
3789 linenr_T lnum;
3790 char_u *new_line;
3791 char_u *old_line;
3792 colnr_T new_len;
3793 int old_len;
3794 int extra;
3795 int idx;
3796 int start;
3797 int count;
3798 int i;
Bram Moolenaar0ca4b352010-02-11 18:54:43 +01003799 static int entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003800
3801 if (buf->b_ml.ml_line_lnum == 0 || buf->b_ml.ml_mfp == NULL)
3802 return; /* nothing to do */
3803
3804 if (buf->b_ml.ml_flags & ML_LINE_DIRTY)
3805 {
Bram Moolenaar0ca4b352010-02-11 18:54:43 +01003806 /* This code doesn't work recursively, but Netbeans may call back here
3807 * when obtaining the cursor position. */
3808 if (entered)
3809 return;
3810 entered = TRUE;
3811
Bram Moolenaar071d4272004-06-13 20:20:40 +00003812 lnum = buf->b_ml.ml_line_lnum;
3813 new_line = buf->b_ml.ml_line_ptr;
3814
3815 hp = ml_find_line(buf, lnum, ML_FIND);
3816 if (hp == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003817 siemsg(_("E320: Cannot find line %ld"), lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003818 else
3819 {
3820 dp = (DATA_BL *)(hp->bh_data);
3821 idx = lnum - buf->b_ml.ml_locked_low;
3822 start = ((dp->db_index[idx]) & DB_INDEX_MASK);
3823 old_line = (char_u *)dp + start;
3824 if (idx == 0) /* line is last in block */
3825 old_len = dp->db_txt_end - start;
3826 else /* text of previous line follows */
3827 old_len = (dp->db_index[idx - 1] & DB_INDEX_MASK) - start;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003828 new_len = buf->b_ml.ml_line_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003829 extra = new_len - old_len; /* negative if lines gets smaller */
3830
3831 /*
3832 * if new line fits in data block, replace directly
3833 */
3834 if ((int)dp->db_free >= extra)
3835 {
3836 /* if the length changes and there are following lines */
3837 count = buf->b_ml.ml_locked_high - buf->b_ml.ml_locked_low + 1;
3838 if (extra != 0 && idx < count - 1)
3839 {
3840 /* move text of following lines */
3841 mch_memmove((char *)dp + dp->db_txt_start - extra,
3842 (char *)dp + dp->db_txt_start,
3843 (size_t)(start - dp->db_txt_start));
3844
3845 /* adjust pointers of this and following lines */
3846 for (i = idx + 1; i < count; ++i)
3847 dp->db_index[i] -= extra;
3848 }
3849 dp->db_index[idx] -= extra;
3850
3851 /* adjust free space */
3852 dp->db_free -= extra;
3853 dp->db_txt_start -= extra;
3854
3855 /* copy new line into the data block */
3856 mch_memmove(old_line - extra, new_line, (size_t)new_len);
3857 buf->b_ml.ml_flags |= (ML_LOCKED_DIRTY | ML_LOCKED_POS);
3858#ifdef FEAT_BYTEOFF
3859 /* The else case is already covered by the insert and delete */
3860 ml_updatechunk(buf, lnum, (long)extra, ML_CHNK_UPDLINE);
3861#endif
3862 }
3863 else
3864 {
3865 /*
3866 * Cannot do it in one data block: Delete and append.
3867 * Append first, because ml_delete_int() cannot delete the
3868 * last line in a buffer, which causes trouble for a buffer
3869 * that has only one line.
3870 * Don't forget to copy the mark!
3871 */
3872 /* How about handling errors??? */
3873 (void)ml_append_int(buf, lnum, new_line, new_len, FALSE,
3874 (dp->db_index[idx] & DB_MARKED));
3875 (void)ml_delete_int(buf, lnum, FALSE);
3876 }
3877 }
3878 vim_free(new_line);
Bram Moolenaar0ca4b352010-02-11 18:54:43 +01003879
3880 entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003881 }
3882
3883 buf->b_ml.ml_line_lnum = 0;
3884}
3885
3886/*
3887 * create a new, empty, data block
3888 */
3889 static bhdr_T *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003890ml_new_data(memfile_T *mfp, int negative, int page_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003891{
3892 bhdr_T *hp;
3893 DATA_BL *dp;
3894
3895 if ((hp = mf_new(mfp, negative, page_count)) == NULL)
3896 return NULL;
3897
3898 dp = (DATA_BL *)(hp->bh_data);
3899 dp->db_id = DATA_ID;
3900 dp->db_txt_start = dp->db_txt_end = page_count * mfp->mf_page_size;
3901 dp->db_free = dp->db_txt_start - HEADER_SIZE;
3902 dp->db_line_count = 0;
3903
3904 return hp;
3905}
3906
3907/*
3908 * create a new, empty, pointer block
3909 */
3910 static bhdr_T *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003911ml_new_ptr(memfile_T *mfp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003912{
3913 bhdr_T *hp;
3914 PTR_BL *pp;
3915
3916 if ((hp = mf_new(mfp, FALSE, 1)) == NULL)
3917 return NULL;
3918
3919 pp = (PTR_BL *)(hp->bh_data);
3920 pp->pb_id = PTR_ID;
3921 pp->pb_count = 0;
Bram Moolenaar20a825a2010-05-31 21:27:30 +02003922 pp->pb_count_max = (short_u)((mfp->mf_page_size - sizeof(PTR_BL))
3923 / sizeof(PTR_EN) + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003924
3925 return hp;
3926}
3927
3928/*
Bram Moolenaarc1a9bc12018-12-28 21:59:29 +01003929 * Lookup line 'lnum' in a memline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003930 *
3931 * action: if ML_DELETE or ML_INSERT the line count is updated while searching
3932 * if ML_FLUSH only flush a locked block
3933 * if ML_FIND just find the line
3934 *
3935 * If the block was found it is locked and put in ml_locked.
3936 * The stack is updated to lead to the locked block. The ip_high field in
3937 * the stack is updated to reflect the last line in the block AFTER the
3938 * insert or delete, also if the pointer block has not been updated yet. But
Bram Moolenaar6b803a72007-05-06 14:25:46 +00003939 * if ml_locked != NULL ml_locked_lineadd must be added to ip_high.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003940 *
3941 * return: NULL for failure, pointer to block header otherwise
3942 */
3943 static bhdr_T *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003944ml_find_line(buf_T *buf, linenr_T lnum, int action)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003945{
3946 DATA_BL *dp;
3947 PTR_BL *pp;
3948 infoptr_T *ip;
3949 bhdr_T *hp;
3950 memfile_T *mfp;
3951 linenr_T t;
3952 blocknr_T bnum, bnum2;
3953 int dirty;
3954 linenr_T low, high;
3955 int top;
3956 int page_count;
3957 int idx;
3958
3959 mfp = buf->b_ml.ml_mfp;
3960
3961 /*
3962 * If there is a locked block check if the wanted line is in it.
3963 * If not, flush and release the locked block.
3964 * Don't do this for ML_INSERT_SAME, because the stack need to be updated.
3965 * Don't do this for ML_FLUSH, because we want to flush the locked block.
Bram Moolenaar47b8b152007-02-07 02:41:57 +00003966 * Don't do this when 'swapfile' is reset, we want to load all the blocks.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003967 */
3968 if (buf->b_ml.ml_locked)
3969 {
Bram Moolenaar47b8b152007-02-07 02:41:57 +00003970 if (ML_SIMPLE(action)
3971 && buf->b_ml.ml_locked_low <= lnum
3972 && buf->b_ml.ml_locked_high >= lnum
3973 && !mf_dont_release)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003974 {
Bram Moolenaar47b8b152007-02-07 02:41:57 +00003975 /* remember to update pointer blocks and stack later */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003976 if (action == ML_INSERT)
3977 {
3978 ++(buf->b_ml.ml_locked_lineadd);
3979 ++(buf->b_ml.ml_locked_high);
3980 }
3981 else if (action == ML_DELETE)
3982 {
3983 --(buf->b_ml.ml_locked_lineadd);
3984 --(buf->b_ml.ml_locked_high);
3985 }
3986 return (buf->b_ml.ml_locked);
3987 }
3988
3989 mf_put(mfp, buf->b_ml.ml_locked, buf->b_ml.ml_flags & ML_LOCKED_DIRTY,
3990 buf->b_ml.ml_flags & ML_LOCKED_POS);
3991 buf->b_ml.ml_locked = NULL;
3992
Bram Moolenaar6b803a72007-05-06 14:25:46 +00003993 /*
3994 * If lines have been added or deleted in the locked block, need to
3995 * update the line count in pointer blocks.
3996 */
3997 if (buf->b_ml.ml_locked_lineadd != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003998 ml_lineadd(buf, buf->b_ml.ml_locked_lineadd);
3999 }
4000
4001 if (action == ML_FLUSH) /* nothing else to do */
4002 return NULL;
4003
4004 bnum = 1; /* start at the root of the tree */
4005 page_count = 1;
4006 low = 1;
4007 high = buf->b_ml.ml_line_count;
4008
4009 if (action == ML_FIND) /* first try stack entries */
4010 {
4011 for (top = buf->b_ml.ml_stack_top - 1; top >= 0; --top)
4012 {
4013 ip = &(buf->b_ml.ml_stack[top]);
4014 if (ip->ip_low <= lnum && ip->ip_high >= lnum)
4015 {
4016 bnum = ip->ip_bnum;
4017 low = ip->ip_low;
4018 high = ip->ip_high;
4019 buf->b_ml.ml_stack_top = top; /* truncate stack at prev entry */
4020 break;
4021 }
4022 }
4023 if (top < 0)
4024 buf->b_ml.ml_stack_top = 0; /* not found, start at the root */
4025 }
4026 else /* ML_DELETE or ML_INSERT */
4027 buf->b_ml.ml_stack_top = 0; /* start at the root */
4028
4029/*
4030 * search downwards in the tree until a data block is found
4031 */
4032 for (;;)
4033 {
4034 if ((hp = mf_get(mfp, bnum, page_count)) == NULL)
4035 goto error_noblock;
4036
4037 /*
4038 * update high for insert/delete
4039 */
4040 if (action == ML_INSERT)
4041 ++high;
4042 else if (action == ML_DELETE)
4043 --high;
4044
4045 dp = (DATA_BL *)(hp->bh_data);
4046 if (dp->db_id == DATA_ID) /* data block */
4047 {
4048 buf->b_ml.ml_locked = hp;
4049 buf->b_ml.ml_locked_low = low;
4050 buf->b_ml.ml_locked_high = high;
4051 buf->b_ml.ml_locked_lineadd = 0;
4052 buf->b_ml.ml_flags &= ~(ML_LOCKED_DIRTY | ML_LOCKED_POS);
4053 return hp;
4054 }
4055
4056 pp = (PTR_BL *)(dp); /* must be pointer block */
4057 if (pp->pb_id != PTR_ID)
4058 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004059 iemsg(_("E317: pointer block id wrong"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004060 goto error_block;
4061 }
4062
4063 if ((top = ml_add_stack(buf)) < 0) /* add new entry to stack */
4064 goto error_block;
4065 ip = &(buf->b_ml.ml_stack[top]);
4066 ip->ip_bnum = bnum;
4067 ip->ip_low = low;
4068 ip->ip_high = high;
4069 ip->ip_index = -1; /* index not known yet */
4070
4071 dirty = FALSE;
4072 for (idx = 0; idx < (int)pp->pb_count; ++idx)
4073 {
4074 t = pp->pb_pointer[idx].pe_line_count;
4075 CHECK(t == 0, _("pe_line_count is zero"));
4076 if ((low += t) > lnum)
4077 {
4078 ip->ip_index = idx;
4079 bnum = pp->pb_pointer[idx].pe_bnum;
4080 page_count = pp->pb_pointer[idx].pe_page_count;
4081 high = low - 1;
4082 low -= t;
4083
4084 /*
4085 * a negative block number may have been changed
4086 */
4087 if (bnum < 0)
4088 {
4089 bnum2 = mf_trans_del(mfp, bnum);
4090 if (bnum != bnum2)
4091 {
4092 bnum = bnum2;
4093 pp->pb_pointer[idx].pe_bnum = bnum;
4094 dirty = TRUE;
4095 }
4096 }
4097
4098 break;
4099 }
4100 }
4101 if (idx >= (int)pp->pb_count) /* past the end: something wrong! */
4102 {
4103 if (lnum > buf->b_ml.ml_line_count)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004104 siemsg(_("E322: line number out of range: %ld past the end"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00004105 lnum - buf->b_ml.ml_line_count);
4106
4107 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004108 siemsg(_("E323: line count wrong in block %ld"), bnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004109 goto error_block;
4110 }
4111 if (action == ML_DELETE)
4112 {
4113 pp->pb_pointer[idx].pe_line_count--;
4114 dirty = TRUE;
4115 }
4116 else if (action == ML_INSERT)
4117 {
4118 pp->pb_pointer[idx].pe_line_count++;
4119 dirty = TRUE;
4120 }
4121 mf_put(mfp, hp, dirty, FALSE);
4122 }
4123
4124error_block:
4125 mf_put(mfp, hp, FALSE, FALSE);
4126error_noblock:
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02004127 /*
4128 * If action is ML_DELETE or ML_INSERT we have to correct the tree for
4129 * the incremented/decremented line counts, because there won't be a line
4130 * inserted/deleted after all.
4131 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004132 if (action == ML_DELETE)
4133 ml_lineadd(buf, 1);
4134 else if (action == ML_INSERT)
4135 ml_lineadd(buf, -1);
4136 buf->b_ml.ml_stack_top = 0;
4137 return NULL;
4138}
4139
4140/*
4141 * add an entry to the info pointer stack
4142 *
4143 * return -1 for failure, number of the new entry otherwise
4144 */
4145 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004146ml_add_stack(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004147{
4148 int top;
4149 infoptr_T *newstack;
4150
4151 top = buf->b_ml.ml_stack_top;
4152
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02004153 /* may have to increase the stack size */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004154 if (top == buf->b_ml.ml_stack_size)
4155 {
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02004156 CHECK(top > 0, _("Stack size increases")); /* more than 5 levels??? */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004157
4158 newstack = (infoptr_T *)alloc((unsigned)sizeof(infoptr_T) *
4159 (buf->b_ml.ml_stack_size + STACK_INCR));
4160 if (newstack == NULL)
4161 return -1;
Bram Moolenaarfbd302f2015-08-08 18:23:46 +02004162 if (top > 0)
4163 mch_memmove(newstack, buf->b_ml.ml_stack,
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004164 (size_t)top * sizeof(infoptr_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004165 vim_free(buf->b_ml.ml_stack);
4166 buf->b_ml.ml_stack = newstack;
4167 buf->b_ml.ml_stack_size += STACK_INCR;
4168 }
4169
4170 buf->b_ml.ml_stack_top++;
4171 return top;
4172}
4173
4174/*
4175 * Update the pointer blocks on the stack for inserted/deleted lines.
4176 * The stack itself is also updated.
4177 *
4178 * When a insert/delete line action fails, the line is not inserted/deleted,
4179 * but the pointer blocks have already been updated. That is fixed here by
4180 * walking through the stack.
4181 *
4182 * Count is the number of lines added, negative if lines have been deleted.
4183 */
4184 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004185ml_lineadd(buf_T *buf, int count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004186{
4187 int idx;
4188 infoptr_T *ip;
4189 PTR_BL *pp;
4190 memfile_T *mfp = buf->b_ml.ml_mfp;
4191 bhdr_T *hp;
4192
4193 for (idx = buf->b_ml.ml_stack_top - 1; idx >= 0; --idx)
4194 {
4195 ip = &(buf->b_ml.ml_stack[idx]);
4196 if ((hp = mf_get(mfp, ip->ip_bnum, 1)) == NULL)
4197 break;
4198 pp = (PTR_BL *)(hp->bh_data); /* must be pointer block */
4199 if (pp->pb_id != PTR_ID)
4200 {
4201 mf_put(mfp, hp, FALSE, FALSE);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004202 iemsg(_("E317: pointer block id wrong 2"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004203 break;
4204 }
4205 pp->pb_pointer[ip->ip_index].pe_line_count += count;
4206 ip->ip_high += count;
4207 mf_put(mfp, hp, TRUE, FALSE);
4208 }
4209}
4210
Bram Moolenaar55debbe2010-05-23 23:34:36 +02004211#if defined(HAVE_READLINK) || defined(PROTO)
Bram Moolenaar900b4d72005-12-12 22:05:50 +00004212/*
4213 * Resolve a symlink in the last component of a file name.
4214 * Note that f_resolve() does it for every part of the path, we don't do that
4215 * here.
4216 * If it worked returns OK and the resolved link in "buf[MAXPATHL]".
4217 * Otherwise returns FAIL.
4218 */
Bram Moolenaar55debbe2010-05-23 23:34:36 +02004219 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004220resolve_symlink(char_u *fname, char_u *buf)
Bram Moolenaar900b4d72005-12-12 22:05:50 +00004221{
4222 char_u tmp[MAXPATHL];
4223 int ret;
4224 int depth = 0;
4225
4226 if (fname == NULL)
4227 return FAIL;
4228
4229 /* Put the result so far in tmp[], starting with the original name. */
4230 vim_strncpy(tmp, fname, MAXPATHL - 1);
4231
4232 for (;;)
4233 {
4234 /* Limit symlink depth to 100, catch recursive loops. */
4235 if (++depth == 100)
4236 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004237 semsg(_("E773: Symlink loop for \"%s\""), fname);
Bram Moolenaar900b4d72005-12-12 22:05:50 +00004238 return FAIL;
4239 }
4240
4241 ret = readlink((char *)tmp, (char *)buf, MAXPATHL - 1);
4242 if (ret <= 0)
4243 {
Bram Moolenaarcc984262005-12-23 22:19:46 +00004244 if (errno == EINVAL || errno == ENOENT)
Bram Moolenaar900b4d72005-12-12 22:05:50 +00004245 {
Bram Moolenaarcc984262005-12-23 22:19:46 +00004246 /* Found non-symlink or not existing file, stop here.
Bram Moolenaar9439cdd2009-04-22 13:39:36 +00004247 * When at the first level use the unmodified name, skip the
Bram Moolenaar900b4d72005-12-12 22:05:50 +00004248 * call to vim_FullName(). */
4249 if (depth == 1)
4250 return FAIL;
4251
4252 /* Use the resolved name in tmp[]. */
4253 break;
4254 }
4255
4256 /* There must be some error reading links, use original name. */
4257 return FAIL;
4258 }
4259 buf[ret] = NUL;
4260
4261 /*
4262 * Check whether the symlink is relative or absolute.
4263 * If it's relative, build a new path based on the directory
4264 * portion of the filename (if any) and the path the symlink
4265 * points to.
4266 */
4267 if (mch_isFullName(buf))
4268 STRCPY(tmp, buf);
4269 else
4270 {
4271 char_u *tail;
4272
4273 tail = gettail(tmp);
4274 if (STRLEN(tail) + STRLEN(buf) >= MAXPATHL)
4275 return FAIL;
4276 STRCPY(tail, buf);
4277 }
4278 }
4279
4280 /*
4281 * Try to resolve the full name of the file so that the swapfile name will
4282 * be consistent even when opening a relative symlink from different
4283 * working directories.
4284 */
4285 return vim_FullName(tmp, buf, MAXPATHL, TRUE);
4286}
4287#endif
4288
Bram Moolenaar071d4272004-06-13 20:20:40 +00004289/*
Bram Moolenaar04a09c12005-08-01 22:02:32 +00004290 * Make swap file name out of the file name and a directory name.
4291 * Returns pointer to allocated memory or NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004292 */
Bram Moolenaar04a09c12005-08-01 22:02:32 +00004293 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004294makeswapname(
4295 char_u *fname,
4296 char_u *ffname UNUSED,
4297 buf_T *buf,
4298 char_u *dir_name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004299{
4300 char_u *r, *s;
Bram Moolenaar9dbe4752010-05-14 17:52:42 +02004301 char_u *fname_res = fname;
Bram Moolenaar900b4d72005-12-12 22:05:50 +00004302#ifdef HAVE_READLINK
4303 char_u fname_buf[MAXPATHL];
Bram Moolenaar900b4d72005-12-12 22:05:50 +00004304#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004305
Bram Moolenaar4f974752019-02-17 17:44:42 +01004306#if defined(UNIX) || defined(MSWIN) // Need _very_ long file names
Bram Moolenaarb113c3a2017-02-28 21:26:17 +01004307 int len = (int)STRLEN(dir_name);
Bram Moolenaarc525e3a2017-02-18 16:59:02 +01004308
4309 s = dir_name + len;
4310 if (after_pathsep(dir_name, s) && len > 1 && s[-1] == s[-2])
Bram Moolenaar071d4272004-06-13 20:20:40 +00004311 { /* Ends with '//', Use Full path */
4312 r = NULL;
Bram Moolenaar04a09c12005-08-01 22:02:32 +00004313 if ((s = make_percent_swname(dir_name, fname)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004314 {
4315 r = modname(s, (char_u *)".swp", FALSE);
4316 vim_free(s);
4317 }
4318 return r;
4319 }
4320#endif
4321
Bram Moolenaar900b4d72005-12-12 22:05:50 +00004322#ifdef HAVE_READLINK
4323 /* Expand symlink in the file name, so that we put the swap file with the
4324 * actual file instead of with the symlink. */
4325 if (resolve_symlink(fname, fname_buf) == OK)
4326 fname_res = fname_buf;
Bram Moolenaar900b4d72005-12-12 22:05:50 +00004327#endif
4328
Bram Moolenaar071d4272004-06-13 20:20:40 +00004329 r = buf_modname(
Bram Moolenaar071d4272004-06-13 20:20:40 +00004330 (buf->b_p_sn || buf->b_shortname),
Bram Moolenaar900b4d72005-12-12 22:05:50 +00004331 fname_res,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004332 (char_u *)
Bram Moolenaare60acc12011-05-10 16:41:25 +02004333#if defined(VMS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004334 "_swp",
4335#else
4336 ".swp",
4337#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004338 /* Prepend a '.' to the swap file name for the current directory. */
Bram Moolenaar48e330a2016-02-23 14:53:34 +01004339 dir_name[0] == '.' && dir_name[1] == NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004340 if (r == NULL) /* out of memory */
4341 return NULL;
4342
4343 s = get_file_in_dir(r, dir_name);
4344 vim_free(r);
4345 return s;
4346}
4347
4348/*
4349 * Get file name to use for swap file or backup file.
4350 * Use the name of the edited file "fname" and an entry in the 'dir' or 'bdir'
4351 * option "dname".
4352 * - If "dname" is ".", return "fname" (swap file in dir of file).
4353 * - If "dname" starts with "./", insert "dname" in "fname" (swap file
4354 * relative to dir of file).
4355 * - Otherwise, prepend "dname" to the tail of "fname" (swap file in specific
4356 * dir).
4357 *
4358 * The return value is an allocated string and can be NULL.
4359 */
4360 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004361get_file_in_dir(
4362 char_u *fname,
4363 char_u *dname) /* don't use "dirname", it is a global for Alpha */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004364{
4365 char_u *t;
4366 char_u *tail;
4367 char_u *retval;
4368 int save_char;
4369
4370 tail = gettail(fname);
4371
4372 if (dname[0] == '.' && dname[1] == NUL)
4373 retval = vim_strsave(fname);
4374 else if (dname[0] == '.' && vim_ispathsep(dname[1]))
4375 {
4376 if (tail == fname) /* no path before file name */
4377 retval = concat_fnames(dname + 2, tail, TRUE);
4378 else
4379 {
4380 save_char = *tail;
4381 *tail = NUL;
4382 t = concat_fnames(fname, dname + 2, TRUE);
4383 *tail = save_char;
4384 if (t == NULL) /* out of memory */
4385 retval = NULL;
4386 else
4387 {
4388 retval = concat_fnames(t, tail, TRUE);
4389 vim_free(t);
4390 }
4391 }
4392 }
4393 else
4394 retval = concat_fnames(dname, tail, TRUE);
4395
Bram Moolenaar4f974752019-02-17 17:44:42 +01004396#ifdef MSWIN
Bram Moolenaar69c35002013-11-04 02:54:12 +01004397 if (retval != NULL)
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004398 for (t = gettail(retval); *t != NUL; MB_PTR_ADV(t))
Bram Moolenaar69c35002013-11-04 02:54:12 +01004399 if (*t == ':')
4400 *t = '%';
4401#endif
4402
Bram Moolenaar071d4272004-06-13 20:20:40 +00004403 return retval;
4404}
4405
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00004406/*
4407 * Print the ATTENTION message: info about an existing swap file.
4408 */
4409 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004410attention_message(
4411 buf_T *buf, /* buffer being edited */
4412 char_u *fname) /* swap file name */
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00004413{
Bram Moolenaar8767f522016-07-01 17:17:39 +02004414 stat_T st;
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00004415 time_t x, sx;
Bram Moolenaar31e97bf2006-10-10 14:20:13 +00004416 char *p;
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00004417
4418 ++no_wait_return;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004419 (void)emsg(_("E325: ATTENTION"));
Bram Moolenaar32526b32019-01-19 17:43:09 +01004420 msg_puts(_("\nFound a swap file by the name \""));
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00004421 msg_home_replace(fname);
Bram Moolenaar32526b32019-01-19 17:43:09 +01004422 msg_puts("\"\n");
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00004423 sx = swapfile_info(fname);
Bram Moolenaar32526b32019-01-19 17:43:09 +01004424 msg_puts(_("While opening file \""));
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00004425 msg_outtrans(buf->b_fname);
Bram Moolenaar32526b32019-01-19 17:43:09 +01004426 msg_puts("\"\n");
Bram Moolenaard6105cb2018-10-13 19:06:27 +02004427 if (mch_stat((char *)buf->b_fname, &st) == -1)
4428 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004429 msg_puts(_(" CANNOT BE FOUND"));
Bram Moolenaard6105cb2018-10-13 19:06:27 +02004430 }
4431 else
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00004432 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004433 msg_puts(_(" dated: "));
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00004434 x = st.st_mtime; /* Manx C can't do &st.st_mtime */
Bram Moolenaar31e97bf2006-10-10 14:20:13 +00004435 p = ctime(&x); /* includes '\n' */
4436 if (p == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004437 msg_puts("(invalid)\n");
Bram Moolenaar31e97bf2006-10-10 14:20:13 +00004438 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01004439 msg_puts(p);
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00004440 if (sx != 0 && x > sx)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004441 msg_puts(_(" NEWER than swap file!\n"));
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00004442 }
4443 /* Some of these messages are long to allow translation to
4444 * other languages. */
Bram Moolenaar32526b32019-01-19 17:43:09 +01004445 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. Quit, or continue with caution.\n"));
4446 msg_puts(_("(2) An edit session for this file crashed.\n"));
4447 msg_puts(_(" If this is the case, use \":recover\" or \"vim -r "));
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00004448 msg_outtrans(buf->b_fname);
Bram Moolenaar32526b32019-01-19 17:43:09 +01004449 msg_puts(_("\"\n to recover the changes (see \":help recovery\").\n"));
4450 msg_puts(_(" If you did this already, delete the swap file \""));
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00004451 msg_outtrans(fname);
Bram Moolenaar32526b32019-01-19 17:43:09 +01004452 msg_puts(_("\"\n to avoid this message.\n"));
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00004453 cmdline_row = msg_row;
4454 --no_wait_return;
4455}
4456
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004457#if defined(FEAT_EVAL)
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00004458/*
4459 * Trigger the SwapExists autocommands.
4460 * Returns a value for equivalent to do_dialog() (see below):
4461 * 0: still need to ask for a choice
4462 * 1: open read-only
4463 * 2: edit anyway
4464 * 3: recover
4465 * 4: delete it
4466 * 5: quit
4467 * 6: abort
4468 */
4469 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004470do_swapexists(buf_T *buf, char_u *fname)
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00004471{
4472 set_vim_var_string(VV_SWAPNAME, fname, -1);
4473 set_vim_var_string(VV_SWAPCHOICE, NULL, -1);
4474
4475 /* Trigger SwapExists autocommands with <afile> set to the file being
Bram Moolenaar12c22ce2009-04-22 13:58:46 +00004476 * edited. Disallow changing directory here. */
4477 ++allbuf_lock;
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00004478 apply_autocmds(EVENT_SWAPEXISTS, buf->b_fname, NULL, FALSE, NULL);
Bram Moolenaar12c22ce2009-04-22 13:58:46 +00004479 --allbuf_lock;
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00004480
4481 set_vim_var_string(VV_SWAPNAME, NULL, -1);
4482
4483 switch (*get_vim_var_str(VV_SWAPCHOICE))
4484 {
4485 case 'o': return 1;
4486 case 'e': return 2;
4487 case 'r': return 3;
4488 case 'd': return 4;
4489 case 'q': return 5;
4490 case 'a': return 6;
4491 }
4492
4493 return 0;
4494}
4495#endif
4496
Bram Moolenaar071d4272004-06-13 20:20:40 +00004497/*
4498 * Find out what name to use for the swap file for buffer 'buf'.
4499 *
4500 * Several names are tried to find one that does not exist
Bram Moolenaar04a09c12005-08-01 22:02:32 +00004501 * Returns the name in allocated memory or NULL.
Bram Moolenaarf541c362011-10-26 11:44:18 +02004502 * When out of memory "dirp" is set to NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004503 *
4504 * Note: If BASENAMELEN is not correct, you will get error messages for
Bram Moolenaar55debbe2010-05-23 23:34:36 +02004505 * not being able to open the swap or undo file
Bram Moolenaar12c22ce2009-04-22 13:58:46 +00004506 * Note: May trigger SwapExists autocmd, pointers may change!
Bram Moolenaar071d4272004-06-13 20:20:40 +00004507 */
4508 static char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004509findswapname(
4510 buf_T *buf,
4511 char_u **dirp, /* pointer to list of directories */
4512 char_u *old_fname) /* don't give warning for this file name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004513{
4514 char_u *fname;
4515 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004516 char_u *dir_name;
4517#ifdef AMIGA
4518 BPTR fh;
4519#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004520 int r;
Bram Moolenaar69c35002013-11-04 02:54:12 +01004521 char_u *buf_fname = buf->b_fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004522
Bram Moolenaar48e330a2016-02-23 14:53:34 +01004523#if !defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004524# define CREATE_DUMMY_FILE
4525 FILE *dummyfd = NULL;
4526
Bram Moolenaar4f974752019-02-17 17:44:42 +01004527# ifdef MSWIN
Bram Moolenaar69c35002013-11-04 02:54:12 +01004528 if (buf_fname != NULL && !mch_isFullName(buf_fname)
4529 && vim_strchr(gettail(buf_fname), ':'))
4530 {
4531 char_u *t;
4532
4533 buf_fname = vim_strsave(buf_fname);
4534 if (buf_fname == NULL)
4535 buf_fname = buf->b_fname;
4536 else
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004537 for (t = gettail(buf_fname); *t != NUL; MB_PTR_ADV(t))
Bram Moolenaar69c35002013-11-04 02:54:12 +01004538 if (*t == ':')
4539 *t = '%';
4540 }
4541# endif
4542
Bram Moolenaar55debbe2010-05-23 23:34:36 +02004543 /*
4544 * If we start editing a new file, e.g. "test.doc", which resides on an
4545 * MSDOS compatible filesystem, it is possible that the file
4546 * "test.doc.swp" which we create will be exactly the same file. To avoid
4547 * this problem we temporarily create "test.doc". Don't do this when the
4548 * check below for a 8.3 file name is used.
4549 */
Bram Moolenaar69c35002013-11-04 02:54:12 +01004550 if (!(buf->b_p_sn || buf->b_shortname) && buf_fname != NULL
4551 && mch_getperm(buf_fname) < 0)
4552 dummyfd = mch_fopen((char *)buf_fname, "w");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004553#endif
4554
Bram Moolenaar55debbe2010-05-23 23:34:36 +02004555 /*
4556 * Isolate a directory name from *dirp and put it in dir_name.
4557 * First allocate some memory to put the directory name in.
4558 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004559 dir_name = alloc((unsigned)STRLEN(*dirp) + 1);
Bram Moolenaarf541c362011-10-26 11:44:18 +02004560 if (dir_name == NULL)
4561 *dirp = NULL;
4562 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004563 (void)copy_option_part(dirp, dir_name, 31000, ",");
4564
Bram Moolenaar55debbe2010-05-23 23:34:36 +02004565 /*
4566 * we try different names until we find one that does not exist yet
4567 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004568 if (dir_name == NULL) /* out of memory */
4569 fname = NULL;
4570 else
Bram Moolenaar69c35002013-11-04 02:54:12 +01004571 fname = makeswapname(buf_fname, buf->b_ffname, buf, dir_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004572
4573 for (;;)
4574 {
4575 if (fname == NULL) /* must be out of memory */
4576 break;
4577 if ((n = (int)STRLEN(fname)) == 0) /* safety check */
4578 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01004579 VIM_CLEAR(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004580 break;
4581 }
Bram Moolenaar48e330a2016-02-23 14:53:34 +01004582#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004583/*
4584 * Some systems have a MS-DOS compatible filesystem that use 8.3 character
4585 * file names. If this is the first try and the swap file name does not fit in
4586 * 8.3, detect if this is the case, set shortname and try again.
4587 */
4588 if (fname[n - 2] == 'w' && fname[n - 1] == 'p'
4589 && !(buf->b_p_sn || buf->b_shortname))
4590 {
4591 char_u *tail;
4592 char_u *fname2;
Bram Moolenaar8767f522016-07-01 17:17:39 +02004593 stat_T s1, s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004594 int f1, f2;
4595 int created1 = FALSE, created2 = FALSE;
4596 int same = FALSE;
4597
4598 /*
4599 * Check if swapfile name does not fit in 8.3:
4600 * It either contains two dots, is longer than 8 chars, or starts
4601 * with a dot.
4602 */
Bram Moolenaar69c35002013-11-04 02:54:12 +01004603 tail = gettail(buf_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004604 if ( vim_strchr(tail, '.') != NULL
4605 || STRLEN(tail) > (size_t)8
4606 || *gettail(fname) == '.')
4607 {
4608 fname2 = alloc(n + 2);
4609 if (fname2 != NULL)
4610 {
4611 STRCPY(fname2, fname);
4612 /* if fname == "xx.xx.swp", fname2 = "xx.xx.swx"
4613 * if fname == ".xx.swp", fname2 = ".xx.swpx"
4614 * if fname == "123456789.swp", fname2 = "12345678x.swp"
4615 */
4616 if (vim_strchr(tail, '.') != NULL)
4617 fname2[n - 1] = 'x';
4618 else if (*gettail(fname) == '.')
4619 {
4620 fname2[n] = 'x';
4621 fname2[n + 1] = NUL;
4622 }
4623 else
4624 fname2[n - 5] += 1;
4625 /*
4626 * may need to create the files to be able to use mch_stat()
4627 */
4628 f1 = mch_open((char *)fname, O_RDONLY | O_EXTRA, 0);
4629 if (f1 < 0)
4630 {
4631 f1 = mch_open_rw((char *)fname,
4632 O_RDWR|O_CREAT|O_EXCL|O_EXTRA);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004633 created1 = TRUE;
4634 }
4635 if (f1 >= 0)
4636 {
4637 f2 = mch_open((char *)fname2, O_RDONLY | O_EXTRA, 0);
4638 if (f2 < 0)
4639 {
4640 f2 = mch_open_rw((char *)fname2,
4641 O_RDWR|O_CREAT|O_EXCL|O_EXTRA);
4642 created2 = TRUE;
4643 }
4644 if (f2 >= 0)
4645 {
4646 /*
4647 * Both files exist now. If mch_stat() returns the
4648 * same device and inode they are the same file.
4649 */
4650 if (mch_fstat(f1, &s1) != -1
4651 && mch_fstat(f2, &s2) != -1
4652 && s1.st_dev == s2.st_dev
4653 && s1.st_ino == s2.st_ino)
4654 same = TRUE;
4655 close(f2);
4656 if (created2)
4657 mch_remove(fname2);
4658 }
4659 close(f1);
4660 if (created1)
4661 mch_remove(fname);
4662 }
4663 vim_free(fname2);
4664 if (same)
4665 {
4666 buf->b_shortname = TRUE;
4667 vim_free(fname);
Bram Moolenaar69c35002013-11-04 02:54:12 +01004668 fname = makeswapname(buf_fname, buf->b_ffname,
Bram Moolenaar04a09c12005-08-01 22:02:32 +00004669 buf, dir_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004670 continue; /* try again with b_shortname set */
4671 }
4672 }
4673 }
4674 }
4675#endif
4676 /*
4677 * check if the swapfile already exists
4678 */
4679 if (mch_getperm(fname) < 0) /* it does not exist */
4680 {
4681#ifdef HAVE_LSTAT
Bram Moolenaar8767f522016-07-01 17:17:39 +02004682 stat_T sb;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004683
4684 /*
4685 * Extra security check: When a swap file is a symbolic link, this
4686 * is most likely a symlink attack.
4687 */
4688 if (mch_lstat((char *)fname, &sb) < 0)
4689#else
4690# ifdef AMIGA
4691 fh = Open((UBYTE *)fname, (long)MODE_NEWFILE);
4692 /*
4693 * on the Amiga mch_getperm() will return -1 when the file exists
4694 * but is being used by another program. This happens if you edit
4695 * a file twice.
4696 */
4697 if (fh != (BPTR)NULL) /* can open file, OK */
4698 {
4699 Close(fh);
4700 mch_remove(fname);
4701 break;
4702 }
4703 if (IoErr() != ERROR_OBJECT_IN_USE
4704 && IoErr() != ERROR_OBJECT_EXISTS)
4705# endif
4706#endif
4707 break;
4708 }
4709
4710 /*
4711 * A file name equal to old_fname is OK to use.
4712 */
4713 if (old_fname != NULL && fnamecmp(fname, old_fname) == 0)
4714 break;
4715
4716 /*
4717 * get here when file already exists
4718 */
4719 if (fname[n - 2] == 'w' && fname[n - 1] == 'p') /* first try */
4720 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004721 /*
4722 * on MS-DOS compatible filesystems (e.g. messydos) file.doc.swp
4723 * and file.doc are the same file. To guess if this problem is
4724 * present try if file.doc.swx exists. If it does, we set
4725 * buf->b_shortname and try file_doc.swp (dots replaced by
4726 * underscores for this file), and try again. If it doesn't we
4727 * assume that "file.doc.swp" already exists.
4728 */
4729 if (!(buf->b_p_sn || buf->b_shortname)) /* not tried yet */
4730 {
4731 fname[n - 1] = 'x';
4732 r = mch_getperm(fname); /* try "file.swx" */
4733 fname[n - 1] = 'p';
4734 if (r >= 0) /* "file.swx" seems to exist */
4735 {
4736 buf->b_shortname = TRUE;
4737 vim_free(fname);
Bram Moolenaar69c35002013-11-04 02:54:12 +01004738 fname = makeswapname(buf_fname, buf->b_ffname,
Bram Moolenaar04a09c12005-08-01 22:02:32 +00004739 buf, dir_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004740 continue; /* try again with '.' replaced with '_' */
4741 }
4742 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004743 /*
4744 * If we get here the ".swp" file really exists.
4745 * Give an error message, unless recovering, no file name, we are
4746 * viewing a help file or when the path of the file is different
4747 * (happens when all .swp files are in one directory).
4748 */
Bram Moolenaar69c35002013-11-04 02:54:12 +01004749 if (!recoverymode && buf_fname != NULL
Bram Moolenaar8fc061c2004-12-29 21:03:02 +00004750 && !buf->b_help && !(buf->b_flags & BF_DUMMY))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004751 {
4752 int fd;
4753 struct block0 b0;
4754 int differ = FALSE;
4755
4756 /*
4757 * Try to read block 0 from the swap file to get the original
4758 * file name (and inode number).
4759 */
4760 fd = mch_open((char *)fname, O_RDONLY | O_EXTRA, 0);
4761 if (fd >= 0)
4762 {
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01004763 if (read_eintr(fd, &b0, sizeof(b0)) == sizeof(b0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004764 {
4765 /*
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004766 * If the swapfile has the same directory as the
4767 * buffer don't compare the directory names, they can
4768 * have a different mountpoint.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004769 */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004770 if (b0.b0_flags & B0_SAME_DIR)
4771 {
4772 if (fnamecmp(gettail(buf->b_ffname),
4773 gettail(b0.b0_fname)) != 0
4774 || !same_directory(fname, buf->b_ffname))
Bram Moolenaar900b4d72005-12-12 22:05:50 +00004775 {
4776#ifdef CHECK_INODE
4777 /* Symlinks may point to the same file even
4778 * when the name differs, need to check the
4779 * inode too. */
4780 expand_env(b0.b0_fname, NameBuff, MAXPATHL);
4781 if (fnamecmp_ino(buf->b_ffname, NameBuff,
4782 char_to_long(b0.b0_ino)))
4783#endif
4784 differ = TRUE;
4785 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004786 }
4787 else
4788 {
4789 /*
4790 * The name in the swap file may be
4791 * "~user/path/file". Expand it first.
4792 */
4793 expand_env(b0.b0_fname, NameBuff, MAXPATHL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004794#ifdef CHECK_INODE
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004795 if (fnamecmp_ino(buf->b_ffname, NameBuff,
Bram Moolenaar900b4d72005-12-12 22:05:50 +00004796 char_to_long(b0.b0_ino)))
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004797 differ = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004798#else
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004799 if (fnamecmp(NameBuff, buf->b_ffname) != 0)
4800 differ = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004801#endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004802 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004803 }
4804 close(fd);
4805 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004806
4807 /* give the ATTENTION message when there is an old swap file
4808 * for the current file, and the buffer was not recovered. */
4809 if (differ == FALSE && !(curbuf->b_flags & BF_RECOVERED)
4810 && vim_strchr(p_shm, SHM_ATTENTION) == NULL)
4811 {
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00004812 int choice = 0;
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02004813 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004814#ifdef CREATE_DUMMY_FILE
4815 int did_use_dummy = FALSE;
4816
4817 /* Avoid getting a warning for the file being created
4818 * outside of Vim, it was created at the start of this
4819 * function. Delete the file now, because Vim might exit
4820 * here if the window is closed. */
4821 if (dummyfd != NULL)
4822 {
4823 fclose(dummyfd);
4824 dummyfd = NULL;
Bram Moolenaar69c35002013-11-04 02:54:12 +01004825 mch_remove(buf_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004826 did_use_dummy = TRUE;
4827 }
4828#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004829
Bram Moolenaar1b243ea2019-04-28 22:50:40 +02004830#ifdef HAVE_PROCESS_STILL_RUNNING
Bram Moolenaar071d4272004-06-13 20:20:40 +00004831 process_still_running = FALSE;
4832#endif
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02004833 // It's safe to delete the swap file if all these are true:
4834 // - the edited file exists
4835 // - the swap file has no changes and looks OK
4836 if (mch_stat((char *)buf->b_fname, &st) == 0
4837 && swapfile_unchanged(fname))
4838 {
4839 choice = 4;
4840 if (p_verbose > 0)
4841 verb_msg(_("Found a swap file that is not useful, deleting it"));
4842 }
4843
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004844#if defined(FEAT_EVAL)
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00004845 /*
4846 * If there is an SwapExists autocommand and we can handle
4847 * the response, trigger it. It may return 0 to ask the
4848 * user anyway.
4849 */
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02004850 if (choice == 0
4851 && swap_exists_action != SEA_NONE
Bram Moolenaar69c35002013-11-04 02:54:12 +01004852 && has_autocmd(EVENT_SWAPEXISTS, buf_fname, buf))
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00004853 choice = do_swapexists(buf, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004854
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00004855 if (choice == 0)
4856#endif
4857 {
4858#ifdef FEAT_GUI
Bram Moolenaar798184c2018-10-07 20:48:39 +02004859 // If we are supposed to start the GUI but it wasn't
4860 // completely started yet, start it now. This makes
4861 // the messages displayed in the Vim window when
4862 // loading a session from the .gvimrc file.
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00004863 if (gui.starting && !gui.in_use)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004864 gui_start(NULL);
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00004865#endif
Bram Moolenaar798184c2018-10-07 20:48:39 +02004866 // Show info about the existing swap file.
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00004867 attention_message(buf, fname);
4868
Bram Moolenaar798184c2018-10-07 20:48:39 +02004869 // We don't want a 'q' typed at the more-prompt
4870 // interrupt loading a file.
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00004871 got_int = FALSE;
Bram Moolenaar798184c2018-10-07 20:48:39 +02004872
4873 // If vimrc has "simalt ~x" we don't want it to
4874 // interfere with the prompt here.
Bram Moolenaar6a2633b2018-10-07 23:16:36 +02004875 flush_buffers(FLUSH_TYPEAHEAD);
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00004876 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004877
4878#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00004879 if (swap_exists_action != SEA_NONE && choice == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004880 {
4881 char_u *name;
4882
4883 name = alloc((unsigned)(STRLEN(fname)
4884 + STRLEN(_("Swap file \""))
4885 + STRLEN(_("\" already exists!")) + 5));
4886 if (name != NULL)
4887 {
4888 STRCPY(name, _("Swap file \""));
4889 home_replace(NULL, fname, name + STRLEN(name),
4890 1000, TRUE);
4891 STRCAT(name, _("\" already exists!"));
4892 }
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00004893 choice = do_dialog(VIM_WARNING,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004894 (char_u *)_("VIM - ATTENTION"),
4895 name == NULL
4896 ? (char_u *)_("Swap file already exists!")
4897 : name,
Bram Moolenaar1b243ea2019-04-28 22:50:40 +02004898# ifdef HAVE_PROCESS_STILL_RUNNING
Bram Moolenaar071d4272004-06-13 20:20:40 +00004899 process_still_running
4900 ? (char_u *)_("&Open Read-Only\n&Edit anyway\n&Recover\n&Quit\n&Abort") :
4901# endif
Bram Moolenaard2c340a2011-01-17 20:08:11 +01004902 (char_u *)_("&Open Read-Only\n&Edit anyway\n&Recover\n&Delete it\n&Quit\n&Abort"), 1, NULL, FALSE);
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00004903
Bram Moolenaar1b243ea2019-04-28 22:50:40 +02004904# ifdef HAVE_PROCESS_STILL_RUNNING
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00004905 if (process_still_running && choice >= 4)
4906 choice++; /* Skip missing "Delete it" button */
4907# endif
4908 vim_free(name);
4909
4910 /* pretend screen didn't scroll, need redraw anyway */
4911 msg_scrolled = 0;
4912 redraw_all_later(NOT_VALID);
4913 }
4914#endif
4915
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00004916 if (choice > 0)
4917 {
4918 switch (choice)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004919 {
4920 case 1:
4921 buf->b_p_ro = TRUE;
4922 break;
4923 case 2:
4924 break;
4925 case 3:
4926 swap_exists_action = SEA_RECOVER;
4927 break;
4928 case 4:
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00004929 mch_remove(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004930 break;
4931 case 5:
4932 swap_exists_action = SEA_QUIT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004933 break;
4934 case 6:
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00004935 swap_exists_action = SEA_QUIT;
4936 got_int = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004937 break;
4938 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004939
4940 /* If the file was deleted this fname can be used. */
4941 if (mch_getperm(fname) < 0)
4942 break;
4943 }
4944 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004945 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004946 msg_puts("\n");
Bram Moolenaar4770d092006-01-12 23:22:24 +00004947 if (msg_silent == 0)
4948 /* call wait_return() later */
4949 need_wait_return = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004950 }
4951
4952#ifdef CREATE_DUMMY_FILE
4953 /* Going to try another name, need the dummy file again. */
4954 if (did_use_dummy)
Bram Moolenaar69c35002013-11-04 02:54:12 +01004955 dummyfd = mch_fopen((char *)buf_fname, "w");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004956#endif
4957 }
4958 }
4959 }
4960
4961 /*
4962 * Change the ".swp" extension to find another file that can be used.
4963 * First decrement the last char: ".swo", ".swn", etc.
4964 * If that still isn't enough decrement the last but one char: ".svz"
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004965 * Can happen when editing many "No Name" buffers.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004966 */
4967 if (fname[n - 1] == 'a') /* ".s?a" */
4968 {
4969 if (fname[n - 2] == 'a') /* ".saa": tried enough, give up */
4970 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004971 emsg(_("E326: Too many swap files found"));
Bram Moolenaard23a8232018-02-10 18:45:26 +01004972 VIM_CLEAR(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004973 break;
4974 }
4975 --fname[n - 2]; /* ".svz", ".suz", etc. */
4976 fname[n - 1] = 'z' + 1;
4977 }
4978 --fname[n - 1]; /* ".swo", ".swn", etc. */
4979 }
4980
4981 vim_free(dir_name);
4982#ifdef CREATE_DUMMY_FILE
4983 if (dummyfd != NULL) /* file has been created temporarily */
4984 {
4985 fclose(dummyfd);
Bram Moolenaar69c35002013-11-04 02:54:12 +01004986 mch_remove(buf_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004987 }
4988#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01004989#ifdef MSWIN
Bram Moolenaar69c35002013-11-04 02:54:12 +01004990 if (buf_fname != buf->b_fname)
4991 vim_free(buf_fname);
4992#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004993 return fname;
4994}
4995
4996 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004997b0_magic_wrong(ZERO_BL *b0p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004998{
4999 return (b0p->b0_magic_long != (long)B0_MAGIC_LONG
5000 || b0p->b0_magic_int != (int)B0_MAGIC_INT
5001 || b0p->b0_magic_short != (short)B0_MAGIC_SHORT
5002 || b0p->b0_magic_char != B0_MAGIC_CHAR);
5003}
5004
5005#ifdef CHECK_INODE
5006/*
5007 * Compare current file name with file name from swap file.
5008 * Try to use inode numbers when possible.
5009 * Return non-zero when files are different.
5010 *
5011 * When comparing file names a few things have to be taken into consideration:
5012 * - When working over a network the full path of a file depends on the host.
5013 * We check the inode number if possible. It is not 100% reliable though,
5014 * because the device number cannot be used over a network.
5015 * - When a file does not exist yet (editing a new file) there is no inode
5016 * number.
5017 * - The file name in a swap file may not be valid on the current host. The
5018 * "~user" form is used whenever possible to avoid this.
5019 *
5020 * This is getting complicated, let's make a table:
5021 *
5022 * ino_c ino_s fname_c fname_s differ =
5023 *
5024 * both files exist -> compare inode numbers:
5025 * != 0 != 0 X X ino_c != ino_s
5026 *
5027 * inode number(s) unknown, file names available -> compare file names
5028 * == 0 X OK OK fname_c != fname_s
5029 * X == 0 OK OK fname_c != fname_s
5030 *
5031 * current file doesn't exist, file for swap file exist, file name(s) not
5032 * available -> probably different
5033 * == 0 != 0 FAIL X TRUE
5034 * == 0 != 0 X FAIL TRUE
5035 *
5036 * current file exists, inode for swap unknown, file name(s) not
5037 * available -> probably different
5038 * != 0 == 0 FAIL X TRUE
5039 * != 0 == 0 X FAIL TRUE
5040 *
5041 * current file doesn't exist, inode for swap unknown, one file name not
5042 * available -> probably different
5043 * == 0 == 0 FAIL OK TRUE
5044 * == 0 == 0 OK FAIL TRUE
5045 *
5046 * current file doesn't exist, inode for swap unknown, both file names not
Bram Moolenaar8c3169c2018-05-12 17:04:12 +02005047 * available -> compare file names
5048 * == 0 == 0 FAIL FAIL fname_c != fname_s
Bram Moolenaar071d4272004-06-13 20:20:40 +00005049 *
5050 * Note that when the ino_t is 64 bits, only the last 32 will be used. This
5051 * can't be changed without making the block 0 incompatible with 32 bit
5052 * versions.
5053 */
5054
5055 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01005056fnamecmp_ino(
5057 char_u *fname_c, /* current file name */
5058 char_u *fname_s, /* file name from swap file */
5059 long ino_block0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005060{
Bram Moolenaar8767f522016-07-01 17:17:39 +02005061 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005062 ino_t ino_c = 0; /* ino of current file */
5063 ino_t ino_s; /* ino of file from swap file */
5064 char_u buf_c[MAXPATHL]; /* full path of fname_c */
5065 char_u buf_s[MAXPATHL]; /* full path of fname_s */
5066 int retval_c; /* flag: buf_c valid */
5067 int retval_s; /* flag: buf_s valid */
5068
5069 if (mch_stat((char *)fname_c, &st) == 0)
5070 ino_c = (ino_t)st.st_ino;
5071
5072 /*
5073 * First we try to get the inode from the file name, because the inode in
5074 * the swap file may be outdated. If that fails (e.g. this path is not
5075 * valid on this machine), use the inode from block 0.
5076 */
5077 if (mch_stat((char *)fname_s, &st) == 0)
5078 ino_s = (ino_t)st.st_ino;
5079 else
5080 ino_s = (ino_t)ino_block0;
5081
5082 if (ino_c && ino_s)
5083 return (ino_c != ino_s);
5084
5085 /*
5086 * One of the inode numbers is unknown, try a forced vim_FullName() and
5087 * compare the file names.
5088 */
5089 retval_c = vim_FullName(fname_c, buf_c, MAXPATHL, TRUE);
5090 retval_s = vim_FullName(fname_s, buf_s, MAXPATHL, TRUE);
5091 if (retval_c == OK && retval_s == OK)
Bram Moolenaar8c3169c2018-05-12 17:04:12 +02005092 return STRCMP(buf_c, buf_s) != 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005093
5094 /*
5095 * Can't compare inodes or file names, guess that the files are different,
Bram Moolenaar8c3169c2018-05-12 17:04:12 +02005096 * unless both appear not to exist at all, then compare with the file name
5097 * in the swap file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005098 */
5099 if (ino_s == 0 && ino_c == 0 && retval_c == FAIL && retval_s == FAIL)
Bram Moolenaar8c3169c2018-05-12 17:04:12 +02005100 return STRCMP(fname_c, fname_s) != 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005101 return TRUE;
5102}
5103#endif /* CHECK_INODE */
5104
5105/*
5106 * Move a long integer into a four byte character array.
5107 * Used for machine independency in block zero.
5108 */
5109 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01005110long_to_char(long n, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005111{
5112 s[0] = (char_u)(n & 0xff);
5113 n = (unsigned)n >> 8;
5114 s[1] = (char_u)(n & 0xff);
5115 n = (unsigned)n >> 8;
5116 s[2] = (char_u)(n & 0xff);
5117 n = (unsigned)n >> 8;
5118 s[3] = (char_u)(n & 0xff);
5119}
5120
5121 static long
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01005122char_to_long(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005123{
5124 long retval;
5125
5126 retval = s[3];
5127 retval <<= 8;
5128 retval |= s[2];
5129 retval <<= 8;
5130 retval |= s[1];
5131 retval <<= 8;
5132 retval |= s[0];
5133
5134 return retval;
5135}
5136
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005137/*
5138 * Set the flags in the first block of the swap file:
5139 * - file is modified or not: buf->b_changed
5140 * - 'fileformat'
5141 * - 'fileencoding'
5142 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005143 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01005144ml_setflags(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005145{
5146 bhdr_T *hp;
5147 ZERO_BL *b0p;
5148
5149 if (!buf->b_ml.ml_mfp)
5150 return;
5151 for (hp = buf->b_ml.ml_mfp->mf_used_last; hp != NULL; hp = hp->bh_prev)
5152 {
5153 if (hp->bh_bnum == 0)
5154 {
5155 b0p = (ZERO_BL *)(hp->bh_data);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005156 b0p->b0_dirty = buf->b_changed ? B0_DIRTY : 0;
5157 b0p->b0_flags = (b0p->b0_flags & ~B0_FF_MASK)
5158 | (get_fileformat(buf) + 1);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005159 add_b0_fenc(b0p, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005160 hp->bh_flags |= BH_DIRTY;
5161 mf_sync(buf->b_ml.ml_mfp, MFS_ZERO);
5162 break;
5163 }
5164 }
5165}
5166
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02005167#if defined(FEAT_CRYPT) || defined(PROTO)
5168/*
5169 * If "data" points to a data block encrypt the text in it and return a copy
5170 * in allocated memory. Return NULL when out of memory.
5171 * Otherwise return "data".
5172 */
5173 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01005174ml_encrypt_data(
5175 memfile_T *mfp,
5176 char_u *data,
Bram Moolenaar8767f522016-07-01 17:17:39 +02005177 off_T offset,
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01005178 unsigned size)
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02005179{
5180 DATA_BL *dp = (DATA_BL *)data;
5181 char_u *head_end;
5182 char_u *text_start;
5183 char_u *new_data;
5184 int text_len;
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02005185 cryptstate_T *state;
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02005186
5187 if (dp->db_id != DATA_ID)
5188 return data;
5189
Bram Moolenaarbc563362015-06-09 18:35:25 +02005190 state = ml_crypt_prepare(mfp, offset, FALSE);
5191 if (state == NULL)
5192 return data;
5193
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02005194 new_data = (char_u *)alloc(size);
5195 if (new_data == NULL)
5196 return NULL;
5197 head_end = (char_u *)(&dp->db_index[dp->db_line_count]);
5198 text_start = (char_u *)dp + dp->db_txt_start;
5199 text_len = size - dp->db_txt_start;
5200
5201 /* Copy the header and the text. */
5202 mch_memmove(new_data, dp, head_end - (char_u *)dp);
5203
5204 /* Encrypt the text. */
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02005205 crypt_encode(state, text_start, text_len, new_data + dp->db_txt_start);
5206 crypt_free_state(state);
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02005207
5208 /* Clear the gap. */
5209 if (head_end < text_start)
5210 vim_memset(new_data + (head_end - data), 0, text_start - head_end);
5211
5212 return new_data;
5213}
5214
5215/*
Bram Moolenaarbc563362015-06-09 18:35:25 +02005216 * Decrypt the text in "data" if it points to an encrypted data block.
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02005217 */
5218 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01005219ml_decrypt_data(
5220 memfile_T *mfp,
5221 char_u *data,
Bram Moolenaar8767f522016-07-01 17:17:39 +02005222 off_T offset,
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01005223 unsigned size)
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02005224{
5225 DATA_BL *dp = (DATA_BL *)data;
5226 char_u *head_end;
5227 char_u *text_start;
5228 int text_len;
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02005229 cryptstate_T *state;
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02005230
5231 if (dp->db_id == DATA_ID)
5232 {
5233 head_end = (char_u *)(&dp->db_index[dp->db_line_count]);
5234 text_start = (char_u *)dp + dp->db_txt_start;
5235 text_len = dp->db_txt_end - dp->db_txt_start;
5236
5237 if (head_end > text_start || dp->db_txt_start > size
5238 || dp->db_txt_end > size)
5239 return; /* data was messed up */
5240
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02005241 state = ml_crypt_prepare(mfp, offset, TRUE);
Bram Moolenaarbc563362015-06-09 18:35:25 +02005242 if (state != NULL)
5243 {
5244 /* Decrypt the text in place. */
5245 crypt_decode_inplace(state, text_start, text_len);
5246 crypt_free_state(state);
5247 }
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02005248 }
5249}
5250
5251/*
5252 * Prepare for encryption/decryption, using the key, seed and offset.
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02005253 * Return an allocated cryptstate_T *.
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02005254 */
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02005255 static cryptstate_T *
Bram Moolenaar8767f522016-07-01 17:17:39 +02005256ml_crypt_prepare(memfile_T *mfp, off_T offset, int reading)
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02005257{
5258 buf_T *buf = mfp->mf_buffer;
5259 char_u salt[50];
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02005260 int method_nr;
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02005261 char_u *key;
5262 char_u *seed;
5263
5264 if (reading && mfp->mf_old_key != NULL)
5265 {
5266 /* Reading back blocks with the previous key/method/seed. */
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02005267 method_nr = mfp->mf_old_cm;
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02005268 key = mfp->mf_old_key;
5269 seed = mfp->mf_old_seed;
5270 }
5271 else
5272 {
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02005273 method_nr = crypt_get_method_nr(buf);
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02005274 key = buf->b_p_key;
5275 seed = mfp->mf_seed;
5276 }
Bram Moolenaarbc563362015-06-09 18:35:25 +02005277 if (*key == NUL)
5278 return NULL;
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02005279
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02005280 if (method_nr == CRYPT_M_ZIP)
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02005281 {
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02005282 /* For PKzip: Append the offset to the key, so that we use a different
5283 * key for every block. */
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02005284 vim_snprintf((char *)salt, sizeof(salt), "%s%ld", key, (long)offset);
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02005285 return crypt_create(method_nr, salt, NULL, 0, NULL, 0);
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02005286 }
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02005287
5288 /* Using blowfish or better: add salt and seed. We use the byte offset
5289 * of the block for the salt. */
5290 vim_snprintf((char *)salt, sizeof(salt), "%ld", (long)offset);
5291 return crypt_create(method_nr, key, salt, (int)STRLEN(salt),
5292 seed, MF_SEED_LEN);
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02005293}
5294
5295#endif
5296
5297
Bram Moolenaar071d4272004-06-13 20:20:40 +00005298#if defined(FEAT_BYTEOFF) || defined(PROTO)
5299
5300#define MLCS_MAXL 800 /* max no of lines in chunk */
5301#define MLCS_MINL 400 /* should be half of MLCS_MAXL */
5302
5303/*
Bram Moolenaar0ad014c2010-07-25 14:00:46 +02005304 * Keep information for finding byte offset of a line, updtype may be one of:
Bram Moolenaar071d4272004-06-13 20:20:40 +00005305 * ML_CHNK_ADDLINE: Add len to parent chunk, possibly splitting it
5306 * Careful: ML_CHNK_ADDLINE may cause ml_find_line() to be called.
5307 * ML_CHNK_DELLINE: Subtract len from parent chunk, possibly deleting it
5308 * ML_CHNK_UPDLINE: Add len to parent chunk, as a signed entity.
5309 */
5310 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01005311ml_updatechunk(
5312 buf_T *buf,
5313 linenr_T line,
5314 long len,
5315 int updtype)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005316{
5317 static buf_T *ml_upd_lastbuf = NULL;
5318 static linenr_T ml_upd_lastline;
5319 static linenr_T ml_upd_lastcurline;
5320 static int ml_upd_lastcurix;
5321
5322 linenr_T curline = ml_upd_lastcurline;
5323 int curix = ml_upd_lastcurix;
5324 long size;
5325 chunksize_T *curchnk;
5326 int rest;
5327 bhdr_T *hp;
5328 DATA_BL *dp;
5329
5330 if (buf->b_ml.ml_usedchunks == -1 || len == 0)
5331 return;
5332 if (buf->b_ml.ml_chunksize == NULL)
5333 {
5334 buf->b_ml.ml_chunksize = (chunksize_T *)
5335 alloc((unsigned)sizeof(chunksize_T) * 100);
5336 if (buf->b_ml.ml_chunksize == NULL)
5337 {
5338 buf->b_ml.ml_usedchunks = -1;
5339 return;
5340 }
5341 buf->b_ml.ml_numchunks = 100;
5342 buf->b_ml.ml_usedchunks = 1;
5343 buf->b_ml.ml_chunksize[0].mlcs_numlines = 1;
5344 buf->b_ml.ml_chunksize[0].mlcs_totalsize = 1;
5345 }
5346
5347 if (updtype == ML_CHNK_UPDLINE && buf->b_ml.ml_line_count == 1)
5348 {
5349 /*
5350 * First line in empty buffer from ml_flush_line() -- reset
5351 */
5352 buf->b_ml.ml_usedchunks = 1;
5353 buf->b_ml.ml_chunksize[0].mlcs_numlines = 1;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01005354 buf->b_ml.ml_chunksize[0].mlcs_totalsize = (long)buf->b_ml.ml_line_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005355 return;
5356 }
5357
5358 /*
5359 * Find chunk that our line belongs to, curline will be at start of the
5360 * chunk.
5361 */
5362 if (buf != ml_upd_lastbuf || line != ml_upd_lastline + 1
5363 || updtype != ML_CHNK_ADDLINE)
5364 {
5365 for (curline = 1, curix = 0;
5366 curix < buf->b_ml.ml_usedchunks - 1
5367 && line >= curline + buf->b_ml.ml_chunksize[curix].mlcs_numlines;
5368 curix++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005369 curline += buf->b_ml.ml_chunksize[curix].mlcs_numlines;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005370 }
Bram Moolenaara9a8e042018-10-30 22:15:55 +01005371 else if (curix < buf->b_ml.ml_usedchunks - 1
5372 && line >= curline + buf->b_ml.ml_chunksize[curix].mlcs_numlines)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005373 {
5374 /* Adjust cached curix & curline */
5375 curline += buf->b_ml.ml_chunksize[curix].mlcs_numlines;
5376 curix++;
5377 }
5378 curchnk = buf->b_ml.ml_chunksize + curix;
5379
5380 if (updtype == ML_CHNK_DELLINE)
Bram Moolenaar5a6404c2006-11-01 17:12:57 +00005381 len = -len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005382 curchnk->mlcs_totalsize += len;
5383 if (updtype == ML_CHNK_ADDLINE)
5384 {
5385 curchnk->mlcs_numlines++;
5386
5387 /* May resize here so we don't have to do it in both cases below */
5388 if (buf->b_ml.ml_usedchunks + 1 >= buf->b_ml.ml_numchunks)
5389 {
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01005390 chunksize_T *t_chunksize = buf->b_ml.ml_chunksize;
5391
Bram Moolenaar071d4272004-06-13 20:20:40 +00005392 buf->b_ml.ml_numchunks = buf->b_ml.ml_numchunks * 3 / 2;
5393 buf->b_ml.ml_chunksize = (chunksize_T *)
5394 vim_realloc(buf->b_ml.ml_chunksize,
5395 sizeof(chunksize_T) * buf->b_ml.ml_numchunks);
5396 if (buf->b_ml.ml_chunksize == NULL)
5397 {
5398 /* Hmmmm, Give up on offset for this buffer */
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01005399 vim_free(t_chunksize);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005400 buf->b_ml.ml_usedchunks = -1;
5401 return;
5402 }
5403 }
5404
5405 if (buf->b_ml.ml_chunksize[curix].mlcs_numlines >= MLCS_MAXL)
5406 {
5407 int count; /* number of entries in block */
5408 int idx;
Bram Moolenaarb413d2e2018-12-25 23:15:46 +01005409 int end_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005410 int text_end;
5411 int linecnt;
5412
5413 mch_memmove(buf->b_ml.ml_chunksize + curix + 1,
5414 buf->b_ml.ml_chunksize + curix,
5415 (buf->b_ml.ml_usedchunks - curix) *
5416 sizeof(chunksize_T));
Bram Moolenaar9439cdd2009-04-22 13:39:36 +00005417 /* Compute length of first half of lines in the split chunk */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005418 size = 0;
5419 linecnt = 0;
5420 while (curline < buf->b_ml.ml_line_count
5421 && linecnt < MLCS_MINL)
5422 {
5423 if ((hp = ml_find_line(buf, curline, ML_FIND)) == NULL)
5424 {
5425 buf->b_ml.ml_usedchunks = -1;
5426 return;
5427 }
5428 dp = (DATA_BL *)(hp->bh_data);
5429 count = (long)(buf->b_ml.ml_locked_high) -
5430 (long)(buf->b_ml.ml_locked_low) + 1;
5431 idx = curline - buf->b_ml.ml_locked_low;
5432 curline = buf->b_ml.ml_locked_high + 1;
Bram Moolenaarb413d2e2018-12-25 23:15:46 +01005433
5434 // compute index of last line to use in this MEMLINE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005435 rest = count - idx;
5436 if (linecnt + rest > MLCS_MINL)
5437 {
Bram Moolenaarb413d2e2018-12-25 23:15:46 +01005438 end_idx = idx + MLCS_MINL - linecnt - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005439 linecnt = MLCS_MINL;
5440 }
5441 else
5442 {
Bram Moolenaarb413d2e2018-12-25 23:15:46 +01005443 end_idx = count - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005444 linecnt += rest;
5445 }
Bram Moolenaarb413d2e2018-12-25 23:15:46 +01005446#ifdef FEAT_TEXT_PROP
5447 if (buf->b_has_textprop)
5448 {
5449 int i;
5450
5451 // We cannot use the text pointers to get the text length,
5452 // the text prop info would also be counted. Go over the
5453 // lines.
5454 for (i = end_idx; i < idx; ++i)
Bram Moolenaar4b7214e2019-01-03 21:55:32 +01005455 size += (int)STRLEN((char_u *)dp + (dp->db_index[i] & DB_INDEX_MASK)) + 1;
Bram Moolenaarb413d2e2018-12-25 23:15:46 +01005456 }
5457 else
5458#endif
5459 {
5460 if (idx == 0)/* first line in block, text at the end */
5461 text_end = dp->db_txt_end;
5462 else
5463 text_end = ((dp->db_index[idx - 1]) & DB_INDEX_MASK);
5464 size += text_end - ((dp->db_index[end_idx]) & DB_INDEX_MASK);
5465 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005466 }
5467 buf->b_ml.ml_chunksize[curix].mlcs_numlines = linecnt;
5468 buf->b_ml.ml_chunksize[curix + 1].mlcs_numlines -= linecnt;
5469 buf->b_ml.ml_chunksize[curix].mlcs_totalsize = size;
5470 buf->b_ml.ml_chunksize[curix + 1].mlcs_totalsize -= size;
5471 buf->b_ml.ml_usedchunks++;
5472 ml_upd_lastbuf = NULL; /* Force recalc of curix & curline */
5473 return;
5474 }
5475 else if (buf->b_ml.ml_chunksize[curix].mlcs_numlines >= MLCS_MINL
5476 && curix == buf->b_ml.ml_usedchunks - 1
5477 && buf->b_ml.ml_line_count - line <= 1)
5478 {
5479 /*
5480 * We are in the last chunk and it is cheap to crate a new one
5481 * after this. Do it now to avoid the loop above later on
5482 */
5483 curchnk = buf->b_ml.ml_chunksize + curix + 1;
5484 buf->b_ml.ml_usedchunks++;
5485 if (line == buf->b_ml.ml_line_count)
5486 {
5487 curchnk->mlcs_numlines = 0;
5488 curchnk->mlcs_totalsize = 0;
5489 }
5490 else
5491 {
5492 /*
5493 * Line is just prior to last, move count for last
5494 * This is the common case when loading a new file
5495 */
5496 hp = ml_find_line(buf, buf->b_ml.ml_line_count, ML_FIND);
5497 if (hp == NULL)
5498 {
5499 buf->b_ml.ml_usedchunks = -1;
5500 return;
5501 }
5502 dp = (DATA_BL *)(hp->bh_data);
5503 if (dp->db_line_count == 1)
5504 rest = dp->db_txt_end - dp->db_txt_start;
5505 else
5506 rest =
5507 ((dp->db_index[dp->db_line_count - 2]) & DB_INDEX_MASK)
5508 - dp->db_txt_start;
5509 curchnk->mlcs_totalsize = rest;
5510 curchnk->mlcs_numlines = 1;
5511 curchnk[-1].mlcs_totalsize -= rest;
5512 curchnk[-1].mlcs_numlines -= 1;
5513 }
5514 }
5515 }
5516 else if (updtype == ML_CHNK_DELLINE)
5517 {
5518 curchnk->mlcs_numlines--;
5519 ml_upd_lastbuf = NULL; /* Force recalc of curix & curline */
5520 if (curix < (buf->b_ml.ml_usedchunks - 1)
5521 && (curchnk->mlcs_numlines + curchnk[1].mlcs_numlines)
5522 <= MLCS_MINL)
5523 {
5524 curix++;
5525 curchnk = buf->b_ml.ml_chunksize + curix;
5526 }
5527 else if (curix == 0 && curchnk->mlcs_numlines <= 0)
5528 {
5529 buf->b_ml.ml_usedchunks--;
5530 mch_memmove(buf->b_ml.ml_chunksize, buf->b_ml.ml_chunksize + 1,
5531 buf->b_ml.ml_usedchunks * sizeof(chunksize_T));
5532 return;
5533 }
5534 else if (curix == 0 || (curchnk->mlcs_numlines > 10
5535 && (curchnk->mlcs_numlines + curchnk[-1].mlcs_numlines)
5536 > MLCS_MINL))
5537 {
5538 return;
5539 }
5540
5541 /* Collapse chunks */
5542 curchnk[-1].mlcs_numlines += curchnk->mlcs_numlines;
5543 curchnk[-1].mlcs_totalsize += curchnk->mlcs_totalsize;
5544 buf->b_ml.ml_usedchunks--;
5545 if (curix < buf->b_ml.ml_usedchunks)
5546 {
5547 mch_memmove(buf->b_ml.ml_chunksize + curix,
5548 buf->b_ml.ml_chunksize + curix + 1,
5549 (buf->b_ml.ml_usedchunks - curix) *
5550 sizeof(chunksize_T));
5551 }
5552 return;
5553 }
5554 ml_upd_lastbuf = buf;
5555 ml_upd_lastline = line;
5556 ml_upd_lastcurline = curline;
5557 ml_upd_lastcurix = curix;
5558}
5559
5560/*
5561 * Find offset for line or line with offset.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00005562 * Find line with offset if "lnum" is 0; return remaining offset in offp
5563 * Find offset of line if "lnum" > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005564 * return -1 if information is not available
5565 */
5566 long
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01005567ml_find_line_or_offset(buf_T *buf, linenr_T lnum, long *offp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005568{
5569 linenr_T curline;
5570 int curix;
5571 long size;
5572 bhdr_T *hp;
5573 DATA_BL *dp;
5574 int count; /* number of entries in block */
5575 int idx;
5576 int start_idx;
5577 int text_end;
5578 long offset;
5579 int len;
5580 int ffdos = (get_fileformat(buf) == EOL_DOS);
5581 int extra = 0;
5582
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00005583 /* take care of cached line first */
5584 ml_flush_line(curbuf);
5585
Bram Moolenaar071d4272004-06-13 20:20:40 +00005586 if (buf->b_ml.ml_usedchunks == -1
5587 || buf->b_ml.ml_chunksize == NULL
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00005588 || lnum < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005589 return -1;
5590
5591 if (offp == NULL)
5592 offset = 0;
5593 else
5594 offset = *offp;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00005595 if (lnum == 0 && offset <= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005596 return 1; /* Not a "find offset" and offset 0 _must_ be in line 1 */
5597 /*
5598 * Find the last chunk before the one containing our line. Last chunk is
5599 * special because it will never qualify
5600 */
5601 curline = 1;
5602 curix = size = 0;
5603 while (curix < buf->b_ml.ml_usedchunks - 1
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00005604 && ((lnum != 0
5605 && lnum >= curline + buf->b_ml.ml_chunksize[curix].mlcs_numlines)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005606 || (offset != 0
5607 && offset > size + buf->b_ml.ml_chunksize[curix].mlcs_totalsize
5608 + ffdos * buf->b_ml.ml_chunksize[curix].mlcs_numlines)))
5609 {
5610 curline += buf->b_ml.ml_chunksize[curix].mlcs_numlines;
5611 size += buf->b_ml.ml_chunksize[curix].mlcs_totalsize;
5612 if (offset && ffdos)
5613 size += buf->b_ml.ml_chunksize[curix].mlcs_numlines;
5614 curix++;
5615 }
5616
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00005617 while ((lnum != 0 && curline < lnum) || (offset != 0 && size < offset))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005618 {
5619 if (curline > buf->b_ml.ml_line_count
5620 || (hp = ml_find_line(buf, curline, ML_FIND)) == NULL)
5621 return -1;
5622 dp = (DATA_BL *)(hp->bh_data);
5623 count = (long)(buf->b_ml.ml_locked_high) -
5624 (long)(buf->b_ml.ml_locked_low) + 1;
5625 start_idx = idx = curline - buf->b_ml.ml_locked_low;
5626 if (idx == 0)/* first line in block, text at the end */
5627 text_end = dp->db_txt_end;
5628 else
5629 text_end = ((dp->db_index[idx - 1]) & DB_INDEX_MASK);
5630 /* Compute index of last line to use in this MEMLINE */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00005631 if (lnum != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005632 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00005633 if (curline + (count - idx) >= lnum)
5634 idx += lnum - curline - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005635 else
5636 idx = count - 1;
5637 }
5638 else
5639 {
5640 extra = 0;
5641 while (offset >= size
5642 + text_end - (int)((dp->db_index[idx]) & DB_INDEX_MASK)
5643 + ffdos)
5644 {
5645 if (ffdos)
5646 size++;
5647 if (idx == count - 1)
5648 {
5649 extra = 1;
5650 break;
5651 }
5652 idx++;
5653 }
5654 }
Bram Moolenaarb413d2e2018-12-25 23:15:46 +01005655#ifdef FEAT_TEXT_PROP
5656 if (buf->b_has_textprop)
5657 {
5658 int i;
5659
5660 // cannot use the db_index pointer, need to get the actual text
5661 // lengths.
5662 len = 0;
5663 for (i = start_idx; i <= idx; ++i)
Bram Moolenaar4b7214e2019-01-03 21:55:32 +01005664 len += (int)STRLEN((char_u *)dp + ((dp->db_index[i]) & DB_INDEX_MASK)) + 1;
Bram Moolenaarb413d2e2018-12-25 23:15:46 +01005665 }
5666 else
5667#endif
5668 len = text_end - ((dp->db_index[idx]) & DB_INDEX_MASK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005669 size += len;
5670 if (offset != 0 && size >= offset)
5671 {
5672 if (size + ffdos == offset)
5673 *offp = 0;
5674 else if (idx == start_idx)
5675 *offp = offset - size + len;
5676 else
5677 *offp = offset - size + len
5678 - (text_end - ((dp->db_index[idx - 1]) & DB_INDEX_MASK));
5679 curline += idx - start_idx + extra;
5680 if (curline > buf->b_ml.ml_line_count)
5681 return -1; /* exactly one byte beyond the end */
5682 return curline;
5683 }
5684 curline = buf->b_ml.ml_locked_high + 1;
5685 }
5686
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00005687 if (lnum != 0)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005688 {
5689 /* Count extra CR characters. */
5690 if (ffdos)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00005691 size += lnum - 1;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005692
Bram Moolenaar34d72d42015-07-17 14:18:08 +02005693 /* Don't count the last line break if 'noeol' and ('bin' or
5694 * 'nofixeol'). */
5695 if ((!buf->b_p_fixeol || buf->b_p_bin) && !buf->b_p_eol
Bram Moolenaarc26f7c62018-08-20 22:53:04 +02005696 && lnum > buf->b_ml.ml_line_count)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005697 size -= ffdos + 1;
5698 }
5699
Bram Moolenaar071d4272004-06-13 20:20:40 +00005700 return size;
5701}
5702
5703/*
5704 * Goto byte in buffer with offset 'cnt'.
5705 */
5706 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01005707goto_byte(long cnt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005708{
5709 long boff = cnt;
5710 linenr_T lnum;
5711
5712 ml_flush_line(curbuf); /* cached line may be dirty */
5713 setpcmark();
5714 if (boff)
5715 --boff;
5716 lnum = ml_find_line_or_offset(curbuf, (linenr_T)0, &boff);
5717 if (lnum < 1) /* past the end */
5718 {
5719 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
5720 curwin->w_curswant = MAXCOL;
5721 coladvance((colnr_T)MAXCOL);
5722 }
5723 else
5724 {
5725 curwin->w_cursor.lnum = lnum;
5726 curwin->w_cursor.col = (colnr_T)boff;
Bram Moolenaar943d2b52005-12-02 00:50:49 +00005727 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005728 curwin->w_set_curswant = TRUE;
5729 }
5730 check_cursor();
5731
Bram Moolenaar071d4272004-06-13 20:20:40 +00005732 /* Make sure the cursor is on the first byte of a multi-byte char. */
5733 if (has_mbyte)
5734 mb_adjust_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005735}
5736#endif