blob: 0e3bdb8b6b324132348f5ef5bd370b25a1c3c07e [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 Moolenaar99499b12019-05-23 21:35:48 +02001087 * If "checkext" is TRUE, check the extension and detect whether it is
1088 * a swap file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001089 */
1090 void
Bram Moolenaar99499b12019-05-23 21:35:48 +02001091ml_recover(int checkext)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001092{
1093 buf_T *buf = NULL;
1094 memfile_T *mfp = NULL;
1095 char_u *fname;
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02001096 char_u *fname_used = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001097 bhdr_T *hp = NULL;
1098 ZERO_BL *b0p;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001099 int b0_ff;
1100 char_u *b0_fenc = NULL;
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02001101#ifdef FEAT_CRYPT
1102 int b0_cm = -1;
1103#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001104 PTR_BL *pp;
1105 DATA_BL *dp;
1106 infoptr_T *ip;
1107 blocknr_T bnum;
1108 int page_count;
Bram Moolenaar8767f522016-07-01 17:17:39 +02001109 stat_T org_stat, swp_stat;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001110 int len;
1111 int directly;
1112 linenr_T lnum;
1113 char_u *p;
1114 int i;
1115 long error;
1116 int cannot_open;
1117 linenr_T line_count;
1118 int has_error;
1119 int idx;
1120 int top;
1121 int txt_start;
Bram Moolenaar8767f522016-07-01 17:17:39 +02001122 off_T size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001123 int called_from_main;
1124 int serious_error = TRUE;
1125 long mtime;
1126 int attr;
Bram Moolenaarfc2d5bd2010-05-15 17:06:53 +02001127 int orig_file_status = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001128
1129 recoverymode = TRUE;
1130 called_from_main = (curbuf->b_ml.ml_mfp == NULL);
Bram Moolenaar8820b482017-03-16 17:23:31 +01001131 attr = HL_ATTR(HLF_E);
Bram Moolenaard0ba34a2009-11-03 12:06:23 +00001132
1133 /*
Bram Moolenaaraf903e52017-12-02 15:11:22 +01001134 * 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 +00001135 * Otherwise a search is done to find the swap file(s).
1136 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001137 fname = curbuf->b_fname;
1138 if (fname == NULL) /* When there is no file name */
1139 fname = (char_u *)"";
1140 len = (int)STRLEN(fname);
Bram Moolenaar99499b12019-05-23 21:35:48 +02001141 if (checkext && len >= 4 &&
Bram Moolenaare60acc12011-05-10 16:41:25 +02001142#if defined(VMS)
Bram Moolenaar79518e22017-02-17 16:31:35 +01001143 STRNICMP(fname + len - 4, "_s", 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001144#else
Bram Moolenaar79518e22017-02-17 16:31:35 +01001145 STRNICMP(fname + len - 4, ".s", 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001146#endif
Bram Moolenaar79518e22017-02-17 16:31:35 +01001147 == 0
Bram Moolenaaraf903e52017-12-02 15:11:22 +01001148 && vim_strchr((char_u *)"abcdefghijklmnopqrstuvw",
1149 TOLOWER_ASC(fname[len - 2])) != NULL
Bram Moolenaard0ba34a2009-11-03 12:06:23 +00001150 && ASCII_ISALPHA(fname[len - 1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001151 {
1152 directly = TRUE;
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02001153 fname_used = vim_strsave(fname); /* make a copy for mf_open() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001154 }
1155 else
1156 {
1157 directly = FALSE;
1158
1159 /* count the number of matching swap files */
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02001160 len = recover_names(fname, FALSE, 0, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001161 if (len == 0) /* no swap files found */
1162 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001163 semsg(_("E305: No swap file found for %s"), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001164 goto theend;
1165 }
1166 if (len == 1) /* one swap file found, use it */
1167 i = 1;
1168 else /* several swap files found, choose */
1169 {
1170 /* list the names of the swap files */
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02001171 (void)recover_names(fname, TRUE, 0, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001172 msg_putchar('\n');
Bram Moolenaar32526b32019-01-19 17:43:09 +01001173 msg_puts(_("Enter number of swap file to use (0 to quit): "));
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001174 i = get_number(FALSE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001175 if (i < 1 || i > len)
1176 goto theend;
1177 }
1178 /* get the swap file name that will be used */
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02001179 (void)recover_names(fname, FALSE, i, &fname_used);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001180 }
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02001181 if (fname_used == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001182 goto theend; /* out of memory */
1183
1184 /* When called from main() still need to initialize storage structure */
Bram Moolenaar4770d092006-01-12 23:22:24 +00001185 if (called_from_main && ml_open(curbuf) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001186 getout(1);
1187
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02001188 /*
1189 * Allocate a buffer structure for the swap file that is used for recovery.
Bram Moolenaar0ad014c2010-07-25 14:00:46 +02001190 * Only the memline and crypt information in it are really used.
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02001191 */
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001192 buf = ALLOC_ONE(buf_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001193 if (buf == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001194 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001195
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02001196 /*
1197 * init fields in memline struct
1198 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001199 buf->b_ml.ml_stack_size = 0; /* no stack yet */
1200 buf->b_ml.ml_stack = NULL; /* no stack yet */
1201 buf->b_ml.ml_stack_top = 0; /* nothing in the stack */
1202 buf->b_ml.ml_line_lnum = 0; /* no cached line */
1203 buf->b_ml.ml_locked = NULL; /* no locked block */
1204 buf->b_ml.ml_flags = 0;
Bram Moolenaar0fe849a2010-07-25 15:11:11 +02001205#ifdef FEAT_CRYPT
1206 buf->b_p_key = empty_option;
1207 buf->b_p_cm = empty_option;
1208#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001209
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02001210 /*
1211 * open the memfile from the old swap file
1212 */
1213 p = vim_strsave(fname_used); /* save "fname_used" for the message:
1214 mf_open() will consume "fname_used"! */
1215 mfp = mf_open(fname_used, O_RDONLY);
1216 fname_used = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001217 if (mfp == NULL || mfp->mf_fd < 0)
1218 {
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02001219 if (fname_used != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001220 semsg(_("E306: Cannot open %s"), fname_used);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001221 goto theend;
1222 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001223 buf->b_ml.ml_mfp = mfp;
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02001224#ifdef FEAT_CRYPT
1225 mfp->mf_buffer = buf;
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02001226#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001227
1228 /*
1229 * The page size set in mf_open() might be different from the page size
1230 * used in the swap file, we must get it from block 0. But to read block
1231 * 0 we need a page size. Use the minimal size for block 0 here, it will
1232 * be set to the real value below.
1233 */
1234 mfp->mf_page_size = MIN_SWAP_PAGE_SIZE;
1235
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02001236 /*
1237 * try to read block 0
1238 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001239 if ((hp = mf_get(mfp, (blocknr_T)0, 1)) == NULL)
1240 {
1241 msg_start();
Bram Moolenaar32526b32019-01-19 17:43:09 +01001242 msg_puts_attr(_("Unable to read block 0 from "), attr | MSG_HIST);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001243 msg_outtrans_attr(mfp->mf_fname, attr | MSG_HIST);
Bram Moolenaar32526b32019-01-19 17:43:09 +01001244 msg_puts_attr(_("\nMaybe no changes were made or Vim did not update the swap file."),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001245 attr | MSG_HIST);
1246 msg_end();
1247 goto theend;
1248 }
1249 b0p = (ZERO_BL *)(hp->bh_data);
1250 if (STRNCMP(b0p->b0_version, "VIM 3.0", 7) == 0)
1251 {
1252 msg_start();
1253 msg_outtrans_attr(mfp->mf_fname, MSG_HIST);
Bram Moolenaar32526b32019-01-19 17:43:09 +01001254 msg_puts_attr(_(" cannot be used with this version of Vim.\n"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001255 MSG_HIST);
Bram Moolenaar32526b32019-01-19 17:43:09 +01001256 msg_puts_attr(_("Use Vim version 3.0.\n"), MSG_HIST);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001257 msg_end();
1258 goto theend;
1259 }
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02001260 if (ml_check_b0_id(b0p) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001261 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001262 semsg(_("E307: %s does not look like a Vim swap file"), mfp->mf_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001263 goto theend;
1264 }
1265 if (b0_magic_wrong(b0p))
1266 {
1267 msg_start();
1268 msg_outtrans_attr(mfp->mf_fname, attr | MSG_HIST);
Bram Moolenaar48e330a2016-02-23 14:53:34 +01001269#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001270 if (STRNCMP(b0p->b0_hname, "PC ", 3) == 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001271 msg_puts_attr(_(" cannot be used with this version of Vim.\n"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001272 attr | MSG_HIST);
1273 else
1274#endif
Bram Moolenaar32526b32019-01-19 17:43:09 +01001275 msg_puts_attr(_(" cannot be used on this computer.\n"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001276 attr | MSG_HIST);
Bram Moolenaar32526b32019-01-19 17:43:09 +01001277 msg_puts_attr(_("The file was created on "), attr | MSG_HIST);
Bram Moolenaare242b832010-06-24 05:39:03 +02001278 /* avoid going past the end of a corrupted hostname */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001279 b0p->b0_fname[0] = NUL;
Bram Moolenaar32526b32019-01-19 17:43:09 +01001280 msg_puts_attr((char *)b0p->b0_hname, attr | MSG_HIST);
1281 msg_puts_attr(_(",\nor the file has been damaged."), attr | MSG_HIST);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001282 msg_end();
1283 goto theend;
1284 }
Bram Moolenaar1c536282007-04-26 15:21:56 +00001285
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02001286#ifdef FEAT_CRYPT
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001287 for (i = 0; i < (int)(sizeof(id1_codes) / sizeof(int)); ++i)
1288 if (id1_codes[i] == b0p->b0_id[1])
1289 b0_cm = i;
1290 if (b0_cm > 0)
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02001291 mch_memmove(mfp->mf_seed, &b0p->b0_seed, MF_SEED_LEN);
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001292 crypt_set_cm_option(buf, b0_cm < 0 ? 0 : b0_cm);
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02001293#else
1294 if (b0p->b0_id[1] != BLOCK0_ID1)
1295 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001296 semsg(_("E833: %s is encrypted and this version of Vim does not support encryption"), mfp->mf_fname);
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02001297 goto theend;
1298 }
1299#endif
1300
Bram Moolenaar071d4272004-06-13 20:20:40 +00001301 /*
1302 * If we guessed the wrong page size, we have to recalculate the
1303 * highest block number in the file.
1304 */
1305 if (mfp->mf_page_size != (unsigned)char_to_long(b0p->b0_page_size))
1306 {
Bram Moolenaar1c536282007-04-26 15:21:56 +00001307 unsigned previous_page_size = mfp->mf_page_size;
1308
Bram Moolenaar071d4272004-06-13 20:20:40 +00001309 mf_new_page_size(mfp, (unsigned)char_to_long(b0p->b0_page_size));
Bram Moolenaar1c536282007-04-26 15:21:56 +00001310 if (mfp->mf_page_size < previous_page_size)
1311 {
1312 msg_start();
1313 msg_outtrans_attr(mfp->mf_fname, attr | MSG_HIST);
Bram Moolenaar32526b32019-01-19 17:43:09 +01001314 msg_puts_attr(_(" has been damaged (page size is smaller than minimum value).\n"),
Bram Moolenaar1c536282007-04-26 15:21:56 +00001315 attr | MSG_HIST);
1316 msg_end();
1317 goto theend;
1318 }
Bram Moolenaar8767f522016-07-01 17:17:39 +02001319 if ((size = vim_lseek(mfp->mf_fd, (off_T)0L, SEEK_END)) <= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001320 mfp->mf_blocknr_max = 0; /* no file or empty file */
1321 else
1322 mfp->mf_blocknr_max = (blocknr_T)(size / mfp->mf_page_size);
1323 mfp->mf_infile_count = mfp->mf_blocknr_max;
Bram Moolenaar1c536282007-04-26 15:21:56 +00001324
1325 /* need to reallocate the memory used to store the data */
1326 p = alloc(mfp->mf_page_size);
1327 if (p == NULL)
1328 goto theend;
1329 mch_memmove(p, hp->bh_data, previous_page_size);
1330 vim_free(hp->bh_data);
1331 hp->bh_data = p;
1332 b0p = (ZERO_BL *)(hp->bh_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001333 }
1334
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02001335 /*
1336 * If .swp file name given directly, use name from swap file for buffer.
1337 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001338 if (directly)
1339 {
1340 expand_env(b0p->b0_fname, NameBuff, MAXPATHL);
1341 if (setfname(curbuf, NameBuff, NULL, TRUE) == FAIL)
1342 goto theend;
1343 }
1344
1345 home_replace(NULL, mfp->mf_fname, NameBuff, MAXPATHL, TRUE);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001346 smsg(_("Using swap file \"%s\""), NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001347
1348 if (buf_spname(curbuf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02001349 vim_strncpy(NameBuff, buf_spname(curbuf), MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001350 else
1351 home_replace(NULL, curbuf->b_ffname, NameBuff, MAXPATHL, TRUE);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001352 smsg(_("Original file \"%s\""), NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001353 msg_putchar('\n');
1354
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02001355 /*
1356 * check date of swap file and original file
1357 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001358 mtime = char_to_long(b0p->b0_mtime);
1359 if (curbuf->b_ffname != NULL
1360 && mch_stat((char *)curbuf->b_ffname, &org_stat) != -1
1361 && ((mch_stat((char *)mfp->mf_fname, &swp_stat) != -1
1362 && org_stat.st_mtime > swp_stat.st_mtime)
1363 || org_stat.st_mtime != mtime))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001364 emsg(_("E308: Warning: Original file may have been changed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001365 out_flush();
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001366
1367 /* Get the 'fileformat' and 'fileencoding' from block zero. */
1368 b0_ff = (b0p->b0_flags & B0_FF_MASK);
1369 if (b0p->b0_flags & B0_HAS_FENC)
1370 {
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02001371 int fnsize = B0_FNAME_SIZE_NOCRYPT;
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02001372
1373#ifdef FEAT_CRYPT
1374 /* Use the same size as in add_b0_fenc(). */
1375 if (b0p->b0_id[1] != BLOCK0_ID1)
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02001376 fnsize = B0_FNAME_SIZE_CRYPT;
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02001377#endif
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02001378 for (p = b0p->b0_fname + fnsize; p > b0p->b0_fname && p[-1] != NUL; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001379 ;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02001380 b0_fenc = vim_strnsave(p, (int)(b0p->b0_fname + fnsize - p));
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001381 }
1382
Bram Moolenaar071d4272004-06-13 20:20:40 +00001383 mf_put(mfp, hp, FALSE, FALSE); /* release block 0 */
1384 hp = NULL;
1385
1386 /*
1387 * Now that we are sure that the file is going to be recovered, clear the
1388 * contents of the current buffer.
1389 */
1390 while (!(curbuf->b_ml.ml_flags & ML_EMPTY))
1391 ml_delete((linenr_T)1, FALSE);
1392
1393 /*
1394 * Try reading the original file to obtain the values of 'fileformat',
1395 * 'fileencoding', etc. Ignore errors. The text itself is not used.
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02001396 * When the file is encrypted the user is asked to enter the key.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001397 */
1398 if (curbuf->b_ffname != NULL)
Bram Moolenaarfc2d5bd2010-05-15 17:06:53 +02001399 orig_file_status = readfile(curbuf->b_ffname, NULL, (linenr_T)0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001400 (linenr_T)0, (linenr_T)MAXLNUM, NULL, READ_NEW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001401
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02001402#ifdef FEAT_CRYPT
1403 if (b0_cm >= 0)
1404 {
1405 /* Need to ask the user for the crypt key. If this fails we continue
1406 * without a key, will probably get garbage text. */
1407 if (*curbuf->b_p_key != NUL)
1408 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001409 smsg(_("Swap file is encrypted: \"%s\""), fname_used);
Bram Moolenaar32526b32019-01-19 17:43:09 +01001410 msg_puts(_("\nIf you entered a new crypt key but did not write the text file,"));
1411 msg_puts(_("\nenter the new crypt key."));
1412 msg_puts(_("\nIf you wrote the text file after changing the crypt key press enter"));
1413 msg_puts(_("\nto use the same key for text file and swap file"));
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02001414 }
1415 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001416 smsg(_(need_key_msg), fname_used);
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02001417 buf->b_p_key = crypt_get_key(FALSE, FALSE);
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02001418 if (buf->b_p_key == NULL)
1419 buf->b_p_key = curbuf->b_p_key;
1420 else if (*buf->b_p_key == NUL)
1421 {
1422 vim_free(buf->b_p_key);
1423 buf->b_p_key = curbuf->b_p_key;
1424 }
1425 if (buf->b_p_key == NULL)
1426 buf->b_p_key = empty_option;
1427 }
1428#endif
1429
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001430 /* Use the 'fileformat' and 'fileencoding' as stored in the swap file. */
1431 if (b0_ff != 0)
1432 set_fileformat(b0_ff - 1, OPT_LOCAL);
1433 if (b0_fenc != NULL)
1434 {
1435 set_option_value((char_u *)"fenc", 0L, b0_fenc, OPT_LOCAL);
1436 vim_free(b0_fenc);
1437 }
Bram Moolenaarc024b462019-06-08 18:07:21 +02001438 unchanged(curbuf, TRUE, TRUE);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001439
Bram Moolenaar071d4272004-06-13 20:20:40 +00001440 bnum = 1; /* start with block 1 */
1441 page_count = 1; /* which is 1 page */
1442 lnum = 0; /* append after line 0 in curbuf */
1443 line_count = 0;
1444 idx = 0; /* start with first index in block 1 */
1445 error = 0;
1446 buf->b_ml.ml_stack_top = 0;
1447 buf->b_ml.ml_stack = NULL;
1448 buf->b_ml.ml_stack_size = 0; /* no stack yet */
1449
1450 if (curbuf->b_ffname == NULL)
1451 cannot_open = TRUE;
1452 else
1453 cannot_open = FALSE;
1454
1455 serious_error = FALSE;
1456 for ( ; !got_int; line_breakcheck())
1457 {
1458 if (hp != NULL)
1459 mf_put(mfp, hp, FALSE, FALSE); /* release previous block */
1460
1461 /*
1462 * get block
1463 */
1464 if ((hp = mf_get(mfp, (blocknr_T)bnum, page_count)) == NULL)
1465 {
1466 if (bnum == 1)
1467 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001468 semsg(_("E309: Unable to read block 1 from %s"), mfp->mf_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001469 goto theend;
1470 }
1471 ++error;
1472 ml_append(lnum++, (char_u *)_("???MANY LINES MISSING"),
1473 (colnr_T)0, TRUE);
1474 }
1475 else /* there is a block */
1476 {
1477 pp = (PTR_BL *)(hp->bh_data);
1478 if (pp->pb_id == PTR_ID) /* it is a pointer block */
1479 {
1480 /* check line count when using pointer block first time */
1481 if (idx == 0 && line_count != 0)
1482 {
1483 for (i = 0; i < (int)pp->pb_count; ++i)
1484 line_count -= pp->pb_pointer[i].pe_line_count;
1485 if (line_count != 0)
1486 {
1487 ++error;
1488 ml_append(lnum++, (char_u *)_("???LINE COUNT WRONG"),
1489 (colnr_T)0, TRUE);
1490 }
1491 }
1492
1493 if (pp->pb_count == 0)
1494 {
1495 ml_append(lnum++, (char_u *)_("???EMPTY BLOCK"),
1496 (colnr_T)0, TRUE);
1497 ++error;
1498 }
1499 else if (idx < (int)pp->pb_count) /* go a block deeper */
1500 {
1501 if (pp->pb_pointer[idx].pe_bnum < 0)
1502 {
1503 /*
1504 * Data block with negative block number.
1505 * Try to read lines from the original file.
1506 * This is slow, but it works.
1507 */
1508 if (!cannot_open)
1509 {
1510 line_count = pp->pb_pointer[idx].pe_line_count;
1511 if (readfile(curbuf->b_ffname, NULL, lnum,
1512 pp->pb_pointer[idx].pe_old_lnum - 1,
Bram Moolenaare13b9af2017-01-13 22:01:02 +01001513 line_count, NULL, 0) != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001514 cannot_open = TRUE;
1515 else
1516 lnum += line_count;
1517 }
1518 if (cannot_open)
1519 {
1520 ++error;
1521 ml_append(lnum++, (char_u *)_("???LINES MISSING"),
1522 (colnr_T)0, TRUE);
1523 }
1524 ++idx; /* get same block again for next index */
1525 continue;
1526 }
1527
1528 /*
1529 * going one block deeper in the tree
1530 */
1531 if ((top = ml_add_stack(buf)) < 0) /* new entry in stack */
1532 {
1533 ++error;
1534 break; /* out of memory */
1535 }
1536 ip = &(buf->b_ml.ml_stack[top]);
1537 ip->ip_bnum = bnum;
1538 ip->ip_index = idx;
1539
1540 bnum = pp->pb_pointer[idx].pe_bnum;
1541 line_count = pp->pb_pointer[idx].pe_line_count;
1542 page_count = pp->pb_pointer[idx].pe_page_count;
Bram Moolenaar986a0032011-06-13 01:07:27 +02001543 idx = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001544 continue;
1545 }
1546 }
1547 else /* not a pointer block */
1548 {
1549 dp = (DATA_BL *)(hp->bh_data);
1550 if (dp->db_id != DATA_ID) /* block id wrong */
1551 {
1552 if (bnum == 1)
1553 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001554 semsg(_("E310: Block 1 ID wrong (%s not a .swp file?)"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001555 mfp->mf_fname);
1556 goto theend;
1557 }
1558 ++error;
1559 ml_append(lnum++, (char_u *)_("???BLOCK MISSING"),
1560 (colnr_T)0, TRUE);
1561 }
1562 else
1563 {
1564 /*
1565 * it is a data block
1566 * Append all the lines in this block
1567 */
1568 has_error = FALSE;
1569 /*
1570 * check length of block
1571 * if wrong, use length in pointer block
1572 */
1573 if (page_count * mfp->mf_page_size != dp->db_txt_end)
1574 {
1575 ml_append(lnum++, (char_u *)_("??? from here until ???END lines may be messed up"),
1576 (colnr_T)0, TRUE);
1577 ++error;
1578 has_error = TRUE;
1579 dp->db_txt_end = page_count * mfp->mf_page_size;
1580 }
1581
1582 /* make sure there is a NUL at the end of the block */
1583 *((char_u *)dp + dp->db_txt_end - 1) = NUL;
1584
1585 /*
1586 * check number of lines in block
1587 * if wrong, use count in data block
1588 */
1589 if (line_count != dp->db_line_count)
1590 {
1591 ml_append(lnum++, (char_u *)_("??? from here until ???END lines may have been inserted/deleted"),
1592 (colnr_T)0, TRUE);
1593 ++error;
1594 has_error = TRUE;
1595 }
1596
1597 for (i = 0; i < dp->db_line_count; ++i)
1598 {
1599 txt_start = (dp->db_index[i] & DB_INDEX_MASK);
Bram Moolenaar740885b2009-11-03 14:33:17 +00001600 if (txt_start <= (int)HEADER_SIZE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001601 || txt_start >= (int)dp->db_txt_end)
1602 {
1603 p = (char_u *)"???";
1604 ++error;
1605 }
1606 else
1607 p = (char_u *)dp + txt_start;
1608 ml_append(lnum++, p, (colnr_T)0, TRUE);
1609 }
1610 if (has_error)
Bram Moolenaar740885b2009-11-03 14:33:17 +00001611 ml_append(lnum++, (char_u *)_("???END"),
1612 (colnr_T)0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001613 }
1614 }
1615 }
1616
1617 if (buf->b_ml.ml_stack_top == 0) /* finished */
1618 break;
1619
1620 /*
1621 * go one block up in the tree
1622 */
1623 ip = &(buf->b_ml.ml_stack[--(buf->b_ml.ml_stack_top)]);
1624 bnum = ip->ip_bnum;
1625 idx = ip->ip_index + 1; /* go to next index */
1626 page_count = 1;
1627 }
1628
1629 /*
Bram Moolenaarfc2d5bd2010-05-15 17:06:53 +02001630 * Compare the buffer contents with the original file. When they differ
1631 * set the 'modified' flag.
1632 * Lines 1 - lnum are the new contents.
1633 * Lines lnum + 1 to ml_line_count are the original contents.
1634 * Line ml_line_count + 1 in the dummy empty line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001635 */
Bram Moolenaarfc2d5bd2010-05-15 17:06:53 +02001636 if (orig_file_status != OK || curbuf->b_ml.ml_line_count != lnum * 2 + 1)
1637 {
1638 /* Recovering an empty file results in two lines and the first line is
1639 * empty. Don't set the modified flag then. */
1640 if (!(curbuf->b_ml.ml_line_count == 2 && *ml_get(1) == NUL))
1641 {
Bram Moolenaarec28d152019-05-11 18:36:34 +02001642 changed_internal();
Bram Moolenaar95c526e2017-02-25 14:59:34 +01001643 ++CHANGEDTICK(curbuf);
Bram Moolenaarfc2d5bd2010-05-15 17:06:53 +02001644 }
1645 }
1646 else
1647 {
1648 for (idx = 1; idx <= lnum; ++idx)
1649 {
1650 /* Need to copy one line, fetching the other one may flush it. */
1651 p = vim_strsave(ml_get(idx));
1652 i = STRCMP(p, ml_get(idx + lnum));
1653 vim_free(p);
1654 if (i != 0)
1655 {
Bram Moolenaarec28d152019-05-11 18:36:34 +02001656 changed_internal();
Bram Moolenaar95c526e2017-02-25 14:59:34 +01001657 ++CHANGEDTICK(curbuf);
Bram Moolenaarfc2d5bd2010-05-15 17:06:53 +02001658 break;
1659 }
1660 }
1661 }
1662
1663 /*
1664 * Delete the lines from the original file and the dummy line from the
1665 * empty buffer. These will now be after the last line in the buffer.
1666 */
1667 while (curbuf->b_ml.ml_line_count > lnum
1668 && !(curbuf->b_ml.ml_flags & ML_EMPTY))
1669 ml_delete(curbuf->b_ml.ml_line_count, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001670 curbuf->b_flags |= BF_RECOVERED;
1671
1672 recoverymode = FALSE;
1673 if (got_int)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001674 emsg(_("E311: Recovery Interrupted"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001675 else if (error)
1676 {
1677 ++no_wait_return;
Bram Moolenaar32526b32019-01-19 17:43:09 +01001678 msg(">>>>>>>>>>>>>");
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001679 emsg(_("E312: Errors detected while recovering; look for lines starting with ???"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001680 --no_wait_return;
Bram Moolenaar32526b32019-01-19 17:43:09 +01001681 msg(_("See \":help E312\" for more information."));
1682 msg(">>>>>>>>>>>>>");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001683 }
1684 else
1685 {
Bram Moolenaarfc2d5bd2010-05-15 17:06:53 +02001686 if (curbuf->b_changed)
1687 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01001688 msg(_("Recovery completed. You should check if everything is OK."));
1689 msg_puts(_("\n(You might want to write out this file under another name\n"));
1690 msg_puts(_("and run diff with the original file to check for changes)"));
Bram Moolenaarfc2d5bd2010-05-15 17:06:53 +02001691 }
1692 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01001693 msg(_("Recovery completed. Buffer contents equals file contents."));
1694 msg_puts(_("\nYou may want to delete the .swp file now.\n\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001695 cmdline_row = msg_row;
1696 }
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02001697#ifdef FEAT_CRYPT
1698 if (*buf->b_p_key != NUL && STRCMP(curbuf->b_p_key, buf->b_p_key) != 0)
1699 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01001700 msg_puts(_("Using crypt key from swap file for the text file.\n"));
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02001701 set_option_value((char_u *)"key", 0L, buf->b_p_key, OPT_LOCAL);
1702 }
1703#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001704 redraw_curbuf_later(NOT_VALID);
1705
1706theend:
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02001707 vim_free(fname_used);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001708 recoverymode = FALSE;
1709 if (mfp != NULL)
1710 {
1711 if (hp != NULL)
1712 mf_put(mfp, hp, FALSE, FALSE);
1713 mf_close(mfp, FALSE); /* will also vim_free(mfp->mf_fname) */
1714 }
Bram Moolenaardf88dda2007-01-09 13:34:50 +00001715 if (buf != NULL)
1716 {
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02001717#ifdef FEAT_CRYPT
1718 if (buf->b_p_key != curbuf->b_p_key)
1719 free_string_option(buf->b_p_key);
Bram Moolenaar0ad014c2010-07-25 14:00:46 +02001720 free_string_option(buf->b_p_cm);
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02001721#endif
Bram Moolenaardf88dda2007-01-09 13:34:50 +00001722 vim_free(buf->b_ml.ml_stack);
1723 vim_free(buf);
1724 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001725 if (serious_error && called_from_main)
1726 ml_close(curbuf, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001727 else
1728 {
1729 apply_autocmds(EVENT_BUFREADPOST, NULL, curbuf->b_fname, FALSE, curbuf);
1730 apply_autocmds(EVENT_BUFWINENTER, NULL, curbuf->b_fname, FALSE, curbuf);
1731 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001732 return;
1733}
1734
1735/*
1736 * Find the names of swap files in current directory and the directory given
1737 * with the 'directory' option.
1738 *
1739 * Used to:
1740 * - list the swap files for "vim -r"
1741 * - count the number of swap files when recovering
1742 * - list the swap files when recovering
1743 * - find the name of the n'th swap file when recovering
1744 */
1745 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001746recover_names(
1747 char_u *fname, /* base for swap file name */
1748 int list, /* when TRUE, list the swap file names */
1749 int nr, /* when non-zero, return nr'th swap file name */
1750 char_u **fname_out) /* result when "nr" > 0 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001751{
1752 int num_names;
1753 char_u *(names[6]);
1754 char_u *tail;
1755 char_u *p;
1756 int num_files;
1757 int file_count = 0;
1758 char_u **files;
1759 int i;
1760 char_u *dirp;
1761 char_u *dir_name;
Bram Moolenaar64354da2010-05-25 21:37:17 +02001762 char_u *fname_res = NULL;
Bram Moolenaar9dbe4752010-05-14 17:52:42 +02001763#ifdef HAVE_READLINK
1764 char_u fname_buf[MAXPATHL];
Bram Moolenaar64354da2010-05-25 21:37:17 +02001765#endif
Bram Moolenaar9dbe4752010-05-14 17:52:42 +02001766
Bram Moolenaar64354da2010-05-25 21:37:17 +02001767 if (fname != NULL)
1768 {
1769#ifdef HAVE_READLINK
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02001770 /* Expand symlink in the file name, because the swap file is created
1771 * with the actual file instead of with the symlink. */
1772 if (resolve_symlink(fname, fname_buf) == OK)
1773 fname_res = fname_buf;
1774 else
Bram Moolenaar9dbe4752010-05-14 17:52:42 +02001775#endif
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02001776 fname_res = fname;
Bram Moolenaar64354da2010-05-25 21:37:17 +02001777 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001778
1779 if (list)
1780 {
Bram Moolenaar9dbe4752010-05-14 17:52:42 +02001781 /* use msg() to start the scrolling properly */
Bram Moolenaar32526b32019-01-19 17:43:09 +01001782 msg(_("Swap files found:"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001783 msg_putchar('\n');
1784 }
1785
1786 /*
1787 * Do the loop for every directory in 'directory'.
1788 * First allocate some memory to put the directory name in.
1789 */
Bram Moolenaar964b3742019-05-24 18:54:09 +02001790 dir_name = alloc(STRLEN(p_dir) + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001791 dirp = p_dir;
1792 while (dir_name != NULL && *dirp)
1793 {
1794 /*
1795 * Isolate a directory name from *dirp and put it in dir_name (we know
1796 * it is large enough, so use 31000 for length).
1797 * Advance dirp to next directory name.
1798 */
1799 (void)copy_option_part(&dirp, dir_name, 31000, ",");
1800
1801 if (dir_name[0] == '.' && dir_name[1] == NUL) /* check current dir */
1802 {
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02001803 if (fname == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001804 {
1805#ifdef VMS
1806 names[0] = vim_strsave((char_u *)"*_sw%");
1807#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001808 names[0] = vim_strsave((char_u *)"*.sw?");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001809#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01001810#if defined(UNIX) || defined(MSWIN)
Bram Moolenaar2cc93182006-10-10 19:56:03 +00001811 /* For Unix names starting with a dot are special. MS-Windows
1812 * supports this too, on some file systems. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001813 names[1] = vim_strsave((char_u *)".*.sw?");
1814 names[2] = vim_strsave((char_u *)".sw?");
1815 num_names = 3;
1816#else
1817# ifdef VMS
1818 names[1] = vim_strsave((char_u *)".*_sw%");
1819 num_names = 2;
1820# else
1821 num_names = 1;
1822# endif
1823#endif
1824 }
1825 else
Bram Moolenaar9dbe4752010-05-14 17:52:42 +02001826 num_names = recov_file_names(names, fname_res, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001827 }
1828 else /* check directory dir_name */
1829 {
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02001830 if (fname == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001831 {
1832#ifdef VMS
1833 names[0] = concat_fnames(dir_name, (char_u *)"*_sw%", TRUE);
1834#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001835 names[0] = concat_fnames(dir_name, (char_u *)"*.sw?", TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001836#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01001837#if defined(UNIX) || defined(MSWIN)
Bram Moolenaar2cc93182006-10-10 19:56:03 +00001838 /* For Unix names starting with a dot are special. MS-Windows
1839 * supports this too, on some file systems. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001840 names[1] = concat_fnames(dir_name, (char_u *)".*.sw?", TRUE);
1841 names[2] = concat_fnames(dir_name, (char_u *)".sw?", TRUE);
1842 num_names = 3;
1843#else
1844# ifdef VMS
1845 names[1] = concat_fnames(dir_name, (char_u *)".*_sw%", TRUE);
1846 num_names = 2;
1847# else
1848 num_names = 1;
1849# endif
1850#endif
1851 }
1852 else
1853 {
Bram Moolenaar4f974752019-02-17 17:44:42 +01001854#if defined(UNIX) || defined(MSWIN)
Bram Moolenaarb113c3a2017-02-28 21:26:17 +01001855 int len = (int)STRLEN(dir_name);
Bram Moolenaarc525e3a2017-02-18 16:59:02 +01001856
1857 p = dir_name + len;
1858 if (after_pathsep(dir_name, p) && len > 1 && p[-1] == p[-2])
Bram Moolenaar071d4272004-06-13 20:20:40 +00001859 {
1860 /* Ends with '//', Use Full path for swap name */
Bram Moolenaar9dbe4752010-05-14 17:52:42 +02001861 tail = make_percent_swname(dir_name, fname_res);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001862 }
1863 else
1864#endif
1865 {
Bram Moolenaar9dbe4752010-05-14 17:52:42 +02001866 tail = gettail(fname_res);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001867 tail = concat_fnames(dir_name, tail, TRUE);
1868 }
1869 if (tail == NULL)
1870 num_names = 0;
1871 else
1872 {
1873 num_names = recov_file_names(names, tail, FALSE);
1874 vim_free(tail);
1875 }
1876 }
1877 }
1878
Bram Moolenaar0d3cb732019-05-18 13:05:18 +02001879 // check for out-of-memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00001880 for (i = 0; i < num_names; ++i)
1881 {
1882 if (names[i] == NULL)
1883 {
1884 for (i = 0; i < num_names; ++i)
1885 vim_free(names[i]);
1886 num_names = 0;
1887 }
1888 }
1889 if (num_names == 0)
1890 num_files = 0;
1891 else if (expand_wildcards(num_names, names, &num_files, &files,
Bram Moolenaar99499b12019-05-23 21:35:48 +02001892 EW_NOTENV|EW_KEEPALL|EW_FILE|EW_SILENT) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001893 num_files = 0;
1894
1895 /*
1896 * When no swap file found, wildcard expansion might have failed (e.g.
1897 * not able to execute the shell).
1898 * Try finding a swap file by simply adding ".swp" to the file name.
1899 */
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02001900 if (*dirp == NUL && file_count + num_files == 0 && fname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001901 {
Bram Moolenaar8767f522016-07-01 17:17:39 +02001902 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001903 char_u *swapname;
1904
Bram Moolenaar9dbe4752010-05-14 17:52:42 +02001905 swapname = modname(fname_res,
Bram Moolenaare60acc12011-05-10 16:41:25 +02001906#if defined(VMS)
Bram Moolenaar9dbe4752010-05-14 17:52:42 +02001907 (char_u *)"_swp", FALSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001908#else
Bram Moolenaar9dbe4752010-05-14 17:52:42 +02001909 (char_u *)".swp", TRUE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001910#endif
Bram Moolenaar9dbe4752010-05-14 17:52:42 +02001911 );
Bram Moolenaar071d4272004-06-13 20:20:40 +00001912 if (swapname != NULL)
1913 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001914 if (mch_stat((char *)swapname, &st) != -1) // It exists!
Bram Moolenaar071d4272004-06-13 20:20:40 +00001915 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001916 files = ALLOC_ONE(char_u *);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001917 if (files != NULL)
1918 {
1919 files[0] = swapname;
1920 swapname = NULL;
1921 num_files = 1;
1922 }
1923 }
1924 vim_free(swapname);
1925 }
1926 }
1927
1928 /*
1929 * remove swapfile name of the current buffer, it must be ignored
1930 */
1931 if (curbuf->b_ml.ml_mfp != NULL
1932 && (p = curbuf->b_ml.ml_mfp->mf_fname) != NULL)
1933 {
1934 for (i = 0; i < num_files; ++i)
Bram Moolenaar99499b12019-05-23 21:35:48 +02001935 // Do not expand wildcards, on windows would try to expand
1936 // "%tmp%" in "%tmp%file".
1937 if (fullpathcmp(p, files[i], TRUE, FALSE) & FPC_SAME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001938 {
Bram Moolenaar99499b12019-05-23 21:35:48 +02001939 // Remove the name from files[i]. Move further entries
1940 // down. When the array becomes empty free it here, since
1941 // FreeWild() won't be called below.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001942 vim_free(files[i]);
Bram Moolenaar9439cdd2009-04-22 13:39:36 +00001943 if (--num_files == 0)
1944 vim_free(files);
1945 else
1946 for ( ; i < num_files; ++i)
1947 files[i] = files[i + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001948 }
1949 }
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00001950 if (nr > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001951 {
1952 file_count += num_files;
1953 if (nr <= file_count)
1954 {
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02001955 *fname_out = vim_strsave(
1956 files[nr - 1 + num_files - file_count]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001957 dirp = (char_u *)""; /* stop searching */
1958 }
1959 }
1960 else if (list)
1961 {
1962 if (dir_name[0] == '.' && dir_name[1] == NUL)
1963 {
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02001964 if (fname == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001965 msg_puts(_(" In current directory:\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001966 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01001967 msg_puts(_(" Using specified name:\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001968 }
1969 else
1970 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01001971 msg_puts(_(" In directory "));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001972 msg_home_replace(dir_name);
Bram Moolenaar32526b32019-01-19 17:43:09 +01001973 msg_puts(":\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001974 }
1975
1976 if (num_files)
1977 {
1978 for (i = 0; i < num_files; ++i)
1979 {
1980 /* print the swap file name */
1981 msg_outnum((long)++file_count);
Bram Moolenaar32526b32019-01-19 17:43:09 +01001982 msg_puts(". ");
1983 msg_puts((char *)gettail(files[i]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001984 msg_putchar('\n');
1985 (void)swapfile_info(files[i]);
1986 }
1987 }
1988 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01001989 msg_puts(_(" -- none --\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001990 out_flush();
1991 }
1992 else
1993 file_count += num_files;
1994
1995 for (i = 0; i < num_names; ++i)
1996 vim_free(names[i]);
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00001997 if (num_files > 0)
1998 FreeWild(num_files, files);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001999 }
2000 vim_free(dir_name);
2001 return file_count;
2002}
2003
Bram Moolenaar4f974752019-02-17 17:44:42 +01002004#if defined(UNIX) || defined(MSWIN) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002005/*
Bram Moolenaarb782ba42018-08-07 21:39:28 +02002006 * Need _very_ long file names.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002007 * Append the full path to name with path separators made into percent
2008 * signs, to dir. An unnamed buffer is handled as "" (<currentdir>/"")
2009 */
Bram Moolenaarb782ba42018-08-07 21:39:28 +02002010 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002011make_percent_swname(char_u *dir, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002012{
Bram Moolenaarb782ba42018-08-07 21:39:28 +02002013 char_u *d = NULL, *s, *f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002014
Bram Moolenaarb782ba42018-08-07 21:39:28 +02002015 f = fix_fname(name != NULL ? name : (char_u *)"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002016 if (f != NULL)
2017 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02002018 s = alloc(STRLEN(f) + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002019 if (s != NULL)
2020 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002021 STRCPY(s, f);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002022 for (d = s; *d != NUL; MB_PTR_ADV(d))
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002023 if (vim_ispathsep(*d))
2024 *d = '%';
Bram Moolenaar071d4272004-06-13 20:20:40 +00002025 d = concat_fnames(dir, s, TRUE);
2026 vim_free(s);
2027 }
2028 vim_free(f);
2029 }
2030 return d;
2031}
2032#endif
2033
Bram Moolenaar1b243ea2019-04-28 22:50:40 +02002034#if (defined(UNIX) || defined(VMS) || defined(MSWIN)) \
2035 && (defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG))
2036# define HAVE_PROCESS_STILL_RUNNING
Bram Moolenaar071d4272004-06-13 20:20:40 +00002037static int process_still_running;
2038#endif
2039
Bram Moolenaar47ad5652018-08-21 21:09:07 +02002040#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002041/*
Bram Moolenaar00f123a2018-08-21 20:28:54 +02002042 * Return information found in swapfile "fname" in dictionary "d".
2043 * This is used by the swapinfo() function.
2044 */
2045 void
2046get_b0_dict(char_u *fname, dict_T *d)
2047{
2048 int fd;
2049 struct block0 b0;
2050
2051 if ((fd = mch_open((char *)fname, O_RDONLY | O_EXTRA, 0)) >= 0)
2052 {
2053 if (read_eintr(fd, &b0, sizeof(b0)) == sizeof(b0))
2054 {
Bram Moolenaar47ad5652018-08-21 21:09:07 +02002055 if (ml_check_b0_id(&b0) == FAIL)
Bram Moolenaare6fdf792018-12-26 22:57:42 +01002056 dict_add_string(d, "error", (char_u *)"Not a swap file");
Bram Moolenaar47ad5652018-08-21 21:09:07 +02002057 else if (b0_magic_wrong(&b0))
Bram Moolenaare6fdf792018-12-26 22:57:42 +01002058 dict_add_string(d, "error", (char_u *)"Magic number mismatch");
Bram Moolenaar00f123a2018-08-21 20:28:54 +02002059 else
2060 {
2061 /* we have swap information */
Bram Moolenaare6fdf792018-12-26 22:57:42 +01002062 dict_add_string_len(d, "version", b0.b0_version, 10);
2063 dict_add_string_len(d, "user", b0.b0_uname, B0_UNAME_SIZE);
2064 dict_add_string_len(d, "host", b0.b0_hname, B0_HNAME_SIZE);
2065 dict_add_string_len(d, "fname", b0.b0_fname, B0_FNAME_SIZE_ORG);
Bram Moolenaar00f123a2018-08-21 20:28:54 +02002066
2067 dict_add_number(d, "pid", char_to_long(b0.b0_pid));
2068 dict_add_number(d, "mtime", char_to_long(b0.b0_mtime));
Bram Moolenaar47ad5652018-08-21 21:09:07 +02002069 dict_add_number(d, "dirty", b0.b0_dirty ? 1 : 0);
2070# ifdef CHECK_INODE
Bram Moolenaar00f123a2018-08-21 20:28:54 +02002071 dict_add_number(d, "inode", char_to_long(b0.b0_ino));
Bram Moolenaar47ad5652018-08-21 21:09:07 +02002072# endif
Bram Moolenaar00f123a2018-08-21 20:28:54 +02002073 }
2074 }
2075 else
Bram Moolenaare6fdf792018-12-26 22:57:42 +01002076 dict_add_string(d, "error", (char_u *)"Cannot read file");
Bram Moolenaar00f123a2018-08-21 20:28:54 +02002077 close(fd);
2078 }
2079 else
Bram Moolenaare6fdf792018-12-26 22:57:42 +01002080 dict_add_string(d, "error", (char_u *)"Cannot open file");
Bram Moolenaar00f123a2018-08-21 20:28:54 +02002081}
Bram Moolenaar47ad5652018-08-21 21:09:07 +02002082#endif
Bram Moolenaar00f123a2018-08-21 20:28:54 +02002083
2084/*
Bram Moolenaardb517302019-06-18 22:53:24 +02002085 * Cache of the current timezone name as retrieved from TZ, or an empty string
2086 * where unset, up to 64 octets long including trailing null byte.
2087 */
2088#if defined(HAVE_LOCALTIME_R) && defined(HAVE_TZSET)
2089static char tz_cache[64];
2090#endif
2091
2092/*
2093 * Call either localtime(3) or localtime_r(3) from POSIX libc time.h, with the
2094 * latter version preferred for reentrancy.
2095 *
2096 * If we use localtime_r(3) and we have tzset(3) available, check to see if the
2097 * environment variable TZ has changed since the last run, and call tzset(3) to
2098 * update the global timezone variables if it has. This is because the POSIX
2099 * standard doesn't require localtime_r(3) implementations to do that as it
2100 * does with localtime(3), and we don't want to call tzset(3) every time.
2101 */
2102 struct tm *
2103vim_localtime(
2104 const time_t *timep, // timestamp for local representation
2105 struct tm *result) // pointer to caller return buffer
2106{
2107#ifdef HAVE_LOCALTIME_R
2108# ifdef HAVE_TZSET
2109 char *tz; // pointer for TZ environment var
2110
2111 tz = (char *)mch_getenv((char_u *)"TZ");
2112 if (tz == NULL)
2113 tz = "";
2114 if (STRNCMP(tz_cache, tz, sizeof(tz_cache) - 1) != 0)
2115 {
2116 tzset();
2117 vim_strncpy((char_u *)tz_cache, (char_u *)tz, sizeof(tz_cache) - 1);
2118 }
2119# endif // HAVE_TZSET
2120 return localtime_r(timep, result);
2121#else
2122 return localtime(timep);
2123#endif // HAVE_LOCALTIME_R
2124}
2125
2126/*
Bram Moolenaar63d25552019-05-10 21:28:38 +02002127 * Replacement for ctime(), which is not safe to use.
2128 * Requires strftime(), otherwise returns "(unknown)".
2129 * If "thetime" is invalid returns "(invalid)". Never returns NULL.
2130 * When "add_newline" is TRUE add a newline like ctime() does.
2131 * Uses a static buffer.
2132 */
2133 char *
2134get_ctime(time_t thetime, int add_newline)
2135{
2136 static char buf[50];
2137#ifdef HAVE_STRFTIME
Bram Moolenaar63d25552019-05-10 21:28:38 +02002138 struct tm tmval;
Bram Moolenaar63d25552019-05-10 21:28:38 +02002139 struct tm *curtime;
2140
Bram Moolenaardb517302019-06-18 22:53:24 +02002141 curtime = vim_localtime(&thetime, &tmval);
Bram Moolenaar63d25552019-05-10 21:28:38 +02002142 /* MSVC returns NULL for an invalid value of seconds. */
2143 if (curtime == NULL)
Bram Moolenaar0d3cb732019-05-18 13:05:18 +02002144 vim_strncpy((char_u *)buf, (char_u *)_("(Invalid)"), sizeof(buf) - 1);
Bram Moolenaar63d25552019-05-10 21:28:38 +02002145 else
2146 (void)strftime(buf, sizeof(buf) - 1, "%a %b %d %H:%M:%S %Y", curtime);
2147#else
2148 STRCPY(buf, "(unknown)");
2149#endif
2150 if (add_newline)
2151 STRCAT(buf, "\n");
2152 return buf;
2153}
2154
2155/*
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002156 * Give information about an existing swap file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002157 * Returns timestamp (0 when unknown).
2158 */
2159 static time_t
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002160swapfile_info(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002161{
Bram Moolenaar8767f522016-07-01 17:17:39 +02002162 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002163 int fd;
2164 struct block0 b0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002165#ifdef UNIX
2166 char_u uname[B0_UNAME_SIZE];
2167#endif
2168
Bram Moolenaar63d25552019-05-10 21:28:38 +02002169 // print the swap file date
Bram Moolenaar071d4272004-06-13 20:20:40 +00002170 if (mch_stat((char *)fname, &st) != -1)
2171 {
2172#ifdef UNIX
Bram Moolenaar63d25552019-05-10 21:28:38 +02002173 // print name of owner of the file
Bram Moolenaar071d4272004-06-13 20:20:40 +00002174 if (mch_get_uname(st.st_uid, uname, B0_UNAME_SIZE) == OK)
2175 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01002176 msg_puts(_(" owned by: "));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002177 msg_outtrans(uname);
Bram Moolenaar32526b32019-01-19 17:43:09 +01002178 msg_puts(_(" dated: "));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002179 }
2180 else
2181#endif
Bram Moolenaar32526b32019-01-19 17:43:09 +01002182 msg_puts(_(" dated: "));
Bram Moolenaar63d25552019-05-10 21:28:38 +02002183 msg_puts(get_ctime(st.st_mtime, TRUE));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002184 }
Bram Moolenaar63d25552019-05-10 21:28:38 +02002185 else
2186 st.st_mtime = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002187
2188 /*
2189 * print the original file name
2190 */
2191 fd = mch_open((char *)fname, O_RDONLY | O_EXTRA, 0);
2192 if (fd >= 0)
2193 {
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01002194 if (read_eintr(fd, &b0, sizeof(b0)) == sizeof(b0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002195 {
2196 if (STRNCMP(b0.b0_version, "VIM 3.0", 7) == 0)
2197 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01002198 msg_puts(_(" [from Vim version 3.0]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002199 }
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02002200 else if (ml_check_b0_id(&b0) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002201 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01002202 msg_puts(_(" [does not look like a Vim swap file]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002203 }
2204 else
2205 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01002206 msg_puts(_(" file name: "));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002207 if (b0.b0_fname[0] == NUL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002208 msg_puts(_("[No Name]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002209 else
2210 msg_outtrans(b0.b0_fname);
2211
Bram Moolenaar32526b32019-01-19 17:43:09 +01002212 msg_puts(_("\n modified: "));
2213 msg_puts(b0.b0_dirty ? _("YES") : _("no"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002214
2215 if (*(b0.b0_uname) != NUL)
2216 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01002217 msg_puts(_("\n user name: "));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002218 msg_outtrans(b0.b0_uname);
2219 }
2220
2221 if (*(b0.b0_hname) != NUL)
2222 {
2223 if (*(b0.b0_uname) != NUL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002224 msg_puts(_(" host name: "));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002225 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01002226 msg_puts(_("\n host name: "));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002227 msg_outtrans(b0.b0_hname);
2228 }
2229
2230 if (char_to_long(b0.b0_pid) != 0L)
2231 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01002232 msg_puts(_("\n process ID: "));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002233 msg_outnum(char_to_long(b0.b0_pid));
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02002234#if defined(UNIX) || defined(MSWIN)
Bram Moolenaar1b243ea2019-04-28 22:50:40 +02002235 if (mch_process_running(char_to_long(b0.b0_pid)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002236 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01002237 msg_puts(_(" (STILL RUNNING)"));
Bram Moolenaar1b243ea2019-04-28 22:50:40 +02002238# ifdef HAVE_PROCESS_STILL_RUNNING
Bram Moolenaar071d4272004-06-13 20:20:40 +00002239 process_still_running = TRUE;
2240# endif
2241 }
2242#endif
2243 }
2244
2245 if (b0_magic_wrong(&b0))
2246 {
Bram Moolenaar48e330a2016-02-23 14:53:34 +01002247#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002248 if (STRNCMP(b0.b0_hname, "PC ", 3) == 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002249 msg_puts(_("\n [not usable with this version of Vim]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002250 else
2251#endif
Bram Moolenaar32526b32019-01-19 17:43:09 +01002252 msg_puts(_("\n [not usable on this computer]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002253 }
2254 }
2255 }
2256 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01002257 msg_puts(_(" [cannot be read]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002258 close(fd);
2259 }
2260 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01002261 msg_puts(_(" [cannot be opened]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002262 msg_putchar('\n');
2263
Bram Moolenaar63d25552019-05-10 21:28:38 +02002264 return st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002265}
2266
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02002267/*
2268 * Return TRUE if the swap file looks OK and there are no changes, thus it can
2269 * be safely deleted.
2270 */
2271 static time_t
2272swapfile_unchanged(char_u *fname)
2273{
2274 stat_T st;
2275 int fd;
2276 struct block0 b0;
2277 int ret = TRUE;
Bram Moolenaar1b243ea2019-04-28 22:50:40 +02002278#if defined(UNIX) || defined(MSWIN)
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02002279 long pid;
2280#endif
2281
2282 // must be able to stat the swap file
2283 if (mch_stat((char *)fname, &st) == -1)
2284 return FALSE;
2285
2286 // must be able to read the first block
2287 fd = mch_open((char *)fname, O_RDONLY | O_EXTRA, 0);
2288 if (fd < 0)
2289 return FALSE;
2290 if (read_eintr(fd, &b0, sizeof(b0)) != sizeof(b0))
2291 {
2292 close(fd);
2293 return FALSE;
2294 }
2295
2296 // the ID and magic number must be correct
2297 if (ml_check_b0_id(&b0) == FAIL|| b0_magic_wrong(&b0))
2298 ret = FALSE;
2299
2300 // must be unchanged
2301 if (b0.b0_dirty)
2302 ret = FALSE;
2303
2304#if defined(UNIX) || defined(MSWIN)
2305 // process must known and not be running
2306 pid = char_to_long(b0.b0_pid);
Bram Moolenaar1b243ea2019-04-28 22:50:40 +02002307 if (pid == 0L || mch_process_running(pid))
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02002308 ret = FALSE;
2309#endif
2310
2311 // TODO: Should we check if the swap file was created on the current
2312 // system? And the current user?
2313
2314 close(fd);
2315 return ret;
2316}
2317
Bram Moolenaar071d4272004-06-13 20:20:40 +00002318 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002319recov_file_names(char_u **names, char_u *path, int prepend_dot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002320{
2321 int num_names;
2322
Bram Moolenaar071d4272004-06-13 20:20:40 +00002323 /*
2324 * (Win32 and Win64) never short names, but do prepend a dot.
2325 * (Not MS-DOS or Win32 or Win64) maybe short name, maybe not: Try both.
2326 * Only use the short name if it is different.
2327 */
2328 char_u *p;
2329 int i;
Bram Moolenaar4f974752019-02-17 17:44:42 +01002330# ifndef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00002331 int shortname = curbuf->b_shortname;
2332
2333 curbuf->b_shortname = FALSE;
2334# endif
2335
2336 num_names = 0;
2337
2338 /*
2339 * May also add the file name with a dot prepended, for swap file in same
2340 * dir as original file.
2341 */
2342 if (prepend_dot)
2343 {
2344 names[num_names] = modname(path, (char_u *)".sw?", TRUE);
2345 if (names[num_names] == NULL)
2346 goto end;
2347 ++num_names;
2348 }
2349
2350 /*
2351 * Form the normal swap file name pattern by appending ".sw?".
2352 */
2353#ifdef VMS
2354 names[num_names] = concat_fnames(path, (char_u *)"_sw%", FALSE);
2355#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002356 names[num_names] = concat_fnames(path, (char_u *)".sw?", FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002357#endif
2358 if (names[num_names] == NULL)
2359 goto end;
2360 if (num_names >= 1) /* check if we have the same name twice */
2361 {
2362 p = names[num_names - 1];
2363 i = (int)STRLEN(names[num_names - 1]) - (int)STRLEN(names[num_names]);
2364 if (i > 0)
2365 p += i; /* file name has been expanded to full path */
2366
2367 if (STRCMP(p, names[num_names]) != 0)
2368 ++num_names;
2369 else
2370 vim_free(names[num_names]);
2371 }
2372 else
2373 ++num_names;
2374
Bram Moolenaar4f974752019-02-17 17:44:42 +01002375# ifndef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00002376 /*
2377 * Also try with 'shortname' set, in case the file is on a DOS filesystem.
2378 */
2379 curbuf->b_shortname = TRUE;
2380#ifdef VMS
2381 names[num_names] = modname(path, (char_u *)"_sw%", FALSE);
2382#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002383 names[num_names] = modname(path, (char_u *)".sw?", FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002384#endif
2385 if (names[num_names] == NULL)
2386 goto end;
2387
2388 /*
2389 * Remove the one from 'shortname', if it's the same as with 'noshortname'.
2390 */
2391 p = names[num_names];
2392 i = STRLEN(names[num_names]) - STRLEN(names[num_names - 1]);
2393 if (i > 0)
2394 p += i; /* file name has been expanded to full path */
2395 if (STRCMP(names[num_names - 1], p) == 0)
2396 vim_free(names[num_names]);
2397 else
2398 ++num_names;
2399# endif
2400
2401end:
Bram Moolenaar4f974752019-02-17 17:44:42 +01002402# ifndef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00002403 curbuf->b_shortname = shortname;
2404# endif
2405
Bram Moolenaar071d4272004-06-13 20:20:40 +00002406 return num_names;
2407}
2408
2409/*
2410 * sync all memlines
2411 *
2412 * If 'check_file' is TRUE, check if original file exists and was not changed.
2413 * If 'check_char' is TRUE, stop syncing when character becomes available, but
2414 * always sync at least one block.
2415 */
2416 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002417ml_sync_all(int check_file, int check_char)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002418{
2419 buf_T *buf;
Bram Moolenaar8767f522016-07-01 17:17:39 +02002420 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002421
Bram Moolenaar29323592016-07-24 22:04:11 +02002422 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002423 {
2424 if (buf->b_ml.ml_mfp == NULL || buf->b_ml.ml_mfp->mf_fname == NULL)
2425 continue; /* no file */
2426
2427 ml_flush_line(buf); /* flush buffered line */
2428 /* flush locked block */
2429 (void)ml_find_line(buf, (linenr_T)0, ML_FLUSH);
2430 if (bufIsChanged(buf) && check_file && mf_need_trans(buf->b_ml.ml_mfp)
2431 && buf->b_ffname != NULL)
2432 {
2433 /*
2434 * If the original file does not exist anymore or has been changed
2435 * call ml_preserve() to get rid of all negative numbered blocks.
2436 */
2437 if (mch_stat((char *)buf->b_ffname, &st) == -1
2438 || st.st_mtime != buf->b_mtime_read
Bram Moolenaar914703b2010-05-31 21:59:46 +02002439 || st.st_size != buf->b_orig_size)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002440 {
2441 ml_preserve(buf, FALSE);
2442 did_check_timestamps = FALSE;
2443 need_check_timestamps = TRUE; /* give message later */
2444 }
2445 }
2446 if (buf->b_ml.ml_mfp->mf_dirty)
2447 {
2448 (void)mf_sync(buf->b_ml.ml_mfp, (check_char ? MFS_STOP : 0)
2449 | (bufIsChanged(buf) ? MFS_FLUSH : 0));
2450 if (check_char && ui_char_avail()) /* character available now */
2451 break;
2452 }
2453 }
2454}
2455
2456/*
2457 * sync one buffer, including negative blocks
2458 *
2459 * after this all the blocks are in the swap file
2460 *
2461 * Used for the :preserve command and when the original file has been
2462 * changed or deleted.
2463 *
2464 * when message is TRUE the success of preserving is reported
2465 */
2466 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002467ml_preserve(buf_T *buf, int message)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002468{
2469 bhdr_T *hp;
2470 linenr_T lnum;
2471 memfile_T *mfp = buf->b_ml.ml_mfp;
2472 int status;
2473 int got_int_save = got_int;
2474
2475 if (mfp == NULL || mfp->mf_fname == NULL)
2476 {
2477 if (message)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002478 emsg(_("E313: Cannot preserve, there is no swap file"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002479 return;
2480 }
2481
2482 /* We only want to stop when interrupted here, not when interrupted
2483 * before. */
2484 got_int = FALSE;
2485
2486 ml_flush_line(buf); /* flush buffered line */
2487 (void)ml_find_line(buf, (linenr_T)0, ML_FLUSH); /* flush locked block */
2488 status = mf_sync(mfp, MFS_ALL | MFS_FLUSH);
2489
2490 /* stack is invalid after mf_sync(.., MFS_ALL) */
2491 buf->b_ml.ml_stack_top = 0;
2492
2493 /*
2494 * Some of the data blocks may have been changed from negative to
2495 * positive block number. In that case the pointer blocks need to be
2496 * updated.
2497 *
2498 * We don't know in which pointer block the references are, so we visit
2499 * all data blocks until there are no more translations to be done (or
2500 * we hit the end of the file, which can only happen in case a write fails,
2501 * e.g. when file system if full).
2502 * ml_find_line() does the work by translating the negative block numbers
2503 * when getting the first line of each data block.
2504 */
2505 if (mf_need_trans(mfp) && !got_int)
2506 {
2507 lnum = 1;
2508 while (mf_need_trans(mfp) && lnum <= buf->b_ml.ml_line_count)
2509 {
2510 hp = ml_find_line(buf, lnum, ML_FIND);
2511 if (hp == NULL)
2512 {
2513 status = FAIL;
2514 goto theend;
2515 }
2516 CHECK(buf->b_ml.ml_locked_low != lnum, "low != lnum");
2517 lnum = buf->b_ml.ml_locked_high + 1;
2518 }
2519 (void)ml_find_line(buf, (linenr_T)0, ML_FLUSH); /* flush locked block */
2520 /* sync the updated pointer blocks */
2521 if (mf_sync(mfp, MFS_ALL | MFS_FLUSH) == FAIL)
2522 status = FAIL;
2523 buf->b_ml.ml_stack_top = 0; /* stack is invalid now */
2524 }
2525theend:
2526 got_int |= got_int_save;
2527
2528 if (message)
2529 {
2530 if (status == OK)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002531 msg(_("File preserved"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002532 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002533 emsg(_("E314: Preserve failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002534 }
2535}
2536
2537/*
2538 * NOTE: The pointer returned by the ml_get_*() functions only remains valid
2539 * until the next call!
2540 * line1 = ml_get(1);
2541 * line2 = ml_get(2); // line1 is now invalid!
2542 * Make a copy of the line if necessary.
2543 */
2544/*
Bram Moolenaar2e2e13c2010-12-08 13:17:03 +01002545 * Return a pointer to a (read-only copy of a) line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002546 *
2547 * On failure an error message is given and IObuff is returned (to avoid
2548 * having to check for error everywhere).
2549 */
2550 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002551ml_get(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002552{
2553 return ml_get_buf(curbuf, lnum, FALSE);
2554}
2555
2556/*
Bram Moolenaar2e2e13c2010-12-08 13:17:03 +01002557 * Return pointer to position "pos".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002558 */
2559 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002560ml_get_pos(pos_T *pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002561{
2562 return (ml_get_buf(curbuf, pos->lnum, FALSE) + pos->col);
2563}
2564
2565/*
Bram Moolenaar2e2e13c2010-12-08 13:17:03 +01002566 * Return pointer to cursor line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002567 */
2568 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002569ml_get_curline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002570{
2571 return ml_get_buf(curbuf, curwin->w_cursor.lnum, FALSE);
2572}
2573
2574/*
Bram Moolenaar2e2e13c2010-12-08 13:17:03 +01002575 * Return pointer to cursor position.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002576 */
2577 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002578ml_get_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002579{
2580 return (ml_get_buf(curbuf, curwin->w_cursor.lnum, FALSE) +
2581 curwin->w_cursor.col);
2582}
2583
2584/*
Bram Moolenaar2e2e13c2010-12-08 13:17:03 +01002585 * Return a pointer to a line in a specific buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00002586 *
2587 * "will_change": if TRUE mark the buffer dirty (chars in the line will be
2588 * changed)
2589 */
2590 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002591ml_get_buf(
2592 buf_T *buf,
2593 linenr_T lnum,
2594 int will_change) /* line will be changed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002595{
Bram Moolenaarad40f022007-02-13 03:01:39 +00002596 bhdr_T *hp;
2597 DATA_BL *dp;
Bram Moolenaarad40f022007-02-13 03:01:39 +00002598 static int recursive = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002599
2600 if (lnum > buf->b_ml.ml_line_count) /* invalid line number */
2601 {
Bram Moolenaarad40f022007-02-13 03:01:39 +00002602 if (recursive == 0)
2603 {
2604 /* Avoid giving this message for a recursive call, may happen when
2605 * the GUI redraws part of the text. */
2606 ++recursive;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002607 siemsg(_("E315: ml_get: invalid lnum: %ld"), lnum);
Bram Moolenaarad40f022007-02-13 03:01:39 +00002608 --recursive;
2609 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002610errorret:
2611 STRCPY(IObuff, "???");
Bram Moolenaaradfde112019-05-25 22:11:45 +02002612 buf->b_ml.ml_line_len = 4;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002613 return IObuff;
2614 }
Bram Moolenaaradfde112019-05-25 22:11:45 +02002615 if (lnum <= 0) // pretend line 0 is line 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002616 lnum = 1;
2617
Bram Moolenaaradfde112019-05-25 22:11:45 +02002618 if (buf->b_ml.ml_mfp == NULL) // there are no lines
2619 {
2620 buf->b_ml.ml_line_len = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002621 return (char_u *)"";
Bram Moolenaaradfde112019-05-25 22:11:45 +02002622 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002623
Bram Moolenaar37d619f2010-03-10 14:46:26 +01002624 /*
2625 * See if it is the same line as requested last time.
2626 * Otherwise may need to flush last used line.
2627 * Don't use the last used line when 'swapfile' is reset, need to load all
2628 * blocks.
2629 */
Bram Moolenaar47b8b152007-02-07 02:41:57 +00002630 if (buf->b_ml.ml_line_lnum != lnum || mf_dont_release)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002631 {
Bram Moolenaar98aefe72018-12-13 22:20:09 +01002632 unsigned start, end;
2633 colnr_T len;
2634 int idx;
2635
Bram Moolenaar071d4272004-06-13 20:20:40 +00002636 ml_flush_line(buf);
2637
2638 /*
2639 * Find the data block containing the line.
2640 * This also fills the stack with the blocks from the root to the data
2641 * block and releases any locked block.
2642 */
2643 if ((hp = ml_find_line(buf, lnum, ML_FIND)) == NULL)
2644 {
Bram Moolenaarad40f022007-02-13 03:01:39 +00002645 if (recursive == 0)
2646 {
2647 /* Avoid giving this message for a recursive call, may happen
2648 * when the GUI redraws part of the text. */
2649 ++recursive;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002650 siemsg(_("E316: ml_get: cannot find line %ld"), lnum);
Bram Moolenaarad40f022007-02-13 03:01:39 +00002651 --recursive;
2652 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002653 goto errorret;
2654 }
2655
2656 dp = (DATA_BL *)(hp->bh_data);
2657
Bram Moolenaar98aefe72018-12-13 22:20:09 +01002658 idx = lnum - buf->b_ml.ml_locked_low;
2659 start = ((dp->db_index[idx]) & DB_INDEX_MASK);
2660 // The text ends where the previous line starts. The first line ends
2661 // at the end of the block.
2662 if (idx == 0)
2663 end = dp->db_txt_end;
2664 else
2665 end = ((dp->db_index[idx - 1]) & DB_INDEX_MASK);
2666 len = end - start;
2667
2668 buf->b_ml.ml_line_ptr = (char_u *)dp + start;
2669 buf->b_ml.ml_line_len = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002670 buf->b_ml.ml_line_lnum = lnum;
2671 buf->b_ml.ml_flags &= ~ML_LINE_DIRTY;
2672 }
2673 if (will_change)
2674 buf->b_ml.ml_flags |= (ML_LOCKED_DIRTY | ML_LOCKED_POS);
2675
2676 return buf->b_ml.ml_line_ptr;
2677}
2678
2679/*
2680 * Check if a line that was just obtained by a call to ml_get
2681 * is in allocated memory.
2682 */
2683 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002684ml_line_alloced(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002685{
2686 return (curbuf->b_ml.ml_flags & ML_LINE_DIRTY);
2687}
2688
Bram Moolenaarb56ac042018-12-28 23:22:40 +01002689#ifdef FEAT_TEXT_PROP
2690 static void
2691add_text_props_for_append(
2692 buf_T *buf,
2693 linenr_T lnum,
2694 char_u **line,
2695 int *len,
2696 char_u **tofree)
2697{
2698 int round;
2699 int new_prop_count = 0;
2700 int count;
2701 int n;
2702 char_u *props;
2703 int new_len;
2704 char_u *new_line;
2705 textprop_T prop;
2706
2707 // Make two rounds:
2708 // 1. calculate the extra space needed
2709 // 2. allocate the space and fill it
2710 for (round = 1; round <= 2; ++round)
2711 {
2712 if (round == 2)
2713 {
2714 if (new_prop_count == 0)
2715 return; // nothing to do
2716 new_len = *len + new_prop_count * sizeof(textprop_T);
Bram Moolenaar964b3742019-05-24 18:54:09 +02002717 new_line = alloc(new_len);
Bram Moolenaarb56ac042018-12-28 23:22:40 +01002718 if (new_line == NULL)
2719 return;
2720 mch_memmove(new_line, *line, *len);
2721 new_prop_count = 0;
2722 }
2723
2724 // Get the line above to find any props that continue in the next
2725 // line.
2726 count = get_text_props(buf, lnum, &props, FALSE);
2727 for (n = 0; n < count; ++n)
2728 {
2729 mch_memmove(&prop, props + n * sizeof(textprop_T), sizeof(textprop_T));
2730 if (prop.tp_flags & TP_FLAG_CONT_NEXT)
2731 {
2732 if (round == 2)
2733 {
2734 prop.tp_flags |= TP_FLAG_CONT_PREV;
2735 prop.tp_col = 1;
2736 prop.tp_len = *len;
2737 mch_memmove(new_line + *len + new_prop_count * sizeof(textprop_T), &prop, sizeof(textprop_T));
2738 }
2739 ++new_prop_count;
2740 }
2741 }
2742 }
2743 *line = new_line;
2744 *tofree = new_line;
2745 *len = new_len;
2746}
2747#endif
2748
Bram Moolenaar071d4272004-06-13 20:20:40 +00002749/*
2750 * Append a line after lnum (may be 0 to insert a line in front of the file).
2751 * "line" does not need to be allocated, but can't be another line in a
2752 * buffer, unlocking may make it invalid.
2753 *
2754 * newfile: TRUE when starting to edit a new file, meaning that pe_old_lnum
2755 * will be set for recovery
2756 * Check: The caller of this function should probably also call
2757 * appended_lines().
2758 *
2759 * return FAIL for failure, OK otherwise
2760 */
2761 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002762ml_append(
2763 linenr_T lnum, /* append after this line (can be 0) */
2764 char_u *line, /* text of the new line */
2765 colnr_T len, /* length of new line, including NUL, or 0 */
2766 int newfile) /* flag, see above */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002767{
2768 /* When starting up, we might still need to create the memfile */
Bram Moolenaar59f931e2010-07-24 20:27:03 +02002769 if (curbuf->b_ml.ml_mfp == NULL && open_buffer(FALSE, NULL, 0) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002770 return FAIL;
2771
2772 if (curbuf->b_ml.ml_line_lnum != 0)
2773 ml_flush_line(curbuf);
2774 return ml_append_int(curbuf, lnum, line, len, newfile, FALSE);
2775}
2776
Bram Moolenaar4033c552017-09-16 20:54:51 +02002777#if defined(FEAT_SPELL) || defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar4770d092006-01-12 23:22:24 +00002778/*
2779 * Like ml_append() but for an arbitrary buffer. The buffer must already have
2780 * a memline.
2781 */
2782 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002783ml_append_buf(
2784 buf_T *buf,
2785 linenr_T lnum, /* append after this line (can be 0) */
2786 char_u *line, /* text of the new line */
2787 colnr_T len, /* length of new line, including NUL, or 0 */
2788 int newfile) /* flag, see above */
Bram Moolenaar4770d092006-01-12 23:22:24 +00002789{
2790 if (buf->b_ml.ml_mfp == NULL)
2791 return FAIL;
2792
2793 if (buf->b_ml.ml_line_lnum != 0)
2794 ml_flush_line(buf);
2795 return ml_append_int(buf, lnum, line, len, newfile, FALSE);
2796}
2797#endif
2798
Bram Moolenaar071d4272004-06-13 20:20:40 +00002799 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002800ml_append_int(
2801 buf_T *buf,
Bram Moolenaar98aefe72018-12-13 22:20:09 +01002802 linenr_T lnum, // append after this line (can be 0)
Bram Moolenaarb56ac042018-12-28 23:22:40 +01002803 char_u *line_arg, // text of the new line
Bram Moolenaar98aefe72018-12-13 22:20:09 +01002804 colnr_T len_arg, // length of line, including NUL, or 0
2805 int newfile, // flag, see above
2806 int mark) // mark the new line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002807{
Bram Moolenaarb56ac042018-12-28 23:22:40 +01002808 char_u *line = line_arg;
2809 colnr_T len = len_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002810 int i;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01002811 int line_count; // number of indexes in current block
Bram Moolenaar071d4272004-06-13 20:20:40 +00002812 int offset;
2813 int from, to;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01002814 int space_needed; // space needed for new line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002815 int page_size;
2816 int page_count;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01002817 int db_idx; // index for lnum in data block
Bram Moolenaar071d4272004-06-13 20:20:40 +00002818 bhdr_T *hp;
2819 memfile_T *mfp;
2820 DATA_BL *dp;
2821 PTR_BL *pp;
2822 infoptr_T *ip;
Bram Moolenaarb56ac042018-12-28 23:22:40 +01002823#ifdef FEAT_TEXT_PROP
2824 char_u *tofree = NULL;
2825#endif
2826 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002827
Bram Moolenaar071d4272004-06-13 20:20:40 +00002828 if (lnum > buf->b_ml.ml_line_count || buf->b_ml.ml_mfp == NULL)
Bram Moolenaarb56ac042018-12-28 23:22:40 +01002829 return FAIL; // lnum out of range
Bram Moolenaar071d4272004-06-13 20:20:40 +00002830
2831 if (lowest_marked && lowest_marked > lnum)
2832 lowest_marked = lnum + 1;
2833
2834 if (len == 0)
Bram Moolenaar98aefe72018-12-13 22:20:09 +01002835 len = (colnr_T)STRLEN(line) + 1; // space needed for the text
Bram Moolenaarb56ac042018-12-28 23:22:40 +01002836
Bram Moolenaardda41442019-05-16 22:11:47 +02002837#ifdef FEAT_EVAL
2838 // When inserting above recorded changes: flush the changes before changing
2839 // the text.
2840 may_invoke_listeners(buf, lnum + 1, lnum + 1, 1);
2841#endif
2842
Bram Moolenaarb56ac042018-12-28 23:22:40 +01002843#ifdef FEAT_TEXT_PROP
2844 if (curbuf->b_has_textprop && lnum > 0)
2845 // Add text properties that continue from the previous line.
2846 add_text_props_for_append(buf, lnum, &line, &len, &tofree);
2847#endif
2848
Bram Moolenaar98aefe72018-12-13 22:20:09 +01002849 space_needed = len + INDEX_SIZE; // space needed for text + index
Bram Moolenaar071d4272004-06-13 20:20:40 +00002850
2851 mfp = buf->b_ml.ml_mfp;
2852 page_size = mfp->mf_page_size;
2853
2854/*
2855 * find the data block containing the previous line
2856 * This also fills the stack with the blocks from the root to the data block
2857 * This also releases any locked block.
2858 */
2859 if ((hp = ml_find_line(buf, lnum == 0 ? (linenr_T)1 : lnum,
2860 ML_INSERT)) == NULL)
Bram Moolenaarb56ac042018-12-28 23:22:40 +01002861 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002862
2863 buf->b_ml.ml_flags &= ~ML_EMPTY;
2864
2865 if (lnum == 0) /* got line one instead, correct db_idx */
2866 db_idx = -1; /* careful, it is negative! */
2867 else
2868 db_idx = lnum - buf->b_ml.ml_locked_low;
2869 /* get line count before the insertion */
2870 line_count = buf->b_ml.ml_locked_high - buf->b_ml.ml_locked_low;
2871
2872 dp = (DATA_BL *)(hp->bh_data);
2873
2874/*
2875 * If
2876 * - there is not enough room in the current block
2877 * - appending to the last line in the block
2878 * - not appending to the last line in the file
2879 * insert in front of the next block.
2880 */
2881 if ((int)dp->db_free < space_needed && db_idx == line_count - 1
2882 && lnum < buf->b_ml.ml_line_count)
2883 {
2884 /*
2885 * Now that the line is not going to be inserted in the block that we
2886 * expected, the line count has to be adjusted in the pointer blocks
2887 * by using ml_locked_lineadd.
2888 */
2889 --(buf->b_ml.ml_locked_lineadd);
2890 --(buf->b_ml.ml_locked_high);
2891 if ((hp = ml_find_line(buf, lnum + 1, ML_INSERT)) == NULL)
Bram Moolenaarb56ac042018-12-28 23:22:40 +01002892 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002893
2894 db_idx = -1; /* careful, it is negative! */
2895 /* get line count before the insertion */
2896 line_count = buf->b_ml.ml_locked_high - buf->b_ml.ml_locked_low;
2897 CHECK(buf->b_ml.ml_locked_low != lnum + 1, "locked_low != lnum + 1");
2898
2899 dp = (DATA_BL *)(hp->bh_data);
2900 }
2901
2902 ++buf->b_ml.ml_line_count;
2903
2904 if ((int)dp->db_free >= space_needed) /* enough room in data block */
2905 {
Bram Moolenaarb56ac042018-12-28 23:22:40 +01002906 /*
2907 * Insert the new line in an existing data block, or in the data block
2908 * allocated above.
2909 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002910 dp->db_txt_start -= len;
2911 dp->db_free -= space_needed;
2912 ++(dp->db_line_count);
2913
2914 /*
2915 * move the text of the lines that follow to the front
2916 * adjust the indexes of the lines that follow
2917 */
2918 if (line_count > db_idx + 1) /* if there are following lines */
2919 {
2920 /*
2921 * Offset is the start of the previous line.
2922 * This will become the character just after the new line.
2923 */
2924 if (db_idx < 0)
2925 offset = dp->db_txt_end;
2926 else
2927 offset = ((dp->db_index[db_idx]) & DB_INDEX_MASK);
2928 mch_memmove((char *)dp + dp->db_txt_start,
2929 (char *)dp + dp->db_txt_start + len,
2930 (size_t)(offset - (dp->db_txt_start + len)));
2931 for (i = line_count - 1; i > db_idx; --i)
2932 dp->db_index[i + 1] = dp->db_index[i] - len;
2933 dp->db_index[db_idx + 1] = offset - len;
2934 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01002935 else
2936 // add line at the end (which is the start of the text)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002937 dp->db_index[db_idx + 1] = dp->db_txt_start;
2938
2939 /*
2940 * copy the text into the block
2941 */
2942 mch_memmove((char *)dp + dp->db_index[db_idx + 1], line, (size_t)len);
2943 if (mark)
2944 dp->db_index[db_idx + 1] |= DB_MARKED;
2945
2946 /*
2947 * Mark the block dirty.
2948 */
2949 buf->b_ml.ml_flags |= ML_LOCKED_DIRTY;
2950 if (!newfile)
2951 buf->b_ml.ml_flags |= ML_LOCKED_POS;
2952 }
2953 else /* not enough space in data block */
2954 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002955 long line_count_left, line_count_right;
2956 int page_count_left, page_count_right;
2957 bhdr_T *hp_left;
2958 bhdr_T *hp_right;
2959 bhdr_T *hp_new;
2960 int lines_moved;
2961 int data_moved = 0; /* init to shut up gcc */
2962 int total_moved = 0; /* init to shut up gcc */
2963 DATA_BL *dp_right, *dp_left;
2964 int stack_idx;
2965 int in_left;
2966 int lineadd;
2967 blocknr_T bnum_left, bnum_right;
2968 linenr_T lnum_left, lnum_right;
2969 int pb_idx;
2970 PTR_BL *pp_new;
2971
2972 /*
Bram Moolenaarb56ac042018-12-28 23:22:40 +01002973 * There is not enough room, we have to create a new data block and
2974 * copy some lines into it.
2975 * Then we have to insert an entry in the pointer block.
2976 * If this pointer block also is full, we go up another block, and so
2977 * on, up to the root if necessary.
2978 * The line counts in the pointer blocks have already been adjusted by
2979 * ml_find_line().
2980 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00002981 * We are going to allocate a new data block. Depending on the
2982 * situation it will be put to the left or right of the existing
2983 * block. If possible we put the new line in the left block and move
2984 * the lines after it to the right block. Otherwise the new line is
2985 * also put in the right block. This method is more efficient when
2986 * inserting a lot of lines at one place.
2987 */
2988 if (db_idx < 0) /* left block is new, right block is existing */
2989 {
2990 lines_moved = 0;
2991 in_left = TRUE;
2992 /* space_needed does not change */
2993 }
2994 else /* left block is existing, right block is new */
2995 {
2996 lines_moved = line_count - db_idx - 1;
2997 if (lines_moved == 0)
2998 in_left = FALSE; /* put new line in right block */
2999 /* space_needed does not change */
3000 else
3001 {
3002 data_moved = ((dp->db_index[db_idx]) & DB_INDEX_MASK) -
3003 dp->db_txt_start;
3004 total_moved = data_moved + lines_moved * INDEX_SIZE;
3005 if ((int)dp->db_free + total_moved >= space_needed)
3006 {
3007 in_left = TRUE; /* put new line in left block */
3008 space_needed = total_moved;
3009 }
3010 else
3011 {
3012 in_left = FALSE; /* put new line in right block */
3013 space_needed += total_moved;
3014 }
3015 }
3016 }
3017
3018 page_count = ((space_needed + HEADER_SIZE) + page_size - 1) / page_size;
3019 if ((hp_new = ml_new_data(mfp, newfile, page_count)) == NULL)
3020 {
3021 /* correct line counts in pointer blocks */
3022 --(buf->b_ml.ml_locked_lineadd);
3023 --(buf->b_ml.ml_locked_high);
Bram Moolenaarb56ac042018-12-28 23:22:40 +01003024 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003025 }
3026 if (db_idx < 0) /* left block is new */
3027 {
3028 hp_left = hp_new;
3029 hp_right = hp;
3030 line_count_left = 0;
3031 line_count_right = line_count;
3032 }
3033 else /* right block is new */
3034 {
3035 hp_left = hp;
3036 hp_right = hp_new;
3037 line_count_left = line_count;
3038 line_count_right = 0;
3039 }
3040 dp_right = (DATA_BL *)(hp_right->bh_data);
3041 dp_left = (DATA_BL *)(hp_left->bh_data);
3042 bnum_left = hp_left->bh_bnum;
3043 bnum_right = hp_right->bh_bnum;
3044 page_count_left = hp_left->bh_page_count;
3045 page_count_right = hp_right->bh_page_count;
3046
3047 /*
3048 * May move the new line into the right/new block.
3049 */
3050 if (!in_left)
3051 {
3052 dp_right->db_txt_start -= len;
3053 dp_right->db_free -= len + INDEX_SIZE;
3054 dp_right->db_index[0] = dp_right->db_txt_start;
3055 if (mark)
3056 dp_right->db_index[0] |= DB_MARKED;
3057
3058 mch_memmove((char *)dp_right + dp_right->db_txt_start,
3059 line, (size_t)len);
3060 ++line_count_right;
3061 }
3062 /*
3063 * may move lines from the left/old block to the right/new one.
3064 */
3065 if (lines_moved)
3066 {
3067 /*
3068 */
3069 dp_right->db_txt_start -= data_moved;
3070 dp_right->db_free -= total_moved;
3071 mch_memmove((char *)dp_right + dp_right->db_txt_start,
3072 (char *)dp_left + dp_left->db_txt_start,
3073 (size_t)data_moved);
3074 offset = dp_right->db_txt_start - dp_left->db_txt_start;
3075 dp_left->db_txt_start += data_moved;
3076 dp_left->db_free += total_moved;
3077
3078 /*
3079 * update indexes in the new block
3080 */
3081 for (to = line_count_right, from = db_idx + 1;
3082 from < line_count_left; ++from, ++to)
3083 dp_right->db_index[to] = dp->db_index[from] + offset;
3084 line_count_right += lines_moved;
3085 line_count_left -= lines_moved;
3086 }
3087
3088 /*
3089 * May move the new line into the left (old or new) block.
3090 */
3091 if (in_left)
3092 {
3093 dp_left->db_txt_start -= len;
3094 dp_left->db_free -= len + INDEX_SIZE;
3095 dp_left->db_index[line_count_left] = dp_left->db_txt_start;
3096 if (mark)
3097 dp_left->db_index[line_count_left] |= DB_MARKED;
3098 mch_memmove((char *)dp_left + dp_left->db_txt_start,
3099 line, (size_t)len);
3100 ++line_count_left;
3101 }
3102
3103 if (db_idx < 0) /* left block is new */
3104 {
3105 lnum_left = lnum + 1;
3106 lnum_right = 0;
3107 }
3108 else /* right block is new */
3109 {
3110 lnum_left = 0;
3111 if (in_left)
3112 lnum_right = lnum + 2;
3113 else
3114 lnum_right = lnum + 1;
3115 }
3116 dp_left->db_line_count = line_count_left;
3117 dp_right->db_line_count = line_count_right;
3118
3119 /*
3120 * release the two data blocks
3121 * The new one (hp_new) already has a correct blocknumber.
3122 * The old one (hp, in ml_locked) gets a positive blocknumber if
3123 * we changed it and we are not editing a new file.
3124 */
3125 if (lines_moved || in_left)
3126 buf->b_ml.ml_flags |= ML_LOCKED_DIRTY;
3127 if (!newfile && db_idx >= 0 && in_left)
3128 buf->b_ml.ml_flags |= ML_LOCKED_POS;
3129 mf_put(mfp, hp_new, TRUE, FALSE);
3130
3131 /*
3132 * flush the old data block
3133 * set ml_locked_lineadd to 0, because the updating of the
3134 * pointer blocks is done below
3135 */
3136 lineadd = buf->b_ml.ml_locked_lineadd;
3137 buf->b_ml.ml_locked_lineadd = 0;
3138 ml_find_line(buf, (linenr_T)0, ML_FLUSH); /* flush data block */
3139
3140 /*
3141 * update pointer blocks for the new data block
3142 */
3143 for (stack_idx = buf->b_ml.ml_stack_top - 1; stack_idx >= 0;
3144 --stack_idx)
3145 {
3146 ip = &(buf->b_ml.ml_stack[stack_idx]);
3147 pb_idx = ip->ip_index;
3148 if ((hp = mf_get(mfp, ip->ip_bnum, 1)) == NULL)
Bram Moolenaarb56ac042018-12-28 23:22:40 +01003149 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003150 pp = (PTR_BL *)(hp->bh_data); /* must be pointer block */
3151 if (pp->pb_id != PTR_ID)
3152 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003153 iemsg(_("E317: pointer block id wrong 3"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003154 mf_put(mfp, hp, FALSE, FALSE);
Bram Moolenaarb56ac042018-12-28 23:22:40 +01003155 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003156 }
3157 /*
3158 * TODO: If the pointer block is full and we are adding at the end
3159 * try to insert in front of the next block
3160 */
3161 /* block not full, add one entry */
3162 if (pp->pb_count < pp->pb_count_max)
3163 {
3164 if (pb_idx + 1 < (int)pp->pb_count)
3165 mch_memmove(&pp->pb_pointer[pb_idx + 2],
3166 &pp->pb_pointer[pb_idx + 1],
3167 (size_t)(pp->pb_count - pb_idx - 1) * sizeof(PTR_EN));
3168 ++pp->pb_count;
3169 pp->pb_pointer[pb_idx].pe_line_count = line_count_left;
3170 pp->pb_pointer[pb_idx].pe_bnum = bnum_left;
3171 pp->pb_pointer[pb_idx].pe_page_count = page_count_left;
3172 pp->pb_pointer[pb_idx + 1].pe_line_count = line_count_right;
3173 pp->pb_pointer[pb_idx + 1].pe_bnum = bnum_right;
3174 pp->pb_pointer[pb_idx + 1].pe_page_count = page_count_right;
3175
3176 if (lnum_left != 0)
3177 pp->pb_pointer[pb_idx].pe_old_lnum = lnum_left;
3178 if (lnum_right != 0)
3179 pp->pb_pointer[pb_idx + 1].pe_old_lnum = lnum_right;
3180
3181 mf_put(mfp, hp, TRUE, FALSE);
3182 buf->b_ml.ml_stack_top = stack_idx + 1; /* truncate stack */
3183
3184 if (lineadd)
3185 {
3186 --(buf->b_ml.ml_stack_top);
Bram Moolenaar6b803a72007-05-06 14:25:46 +00003187 /* fix line count for rest of blocks in the stack */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003188 ml_lineadd(buf, lineadd);
3189 /* fix stack itself */
3190 buf->b_ml.ml_stack[buf->b_ml.ml_stack_top].ip_high +=
3191 lineadd;
3192 ++(buf->b_ml.ml_stack_top);
3193 }
3194
3195 /*
3196 * We are finished, break the loop here.
3197 */
3198 break;
3199 }
3200 else /* pointer block full */
3201 {
3202 /*
3203 * split the pointer block
3204 * allocate a new pointer block
3205 * move some of the pointer into the new block
3206 * prepare for updating the parent block
3207 */
3208 for (;;) /* do this twice when splitting block 1 */
3209 {
3210 hp_new = ml_new_ptr(mfp);
3211 if (hp_new == NULL) /* TODO: try to fix tree */
Bram Moolenaarb56ac042018-12-28 23:22:40 +01003212 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003213 pp_new = (PTR_BL *)(hp_new->bh_data);
3214
3215 if (hp->bh_bnum != 1)
3216 break;
3217
3218 /*
3219 * if block 1 becomes full the tree is given an extra level
3220 * The pointers from block 1 are moved into the new block.
3221 * block 1 is updated to point to the new block
3222 * then continue to split the new block
3223 */
3224 mch_memmove(pp_new, pp, (size_t)page_size);
3225 pp->pb_count = 1;
3226 pp->pb_pointer[0].pe_bnum = hp_new->bh_bnum;
3227 pp->pb_pointer[0].pe_line_count = buf->b_ml.ml_line_count;
3228 pp->pb_pointer[0].pe_old_lnum = 1;
3229 pp->pb_pointer[0].pe_page_count = 1;
3230 mf_put(mfp, hp, TRUE, FALSE); /* release block 1 */
3231 hp = hp_new; /* new block is to be split */
3232 pp = pp_new;
3233 CHECK(stack_idx != 0, _("stack_idx should be 0"));
3234 ip->ip_index = 0;
3235 ++stack_idx; /* do block 1 again later */
3236 }
3237 /*
3238 * move the pointers after the current one to the new block
3239 * If there are none, the new entry will be in the new block.
3240 */
3241 total_moved = pp->pb_count - pb_idx - 1;
3242 if (total_moved)
3243 {
3244 mch_memmove(&pp_new->pb_pointer[0],
3245 &pp->pb_pointer[pb_idx + 1],
3246 (size_t)(total_moved) * sizeof(PTR_EN));
3247 pp_new->pb_count = total_moved;
3248 pp->pb_count -= total_moved - 1;
3249 pp->pb_pointer[pb_idx + 1].pe_bnum = bnum_right;
3250 pp->pb_pointer[pb_idx + 1].pe_line_count = line_count_right;
3251 pp->pb_pointer[pb_idx + 1].pe_page_count = page_count_right;
3252 if (lnum_right)
3253 pp->pb_pointer[pb_idx + 1].pe_old_lnum = lnum_right;
3254 }
3255 else
3256 {
3257 pp_new->pb_count = 1;
3258 pp_new->pb_pointer[0].pe_bnum = bnum_right;
3259 pp_new->pb_pointer[0].pe_line_count = line_count_right;
3260 pp_new->pb_pointer[0].pe_page_count = page_count_right;
3261 pp_new->pb_pointer[0].pe_old_lnum = lnum_right;
3262 }
3263 pp->pb_pointer[pb_idx].pe_bnum = bnum_left;
3264 pp->pb_pointer[pb_idx].pe_line_count = line_count_left;
3265 pp->pb_pointer[pb_idx].pe_page_count = page_count_left;
3266 if (lnum_left)
3267 pp->pb_pointer[pb_idx].pe_old_lnum = lnum_left;
3268 lnum_left = 0;
3269 lnum_right = 0;
3270
3271 /*
3272 * recompute line counts
3273 */
3274 line_count_right = 0;
3275 for (i = 0; i < (int)pp_new->pb_count; ++i)
3276 line_count_right += pp_new->pb_pointer[i].pe_line_count;
3277 line_count_left = 0;
3278 for (i = 0; i < (int)pp->pb_count; ++i)
3279 line_count_left += pp->pb_pointer[i].pe_line_count;
3280
3281 bnum_left = hp->bh_bnum;
3282 bnum_right = hp_new->bh_bnum;
3283 page_count_left = 1;
3284 page_count_right = 1;
3285 mf_put(mfp, hp, TRUE, FALSE);
3286 mf_put(mfp, hp_new, TRUE, FALSE);
3287 }
3288 }
3289
3290 /*
3291 * Safety check: fallen out of for loop?
3292 */
3293 if (stack_idx < 0)
3294 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003295 iemsg(_("E318: Updated too many blocks?"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003296 buf->b_ml.ml_stack_top = 0; /* invalidate stack */
3297 }
3298 }
3299
3300#ifdef FEAT_BYTEOFF
3301 /* The line was inserted below 'lnum' */
3302 ml_updatechunk(buf, lnum + 1, (long)len, ML_CHNK_ADDLINE);
3303#endif
3304#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003305 if (netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003306 {
3307 if (STRLEN(line) > 0)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003308 netbeans_inserted(buf, lnum+1, (colnr_T)0, line, (int)STRLEN(line));
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +00003309 netbeans_inserted(buf, lnum+1, (colnr_T)STRLEN(line),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003310 (char_u *)"\n", 1);
3311 }
3312#endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01003313#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar99ef0622016-03-06 20:22:25 +01003314 if (buf->b_write_to_channel)
3315 channel_write_new_lines(buf);
3316#endif
Bram Moolenaarb56ac042018-12-28 23:22:40 +01003317 ret = OK;
Bram Moolenaar99ef0622016-03-06 20:22:25 +01003318
Bram Moolenaarb56ac042018-12-28 23:22:40 +01003319theend:
3320#ifdef FEAT_TEXT_PROP
3321 vim_free(tofree);
3322#endif
3323 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003324}
3325
3326/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00003327 * Replace line lnum, with buffering, in current buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003328 *
Bram Moolenaar1056d982006-03-09 22:37:52 +00003329 * If "copy" is TRUE, make a copy of the line, otherwise the line has been
Bram Moolenaar071d4272004-06-13 20:20:40 +00003330 * copied to allocated memory already.
3331 *
3332 * Check: The caller of this function should probably also call
3333 * changed_lines(), unless update_screen(NOT_VALID) is used.
3334 *
3335 * return FAIL for failure, OK otherwise
3336 */
3337 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003338ml_replace(linenr_T lnum, char_u *line, int copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003339{
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003340 colnr_T len = -1;
3341
3342 if (line != NULL)
Bram Moolenaar4efe73b2018-12-16 14:37:39 +01003343 len = (colnr_T)STRLEN(line);
Bram Moolenaarccae4672019-01-04 15:09:57 +01003344 return ml_replace_len(lnum, line, len, FALSE, copy);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003345}
3346
Bram Moolenaarccae4672019-01-04 15:09:57 +01003347/*
3348 * Replace a line for the current buffer. Like ml_replace() with:
3349 * "len_arg" is the length of the text, excluding NUL.
3350 * If "has_props" is TRUE then "line_arg" includes the text properties and
3351 * "len_arg" includes the NUL of the text.
3352 */
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003353 int
Bram Moolenaarccae4672019-01-04 15:09:57 +01003354ml_replace_len(
3355 linenr_T lnum,
3356 char_u *line_arg,
3357 colnr_T len_arg,
3358 int has_props,
3359 int copy)
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003360{
3361 char_u *line = line_arg;
3362 colnr_T len = len_arg;
3363
Bram Moolenaar071d4272004-06-13 20:20:40 +00003364 if (line == NULL) /* just checking... */
3365 return FAIL;
3366
3367 /* When starting up, we might still need to create the memfile */
Bram Moolenaar59f931e2010-07-24 20:27:03 +02003368 if (curbuf->b_ml.ml_mfp == NULL && open_buffer(FALSE, NULL, 0) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003369 return FAIL;
3370
Bram Moolenaarccae4672019-01-04 15:09:57 +01003371 if (!has_props)
3372 ++len; // include the NUL after the text
3373 if (copy)
3374 {
3375 // copy the line to allocated memory
3376#ifdef FEAT_TEXT_PROP
3377 if (has_props)
3378 line = vim_memsave(line, len);
3379 else
3380#endif
3381 line = vim_strnsave(line, len - 1);
3382 if (line == NULL)
3383 return FAIL;
3384 }
3385
Bram Moolenaar071d4272004-06-13 20:20:40 +00003386#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003387 if (netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003388 {
3389 netbeans_removed(curbuf, lnum, 0, (long)STRLEN(ml_get(lnum)));
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003390 netbeans_inserted(curbuf, lnum, 0, line, (int)STRLEN(line));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003391 }
3392#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003393 if (curbuf->b_ml.ml_line_lnum != lnum)
3394 {
3395 // another line is buffered, flush it
3396 ml_flush_line(curbuf);
Bram Moolenaarca79a5f2018-12-13 23:16:36 +01003397 curbuf->b_ml.ml_flags &= ~ML_LINE_DIRTY;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003398
3399#ifdef FEAT_TEXT_PROP
Bram Moolenaarccae4672019-01-04 15:09:57 +01003400 if (curbuf->b_has_textprop && !has_props)
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003401 // Need to fetch the old line to copy over any text properties.
3402 ml_get_buf(curbuf, lnum, TRUE);
3403#endif
3404 }
3405
3406#ifdef FEAT_TEXT_PROP
Bram Moolenaarccae4672019-01-04 15:09:57 +01003407 if (curbuf->b_has_textprop && !has_props)
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003408 {
3409 size_t oldtextlen = STRLEN(curbuf->b_ml.ml_line_ptr) + 1;
3410
3411 if (oldtextlen < (size_t)curbuf->b_ml.ml_line_len)
3412 {
3413 char_u *newline;
3414 size_t textproplen = curbuf->b_ml.ml_line_len - oldtextlen;
3415
3416 // Need to copy over text properties, stored after the text.
Bram Moolenaarccae4672019-01-04 15:09:57 +01003417 newline = alloc(len + (int)textproplen);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003418 if (newline != NULL)
3419 {
Bram Moolenaarccae4672019-01-04 15:09:57 +01003420 mch_memmove(newline, line, len);
Bram Moolenaar0d3cb732019-05-18 13:05:18 +02003421 mch_memmove(newline + len, curbuf->b_ml.ml_line_ptr
3422 + oldtextlen, textproplen);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003423 vim_free(line);
3424 line = newline;
Bram Moolenaar4efe73b2018-12-16 14:37:39 +01003425 len += (colnr_T)textproplen;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003426 }
3427 }
3428 }
3429#endif
3430
Bram Moolenaarccae4672019-01-04 15:09:57 +01003431 if (curbuf->b_ml.ml_flags & ML_LINE_DIRTY) // same line allocated
3432 vim_free(curbuf->b_ml.ml_line_ptr); // free it
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003433
Bram Moolenaar071d4272004-06-13 20:20:40 +00003434 curbuf->b_ml.ml_line_ptr = line;
Bram Moolenaarccae4672019-01-04 15:09:57 +01003435 curbuf->b_ml.ml_line_len = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003436 curbuf->b_ml.ml_line_lnum = lnum;
3437 curbuf->b_ml.ml_flags = (curbuf->b_ml.ml_flags | ML_LINE_DIRTY) & ~ML_EMPTY;
3438
3439 return OK;
3440}
3441
Bram Moolenaarc1a9bc12018-12-28 21:59:29 +01003442#ifdef FEAT_TEXT_PROP
3443/*
3444 * Adjust text properties in line "lnum" for a deleted line.
3445 * When "above" is true this is the line above the deleted line.
3446 * "del_props" are the properties of the deleted line.
3447 */
3448 static void
3449adjust_text_props_for_delete(
3450 buf_T *buf,
3451 linenr_T lnum,
3452 char_u *del_props,
3453 int del_props_len,
3454 int above)
3455{
3456 int did_get_line = FALSE;
3457 int done_del;
3458 int done_this;
3459 textprop_T prop_del;
3460 textprop_T prop_this;
3461 bhdr_T *hp;
3462 DATA_BL *dp;
3463 int idx;
3464 int line_start;
3465 long line_size;
3466 int this_props_len;
3467 char_u *text;
3468 size_t textlen;
3469 int found;
3470
3471 for (done_del = 0; done_del < del_props_len; done_del += sizeof(textprop_T))
3472 {
3473 mch_memmove(&prop_del, del_props + done_del, sizeof(textprop_T));
3474 if ((above && (prop_del.tp_flags & TP_FLAG_CONT_PREV)
3475 && !(prop_del.tp_flags & TP_FLAG_CONT_NEXT))
3476 || (!above && (prop_del.tp_flags & TP_FLAG_CONT_NEXT)
3477 && !(prop_del.tp_flags & TP_FLAG_CONT_PREV)))
3478 {
3479 if (!did_get_line)
3480 {
3481 did_get_line = TRUE;
3482 if ((hp = ml_find_line(buf, lnum, ML_FIND)) == NULL)
3483 return;
3484
3485 dp = (DATA_BL *)(hp->bh_data);
3486 idx = lnum - buf->b_ml.ml_locked_low;
3487 line_start = ((dp->db_index[idx]) & DB_INDEX_MASK);
3488 if (idx == 0) // first line in block, text at the end
3489 line_size = dp->db_txt_end - line_start;
3490 else
3491 line_size = ((dp->db_index[idx - 1]) & DB_INDEX_MASK) - line_start;
3492 text = (char_u *)dp + line_start;
3493 textlen = STRLEN(text) + 1;
3494 if ((long)textlen >= line_size)
3495 {
3496 if (above)
3497 internal_error("no text property above deleted line");
3498 else
3499 internal_error("no text property below deleted line");
3500 return;
3501 }
Bram Moolenaar4b7214e2019-01-03 21:55:32 +01003502 this_props_len = line_size - (int)textlen;
Bram Moolenaarc1a9bc12018-12-28 21:59:29 +01003503 }
3504
3505 found = FALSE;
3506 for (done_this = 0; done_this < this_props_len; done_this += sizeof(textprop_T))
3507 {
3508 mch_memmove(&prop_this, text + textlen + done_del, sizeof(textprop_T));
3509 if (prop_del.tp_id == prop_this.tp_id
3510 && prop_del.tp_type == prop_this.tp_type)
3511 {
3512 int flag = above ? TP_FLAG_CONT_NEXT : TP_FLAG_CONT_PREV;
3513
3514 found = TRUE;
3515 if (prop_this.tp_flags & flag)
3516 {
3517 prop_this.tp_flags &= ~flag;
3518 mch_memmove(text + textlen + done_del, &prop_this, sizeof(textprop_T));
3519 }
3520 else if (above)
3521 internal_error("text property above deleted line does not continue");
3522 else
3523 internal_error("text property below deleted line does not continue");
3524 }
3525 }
3526 if (!found)
3527 {
3528 if (above)
3529 internal_error("text property above deleted line not found");
3530 else
3531 internal_error("text property below deleted line not found");
3532 }
3533
3534 buf->b_ml.ml_flags |= (ML_LOCKED_DIRTY | ML_LOCKED_POS);
3535 }
3536 }
3537}
3538#endif
3539
Bram Moolenaar071d4272004-06-13 20:20:40 +00003540/*
Bram Moolenaar4033c552017-09-16 20:54:51 +02003541 * Delete line "lnum" in the current buffer.
3542 * When "message" is TRUE may give a "No lines in buffer" message.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003543 *
3544 * Check: The caller of this function should probably also call
3545 * deleted_lines() after this.
3546 *
3547 * return FAIL for failure, OK otherwise
3548 */
3549 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003550ml_delete(linenr_T lnum, int message)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003551{
3552 ml_flush_line(curbuf);
3553 return ml_delete_int(curbuf, lnum, message);
3554}
3555
3556 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003557ml_delete_int(buf_T *buf, linenr_T lnum, int message)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003558{
3559 bhdr_T *hp;
3560 memfile_T *mfp;
3561 DATA_BL *dp;
3562 PTR_BL *pp;
3563 infoptr_T *ip;
3564 int count; /* number of entries in block */
3565 int idx;
3566 int stack_idx;
3567 int text_start;
3568 int line_start;
3569 long line_size;
3570 int i;
Bram Moolenaarc1a9bc12018-12-28 21:59:29 +01003571 int ret = FAIL;
3572#ifdef FEAT_TEXT_PROP
3573 char_u *textprop_save = NULL;
3574 int textprop_save_len;
3575#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003576
3577 if (lnum < 1 || lnum > buf->b_ml.ml_line_count)
3578 return FAIL;
3579
Bram Moolenaardda41442019-05-16 22:11:47 +02003580#ifdef FEAT_EVAL
3581 // When inserting above recorded changes: flush the changes before changing
3582 // the text.
3583 may_invoke_listeners(buf, lnum, lnum + 1, -1);
3584#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003585 if (lowest_marked && lowest_marked > lnum)
3586 lowest_marked--;
3587
3588/*
3589 * If the file becomes empty the last line is replaced by an empty line.
3590 */
3591 if (buf->b_ml.ml_line_count == 1) /* file becomes empty */
3592 {
3593 if (message
3594#ifdef FEAT_NETBEANS_INTG
3595 && !netbeansSuppressNoLines
3596#endif
3597 )
Bram Moolenaar238a5642006-02-21 22:12:05 +00003598 set_keep_msg((char_u *)_(no_lines_msg), 0);
3599
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02003600 /* FEAT_BYTEOFF already handled in there, don't worry 'bout it below */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003601 i = ml_replace((linenr_T)1, (char_u *)"", TRUE);
3602 buf->b_ml.ml_flags |= ML_EMPTY;
3603
3604 return i;
3605 }
3606
3607/*
Bram Moolenaarc1a9bc12018-12-28 21:59:29 +01003608 * Find the data block containing the line.
3609 * This also fills the stack with the blocks from the root to the data block.
3610 * This also releases any locked block..
Bram Moolenaar071d4272004-06-13 20:20:40 +00003611 */
3612 mfp = buf->b_ml.ml_mfp;
3613 if (mfp == NULL)
3614 return FAIL;
3615
3616 if ((hp = ml_find_line(buf, lnum, ML_DELETE)) == NULL)
3617 return FAIL;
3618
3619 dp = (DATA_BL *)(hp->bh_data);
3620 /* compute line count before the delete */
3621 count = (long)(buf->b_ml.ml_locked_high)
3622 - (long)(buf->b_ml.ml_locked_low) + 2;
3623 idx = lnum - buf->b_ml.ml_locked_low;
3624
3625 --buf->b_ml.ml_line_count;
3626
3627 line_start = ((dp->db_index[idx]) & DB_INDEX_MASK);
3628 if (idx == 0) /* first line in block, text at the end */
3629 line_size = dp->db_txt_end - line_start;
3630 else
3631 line_size = ((dp->db_index[idx - 1]) & DB_INDEX_MASK) - line_start;
3632
3633#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003634 if (netbeans_active())
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +00003635 netbeans_removed(buf, lnum, 0, (long)line_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003636#endif
Bram Moolenaarc1a9bc12018-12-28 21:59:29 +01003637#ifdef FEAT_TEXT_PROP
3638 // If there are text properties, make a copy, so that we can update
3639 // properties in preceding and following lines.
3640 if (buf->b_has_textprop)
3641 {
3642 size_t textlen = STRLEN((char_u *)dp + line_start) + 1;
3643
3644 if ((long)textlen < line_size)
3645 {
Bram Moolenaar4b7214e2019-01-03 21:55:32 +01003646 textprop_save_len = line_size - (int)textlen;
Bram Moolenaarc1a9bc12018-12-28 21:59:29 +01003647 textprop_save = vim_memsave((char_u *)dp + line_start + textlen,
3648 textprop_save_len);
3649 }
3650 }
3651#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003652
3653/*
3654 * special case: If there is only one line in the data block it becomes empty.
3655 * Then we have to remove the entry, pointing to this data block, from the
3656 * pointer block. If this pointer block also becomes empty, we go up another
3657 * block, and so on, up to the root if necessary.
3658 * The line counts in the pointer blocks have already been adjusted by
3659 * ml_find_line().
3660 */
3661 if (count == 1)
3662 {
3663 mf_free(mfp, hp); /* free the data block */
3664 buf->b_ml.ml_locked = NULL;
3665
Bram Moolenaare60acc12011-05-10 16:41:25 +02003666 for (stack_idx = buf->b_ml.ml_stack_top - 1; stack_idx >= 0;
3667 --stack_idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003668 {
3669 buf->b_ml.ml_stack_top = 0; /* stack is invalid when failing */
3670 ip = &(buf->b_ml.ml_stack[stack_idx]);
3671 idx = ip->ip_index;
3672 if ((hp = mf_get(mfp, ip->ip_bnum, 1)) == NULL)
Bram Moolenaarc1a9bc12018-12-28 21:59:29 +01003673 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003674 pp = (PTR_BL *)(hp->bh_data); /* must be pointer block */
3675 if (pp->pb_id != PTR_ID)
3676 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003677 iemsg(_("E317: pointer block id wrong 4"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003678 mf_put(mfp, hp, FALSE, FALSE);
Bram Moolenaarc1a9bc12018-12-28 21:59:29 +01003679 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003680 }
3681 count = --(pp->pb_count);
3682 if (count == 0) /* the pointer block becomes empty! */
3683 mf_free(mfp, hp);
3684 else
3685 {
3686 if (count != idx) /* move entries after the deleted one */
3687 mch_memmove(&pp->pb_pointer[idx], &pp->pb_pointer[idx + 1],
3688 (size_t)(count - idx) * sizeof(PTR_EN));
3689 mf_put(mfp, hp, TRUE, FALSE);
3690
3691 buf->b_ml.ml_stack_top = stack_idx; /* truncate stack */
Bram Moolenaar6b803a72007-05-06 14:25:46 +00003692 /* fix line count for rest of blocks in the stack */
3693 if (buf->b_ml.ml_locked_lineadd != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003694 {
3695 ml_lineadd(buf, buf->b_ml.ml_locked_lineadd);
3696 buf->b_ml.ml_stack[buf->b_ml.ml_stack_top].ip_high +=
Bram Moolenaar6b803a72007-05-06 14:25:46 +00003697 buf->b_ml.ml_locked_lineadd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003698 }
3699 ++(buf->b_ml.ml_stack_top);
3700
3701 break;
3702 }
3703 }
3704 CHECK(stack_idx < 0, _("deleted block 1?"));
3705 }
3706 else
3707 {
3708 /*
3709 * delete the text by moving the next lines forwards
3710 */
3711 text_start = dp->db_txt_start;
3712 mch_memmove((char *)dp + text_start + line_size,
3713 (char *)dp + text_start, (size_t)(line_start - text_start));
3714
3715 /*
3716 * delete the index by moving the next indexes backwards
3717 * Adjust the indexes for the text movement.
3718 */
3719 for (i = idx; i < count - 1; ++i)
3720 dp->db_index[i] = dp->db_index[i + 1] + line_size;
3721
3722 dp->db_free += line_size + INDEX_SIZE;
3723 dp->db_txt_start += line_size;
3724 --(dp->db_line_count);
3725
3726 /*
3727 * mark the block dirty and make sure it is in the file (for recovery)
3728 */
3729 buf->b_ml.ml_flags |= (ML_LOCKED_DIRTY | ML_LOCKED_POS);
3730 }
3731
3732#ifdef FEAT_BYTEOFF
3733 ml_updatechunk(buf, lnum, line_size, ML_CHNK_DELLINE);
3734#endif
Bram Moolenaarc1a9bc12018-12-28 21:59:29 +01003735 ret = OK;
3736
3737theend:
3738#ifdef FEAT_TEXT_PROP
3739 if (textprop_save != NULL)
3740 {
3741 // Adjust text properties in the line above and below.
3742 if (lnum > 1)
3743 adjust_text_props_for_delete(buf, lnum - 1, textprop_save, textprop_save_len, TRUE);
3744 if (lnum <= buf->b_ml.ml_line_count)
3745 adjust_text_props_for_delete(buf, lnum, textprop_save, textprop_save_len, FALSE);
3746 }
3747 vim_free(textprop_save);
3748#endif
3749 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003750}
3751
3752/*
Bram Moolenaarc1a9bc12018-12-28 21:59:29 +01003753 * set the DB_MARKED flag for line 'lnum'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003754 */
3755 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003756ml_setmarked(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003757{
3758 bhdr_T *hp;
3759 DATA_BL *dp;
3760 /* invalid line number */
3761 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count
3762 || curbuf->b_ml.ml_mfp == NULL)
3763 return; /* give error message? */
3764
3765 if (lowest_marked == 0 || lowest_marked > lnum)
3766 lowest_marked = lnum;
3767
3768 /*
3769 * find the data block containing the line
3770 * This also fills the stack with the blocks from the root to the data block
3771 * This also releases any locked block.
3772 */
3773 if ((hp = ml_find_line(curbuf, lnum, ML_FIND)) == NULL)
3774 return; /* give error message? */
3775
3776 dp = (DATA_BL *)(hp->bh_data);
3777 dp->db_index[lnum - curbuf->b_ml.ml_locked_low] |= DB_MARKED;
3778 curbuf->b_ml.ml_flags |= ML_LOCKED_DIRTY;
3779}
3780
3781/*
Bram Moolenaarc1a9bc12018-12-28 21:59:29 +01003782 * find the first line with its DB_MARKED flag set
Bram Moolenaar071d4272004-06-13 20:20:40 +00003783 */
3784 linenr_T
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003785ml_firstmarked(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003786{
3787 bhdr_T *hp;
3788 DATA_BL *dp;
3789 linenr_T lnum;
3790 int i;
3791
3792 if (curbuf->b_ml.ml_mfp == NULL)
3793 return (linenr_T) 0;
3794
3795 /*
3796 * The search starts with lowest_marked line. This is the last line where
3797 * a mark was found, adjusted by inserting/deleting lines.
3798 */
3799 for (lnum = lowest_marked; lnum <= curbuf->b_ml.ml_line_count; )
3800 {
3801 /*
3802 * Find the data block containing the line.
3803 * This also fills the stack with the blocks from the root to the data
3804 * block This also releases any locked block.
3805 */
3806 if ((hp = ml_find_line(curbuf, lnum, ML_FIND)) == NULL)
3807 return (linenr_T)0; /* give error message? */
3808
3809 dp = (DATA_BL *)(hp->bh_data);
3810
3811 for (i = lnum - curbuf->b_ml.ml_locked_low;
3812 lnum <= curbuf->b_ml.ml_locked_high; ++i, ++lnum)
3813 if ((dp->db_index[i]) & DB_MARKED)
3814 {
3815 (dp->db_index[i]) &= DB_INDEX_MASK;
3816 curbuf->b_ml.ml_flags |= ML_LOCKED_DIRTY;
3817 lowest_marked = lnum + 1;
3818 return lnum;
3819 }
3820 }
3821
3822 return (linenr_T) 0;
3823}
3824
Bram Moolenaar071d4272004-06-13 20:20:40 +00003825/*
3826 * clear all DB_MARKED flags
3827 */
3828 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003829ml_clearmarked(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003830{
3831 bhdr_T *hp;
3832 DATA_BL *dp;
3833 linenr_T lnum;
3834 int i;
3835
3836 if (curbuf->b_ml.ml_mfp == NULL) /* nothing to do */
3837 return;
3838
3839 /*
3840 * The search starts with line lowest_marked.
3841 */
3842 for (lnum = lowest_marked; lnum <= curbuf->b_ml.ml_line_count; )
3843 {
3844 /*
3845 * Find the data block containing the line.
3846 * This also fills the stack with the blocks from the root to the data
3847 * block and releases any locked block.
3848 */
3849 if ((hp = ml_find_line(curbuf, lnum, ML_FIND)) == NULL)
3850 return; /* give error message? */
3851
3852 dp = (DATA_BL *)(hp->bh_data);
3853
3854 for (i = lnum - curbuf->b_ml.ml_locked_low;
3855 lnum <= curbuf->b_ml.ml_locked_high; ++i, ++lnum)
3856 if ((dp->db_index[i]) & DB_MARKED)
3857 {
3858 (dp->db_index[i]) &= DB_INDEX_MASK;
3859 curbuf->b_ml.ml_flags |= ML_LOCKED_DIRTY;
3860 }
3861 }
3862
3863 lowest_marked = 0;
3864 return;
3865}
3866
3867/*
3868 * flush ml_line if necessary
3869 */
3870 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003871ml_flush_line(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003872{
3873 bhdr_T *hp;
3874 DATA_BL *dp;
3875 linenr_T lnum;
3876 char_u *new_line;
3877 char_u *old_line;
3878 colnr_T new_len;
3879 int old_len;
3880 int extra;
3881 int idx;
3882 int start;
3883 int count;
3884 int i;
Bram Moolenaar0ca4b352010-02-11 18:54:43 +01003885 static int entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003886
3887 if (buf->b_ml.ml_line_lnum == 0 || buf->b_ml.ml_mfp == NULL)
3888 return; /* nothing to do */
3889
3890 if (buf->b_ml.ml_flags & ML_LINE_DIRTY)
3891 {
Bram Moolenaar0ca4b352010-02-11 18:54:43 +01003892 /* This code doesn't work recursively, but Netbeans may call back here
3893 * when obtaining the cursor position. */
3894 if (entered)
3895 return;
3896 entered = TRUE;
3897
Bram Moolenaar071d4272004-06-13 20:20:40 +00003898 lnum = buf->b_ml.ml_line_lnum;
3899 new_line = buf->b_ml.ml_line_ptr;
3900
3901 hp = ml_find_line(buf, lnum, ML_FIND);
3902 if (hp == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003903 siemsg(_("E320: Cannot find line %ld"), lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003904 else
3905 {
3906 dp = (DATA_BL *)(hp->bh_data);
3907 idx = lnum - buf->b_ml.ml_locked_low;
3908 start = ((dp->db_index[idx]) & DB_INDEX_MASK);
3909 old_line = (char_u *)dp + start;
3910 if (idx == 0) /* line is last in block */
3911 old_len = dp->db_txt_end - start;
3912 else /* text of previous line follows */
3913 old_len = (dp->db_index[idx - 1] & DB_INDEX_MASK) - start;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003914 new_len = buf->b_ml.ml_line_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003915 extra = new_len - old_len; /* negative if lines gets smaller */
3916
3917 /*
3918 * if new line fits in data block, replace directly
3919 */
3920 if ((int)dp->db_free >= extra)
3921 {
3922 /* if the length changes and there are following lines */
3923 count = buf->b_ml.ml_locked_high - buf->b_ml.ml_locked_low + 1;
3924 if (extra != 0 && idx < count - 1)
3925 {
3926 /* move text of following lines */
3927 mch_memmove((char *)dp + dp->db_txt_start - extra,
3928 (char *)dp + dp->db_txt_start,
3929 (size_t)(start - dp->db_txt_start));
3930
3931 /* adjust pointers of this and following lines */
3932 for (i = idx + 1; i < count; ++i)
3933 dp->db_index[i] -= extra;
3934 }
3935 dp->db_index[idx] -= extra;
3936
3937 /* adjust free space */
3938 dp->db_free -= extra;
3939 dp->db_txt_start -= extra;
3940
3941 /* copy new line into the data block */
3942 mch_memmove(old_line - extra, new_line, (size_t)new_len);
3943 buf->b_ml.ml_flags |= (ML_LOCKED_DIRTY | ML_LOCKED_POS);
3944#ifdef FEAT_BYTEOFF
3945 /* The else case is already covered by the insert and delete */
3946 ml_updatechunk(buf, lnum, (long)extra, ML_CHNK_UPDLINE);
3947#endif
3948 }
3949 else
3950 {
3951 /*
3952 * Cannot do it in one data block: Delete and append.
3953 * Append first, because ml_delete_int() cannot delete the
3954 * last line in a buffer, which causes trouble for a buffer
3955 * that has only one line.
3956 * Don't forget to copy the mark!
3957 */
3958 /* How about handling errors??? */
3959 (void)ml_append_int(buf, lnum, new_line, new_len, FALSE,
3960 (dp->db_index[idx] & DB_MARKED));
3961 (void)ml_delete_int(buf, lnum, FALSE);
3962 }
3963 }
3964 vim_free(new_line);
Bram Moolenaar0ca4b352010-02-11 18:54:43 +01003965
3966 entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003967 }
3968
3969 buf->b_ml.ml_line_lnum = 0;
3970}
3971
3972/*
3973 * create a new, empty, data block
3974 */
3975 static bhdr_T *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003976ml_new_data(memfile_T *mfp, int negative, int page_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003977{
3978 bhdr_T *hp;
3979 DATA_BL *dp;
3980
3981 if ((hp = mf_new(mfp, negative, page_count)) == NULL)
3982 return NULL;
3983
3984 dp = (DATA_BL *)(hp->bh_data);
3985 dp->db_id = DATA_ID;
3986 dp->db_txt_start = dp->db_txt_end = page_count * mfp->mf_page_size;
3987 dp->db_free = dp->db_txt_start - HEADER_SIZE;
3988 dp->db_line_count = 0;
3989
3990 return hp;
3991}
3992
3993/*
3994 * create a new, empty, pointer block
3995 */
3996 static bhdr_T *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003997ml_new_ptr(memfile_T *mfp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003998{
3999 bhdr_T *hp;
4000 PTR_BL *pp;
4001
4002 if ((hp = mf_new(mfp, FALSE, 1)) == NULL)
4003 return NULL;
4004
4005 pp = (PTR_BL *)(hp->bh_data);
4006 pp->pb_id = PTR_ID;
4007 pp->pb_count = 0;
Bram Moolenaar20a825a2010-05-31 21:27:30 +02004008 pp->pb_count_max = (short_u)((mfp->mf_page_size - sizeof(PTR_BL))
4009 / sizeof(PTR_EN) + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004010
4011 return hp;
4012}
4013
4014/*
Bram Moolenaarc1a9bc12018-12-28 21:59:29 +01004015 * Lookup line 'lnum' in a memline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004016 *
4017 * action: if ML_DELETE or ML_INSERT the line count is updated while searching
4018 * if ML_FLUSH only flush a locked block
4019 * if ML_FIND just find the line
4020 *
4021 * If the block was found it is locked and put in ml_locked.
4022 * The stack is updated to lead to the locked block. The ip_high field in
4023 * the stack is updated to reflect the last line in the block AFTER the
4024 * insert or delete, also if the pointer block has not been updated yet. But
Bram Moolenaar6b803a72007-05-06 14:25:46 +00004025 * if ml_locked != NULL ml_locked_lineadd must be added to ip_high.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004026 *
4027 * return: NULL for failure, pointer to block header otherwise
4028 */
4029 static bhdr_T *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004030ml_find_line(buf_T *buf, linenr_T lnum, int action)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004031{
4032 DATA_BL *dp;
4033 PTR_BL *pp;
4034 infoptr_T *ip;
4035 bhdr_T *hp;
4036 memfile_T *mfp;
4037 linenr_T t;
4038 blocknr_T bnum, bnum2;
4039 int dirty;
4040 linenr_T low, high;
4041 int top;
4042 int page_count;
4043 int idx;
4044
4045 mfp = buf->b_ml.ml_mfp;
4046
4047 /*
4048 * If there is a locked block check if the wanted line is in it.
4049 * If not, flush and release the locked block.
4050 * Don't do this for ML_INSERT_SAME, because the stack need to be updated.
4051 * Don't do this for ML_FLUSH, because we want to flush the locked block.
Bram Moolenaar47b8b152007-02-07 02:41:57 +00004052 * Don't do this when 'swapfile' is reset, we want to load all the blocks.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004053 */
4054 if (buf->b_ml.ml_locked)
4055 {
Bram Moolenaar47b8b152007-02-07 02:41:57 +00004056 if (ML_SIMPLE(action)
4057 && buf->b_ml.ml_locked_low <= lnum
4058 && buf->b_ml.ml_locked_high >= lnum
4059 && !mf_dont_release)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004060 {
Bram Moolenaar47b8b152007-02-07 02:41:57 +00004061 /* remember to update pointer blocks and stack later */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004062 if (action == ML_INSERT)
4063 {
4064 ++(buf->b_ml.ml_locked_lineadd);
4065 ++(buf->b_ml.ml_locked_high);
4066 }
4067 else if (action == ML_DELETE)
4068 {
4069 --(buf->b_ml.ml_locked_lineadd);
4070 --(buf->b_ml.ml_locked_high);
4071 }
4072 return (buf->b_ml.ml_locked);
4073 }
4074
4075 mf_put(mfp, buf->b_ml.ml_locked, buf->b_ml.ml_flags & ML_LOCKED_DIRTY,
4076 buf->b_ml.ml_flags & ML_LOCKED_POS);
4077 buf->b_ml.ml_locked = NULL;
4078
Bram Moolenaar6b803a72007-05-06 14:25:46 +00004079 /*
4080 * If lines have been added or deleted in the locked block, need to
4081 * update the line count in pointer blocks.
4082 */
4083 if (buf->b_ml.ml_locked_lineadd != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004084 ml_lineadd(buf, buf->b_ml.ml_locked_lineadd);
4085 }
4086
4087 if (action == ML_FLUSH) /* nothing else to do */
4088 return NULL;
4089
4090 bnum = 1; /* start at the root of the tree */
4091 page_count = 1;
4092 low = 1;
4093 high = buf->b_ml.ml_line_count;
4094
4095 if (action == ML_FIND) /* first try stack entries */
4096 {
4097 for (top = buf->b_ml.ml_stack_top - 1; top >= 0; --top)
4098 {
4099 ip = &(buf->b_ml.ml_stack[top]);
4100 if (ip->ip_low <= lnum && ip->ip_high >= lnum)
4101 {
4102 bnum = ip->ip_bnum;
4103 low = ip->ip_low;
4104 high = ip->ip_high;
4105 buf->b_ml.ml_stack_top = top; /* truncate stack at prev entry */
4106 break;
4107 }
4108 }
4109 if (top < 0)
4110 buf->b_ml.ml_stack_top = 0; /* not found, start at the root */
4111 }
4112 else /* ML_DELETE or ML_INSERT */
4113 buf->b_ml.ml_stack_top = 0; /* start at the root */
4114
4115/*
4116 * search downwards in the tree until a data block is found
4117 */
4118 for (;;)
4119 {
4120 if ((hp = mf_get(mfp, bnum, page_count)) == NULL)
4121 goto error_noblock;
4122
4123 /*
4124 * update high for insert/delete
4125 */
4126 if (action == ML_INSERT)
4127 ++high;
4128 else if (action == ML_DELETE)
4129 --high;
4130
4131 dp = (DATA_BL *)(hp->bh_data);
4132 if (dp->db_id == DATA_ID) /* data block */
4133 {
4134 buf->b_ml.ml_locked = hp;
4135 buf->b_ml.ml_locked_low = low;
4136 buf->b_ml.ml_locked_high = high;
4137 buf->b_ml.ml_locked_lineadd = 0;
4138 buf->b_ml.ml_flags &= ~(ML_LOCKED_DIRTY | ML_LOCKED_POS);
4139 return hp;
4140 }
4141
4142 pp = (PTR_BL *)(dp); /* must be pointer block */
4143 if (pp->pb_id != PTR_ID)
4144 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004145 iemsg(_("E317: pointer block id wrong"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004146 goto error_block;
4147 }
4148
4149 if ((top = ml_add_stack(buf)) < 0) /* add new entry to stack */
4150 goto error_block;
4151 ip = &(buf->b_ml.ml_stack[top]);
4152 ip->ip_bnum = bnum;
4153 ip->ip_low = low;
4154 ip->ip_high = high;
4155 ip->ip_index = -1; /* index not known yet */
4156
4157 dirty = FALSE;
4158 for (idx = 0; idx < (int)pp->pb_count; ++idx)
4159 {
4160 t = pp->pb_pointer[idx].pe_line_count;
4161 CHECK(t == 0, _("pe_line_count is zero"));
4162 if ((low += t) > lnum)
4163 {
4164 ip->ip_index = idx;
4165 bnum = pp->pb_pointer[idx].pe_bnum;
4166 page_count = pp->pb_pointer[idx].pe_page_count;
4167 high = low - 1;
4168 low -= t;
4169
4170 /*
4171 * a negative block number may have been changed
4172 */
4173 if (bnum < 0)
4174 {
4175 bnum2 = mf_trans_del(mfp, bnum);
4176 if (bnum != bnum2)
4177 {
4178 bnum = bnum2;
4179 pp->pb_pointer[idx].pe_bnum = bnum;
4180 dirty = TRUE;
4181 }
4182 }
4183
4184 break;
4185 }
4186 }
4187 if (idx >= (int)pp->pb_count) /* past the end: something wrong! */
4188 {
4189 if (lnum > buf->b_ml.ml_line_count)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004190 siemsg(_("E322: line number out of range: %ld past the end"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00004191 lnum - buf->b_ml.ml_line_count);
4192
4193 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004194 siemsg(_("E323: line count wrong in block %ld"), bnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004195 goto error_block;
4196 }
4197 if (action == ML_DELETE)
4198 {
4199 pp->pb_pointer[idx].pe_line_count--;
4200 dirty = TRUE;
4201 }
4202 else if (action == ML_INSERT)
4203 {
4204 pp->pb_pointer[idx].pe_line_count++;
4205 dirty = TRUE;
4206 }
4207 mf_put(mfp, hp, dirty, FALSE);
4208 }
4209
4210error_block:
4211 mf_put(mfp, hp, FALSE, FALSE);
4212error_noblock:
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02004213 /*
4214 * If action is ML_DELETE or ML_INSERT we have to correct the tree for
4215 * the incremented/decremented line counts, because there won't be a line
4216 * inserted/deleted after all.
4217 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004218 if (action == ML_DELETE)
4219 ml_lineadd(buf, 1);
4220 else if (action == ML_INSERT)
4221 ml_lineadd(buf, -1);
4222 buf->b_ml.ml_stack_top = 0;
4223 return NULL;
4224}
4225
4226/*
4227 * add an entry to the info pointer stack
4228 *
4229 * return -1 for failure, number of the new entry otherwise
4230 */
4231 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004232ml_add_stack(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004233{
4234 int top;
4235 infoptr_T *newstack;
4236
4237 top = buf->b_ml.ml_stack_top;
4238
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02004239 /* may have to increase the stack size */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004240 if (top == buf->b_ml.ml_stack_size)
4241 {
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02004242 CHECK(top > 0, _("Stack size increases")); /* more than 5 levels??? */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004243
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004244 newstack = ALLOC_MULT(infoptr_T, buf->b_ml.ml_stack_size + STACK_INCR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004245 if (newstack == NULL)
4246 return -1;
Bram Moolenaarfbd302f2015-08-08 18:23:46 +02004247 if (top > 0)
4248 mch_memmove(newstack, buf->b_ml.ml_stack,
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004249 (size_t)top * sizeof(infoptr_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004250 vim_free(buf->b_ml.ml_stack);
4251 buf->b_ml.ml_stack = newstack;
4252 buf->b_ml.ml_stack_size += STACK_INCR;
4253 }
4254
4255 buf->b_ml.ml_stack_top++;
4256 return top;
4257}
4258
4259/*
4260 * Update the pointer blocks on the stack for inserted/deleted lines.
4261 * The stack itself is also updated.
4262 *
4263 * When a insert/delete line action fails, the line is not inserted/deleted,
4264 * but the pointer blocks have already been updated. That is fixed here by
4265 * walking through the stack.
4266 *
4267 * Count is the number of lines added, negative if lines have been deleted.
4268 */
4269 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004270ml_lineadd(buf_T *buf, int count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004271{
4272 int idx;
4273 infoptr_T *ip;
4274 PTR_BL *pp;
4275 memfile_T *mfp = buf->b_ml.ml_mfp;
4276 bhdr_T *hp;
4277
4278 for (idx = buf->b_ml.ml_stack_top - 1; idx >= 0; --idx)
4279 {
4280 ip = &(buf->b_ml.ml_stack[idx]);
4281 if ((hp = mf_get(mfp, ip->ip_bnum, 1)) == NULL)
4282 break;
4283 pp = (PTR_BL *)(hp->bh_data); /* must be pointer block */
4284 if (pp->pb_id != PTR_ID)
4285 {
4286 mf_put(mfp, hp, FALSE, FALSE);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004287 iemsg(_("E317: pointer block id wrong 2"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004288 break;
4289 }
4290 pp->pb_pointer[ip->ip_index].pe_line_count += count;
4291 ip->ip_high += count;
4292 mf_put(mfp, hp, TRUE, FALSE);
4293 }
4294}
4295
Bram Moolenaar55debbe2010-05-23 23:34:36 +02004296#if defined(HAVE_READLINK) || defined(PROTO)
Bram Moolenaar900b4d72005-12-12 22:05:50 +00004297/*
4298 * Resolve a symlink in the last component of a file name.
4299 * Note that f_resolve() does it for every part of the path, we don't do that
4300 * here.
4301 * If it worked returns OK and the resolved link in "buf[MAXPATHL]".
4302 * Otherwise returns FAIL.
4303 */
Bram Moolenaar55debbe2010-05-23 23:34:36 +02004304 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004305resolve_symlink(char_u *fname, char_u *buf)
Bram Moolenaar900b4d72005-12-12 22:05:50 +00004306{
4307 char_u tmp[MAXPATHL];
4308 int ret;
4309 int depth = 0;
4310
4311 if (fname == NULL)
4312 return FAIL;
4313
4314 /* Put the result so far in tmp[], starting with the original name. */
4315 vim_strncpy(tmp, fname, MAXPATHL - 1);
4316
4317 for (;;)
4318 {
4319 /* Limit symlink depth to 100, catch recursive loops. */
4320 if (++depth == 100)
4321 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004322 semsg(_("E773: Symlink loop for \"%s\""), fname);
Bram Moolenaar900b4d72005-12-12 22:05:50 +00004323 return FAIL;
4324 }
4325
4326 ret = readlink((char *)tmp, (char *)buf, MAXPATHL - 1);
4327 if (ret <= 0)
4328 {
Bram Moolenaarcc984262005-12-23 22:19:46 +00004329 if (errno == EINVAL || errno == ENOENT)
Bram Moolenaar900b4d72005-12-12 22:05:50 +00004330 {
Bram Moolenaarcc984262005-12-23 22:19:46 +00004331 /* Found non-symlink or not existing file, stop here.
Bram Moolenaar9439cdd2009-04-22 13:39:36 +00004332 * When at the first level use the unmodified name, skip the
Bram Moolenaar900b4d72005-12-12 22:05:50 +00004333 * call to vim_FullName(). */
4334 if (depth == 1)
4335 return FAIL;
4336
4337 /* Use the resolved name in tmp[]. */
4338 break;
4339 }
4340
4341 /* There must be some error reading links, use original name. */
4342 return FAIL;
4343 }
4344 buf[ret] = NUL;
4345
4346 /*
4347 * Check whether the symlink is relative or absolute.
4348 * If it's relative, build a new path based on the directory
4349 * portion of the filename (if any) and the path the symlink
4350 * points to.
4351 */
4352 if (mch_isFullName(buf))
4353 STRCPY(tmp, buf);
4354 else
4355 {
4356 char_u *tail;
4357
4358 tail = gettail(tmp);
4359 if (STRLEN(tail) + STRLEN(buf) >= MAXPATHL)
4360 return FAIL;
4361 STRCPY(tail, buf);
4362 }
4363 }
4364
4365 /*
4366 * Try to resolve the full name of the file so that the swapfile name will
4367 * be consistent even when opening a relative symlink from different
4368 * working directories.
4369 */
4370 return vim_FullName(tmp, buf, MAXPATHL, TRUE);
4371}
4372#endif
4373
Bram Moolenaar071d4272004-06-13 20:20:40 +00004374/*
Bram Moolenaar04a09c12005-08-01 22:02:32 +00004375 * Make swap file name out of the file name and a directory name.
4376 * Returns pointer to allocated memory or NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004377 */
Bram Moolenaar04a09c12005-08-01 22:02:32 +00004378 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004379makeswapname(
4380 char_u *fname,
4381 char_u *ffname UNUSED,
4382 buf_T *buf,
4383 char_u *dir_name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004384{
4385 char_u *r, *s;
Bram Moolenaar9dbe4752010-05-14 17:52:42 +02004386 char_u *fname_res = fname;
Bram Moolenaar900b4d72005-12-12 22:05:50 +00004387#ifdef HAVE_READLINK
4388 char_u fname_buf[MAXPATHL];
Bram Moolenaar900b4d72005-12-12 22:05:50 +00004389#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004390
Bram Moolenaar4f974752019-02-17 17:44:42 +01004391#if defined(UNIX) || defined(MSWIN) // Need _very_ long file names
Bram Moolenaarb113c3a2017-02-28 21:26:17 +01004392 int len = (int)STRLEN(dir_name);
Bram Moolenaarc525e3a2017-02-18 16:59:02 +01004393
4394 s = dir_name + len;
4395 if (after_pathsep(dir_name, s) && len > 1 && s[-1] == s[-2])
Bram Moolenaar071d4272004-06-13 20:20:40 +00004396 { /* Ends with '//', Use Full path */
4397 r = NULL;
Bram Moolenaar04a09c12005-08-01 22:02:32 +00004398 if ((s = make_percent_swname(dir_name, fname)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004399 {
4400 r = modname(s, (char_u *)".swp", FALSE);
4401 vim_free(s);
4402 }
4403 return r;
4404 }
4405#endif
4406
Bram Moolenaar900b4d72005-12-12 22:05:50 +00004407#ifdef HAVE_READLINK
4408 /* Expand symlink in the file name, so that we put the swap file with the
4409 * actual file instead of with the symlink. */
4410 if (resolve_symlink(fname, fname_buf) == OK)
4411 fname_res = fname_buf;
Bram Moolenaar900b4d72005-12-12 22:05:50 +00004412#endif
4413
Bram Moolenaar071d4272004-06-13 20:20:40 +00004414 r = buf_modname(
Bram Moolenaar071d4272004-06-13 20:20:40 +00004415 (buf->b_p_sn || buf->b_shortname),
Bram Moolenaar900b4d72005-12-12 22:05:50 +00004416 fname_res,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004417 (char_u *)
Bram Moolenaare60acc12011-05-10 16:41:25 +02004418#if defined(VMS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004419 "_swp",
4420#else
4421 ".swp",
4422#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004423 /* Prepend a '.' to the swap file name for the current directory. */
Bram Moolenaar48e330a2016-02-23 14:53:34 +01004424 dir_name[0] == '.' && dir_name[1] == NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004425 if (r == NULL) /* out of memory */
4426 return NULL;
4427
4428 s = get_file_in_dir(r, dir_name);
4429 vim_free(r);
4430 return s;
4431}
4432
4433/*
4434 * Get file name to use for swap file or backup file.
4435 * Use the name of the edited file "fname" and an entry in the 'dir' or 'bdir'
4436 * option "dname".
4437 * - If "dname" is ".", return "fname" (swap file in dir of file).
4438 * - If "dname" starts with "./", insert "dname" in "fname" (swap file
4439 * relative to dir of file).
4440 * - Otherwise, prepend "dname" to the tail of "fname" (swap file in specific
4441 * dir).
4442 *
4443 * The return value is an allocated string and can be NULL.
4444 */
4445 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004446get_file_in_dir(
4447 char_u *fname,
4448 char_u *dname) /* don't use "dirname", it is a global for Alpha */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004449{
4450 char_u *t;
4451 char_u *tail;
4452 char_u *retval;
4453 int save_char;
4454
4455 tail = gettail(fname);
4456
4457 if (dname[0] == '.' && dname[1] == NUL)
4458 retval = vim_strsave(fname);
4459 else if (dname[0] == '.' && vim_ispathsep(dname[1]))
4460 {
4461 if (tail == fname) /* no path before file name */
4462 retval = concat_fnames(dname + 2, tail, TRUE);
4463 else
4464 {
4465 save_char = *tail;
4466 *tail = NUL;
4467 t = concat_fnames(fname, dname + 2, TRUE);
4468 *tail = save_char;
4469 if (t == NULL) /* out of memory */
4470 retval = NULL;
4471 else
4472 {
4473 retval = concat_fnames(t, tail, TRUE);
4474 vim_free(t);
4475 }
4476 }
4477 }
4478 else
4479 retval = concat_fnames(dname, tail, TRUE);
4480
Bram Moolenaar4f974752019-02-17 17:44:42 +01004481#ifdef MSWIN
Bram Moolenaar69c35002013-11-04 02:54:12 +01004482 if (retval != NULL)
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004483 for (t = gettail(retval); *t != NUL; MB_PTR_ADV(t))
Bram Moolenaar69c35002013-11-04 02:54:12 +01004484 if (*t == ':')
4485 *t = '%';
4486#endif
4487
Bram Moolenaar071d4272004-06-13 20:20:40 +00004488 return retval;
4489}
4490
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00004491/*
4492 * Print the ATTENTION message: info about an existing swap file.
4493 */
4494 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004495attention_message(
4496 buf_T *buf, /* buffer being edited */
4497 char_u *fname) /* swap file name */
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00004498{
Bram Moolenaar8767f522016-07-01 17:17:39 +02004499 stat_T st;
Bram Moolenaar63d25552019-05-10 21:28:38 +02004500 time_t swap_mtime;
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00004501
4502 ++no_wait_return;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004503 (void)emsg(_("E325: ATTENTION"));
Bram Moolenaar32526b32019-01-19 17:43:09 +01004504 msg_puts(_("\nFound a swap file by the name \""));
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00004505 msg_home_replace(fname);
Bram Moolenaar32526b32019-01-19 17:43:09 +01004506 msg_puts("\"\n");
Bram Moolenaar63d25552019-05-10 21:28:38 +02004507 swap_mtime = swapfile_info(fname);
Bram Moolenaar32526b32019-01-19 17:43:09 +01004508 msg_puts(_("While opening file \""));
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00004509 msg_outtrans(buf->b_fname);
Bram Moolenaar32526b32019-01-19 17:43:09 +01004510 msg_puts("\"\n");
Bram Moolenaard6105cb2018-10-13 19:06:27 +02004511 if (mch_stat((char *)buf->b_fname, &st) == -1)
4512 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004513 msg_puts(_(" CANNOT BE FOUND"));
Bram Moolenaard6105cb2018-10-13 19:06:27 +02004514 }
4515 else
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00004516 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004517 msg_puts(_(" dated: "));
Bram Moolenaar63d25552019-05-10 21:28:38 +02004518 msg_puts(get_ctime(st.st_mtime, TRUE));
4519 if (swap_mtime != 0 && st.st_mtime > swap_mtime)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004520 msg_puts(_(" NEWER than swap file!\n"));
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00004521 }
4522 /* Some of these messages are long to allow translation to
4523 * other languages. */
Bram Moolenaar32526b32019-01-19 17:43:09 +01004524 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"));
4525 msg_puts(_("(2) An edit session for this file crashed.\n"));
4526 msg_puts(_(" If this is the case, use \":recover\" or \"vim -r "));
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00004527 msg_outtrans(buf->b_fname);
Bram Moolenaar32526b32019-01-19 17:43:09 +01004528 msg_puts(_("\"\n to recover the changes (see \":help recovery\").\n"));
4529 msg_puts(_(" If you did this already, delete the swap file \""));
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00004530 msg_outtrans(fname);
Bram Moolenaar32526b32019-01-19 17:43:09 +01004531 msg_puts(_("\"\n to avoid this message.\n"));
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00004532 cmdline_row = msg_row;
4533 --no_wait_return;
4534}
4535
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004536#if defined(FEAT_EVAL)
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00004537/*
4538 * Trigger the SwapExists autocommands.
4539 * Returns a value for equivalent to do_dialog() (see below):
4540 * 0: still need to ask for a choice
4541 * 1: open read-only
4542 * 2: edit anyway
4543 * 3: recover
4544 * 4: delete it
4545 * 5: quit
4546 * 6: abort
4547 */
4548 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004549do_swapexists(buf_T *buf, char_u *fname)
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00004550{
4551 set_vim_var_string(VV_SWAPNAME, fname, -1);
4552 set_vim_var_string(VV_SWAPCHOICE, NULL, -1);
4553
4554 /* Trigger SwapExists autocommands with <afile> set to the file being
Bram Moolenaar12c22ce2009-04-22 13:58:46 +00004555 * edited. Disallow changing directory here. */
4556 ++allbuf_lock;
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00004557 apply_autocmds(EVENT_SWAPEXISTS, buf->b_fname, NULL, FALSE, NULL);
Bram Moolenaar12c22ce2009-04-22 13:58:46 +00004558 --allbuf_lock;
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00004559
4560 set_vim_var_string(VV_SWAPNAME, NULL, -1);
4561
4562 switch (*get_vim_var_str(VV_SWAPCHOICE))
4563 {
4564 case 'o': return 1;
4565 case 'e': return 2;
4566 case 'r': return 3;
4567 case 'd': return 4;
4568 case 'q': return 5;
4569 case 'a': return 6;
4570 }
4571
4572 return 0;
4573}
4574#endif
4575
Bram Moolenaar071d4272004-06-13 20:20:40 +00004576/*
4577 * Find out what name to use for the swap file for buffer 'buf'.
4578 *
4579 * Several names are tried to find one that does not exist
Bram Moolenaar04a09c12005-08-01 22:02:32 +00004580 * Returns the name in allocated memory or NULL.
Bram Moolenaarf541c362011-10-26 11:44:18 +02004581 * When out of memory "dirp" is set to NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004582 *
4583 * Note: If BASENAMELEN is not correct, you will get error messages for
Bram Moolenaar55debbe2010-05-23 23:34:36 +02004584 * not being able to open the swap or undo file
Bram Moolenaar12c22ce2009-04-22 13:58:46 +00004585 * Note: May trigger SwapExists autocmd, pointers may change!
Bram Moolenaar071d4272004-06-13 20:20:40 +00004586 */
4587 static char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004588findswapname(
4589 buf_T *buf,
4590 char_u **dirp, /* pointer to list of directories */
4591 char_u *old_fname) /* don't give warning for this file name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004592{
4593 char_u *fname;
4594 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004595 char_u *dir_name;
4596#ifdef AMIGA
4597 BPTR fh;
4598#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004599 int r;
Bram Moolenaar69c35002013-11-04 02:54:12 +01004600 char_u *buf_fname = buf->b_fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004601
Bram Moolenaar48e330a2016-02-23 14:53:34 +01004602#if !defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004603# define CREATE_DUMMY_FILE
4604 FILE *dummyfd = NULL;
4605
Bram Moolenaar4f974752019-02-17 17:44:42 +01004606# ifdef MSWIN
Bram Moolenaar69c35002013-11-04 02:54:12 +01004607 if (buf_fname != NULL && !mch_isFullName(buf_fname)
4608 && vim_strchr(gettail(buf_fname), ':'))
4609 {
4610 char_u *t;
4611
4612 buf_fname = vim_strsave(buf_fname);
4613 if (buf_fname == NULL)
4614 buf_fname = buf->b_fname;
4615 else
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004616 for (t = gettail(buf_fname); *t != NUL; MB_PTR_ADV(t))
Bram Moolenaar69c35002013-11-04 02:54:12 +01004617 if (*t == ':')
4618 *t = '%';
4619 }
4620# endif
4621
Bram Moolenaar55debbe2010-05-23 23:34:36 +02004622 /*
4623 * If we start editing a new file, e.g. "test.doc", which resides on an
4624 * MSDOS compatible filesystem, it is possible that the file
4625 * "test.doc.swp" which we create will be exactly the same file. To avoid
4626 * this problem we temporarily create "test.doc". Don't do this when the
4627 * check below for a 8.3 file name is used.
4628 */
Bram Moolenaar69c35002013-11-04 02:54:12 +01004629 if (!(buf->b_p_sn || buf->b_shortname) && buf_fname != NULL
4630 && mch_getperm(buf_fname) < 0)
4631 dummyfd = mch_fopen((char *)buf_fname, "w");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004632#endif
4633
Bram Moolenaar55debbe2010-05-23 23:34:36 +02004634 /*
4635 * Isolate a directory name from *dirp and put it in dir_name.
4636 * First allocate some memory to put the directory name in.
4637 */
Bram Moolenaar964b3742019-05-24 18:54:09 +02004638 dir_name = alloc(STRLEN(*dirp) + 1);
Bram Moolenaarf541c362011-10-26 11:44:18 +02004639 if (dir_name == NULL)
4640 *dirp = NULL;
4641 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004642 (void)copy_option_part(dirp, dir_name, 31000, ",");
4643
Bram Moolenaar55debbe2010-05-23 23:34:36 +02004644 /*
4645 * we try different names until we find one that does not exist yet
4646 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004647 if (dir_name == NULL) /* out of memory */
4648 fname = NULL;
4649 else
Bram Moolenaar69c35002013-11-04 02:54:12 +01004650 fname = makeswapname(buf_fname, buf->b_ffname, buf, dir_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004651
4652 for (;;)
4653 {
4654 if (fname == NULL) /* must be out of memory */
4655 break;
4656 if ((n = (int)STRLEN(fname)) == 0) /* safety check */
4657 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01004658 VIM_CLEAR(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004659 break;
4660 }
Bram Moolenaar48e330a2016-02-23 14:53:34 +01004661#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004662/*
4663 * Some systems have a MS-DOS compatible filesystem that use 8.3 character
4664 * file names. If this is the first try and the swap file name does not fit in
4665 * 8.3, detect if this is the case, set shortname and try again.
4666 */
4667 if (fname[n - 2] == 'w' && fname[n - 1] == 'p'
4668 && !(buf->b_p_sn || buf->b_shortname))
4669 {
4670 char_u *tail;
4671 char_u *fname2;
Bram Moolenaar8767f522016-07-01 17:17:39 +02004672 stat_T s1, s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004673 int f1, f2;
4674 int created1 = FALSE, created2 = FALSE;
4675 int same = FALSE;
4676
4677 /*
4678 * Check if swapfile name does not fit in 8.3:
4679 * It either contains two dots, is longer than 8 chars, or starts
4680 * with a dot.
4681 */
Bram Moolenaar69c35002013-11-04 02:54:12 +01004682 tail = gettail(buf_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004683 if ( vim_strchr(tail, '.') != NULL
4684 || STRLEN(tail) > (size_t)8
4685 || *gettail(fname) == '.')
4686 {
4687 fname2 = alloc(n + 2);
4688 if (fname2 != NULL)
4689 {
4690 STRCPY(fname2, fname);
4691 /* if fname == "xx.xx.swp", fname2 = "xx.xx.swx"
4692 * if fname == ".xx.swp", fname2 = ".xx.swpx"
4693 * if fname == "123456789.swp", fname2 = "12345678x.swp"
4694 */
4695 if (vim_strchr(tail, '.') != NULL)
4696 fname2[n - 1] = 'x';
4697 else if (*gettail(fname) == '.')
4698 {
4699 fname2[n] = 'x';
4700 fname2[n + 1] = NUL;
4701 }
4702 else
4703 fname2[n - 5] += 1;
4704 /*
4705 * may need to create the files to be able to use mch_stat()
4706 */
4707 f1 = mch_open((char *)fname, O_RDONLY | O_EXTRA, 0);
4708 if (f1 < 0)
4709 {
4710 f1 = mch_open_rw((char *)fname,
4711 O_RDWR|O_CREAT|O_EXCL|O_EXTRA);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004712 created1 = TRUE;
4713 }
4714 if (f1 >= 0)
4715 {
4716 f2 = mch_open((char *)fname2, O_RDONLY | O_EXTRA, 0);
4717 if (f2 < 0)
4718 {
4719 f2 = mch_open_rw((char *)fname2,
4720 O_RDWR|O_CREAT|O_EXCL|O_EXTRA);
4721 created2 = TRUE;
4722 }
4723 if (f2 >= 0)
4724 {
4725 /*
4726 * Both files exist now. If mch_stat() returns the
4727 * same device and inode they are the same file.
4728 */
4729 if (mch_fstat(f1, &s1) != -1
4730 && mch_fstat(f2, &s2) != -1
4731 && s1.st_dev == s2.st_dev
4732 && s1.st_ino == s2.st_ino)
4733 same = TRUE;
4734 close(f2);
4735 if (created2)
4736 mch_remove(fname2);
4737 }
4738 close(f1);
4739 if (created1)
4740 mch_remove(fname);
4741 }
4742 vim_free(fname2);
4743 if (same)
4744 {
4745 buf->b_shortname = TRUE;
4746 vim_free(fname);
Bram Moolenaar69c35002013-11-04 02:54:12 +01004747 fname = makeswapname(buf_fname, buf->b_ffname,
Bram Moolenaar04a09c12005-08-01 22:02:32 +00004748 buf, dir_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004749 continue; /* try again with b_shortname set */
4750 }
4751 }
4752 }
4753 }
4754#endif
4755 /*
4756 * check if the swapfile already exists
4757 */
4758 if (mch_getperm(fname) < 0) /* it does not exist */
4759 {
4760#ifdef HAVE_LSTAT
Bram Moolenaar8767f522016-07-01 17:17:39 +02004761 stat_T sb;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004762
4763 /*
4764 * Extra security check: When a swap file is a symbolic link, this
4765 * is most likely a symlink attack.
4766 */
4767 if (mch_lstat((char *)fname, &sb) < 0)
4768#else
4769# ifdef AMIGA
4770 fh = Open((UBYTE *)fname, (long)MODE_NEWFILE);
4771 /*
4772 * on the Amiga mch_getperm() will return -1 when the file exists
4773 * but is being used by another program. This happens if you edit
4774 * a file twice.
4775 */
4776 if (fh != (BPTR)NULL) /* can open file, OK */
4777 {
4778 Close(fh);
4779 mch_remove(fname);
4780 break;
4781 }
4782 if (IoErr() != ERROR_OBJECT_IN_USE
4783 && IoErr() != ERROR_OBJECT_EXISTS)
4784# endif
4785#endif
4786 break;
4787 }
4788
4789 /*
4790 * A file name equal to old_fname is OK to use.
4791 */
4792 if (old_fname != NULL && fnamecmp(fname, old_fname) == 0)
4793 break;
4794
4795 /*
4796 * get here when file already exists
4797 */
4798 if (fname[n - 2] == 'w' && fname[n - 1] == 'p') /* first try */
4799 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004800 /*
4801 * on MS-DOS compatible filesystems (e.g. messydos) file.doc.swp
4802 * and file.doc are the same file. To guess if this problem is
4803 * present try if file.doc.swx exists. If it does, we set
4804 * buf->b_shortname and try file_doc.swp (dots replaced by
4805 * underscores for this file), and try again. If it doesn't we
4806 * assume that "file.doc.swp" already exists.
4807 */
4808 if (!(buf->b_p_sn || buf->b_shortname)) /* not tried yet */
4809 {
4810 fname[n - 1] = 'x';
4811 r = mch_getperm(fname); /* try "file.swx" */
4812 fname[n - 1] = 'p';
4813 if (r >= 0) /* "file.swx" seems to exist */
4814 {
4815 buf->b_shortname = TRUE;
4816 vim_free(fname);
Bram Moolenaar69c35002013-11-04 02:54:12 +01004817 fname = makeswapname(buf_fname, buf->b_ffname,
Bram Moolenaar04a09c12005-08-01 22:02:32 +00004818 buf, dir_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004819 continue; /* try again with '.' replaced with '_' */
4820 }
4821 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004822 /*
4823 * If we get here the ".swp" file really exists.
4824 * Give an error message, unless recovering, no file name, we are
4825 * viewing a help file or when the path of the file is different
4826 * (happens when all .swp files are in one directory).
4827 */
Bram Moolenaar69c35002013-11-04 02:54:12 +01004828 if (!recoverymode && buf_fname != NULL
Bram Moolenaar8fc061c2004-12-29 21:03:02 +00004829 && !buf->b_help && !(buf->b_flags & BF_DUMMY))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004830 {
4831 int fd;
4832 struct block0 b0;
4833 int differ = FALSE;
4834
4835 /*
4836 * Try to read block 0 from the swap file to get the original
4837 * file name (and inode number).
4838 */
4839 fd = mch_open((char *)fname, O_RDONLY | O_EXTRA, 0);
4840 if (fd >= 0)
4841 {
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01004842 if (read_eintr(fd, &b0, sizeof(b0)) == sizeof(b0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004843 {
4844 /*
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004845 * If the swapfile has the same directory as the
4846 * buffer don't compare the directory names, they can
4847 * have a different mountpoint.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004848 */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004849 if (b0.b0_flags & B0_SAME_DIR)
4850 {
4851 if (fnamecmp(gettail(buf->b_ffname),
4852 gettail(b0.b0_fname)) != 0
4853 || !same_directory(fname, buf->b_ffname))
Bram Moolenaar900b4d72005-12-12 22:05:50 +00004854 {
4855#ifdef CHECK_INODE
4856 /* Symlinks may point to the same file even
4857 * when the name differs, need to check the
4858 * inode too. */
4859 expand_env(b0.b0_fname, NameBuff, MAXPATHL);
4860 if (fnamecmp_ino(buf->b_ffname, NameBuff,
4861 char_to_long(b0.b0_ino)))
4862#endif
4863 differ = TRUE;
4864 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004865 }
4866 else
4867 {
4868 /*
4869 * The name in the swap file may be
4870 * "~user/path/file". Expand it first.
4871 */
4872 expand_env(b0.b0_fname, NameBuff, MAXPATHL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004873#ifdef CHECK_INODE
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004874 if (fnamecmp_ino(buf->b_ffname, NameBuff,
Bram Moolenaar900b4d72005-12-12 22:05:50 +00004875 char_to_long(b0.b0_ino)))
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004876 differ = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004877#else
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004878 if (fnamecmp(NameBuff, buf->b_ffname) != 0)
4879 differ = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004880#endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004881 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004882 }
4883 close(fd);
4884 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004885
4886 /* give the ATTENTION message when there is an old swap file
4887 * for the current file, and the buffer was not recovered. */
4888 if (differ == FALSE && !(curbuf->b_flags & BF_RECOVERED)
4889 && vim_strchr(p_shm, SHM_ATTENTION) == NULL)
4890 {
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00004891 int choice = 0;
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02004892 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004893#ifdef CREATE_DUMMY_FILE
4894 int did_use_dummy = FALSE;
4895
4896 /* Avoid getting a warning for the file being created
4897 * outside of Vim, it was created at the start of this
4898 * function. Delete the file now, because Vim might exit
4899 * here if the window is closed. */
4900 if (dummyfd != NULL)
4901 {
4902 fclose(dummyfd);
4903 dummyfd = NULL;
Bram Moolenaar69c35002013-11-04 02:54:12 +01004904 mch_remove(buf_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004905 did_use_dummy = TRUE;
4906 }
4907#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004908
Bram Moolenaar1b243ea2019-04-28 22:50:40 +02004909#ifdef HAVE_PROCESS_STILL_RUNNING
Bram Moolenaar071d4272004-06-13 20:20:40 +00004910 process_still_running = FALSE;
4911#endif
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02004912 // It's safe to delete the swap file if all these are true:
4913 // - the edited file exists
4914 // - the swap file has no changes and looks OK
4915 if (mch_stat((char *)buf->b_fname, &st) == 0
4916 && swapfile_unchanged(fname))
4917 {
4918 choice = 4;
4919 if (p_verbose > 0)
4920 verb_msg(_("Found a swap file that is not useful, deleting it"));
4921 }
4922
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004923#if defined(FEAT_EVAL)
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00004924 /*
4925 * If there is an SwapExists autocommand and we can handle
4926 * the response, trigger it. It may return 0 to ask the
4927 * user anyway.
4928 */
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02004929 if (choice == 0
4930 && swap_exists_action != SEA_NONE
Bram Moolenaar69c35002013-11-04 02:54:12 +01004931 && has_autocmd(EVENT_SWAPEXISTS, buf_fname, buf))
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00004932 choice = do_swapexists(buf, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004933
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00004934 if (choice == 0)
4935#endif
4936 {
4937#ifdef FEAT_GUI
Bram Moolenaar798184c2018-10-07 20:48:39 +02004938 // If we are supposed to start the GUI but it wasn't
4939 // completely started yet, start it now. This makes
4940 // the messages displayed in the Vim window when
4941 // loading a session from the .gvimrc file.
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00004942 if (gui.starting && !gui.in_use)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004943 gui_start(NULL);
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00004944#endif
Bram Moolenaar798184c2018-10-07 20:48:39 +02004945 // Show info about the existing swap file.
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00004946 attention_message(buf, fname);
4947
Bram Moolenaar798184c2018-10-07 20:48:39 +02004948 // We don't want a 'q' typed at the more-prompt
4949 // interrupt loading a file.
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00004950 got_int = FALSE;
Bram Moolenaar798184c2018-10-07 20:48:39 +02004951
4952 // If vimrc has "simalt ~x" we don't want it to
4953 // interfere with the prompt here.
Bram Moolenaar6a2633b2018-10-07 23:16:36 +02004954 flush_buffers(FLUSH_TYPEAHEAD);
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00004955 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004956
4957#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00004958 if (swap_exists_action != SEA_NONE && choice == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004959 {
4960 char_u *name;
4961
Bram Moolenaar964b3742019-05-24 18:54:09 +02004962 name = alloc(STRLEN(fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004963 + STRLEN(_("Swap file \""))
Bram Moolenaar964b3742019-05-24 18:54:09 +02004964 + STRLEN(_("\" already exists!")) + 5);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004965 if (name != NULL)
4966 {
4967 STRCPY(name, _("Swap file \""));
4968 home_replace(NULL, fname, name + STRLEN(name),
4969 1000, TRUE);
4970 STRCAT(name, _("\" already exists!"));
4971 }
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00004972 choice = do_dialog(VIM_WARNING,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004973 (char_u *)_("VIM - ATTENTION"),
4974 name == NULL
4975 ? (char_u *)_("Swap file already exists!")
4976 : name,
Bram Moolenaar1b243ea2019-04-28 22:50:40 +02004977# ifdef HAVE_PROCESS_STILL_RUNNING
Bram Moolenaar071d4272004-06-13 20:20:40 +00004978 process_still_running
4979 ? (char_u *)_("&Open Read-Only\n&Edit anyway\n&Recover\n&Quit\n&Abort") :
4980# endif
Bram Moolenaard2c340a2011-01-17 20:08:11 +01004981 (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 +00004982
Bram Moolenaar1b243ea2019-04-28 22:50:40 +02004983# ifdef HAVE_PROCESS_STILL_RUNNING
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00004984 if (process_still_running && choice >= 4)
4985 choice++; /* Skip missing "Delete it" button */
4986# endif
4987 vim_free(name);
4988
4989 /* pretend screen didn't scroll, need redraw anyway */
4990 msg_scrolled = 0;
4991 redraw_all_later(NOT_VALID);
4992 }
4993#endif
4994
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00004995 if (choice > 0)
4996 {
4997 switch (choice)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004998 {
4999 case 1:
5000 buf->b_p_ro = TRUE;
5001 break;
5002 case 2:
5003 break;
5004 case 3:
5005 swap_exists_action = SEA_RECOVER;
5006 break;
5007 case 4:
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00005008 mch_remove(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005009 break;
5010 case 5:
5011 swap_exists_action = SEA_QUIT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005012 break;
5013 case 6:
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00005014 swap_exists_action = SEA_QUIT;
5015 got_int = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005016 break;
5017 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005018
5019 /* If the file was deleted this fname can be used. */
5020 if (mch_getperm(fname) < 0)
5021 break;
5022 }
5023 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005024 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005025 msg_puts("\n");
Bram Moolenaar4770d092006-01-12 23:22:24 +00005026 if (msg_silent == 0)
5027 /* call wait_return() later */
5028 need_wait_return = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005029 }
5030
5031#ifdef CREATE_DUMMY_FILE
5032 /* Going to try another name, need the dummy file again. */
5033 if (did_use_dummy)
Bram Moolenaar69c35002013-11-04 02:54:12 +01005034 dummyfd = mch_fopen((char *)buf_fname, "w");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005035#endif
5036 }
5037 }
5038 }
5039
5040 /*
5041 * Change the ".swp" extension to find another file that can be used.
5042 * First decrement the last char: ".swo", ".swn", etc.
5043 * If that still isn't enough decrement the last but one char: ".svz"
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005044 * Can happen when editing many "No Name" buffers.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005045 */
5046 if (fname[n - 1] == 'a') /* ".s?a" */
5047 {
5048 if (fname[n - 2] == 'a') /* ".saa": tried enough, give up */
5049 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005050 emsg(_("E326: Too many swap files found"));
Bram Moolenaard23a8232018-02-10 18:45:26 +01005051 VIM_CLEAR(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005052 break;
5053 }
5054 --fname[n - 2]; /* ".svz", ".suz", etc. */
5055 fname[n - 1] = 'z' + 1;
5056 }
5057 --fname[n - 1]; /* ".swo", ".swn", etc. */
5058 }
5059
5060 vim_free(dir_name);
5061#ifdef CREATE_DUMMY_FILE
5062 if (dummyfd != NULL) /* file has been created temporarily */
5063 {
5064 fclose(dummyfd);
Bram Moolenaar69c35002013-11-04 02:54:12 +01005065 mch_remove(buf_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005066 }
5067#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01005068#ifdef MSWIN
Bram Moolenaar69c35002013-11-04 02:54:12 +01005069 if (buf_fname != buf->b_fname)
5070 vim_free(buf_fname);
5071#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005072 return fname;
5073}
5074
5075 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01005076b0_magic_wrong(ZERO_BL *b0p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005077{
5078 return (b0p->b0_magic_long != (long)B0_MAGIC_LONG
5079 || b0p->b0_magic_int != (int)B0_MAGIC_INT
5080 || b0p->b0_magic_short != (short)B0_MAGIC_SHORT
5081 || b0p->b0_magic_char != B0_MAGIC_CHAR);
5082}
5083
5084#ifdef CHECK_INODE
5085/*
5086 * Compare current file name with file name from swap file.
5087 * Try to use inode numbers when possible.
5088 * Return non-zero when files are different.
5089 *
5090 * When comparing file names a few things have to be taken into consideration:
5091 * - When working over a network the full path of a file depends on the host.
5092 * We check the inode number if possible. It is not 100% reliable though,
5093 * because the device number cannot be used over a network.
5094 * - When a file does not exist yet (editing a new file) there is no inode
5095 * number.
5096 * - The file name in a swap file may not be valid on the current host. The
5097 * "~user" form is used whenever possible to avoid this.
5098 *
5099 * This is getting complicated, let's make a table:
5100 *
5101 * ino_c ino_s fname_c fname_s differ =
5102 *
5103 * both files exist -> compare inode numbers:
5104 * != 0 != 0 X X ino_c != ino_s
5105 *
5106 * inode number(s) unknown, file names available -> compare file names
5107 * == 0 X OK OK fname_c != fname_s
5108 * X == 0 OK OK fname_c != fname_s
5109 *
5110 * current file doesn't exist, file for swap file exist, file name(s) not
5111 * available -> probably different
5112 * == 0 != 0 FAIL X TRUE
5113 * == 0 != 0 X FAIL TRUE
5114 *
5115 * current file exists, inode for swap unknown, file name(s) not
5116 * available -> probably different
5117 * != 0 == 0 FAIL X TRUE
5118 * != 0 == 0 X FAIL TRUE
5119 *
5120 * current file doesn't exist, inode for swap unknown, one file name not
5121 * available -> probably different
5122 * == 0 == 0 FAIL OK TRUE
5123 * == 0 == 0 OK FAIL TRUE
5124 *
5125 * current file doesn't exist, inode for swap unknown, both file names not
Bram Moolenaar8c3169c2018-05-12 17:04:12 +02005126 * available -> compare file names
5127 * == 0 == 0 FAIL FAIL fname_c != fname_s
Bram Moolenaar071d4272004-06-13 20:20:40 +00005128 *
5129 * Note that when the ino_t is 64 bits, only the last 32 will be used. This
5130 * can't be changed without making the block 0 incompatible with 32 bit
5131 * versions.
5132 */
5133
5134 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01005135fnamecmp_ino(
5136 char_u *fname_c, /* current file name */
5137 char_u *fname_s, /* file name from swap file */
5138 long ino_block0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005139{
Bram Moolenaar8767f522016-07-01 17:17:39 +02005140 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005141 ino_t ino_c = 0; /* ino of current file */
5142 ino_t ino_s; /* ino of file from swap file */
5143 char_u buf_c[MAXPATHL]; /* full path of fname_c */
5144 char_u buf_s[MAXPATHL]; /* full path of fname_s */
5145 int retval_c; /* flag: buf_c valid */
5146 int retval_s; /* flag: buf_s valid */
5147
5148 if (mch_stat((char *)fname_c, &st) == 0)
5149 ino_c = (ino_t)st.st_ino;
5150
5151 /*
5152 * First we try to get the inode from the file name, because the inode in
5153 * the swap file may be outdated. If that fails (e.g. this path is not
5154 * valid on this machine), use the inode from block 0.
5155 */
5156 if (mch_stat((char *)fname_s, &st) == 0)
5157 ino_s = (ino_t)st.st_ino;
5158 else
5159 ino_s = (ino_t)ino_block0;
5160
5161 if (ino_c && ino_s)
5162 return (ino_c != ino_s);
5163
5164 /*
5165 * One of the inode numbers is unknown, try a forced vim_FullName() and
5166 * compare the file names.
5167 */
5168 retval_c = vim_FullName(fname_c, buf_c, MAXPATHL, TRUE);
5169 retval_s = vim_FullName(fname_s, buf_s, MAXPATHL, TRUE);
5170 if (retval_c == OK && retval_s == OK)
Bram Moolenaar8c3169c2018-05-12 17:04:12 +02005171 return STRCMP(buf_c, buf_s) != 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005172
5173 /*
5174 * Can't compare inodes or file names, guess that the files are different,
Bram Moolenaar8c3169c2018-05-12 17:04:12 +02005175 * unless both appear not to exist at all, then compare with the file name
5176 * in the swap file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005177 */
5178 if (ino_s == 0 && ino_c == 0 && retval_c == FAIL && retval_s == FAIL)
Bram Moolenaar8c3169c2018-05-12 17:04:12 +02005179 return STRCMP(fname_c, fname_s) != 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005180 return TRUE;
5181}
5182#endif /* CHECK_INODE */
5183
5184/*
5185 * Move a long integer into a four byte character array.
5186 * Used for machine independency in block zero.
5187 */
5188 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01005189long_to_char(long n, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005190{
5191 s[0] = (char_u)(n & 0xff);
5192 n = (unsigned)n >> 8;
5193 s[1] = (char_u)(n & 0xff);
5194 n = (unsigned)n >> 8;
5195 s[2] = (char_u)(n & 0xff);
5196 n = (unsigned)n >> 8;
5197 s[3] = (char_u)(n & 0xff);
5198}
5199
5200 static long
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01005201char_to_long(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005202{
5203 long retval;
5204
5205 retval = s[3];
5206 retval <<= 8;
5207 retval |= s[2];
5208 retval <<= 8;
5209 retval |= s[1];
5210 retval <<= 8;
5211 retval |= s[0];
5212
5213 return retval;
5214}
5215
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005216/*
5217 * Set the flags in the first block of the swap file:
5218 * - file is modified or not: buf->b_changed
5219 * - 'fileformat'
5220 * - 'fileencoding'
5221 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005222 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01005223ml_setflags(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005224{
5225 bhdr_T *hp;
5226 ZERO_BL *b0p;
5227
5228 if (!buf->b_ml.ml_mfp)
5229 return;
5230 for (hp = buf->b_ml.ml_mfp->mf_used_last; hp != NULL; hp = hp->bh_prev)
5231 {
5232 if (hp->bh_bnum == 0)
5233 {
5234 b0p = (ZERO_BL *)(hp->bh_data);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005235 b0p->b0_dirty = buf->b_changed ? B0_DIRTY : 0;
5236 b0p->b0_flags = (b0p->b0_flags & ~B0_FF_MASK)
5237 | (get_fileformat(buf) + 1);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005238 add_b0_fenc(b0p, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005239 hp->bh_flags |= BH_DIRTY;
5240 mf_sync(buf->b_ml.ml_mfp, MFS_ZERO);
5241 break;
5242 }
5243 }
5244}
5245
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02005246#if defined(FEAT_CRYPT) || defined(PROTO)
5247/*
5248 * If "data" points to a data block encrypt the text in it and return a copy
5249 * in allocated memory. Return NULL when out of memory.
5250 * Otherwise return "data".
5251 */
5252 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01005253ml_encrypt_data(
5254 memfile_T *mfp,
5255 char_u *data,
Bram Moolenaar8767f522016-07-01 17:17:39 +02005256 off_T offset,
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01005257 unsigned size)
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02005258{
5259 DATA_BL *dp = (DATA_BL *)data;
5260 char_u *head_end;
5261 char_u *text_start;
5262 char_u *new_data;
5263 int text_len;
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02005264 cryptstate_T *state;
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02005265
5266 if (dp->db_id != DATA_ID)
5267 return data;
5268
Bram Moolenaarbc563362015-06-09 18:35:25 +02005269 state = ml_crypt_prepare(mfp, offset, FALSE);
5270 if (state == NULL)
5271 return data;
5272
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005273 new_data = alloc(size);
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02005274 if (new_data == NULL)
5275 return NULL;
5276 head_end = (char_u *)(&dp->db_index[dp->db_line_count]);
5277 text_start = (char_u *)dp + dp->db_txt_start;
5278 text_len = size - dp->db_txt_start;
5279
5280 /* Copy the header and the text. */
5281 mch_memmove(new_data, dp, head_end - (char_u *)dp);
5282
5283 /* Encrypt the text. */
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02005284 crypt_encode(state, text_start, text_len, new_data + dp->db_txt_start);
5285 crypt_free_state(state);
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02005286
5287 /* Clear the gap. */
5288 if (head_end < text_start)
5289 vim_memset(new_data + (head_end - data), 0, text_start - head_end);
5290
5291 return new_data;
5292}
5293
5294/*
Bram Moolenaarbc563362015-06-09 18:35:25 +02005295 * Decrypt the text in "data" if it points to an encrypted data block.
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02005296 */
5297 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01005298ml_decrypt_data(
5299 memfile_T *mfp,
5300 char_u *data,
Bram Moolenaar8767f522016-07-01 17:17:39 +02005301 off_T offset,
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01005302 unsigned size)
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02005303{
5304 DATA_BL *dp = (DATA_BL *)data;
5305 char_u *head_end;
5306 char_u *text_start;
5307 int text_len;
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02005308 cryptstate_T *state;
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02005309
5310 if (dp->db_id == DATA_ID)
5311 {
5312 head_end = (char_u *)(&dp->db_index[dp->db_line_count]);
5313 text_start = (char_u *)dp + dp->db_txt_start;
5314 text_len = dp->db_txt_end - dp->db_txt_start;
5315
5316 if (head_end > text_start || dp->db_txt_start > size
5317 || dp->db_txt_end > size)
5318 return; /* data was messed up */
5319
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02005320 state = ml_crypt_prepare(mfp, offset, TRUE);
Bram Moolenaarbc563362015-06-09 18:35:25 +02005321 if (state != NULL)
5322 {
5323 /* Decrypt the text in place. */
5324 crypt_decode_inplace(state, text_start, text_len);
5325 crypt_free_state(state);
5326 }
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02005327 }
5328}
5329
5330/*
5331 * Prepare for encryption/decryption, using the key, seed and offset.
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02005332 * Return an allocated cryptstate_T *.
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02005333 */
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02005334 static cryptstate_T *
Bram Moolenaar8767f522016-07-01 17:17:39 +02005335ml_crypt_prepare(memfile_T *mfp, off_T offset, int reading)
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02005336{
5337 buf_T *buf = mfp->mf_buffer;
5338 char_u salt[50];
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02005339 int method_nr;
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02005340 char_u *key;
5341 char_u *seed;
5342
5343 if (reading && mfp->mf_old_key != NULL)
5344 {
5345 /* Reading back blocks with the previous key/method/seed. */
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02005346 method_nr = mfp->mf_old_cm;
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02005347 key = mfp->mf_old_key;
5348 seed = mfp->mf_old_seed;
5349 }
5350 else
5351 {
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02005352 method_nr = crypt_get_method_nr(buf);
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02005353 key = buf->b_p_key;
5354 seed = mfp->mf_seed;
5355 }
Bram Moolenaarbc563362015-06-09 18:35:25 +02005356 if (*key == NUL)
5357 return NULL;
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02005358
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02005359 if (method_nr == CRYPT_M_ZIP)
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02005360 {
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02005361 /* For PKzip: Append the offset to the key, so that we use a different
5362 * key for every block. */
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02005363 vim_snprintf((char *)salt, sizeof(salt), "%s%ld", key, (long)offset);
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02005364 return crypt_create(method_nr, salt, NULL, 0, NULL, 0);
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02005365 }
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02005366
5367 /* Using blowfish or better: add salt and seed. We use the byte offset
5368 * of the block for the salt. */
5369 vim_snprintf((char *)salt, sizeof(salt), "%ld", (long)offset);
5370 return crypt_create(method_nr, key, salt, (int)STRLEN(salt),
5371 seed, MF_SEED_LEN);
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02005372}
5373
5374#endif
5375
5376
Bram Moolenaar071d4272004-06-13 20:20:40 +00005377#if defined(FEAT_BYTEOFF) || defined(PROTO)
5378
5379#define MLCS_MAXL 800 /* max no of lines in chunk */
5380#define MLCS_MINL 400 /* should be half of MLCS_MAXL */
5381
5382/*
Bram Moolenaar0ad014c2010-07-25 14:00:46 +02005383 * Keep information for finding byte offset of a line, updtype may be one of:
Bram Moolenaar071d4272004-06-13 20:20:40 +00005384 * ML_CHNK_ADDLINE: Add len to parent chunk, possibly splitting it
5385 * Careful: ML_CHNK_ADDLINE may cause ml_find_line() to be called.
5386 * ML_CHNK_DELLINE: Subtract len from parent chunk, possibly deleting it
5387 * ML_CHNK_UPDLINE: Add len to parent chunk, as a signed entity.
5388 */
5389 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01005390ml_updatechunk(
5391 buf_T *buf,
5392 linenr_T line,
5393 long len,
5394 int updtype)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005395{
5396 static buf_T *ml_upd_lastbuf = NULL;
5397 static linenr_T ml_upd_lastline;
5398 static linenr_T ml_upd_lastcurline;
5399 static int ml_upd_lastcurix;
5400
5401 linenr_T curline = ml_upd_lastcurline;
5402 int curix = ml_upd_lastcurix;
5403 long size;
5404 chunksize_T *curchnk;
5405 int rest;
5406 bhdr_T *hp;
5407 DATA_BL *dp;
5408
5409 if (buf->b_ml.ml_usedchunks == -1 || len == 0)
5410 return;
5411 if (buf->b_ml.ml_chunksize == NULL)
5412 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005413 buf->b_ml.ml_chunksize = ALLOC_MULT(chunksize_T, 100);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005414 if (buf->b_ml.ml_chunksize == NULL)
5415 {
5416 buf->b_ml.ml_usedchunks = -1;
5417 return;
5418 }
5419 buf->b_ml.ml_numchunks = 100;
5420 buf->b_ml.ml_usedchunks = 1;
5421 buf->b_ml.ml_chunksize[0].mlcs_numlines = 1;
5422 buf->b_ml.ml_chunksize[0].mlcs_totalsize = 1;
5423 }
5424
5425 if (updtype == ML_CHNK_UPDLINE && buf->b_ml.ml_line_count == 1)
5426 {
5427 /*
5428 * First line in empty buffer from ml_flush_line() -- reset
5429 */
5430 buf->b_ml.ml_usedchunks = 1;
5431 buf->b_ml.ml_chunksize[0].mlcs_numlines = 1;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01005432 buf->b_ml.ml_chunksize[0].mlcs_totalsize = (long)buf->b_ml.ml_line_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005433 return;
5434 }
5435
5436 /*
5437 * Find chunk that our line belongs to, curline will be at start of the
5438 * chunk.
5439 */
5440 if (buf != ml_upd_lastbuf || line != ml_upd_lastline + 1
5441 || updtype != ML_CHNK_ADDLINE)
5442 {
5443 for (curline = 1, curix = 0;
5444 curix < buf->b_ml.ml_usedchunks - 1
5445 && line >= curline + buf->b_ml.ml_chunksize[curix].mlcs_numlines;
5446 curix++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005447 curline += buf->b_ml.ml_chunksize[curix].mlcs_numlines;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005448 }
Bram Moolenaara9a8e042018-10-30 22:15:55 +01005449 else if (curix < buf->b_ml.ml_usedchunks - 1
5450 && line >= curline + buf->b_ml.ml_chunksize[curix].mlcs_numlines)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005451 {
5452 /* Adjust cached curix & curline */
5453 curline += buf->b_ml.ml_chunksize[curix].mlcs_numlines;
5454 curix++;
5455 }
5456 curchnk = buf->b_ml.ml_chunksize + curix;
5457
5458 if (updtype == ML_CHNK_DELLINE)
Bram Moolenaar5a6404c2006-11-01 17:12:57 +00005459 len = -len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005460 curchnk->mlcs_totalsize += len;
5461 if (updtype == ML_CHNK_ADDLINE)
5462 {
5463 curchnk->mlcs_numlines++;
5464
5465 /* May resize here so we don't have to do it in both cases below */
5466 if (buf->b_ml.ml_usedchunks + 1 >= buf->b_ml.ml_numchunks)
5467 {
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01005468 chunksize_T *t_chunksize = buf->b_ml.ml_chunksize;
5469
Bram Moolenaar071d4272004-06-13 20:20:40 +00005470 buf->b_ml.ml_numchunks = buf->b_ml.ml_numchunks * 3 / 2;
5471 buf->b_ml.ml_chunksize = (chunksize_T *)
5472 vim_realloc(buf->b_ml.ml_chunksize,
5473 sizeof(chunksize_T) * buf->b_ml.ml_numchunks);
5474 if (buf->b_ml.ml_chunksize == NULL)
5475 {
5476 /* Hmmmm, Give up on offset for this buffer */
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01005477 vim_free(t_chunksize);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005478 buf->b_ml.ml_usedchunks = -1;
5479 return;
5480 }
5481 }
5482
5483 if (buf->b_ml.ml_chunksize[curix].mlcs_numlines >= MLCS_MAXL)
5484 {
5485 int count; /* number of entries in block */
5486 int idx;
Bram Moolenaarb413d2e2018-12-25 23:15:46 +01005487 int end_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005488 int text_end;
5489 int linecnt;
5490
5491 mch_memmove(buf->b_ml.ml_chunksize + curix + 1,
5492 buf->b_ml.ml_chunksize + curix,
5493 (buf->b_ml.ml_usedchunks - curix) *
5494 sizeof(chunksize_T));
Bram Moolenaar9439cdd2009-04-22 13:39:36 +00005495 /* Compute length of first half of lines in the split chunk */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005496 size = 0;
5497 linecnt = 0;
5498 while (curline < buf->b_ml.ml_line_count
5499 && linecnt < MLCS_MINL)
5500 {
5501 if ((hp = ml_find_line(buf, curline, ML_FIND)) == NULL)
5502 {
5503 buf->b_ml.ml_usedchunks = -1;
5504 return;
5505 }
5506 dp = (DATA_BL *)(hp->bh_data);
5507 count = (long)(buf->b_ml.ml_locked_high) -
5508 (long)(buf->b_ml.ml_locked_low) + 1;
5509 idx = curline - buf->b_ml.ml_locked_low;
5510 curline = buf->b_ml.ml_locked_high + 1;
Bram Moolenaarb413d2e2018-12-25 23:15:46 +01005511
5512 // compute index of last line to use in this MEMLINE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005513 rest = count - idx;
5514 if (linecnt + rest > MLCS_MINL)
5515 {
Bram Moolenaarb413d2e2018-12-25 23:15:46 +01005516 end_idx = idx + MLCS_MINL - linecnt - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005517 linecnt = MLCS_MINL;
5518 }
5519 else
5520 {
Bram Moolenaarb413d2e2018-12-25 23:15:46 +01005521 end_idx = count - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005522 linecnt += rest;
5523 }
Bram Moolenaarb413d2e2018-12-25 23:15:46 +01005524#ifdef FEAT_TEXT_PROP
5525 if (buf->b_has_textprop)
5526 {
5527 int i;
5528
5529 // We cannot use the text pointers to get the text length,
5530 // the text prop info would also be counted. Go over the
5531 // lines.
5532 for (i = end_idx; i < idx; ++i)
Bram Moolenaar4b7214e2019-01-03 21:55:32 +01005533 size += (int)STRLEN((char_u *)dp + (dp->db_index[i] & DB_INDEX_MASK)) + 1;
Bram Moolenaarb413d2e2018-12-25 23:15:46 +01005534 }
5535 else
5536#endif
5537 {
5538 if (idx == 0)/* first line in block, text at the end */
5539 text_end = dp->db_txt_end;
5540 else
5541 text_end = ((dp->db_index[idx - 1]) & DB_INDEX_MASK);
5542 size += text_end - ((dp->db_index[end_idx]) & DB_INDEX_MASK);
5543 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005544 }
5545 buf->b_ml.ml_chunksize[curix].mlcs_numlines = linecnt;
5546 buf->b_ml.ml_chunksize[curix + 1].mlcs_numlines -= linecnt;
5547 buf->b_ml.ml_chunksize[curix].mlcs_totalsize = size;
5548 buf->b_ml.ml_chunksize[curix + 1].mlcs_totalsize -= size;
5549 buf->b_ml.ml_usedchunks++;
5550 ml_upd_lastbuf = NULL; /* Force recalc of curix & curline */
5551 return;
5552 }
5553 else if (buf->b_ml.ml_chunksize[curix].mlcs_numlines >= MLCS_MINL
5554 && curix == buf->b_ml.ml_usedchunks - 1
5555 && buf->b_ml.ml_line_count - line <= 1)
5556 {
5557 /*
5558 * We are in the last chunk and it is cheap to crate a new one
5559 * after this. Do it now to avoid the loop above later on
5560 */
5561 curchnk = buf->b_ml.ml_chunksize + curix + 1;
5562 buf->b_ml.ml_usedchunks++;
5563 if (line == buf->b_ml.ml_line_count)
5564 {
5565 curchnk->mlcs_numlines = 0;
5566 curchnk->mlcs_totalsize = 0;
5567 }
5568 else
5569 {
5570 /*
5571 * Line is just prior to last, move count for last
5572 * This is the common case when loading a new file
5573 */
5574 hp = ml_find_line(buf, buf->b_ml.ml_line_count, ML_FIND);
5575 if (hp == NULL)
5576 {
5577 buf->b_ml.ml_usedchunks = -1;
5578 return;
5579 }
5580 dp = (DATA_BL *)(hp->bh_data);
5581 if (dp->db_line_count == 1)
5582 rest = dp->db_txt_end - dp->db_txt_start;
5583 else
5584 rest =
5585 ((dp->db_index[dp->db_line_count - 2]) & DB_INDEX_MASK)
5586 - dp->db_txt_start;
5587 curchnk->mlcs_totalsize = rest;
5588 curchnk->mlcs_numlines = 1;
5589 curchnk[-1].mlcs_totalsize -= rest;
5590 curchnk[-1].mlcs_numlines -= 1;
5591 }
5592 }
5593 }
5594 else if (updtype == ML_CHNK_DELLINE)
5595 {
5596 curchnk->mlcs_numlines--;
5597 ml_upd_lastbuf = NULL; /* Force recalc of curix & curline */
5598 if (curix < (buf->b_ml.ml_usedchunks - 1)
5599 && (curchnk->mlcs_numlines + curchnk[1].mlcs_numlines)
5600 <= MLCS_MINL)
5601 {
5602 curix++;
5603 curchnk = buf->b_ml.ml_chunksize + curix;
5604 }
5605 else if (curix == 0 && curchnk->mlcs_numlines <= 0)
5606 {
5607 buf->b_ml.ml_usedchunks--;
5608 mch_memmove(buf->b_ml.ml_chunksize, buf->b_ml.ml_chunksize + 1,
5609 buf->b_ml.ml_usedchunks * sizeof(chunksize_T));
5610 return;
5611 }
5612 else if (curix == 0 || (curchnk->mlcs_numlines > 10
5613 && (curchnk->mlcs_numlines + curchnk[-1].mlcs_numlines)
5614 > MLCS_MINL))
5615 {
5616 return;
5617 }
5618
5619 /* Collapse chunks */
5620 curchnk[-1].mlcs_numlines += curchnk->mlcs_numlines;
5621 curchnk[-1].mlcs_totalsize += curchnk->mlcs_totalsize;
5622 buf->b_ml.ml_usedchunks--;
5623 if (curix < buf->b_ml.ml_usedchunks)
5624 {
5625 mch_memmove(buf->b_ml.ml_chunksize + curix,
5626 buf->b_ml.ml_chunksize + curix + 1,
5627 (buf->b_ml.ml_usedchunks - curix) *
5628 sizeof(chunksize_T));
5629 }
5630 return;
5631 }
5632 ml_upd_lastbuf = buf;
5633 ml_upd_lastline = line;
5634 ml_upd_lastcurline = curline;
5635 ml_upd_lastcurix = curix;
5636}
5637
5638/*
5639 * Find offset for line or line with offset.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00005640 * Find line with offset if "lnum" is 0; return remaining offset in offp
5641 * Find offset of line if "lnum" > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005642 * return -1 if information is not available
5643 */
5644 long
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01005645ml_find_line_or_offset(buf_T *buf, linenr_T lnum, long *offp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005646{
5647 linenr_T curline;
5648 int curix;
5649 long size;
5650 bhdr_T *hp;
5651 DATA_BL *dp;
5652 int count; /* number of entries in block */
5653 int idx;
5654 int start_idx;
5655 int text_end;
5656 long offset;
5657 int len;
5658 int ffdos = (get_fileformat(buf) == EOL_DOS);
5659 int extra = 0;
5660
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00005661 /* take care of cached line first */
5662 ml_flush_line(curbuf);
5663
Bram Moolenaar071d4272004-06-13 20:20:40 +00005664 if (buf->b_ml.ml_usedchunks == -1
5665 || buf->b_ml.ml_chunksize == NULL
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00005666 || lnum < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005667 return -1;
5668
5669 if (offp == NULL)
5670 offset = 0;
5671 else
5672 offset = *offp;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00005673 if (lnum == 0 && offset <= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005674 return 1; /* Not a "find offset" and offset 0 _must_ be in line 1 */
5675 /*
5676 * Find the last chunk before the one containing our line. Last chunk is
5677 * special because it will never qualify
5678 */
5679 curline = 1;
5680 curix = size = 0;
5681 while (curix < buf->b_ml.ml_usedchunks - 1
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00005682 && ((lnum != 0
5683 && lnum >= curline + buf->b_ml.ml_chunksize[curix].mlcs_numlines)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005684 || (offset != 0
5685 && offset > size + buf->b_ml.ml_chunksize[curix].mlcs_totalsize
5686 + ffdos * buf->b_ml.ml_chunksize[curix].mlcs_numlines)))
5687 {
5688 curline += buf->b_ml.ml_chunksize[curix].mlcs_numlines;
5689 size += buf->b_ml.ml_chunksize[curix].mlcs_totalsize;
5690 if (offset && ffdos)
5691 size += buf->b_ml.ml_chunksize[curix].mlcs_numlines;
5692 curix++;
5693 }
5694
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00005695 while ((lnum != 0 && curline < lnum) || (offset != 0 && size < offset))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005696 {
5697 if (curline > buf->b_ml.ml_line_count
5698 || (hp = ml_find_line(buf, curline, ML_FIND)) == NULL)
5699 return -1;
5700 dp = (DATA_BL *)(hp->bh_data);
5701 count = (long)(buf->b_ml.ml_locked_high) -
5702 (long)(buf->b_ml.ml_locked_low) + 1;
5703 start_idx = idx = curline - buf->b_ml.ml_locked_low;
5704 if (idx == 0)/* first line in block, text at the end */
5705 text_end = dp->db_txt_end;
5706 else
5707 text_end = ((dp->db_index[idx - 1]) & DB_INDEX_MASK);
5708 /* Compute index of last line to use in this MEMLINE */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00005709 if (lnum != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005710 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00005711 if (curline + (count - idx) >= lnum)
5712 idx += lnum - curline - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005713 else
5714 idx = count - 1;
5715 }
5716 else
5717 {
5718 extra = 0;
5719 while (offset >= size
5720 + text_end - (int)((dp->db_index[idx]) & DB_INDEX_MASK)
5721 + ffdos)
5722 {
5723 if (ffdos)
5724 size++;
5725 if (idx == count - 1)
5726 {
5727 extra = 1;
5728 break;
5729 }
5730 idx++;
5731 }
5732 }
Bram Moolenaarb413d2e2018-12-25 23:15:46 +01005733#ifdef FEAT_TEXT_PROP
5734 if (buf->b_has_textprop)
5735 {
5736 int i;
5737
5738 // cannot use the db_index pointer, need to get the actual text
5739 // lengths.
5740 len = 0;
5741 for (i = start_idx; i <= idx; ++i)
Bram Moolenaar4b7214e2019-01-03 21:55:32 +01005742 len += (int)STRLEN((char_u *)dp + ((dp->db_index[i]) & DB_INDEX_MASK)) + 1;
Bram Moolenaarb413d2e2018-12-25 23:15:46 +01005743 }
5744 else
5745#endif
5746 len = text_end - ((dp->db_index[idx]) & DB_INDEX_MASK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005747 size += len;
5748 if (offset != 0 && size >= offset)
5749 {
5750 if (size + ffdos == offset)
5751 *offp = 0;
5752 else if (idx == start_idx)
5753 *offp = offset - size + len;
5754 else
5755 *offp = offset - size + len
5756 - (text_end - ((dp->db_index[idx - 1]) & DB_INDEX_MASK));
5757 curline += idx - start_idx + extra;
5758 if (curline > buf->b_ml.ml_line_count)
5759 return -1; /* exactly one byte beyond the end */
5760 return curline;
5761 }
5762 curline = buf->b_ml.ml_locked_high + 1;
5763 }
5764
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00005765 if (lnum != 0)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005766 {
5767 /* Count extra CR characters. */
5768 if (ffdos)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00005769 size += lnum - 1;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005770
Bram Moolenaar34d72d42015-07-17 14:18:08 +02005771 /* Don't count the last line break if 'noeol' and ('bin' or
5772 * 'nofixeol'). */
5773 if ((!buf->b_p_fixeol || buf->b_p_bin) && !buf->b_p_eol
Bram Moolenaarc26f7c62018-08-20 22:53:04 +02005774 && lnum > buf->b_ml.ml_line_count)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005775 size -= ffdos + 1;
5776 }
5777
Bram Moolenaar071d4272004-06-13 20:20:40 +00005778 return size;
5779}
5780
5781/*
5782 * Goto byte in buffer with offset 'cnt'.
5783 */
5784 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01005785goto_byte(long cnt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005786{
5787 long boff = cnt;
5788 linenr_T lnum;
5789
5790 ml_flush_line(curbuf); /* cached line may be dirty */
5791 setpcmark();
5792 if (boff)
5793 --boff;
5794 lnum = ml_find_line_or_offset(curbuf, (linenr_T)0, &boff);
5795 if (lnum < 1) /* past the end */
5796 {
5797 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
5798 curwin->w_curswant = MAXCOL;
5799 coladvance((colnr_T)MAXCOL);
5800 }
5801 else
5802 {
5803 curwin->w_cursor.lnum = lnum;
5804 curwin->w_cursor.col = (colnr_T)boff;
Bram Moolenaar943d2b52005-12-02 00:50:49 +00005805 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005806 curwin->w_set_curswant = TRUE;
5807 }
5808 check_cursor();
5809
Bram Moolenaar071d4272004-06-13 20:20:40 +00005810 /* Make sure the cursor is on the first byte of a multi-byte char. */
5811 if (has_mbyte)
5812 mb_adjust_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005813}
5814#endif