/
memline.c
4867 lines (4483 loc) · 129 KB
/
memline.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* vi:set ts=8 sts=4 sw=4:
*
* VIM - Vi IMproved by Bram Moolenaar
*
* Do ":help uganda" in Vim to read copying and usage conditions.
* Do ":help credits" in Vim to see a list of people who contributed.
* See README.txt for an overview of the Vim source code.
*/
/* for debugging */
/* #define CHECK(c, s) if (c) EMSG(s) */
#define CHECK(c, s)
/*
* memline.c: Contains the functions for appending, deleting and changing the
* text lines. The memfile functions are used to store the information in
* blocks of memory, backed up by a file. The structure of the information is
* a tree. The root of the tree is a pointer block. The leaves of the tree
* are data blocks. In between may be several layers of pointer blocks,
* forming branches.
*
* Three types of blocks are used:
* - Block nr 0 contains information for recovery
* - Pointer blocks contain list of pointers to other blocks.
* - Data blocks contain the actual text.
*
* Block nr 0 contains the block0 structure (see below).
*
* Block nr 1 is the first pointer block. It is the root of the tree.
* Other pointer blocks are branches.
*
* If a line is too big to fit in a single page, the block containing that
* line is made big enough to hold the line. It may span several pages.
* Otherwise all blocks are one page.
*
* A data block that was filled when starting to edit a file and was not
* changed since then, can have a negative block number. This means that it
* has not yet been assigned a place in the file. When recovering, the lines
* in this data block can be read from the original file. When the block is
* changed (lines appended/deleted/changed) or when it is flushed it gets a
* positive number. Use mf_trans_del() to get the new number, before calling
* mf_get().
*/
#if defined(MSDOS) || defined(WIN16) || defined(WIN32) || defined(_WIN64)
# include "vimio.h" /* for mch_open(), must be before vim.h */
#endif
#include "vim.h"
#ifndef UNIX /* it's in os_unix.h for Unix */
# include <time.h>
#endif
#if defined(SASC) || defined(__amigaos4__)
# include <proto/dos.h> /* for Open() and Close() */
#endif
#ifdef HAVE_ERRNO_H
# include <errno.h>
#endif
typedef struct block0 ZERO_BL; /* contents of the first block */
typedef struct pointer_block PTR_BL; /* contents of a pointer block */
typedef struct data_block DATA_BL; /* contents of a data block */
typedef struct pointer_entry PTR_EN; /* block/line-count pair */
#define DATA_ID (('d' << 8) + 'a') /* data block id */
#define PTR_ID (('p' << 8) + 't') /* pointer block id */
#define BLOCK0_ID0 'b' /* block 0 id 0 */
#define BLOCK0_ID1 '0' /* block 0 id 1 */
/*
* pointer to a block, used in a pointer block
*/
struct pointer_entry
{
blocknr_T pe_bnum; /* block number */
linenr_T pe_line_count; /* number of lines in this branch */
linenr_T pe_old_lnum; /* lnum for this block (for recovery) */
int pe_page_count; /* number of pages in block pe_bnum */
};
/*
* A pointer block contains a list of branches in the tree.
*/
struct pointer_block
{
short_u pb_id; /* ID for pointer block: PTR_ID */
short_u pb_count; /* number of pointer in this block */
short_u pb_count_max; /* maximum value for pb_count */
PTR_EN pb_pointer[1]; /* list of pointers to blocks (actually longer)
* followed by empty space until end of page */
};
/*
* A data block is a leaf in the tree.
*
* The text of the lines is at the end of the block. The text of the first line
* in the block is put at the end, the text of the second line in front of it,
* etc. Thus the order of the lines is the opposite of the line number.
*/
struct data_block
{
short_u db_id; /* ID for data block: DATA_ID */
unsigned db_free; /* free space available */
unsigned db_txt_start; /* byte where text starts */
unsigned db_txt_end; /* byte just after data block */
linenr_T db_line_count; /* number of lines in this block */
unsigned db_index[1]; /* index for start of line (actually bigger)
* followed by empty space upto db_txt_start
* followed by the text in the lines until
* end of page */
};
/*
* The low bits of db_index hold the actual index. The topmost bit is
* used for the global command to be able to mark a line.
* This method is not clean, but otherwise there would be at least one extra
* byte used for each line.
* The mark has to be in this place to keep it with the correct line when other
* lines are inserted or deleted.
*/
#define DB_MARKED ((unsigned)1 << ((sizeof(unsigned) * 8) - 1))
#define DB_INDEX_MASK (~DB_MARKED)
#define INDEX_SIZE (sizeof(unsigned)) /* size of one db_index entry */
#define HEADER_SIZE (sizeof(DATA_BL) - INDEX_SIZE) /* size of data block header */
#define B0_FNAME_SIZE_ORG 900 /* what it was in older versions */
#define B0_FNAME_SIZE 898
#define B0_UNAME_SIZE 40
#define B0_HNAME_SIZE 40
/*
* Restrict the numbers to 32 bits, otherwise most compilers will complain.
* This won't detect a 64 bit machine that only swaps a byte in the top 32
* bits, but that is crazy anyway.
*/
#define B0_MAGIC_LONG 0x30313233L
#define B0_MAGIC_INT 0x20212223L
#define B0_MAGIC_SHORT 0x10111213L
#define B0_MAGIC_CHAR 0x55
/*
* Block zero holds all info about the swap file.
*
* NOTE: DEFINITION OF BLOCK 0 SHOULD NOT CHANGE! It would make all existing
* swap files unusable!
*
* If size of block0 changes anyway, adjust MIN_SWAP_PAGE_SIZE in vim.h!!
*
* This block is built up of single bytes, to make it portable across
* different machines. b0_magic_* is used to check the byte order and size of
* variables, because the rest of the swap file is not portable.
*/
struct block0
{
char_u b0_id[2]; /* id for block 0: BLOCK0_ID0 and BLOCK0_ID1 */
char_u b0_version[10]; /* Vim version string */
char_u b0_page_size[4];/* number of bytes per page */
char_u b0_mtime[4]; /* last modification time of file */
char_u b0_ino[4]; /* inode of b0_fname */
char_u b0_pid[4]; /* process id of creator (or 0) */
char_u b0_uname[B0_UNAME_SIZE]; /* name of user (uid if no name) */
char_u b0_hname[B0_HNAME_SIZE]; /* host name (if it has a name) */
char_u b0_fname[B0_FNAME_SIZE_ORG]; /* name of file being edited */
long b0_magic_long; /* check for byte order of long */
int b0_magic_int; /* check for byte order of int */
short b0_magic_short; /* check for byte order of short */
char_u b0_magic_char; /* check for last char */
};
/*
* Note: b0_dirty and b0_flags are put at the end of the file name. For very
* long file names in older versions of Vim they are invalid.
* The 'fileencoding' comes before b0_flags, with a NUL in front. But only
* when there is room, for very long file names it's omitted.
*/
#define B0_DIRTY 0x55
#define b0_dirty b0_fname[B0_FNAME_SIZE_ORG-1]
/*
* The b0_flags field is new in Vim 7.0.
*/
#define b0_flags b0_fname[B0_FNAME_SIZE_ORG-2]
/* The lowest two bits contain the fileformat. Zero means it's not set
* (compatible with Vim 6.x), otherwise it's EOL_UNIX + 1, EOL_DOS + 1 or
* EOL_MAC + 1. */
#define B0_FF_MASK 3
/* Swap file is in directory of edited file. Used to find the file from
* different mount points. */
#define B0_SAME_DIR 4
/* The 'fileencoding' is at the end of b0_fname[], with a NUL in front of it.
* When empty there is only the NUL. */
#define B0_HAS_FENC 8
#define STACK_INCR 5 /* nr of entries added to ml_stack at a time */
/*
* The line number where the first mark may be is remembered.
* If it is 0 there are no marks at all.
* (always used for the current buffer only, no buffer change possible while
* executing a global command).
*/
static linenr_T lowest_marked = 0;
/*
* arguments for ml_find_line()
*/
#define ML_DELETE 0x11 /* delete line */
#define ML_INSERT 0x12 /* insert line */
#define ML_FIND 0x13 /* just find the line */
#define ML_FLUSH 0x02 /* flush locked block */
#define ML_SIMPLE(x) (x & 0x10) /* DEL, INS or FIND */
static void ml_upd_block0 __ARGS((buf_T *buf, int set_fname));
static void set_b0_fname __ARGS((ZERO_BL *, buf_T *buf));
static void set_b0_dir_flag __ARGS((ZERO_BL *b0p, buf_T *buf));
#ifdef FEAT_MBYTE
static void add_b0_fenc __ARGS((ZERO_BL *b0p, buf_T *buf));
#endif
static time_t swapfile_info __ARGS((char_u *));
static int recov_file_names __ARGS((char_u **, char_u *, int prepend_dot));
static int ml_append_int __ARGS((buf_T *, linenr_T, char_u *, colnr_T, int, int));
static int ml_delete_int __ARGS((buf_T *, linenr_T, int));
static char_u *findswapname __ARGS((buf_T *, char_u **, char_u *));
static void ml_flush_line __ARGS((buf_T *));
static bhdr_T *ml_new_data __ARGS((memfile_T *, int, int));
static bhdr_T *ml_new_ptr __ARGS((memfile_T *));
static bhdr_T *ml_find_line __ARGS((buf_T *, linenr_T, int));
static int ml_add_stack __ARGS((buf_T *));
static void ml_lineadd __ARGS((buf_T *, int));
static int b0_magic_wrong __ARGS((ZERO_BL *));
#ifdef CHECK_INODE
static int fnamecmp_ino __ARGS((char_u *, char_u *, long));
#endif
static void long_to_char __ARGS((long, char_u *));
static long char_to_long __ARGS((char_u *));
#if defined(UNIX) || defined(WIN3264)
static char_u *make_percent_swname __ARGS((char_u *dir, char_u *name));
#endif
#ifdef FEAT_BYTEOFF
static void ml_updatechunk __ARGS((buf_T *buf, long line, long len, int updtype));
#endif
/*
* Open a new memline for "buf".
*
* Return FAIL for failure, OK otherwise.
*/
int
ml_open(buf)
buf_T *buf;
{
memfile_T *mfp;
bhdr_T *hp = NULL;
ZERO_BL *b0p;
PTR_BL *pp;
DATA_BL *dp;
/*
* init fields in memline struct
*/
buf->b_ml.ml_stack_size = 0; /* no stack yet */
buf->b_ml.ml_stack = NULL; /* no stack yet */
buf->b_ml.ml_stack_top = 0; /* nothing in the stack */
buf->b_ml.ml_locked = NULL; /* no cached block */
buf->b_ml.ml_line_lnum = 0; /* no cached line */
#ifdef FEAT_BYTEOFF
buf->b_ml.ml_chunksize = NULL;
#endif
/*
* When 'updatecount' is non-zero swap file may be opened later.
*/
if (p_uc && buf->b_p_swf)
buf->b_may_swap = TRUE;
else
buf->b_may_swap = FALSE;
/*
* Open the memfile. No swap file is created yet.
*/
mfp = mf_open(NULL, 0);
if (mfp == NULL)
goto error;
buf->b_ml.ml_mfp = mfp;
buf->b_ml.ml_flags = ML_EMPTY;
buf->b_ml.ml_line_count = 1;
#ifdef FEAT_LINEBREAK
curwin->w_nrwidth_line_count = 0;
#endif
#if defined(MSDOS) && !defined(DJGPP)
/* for 16 bit MS-DOS create a swapfile now, because we run out of
* memory very quickly */
if (p_uc != 0)
ml_open_file(buf);
#endif
/*
* fill block0 struct and write page 0
*/
if ((hp = mf_new(mfp, FALSE, 1)) == NULL)
goto error;
if (hp->bh_bnum != 0)
{
EMSG(_("E298: Didn't get block nr 0?"));
goto error;
}
b0p = (ZERO_BL *)(hp->bh_data);
b0p->b0_id[0] = BLOCK0_ID0;
b0p->b0_id[1] = BLOCK0_ID1;
b0p->b0_magic_long = (long)B0_MAGIC_LONG;
b0p->b0_magic_int = (int)B0_MAGIC_INT;
b0p->b0_magic_short = (short)B0_MAGIC_SHORT;
b0p->b0_magic_char = B0_MAGIC_CHAR;
STRNCPY(b0p->b0_version, "VIM ", 4);
STRNCPY(b0p->b0_version + 4, Version, 6);
long_to_char((long)mfp->mf_page_size, b0p->b0_page_size);
#ifdef FEAT_SPELL
if (!buf->b_spell)
#endif
{
b0p->b0_dirty = buf->b_changed ? B0_DIRTY : 0;
b0p->b0_flags = get_fileformat(buf) + 1;
set_b0_fname(b0p, buf);
(void)get_user_name(b0p->b0_uname, B0_UNAME_SIZE);
b0p->b0_uname[B0_UNAME_SIZE - 1] = NUL;
mch_get_host_name(b0p->b0_hname, B0_HNAME_SIZE);
b0p->b0_hname[B0_HNAME_SIZE - 1] = NUL;
long_to_char(mch_get_pid(), b0p->b0_pid);
}
/*
* Always sync block number 0 to disk, so we can check the file name in
* the swap file in findswapname(). Don't do this for help files though
* and spell buffer though.
* Only works when there's a swapfile, otherwise it's done when the file
* is created.
*/
mf_put(mfp, hp, TRUE, FALSE);
if (!buf->b_help && !B_SPELL(buf))
(void)mf_sync(mfp, 0);
/*
* Fill in root pointer block and write page 1.
*/
if ((hp = ml_new_ptr(mfp)) == NULL)
goto error;
if (hp->bh_bnum != 1)
{
EMSG(_("E298: Didn't get block nr 1?"));
goto error;
}
pp = (PTR_BL *)(hp->bh_data);
pp->pb_count = 1;
pp->pb_pointer[0].pe_bnum = 2;
pp->pb_pointer[0].pe_page_count = 1;
pp->pb_pointer[0].pe_old_lnum = 1;
pp->pb_pointer[0].pe_line_count = 1; /* line count after insertion */
mf_put(mfp, hp, TRUE, FALSE);
/*
* Allocate first data block and create an empty line 1.
*/
if ((hp = ml_new_data(mfp, FALSE, 1)) == NULL)
goto error;
if (hp->bh_bnum != 2)
{
EMSG(_("E298: Didn't get block nr 2?"));
goto error;
}
dp = (DATA_BL *)(hp->bh_data);
dp->db_index[0] = --dp->db_txt_start; /* at end of block */
dp->db_free -= 1 + INDEX_SIZE;
dp->db_line_count = 1;
*((char_u *)dp + dp->db_txt_start) = NUL; /* emtpy line */
return OK;
error:
if (mfp != NULL)
{
if (hp)
mf_put(mfp, hp, FALSE, FALSE);
mf_close(mfp, TRUE); /* will also free(mfp->mf_fname) */
}
buf->b_ml.ml_mfp = NULL;
return FAIL;
}
/*
* ml_setname() is called when the file name of "buf" has been changed.
* It may rename the swap file.
*/
void
ml_setname(buf)
buf_T *buf;
{
int success = FALSE;
memfile_T *mfp;
char_u *fname;
char_u *dirp;
#if defined(MSDOS) || defined(MSWIN)
char_u *p;
#endif
mfp = buf->b_ml.ml_mfp;
if (mfp->mf_fd < 0) /* there is no swap file yet */
{
/*
* When 'updatecount' is 0 and 'noswapfile' there is no swap file.
* For help files we will make a swap file now.
*/
if (p_uc != 0)
ml_open_file(buf); /* create a swap file */
return;
}
/*
* Try all directories in the 'directory' option.
*/
dirp = p_dir;
for (;;)
{
if (*dirp == NUL) /* tried all directories, fail */
break;
fname = findswapname(buf, &dirp, mfp->mf_fname);
/* alloc's fname */
if (fname == NULL) /* no file name found for this dir */
continue;
#if defined(MSDOS) || defined(MSWIN)
/*
* Set full pathname for swap file now, because a ":!cd dir" may
* change directory without us knowing it.
*/
p = FullName_save(fname, FALSE);
vim_free(fname);
fname = p;
if (fname == NULL)
continue;
#endif
/* if the file name is the same we don't have to do anything */
if (fnamecmp(fname, mfp->mf_fname) == 0)
{
vim_free(fname);
success = TRUE;
break;
}
/* need to close the swap file before renaming */
if (mfp->mf_fd >= 0)
{
close(mfp->mf_fd);
mfp->mf_fd = -1;
}
/* try to rename the swap file */
if (vim_rename(mfp->mf_fname, fname) == 0)
{
success = TRUE;
vim_free(mfp->mf_fname);
mfp->mf_fname = fname;
vim_free(mfp->mf_ffname);
#if defined(MSDOS) || defined(MSWIN)
mfp->mf_ffname = NULL; /* mf_fname is full pathname already */
#else
mf_set_ffname(mfp);
#endif
ml_upd_block0(buf, FALSE);
break;
}
vim_free(fname); /* this fname didn't work, try another */
}
if (mfp->mf_fd == -1) /* need to (re)open the swap file */
{
mfp->mf_fd = mch_open((char *)mfp->mf_fname, O_RDWR | O_EXTRA, 0);
if (mfp->mf_fd < 0)
{
/* could not (re)open the swap file, what can we do???? */
EMSG(_("E301: Oops, lost the swap file!!!"));
return;
}
}
if (!success)
EMSG(_("E302: Could not rename swap file"));
}
/*
* Open a file for the memfile for all buffers that are not readonly or have
* been modified.
* Used when 'updatecount' changes from zero to non-zero.
*/
void
ml_open_files()
{
buf_T *buf;
for (buf = firstbuf; buf != NULL; buf = buf->b_next)
if (!buf->b_p_ro || buf->b_changed)
ml_open_file(buf);
}
/*
* Open a swap file for an existing memfile, if there is no swap file yet.
* If we are unable to find a file name, mf_fname will be NULL
* and the memfile will be in memory only (no recovery possible).
*/
void
ml_open_file(buf)
buf_T *buf;
{
memfile_T *mfp;
char_u *fname;
char_u *dirp;
mfp = buf->b_ml.ml_mfp;
if (mfp == NULL || mfp->mf_fd >= 0 || !buf->b_p_swf)
return; /* nothing to do */
#ifdef FEAT_SPELL
/* For a spell buffer use a temp file name. */
if (buf->b_spell)
{
fname = vim_tempname('s');
if (fname != NULL)
(void)mf_open_file(mfp, fname); /* consumes fname! */
buf->b_may_swap = FALSE;
return;
}
#endif
/*
* Try all directories in 'directory' option.
*/
dirp = p_dir;
for (;;)
{
if (*dirp == NUL)
break;
/* There is a small chance that between chosing the swap file name and
* creating it, another Vim creates the file. In that case the
* creation will fail and we will use another directory. */
fname = findswapname(buf, &dirp, NULL); /* allocates fname */
if (fname == NULL)
continue;
if (mf_open_file(mfp, fname) == OK) /* consumes fname! */
{
#if defined(MSDOS) || defined(MSWIN) || defined(RISCOS)
/*
* set full pathname for swap file now, because a ":!cd dir" may
* change directory without us knowing it.
*/
mf_fullname(mfp);
#endif
ml_upd_block0(buf, FALSE);
/* Flush block zero, so others can read it */
if (mf_sync(mfp, MFS_ZERO) == OK)
{
/* Mark all blocks that should be in the swapfile as dirty.
* Needed for when the 'swapfile' option was reset, so that
* the swap file was deleted, and then on again. */
mf_set_dirty(mfp);
break;
}
/* Writing block 0 failed: close the file and try another dir */
mf_close_file(buf, FALSE);
}
}
if (mfp->mf_fname == NULL) /* Failed! */
{
need_wait_return = TRUE; /* call wait_return later */
++no_wait_return;
(void)EMSG2(_("E303: Unable to open swap file for \"%s\", recovery impossible"),
buf_spname(buf) != NULL
? (char_u *)buf_spname(buf)
: buf->b_fname);
--no_wait_return;
}
/* don't try to open a swap file again */
buf->b_may_swap = FALSE;
}
/*
* If still need to create a swap file, and starting to edit a not-readonly
* file, or reading into an existing buffer, create a swap file now.
*/
void
check_need_swap(newfile)
int newfile; /* reading file into new buffer */
{
if (curbuf->b_may_swap && (!curbuf->b_p_ro || !newfile))
ml_open_file(curbuf);
}
/*
* Close memline for buffer 'buf'.
* If 'del_file' is TRUE, delete the swap file
*/
void
ml_close(buf, del_file)
buf_T *buf;
int del_file;
{
if (buf->b_ml.ml_mfp == NULL) /* not open */
return;
mf_close(buf->b_ml.ml_mfp, del_file); /* close the .swp file */
if (buf->b_ml.ml_line_lnum != 0 && (buf->b_ml.ml_flags & ML_LINE_DIRTY))
vim_free(buf->b_ml.ml_line_ptr);
vim_free(buf->b_ml.ml_stack);
#ifdef FEAT_BYTEOFF
vim_free(buf->b_ml.ml_chunksize);
buf->b_ml.ml_chunksize = NULL;
#endif
buf->b_ml.ml_mfp = NULL;
/* Reset the "recovered" flag, give the ATTENTION prompt the next time
* this buffer is loaded. */
buf->b_flags &= ~BF_RECOVERED;
}
/*
* Close all existing memlines and memfiles.
* Only used when exiting.
* When 'del_file' is TRUE, delete the memfiles.
* But don't delete files that were ":preserve"d when we are POSIX compatible.
*/
void
ml_close_all(del_file)
int del_file;
{
buf_T *buf;
for (buf = firstbuf; buf != NULL; buf = buf->b_next)
ml_close(buf, del_file && ((buf->b_flags & BF_PRESERVED) == 0
|| vim_strchr(p_cpo, CPO_PRESERVE) == NULL));
#ifdef TEMPDIRNAMES
vim_deltempdir(); /* delete created temp directory */
#endif
}
/*
* Close all memfiles for not modified buffers.
* Only use just before exiting!
*/
void
ml_close_notmod()
{
buf_T *buf;
for (buf = firstbuf; buf != NULL; buf = buf->b_next)
if (!bufIsChanged(buf))
ml_close(buf, TRUE); /* close all not-modified buffers */
}
/*
* Update the timestamp in the .swp file.
* Used when the file has been written.
*/
void
ml_timestamp(buf)
buf_T *buf;
{
ml_upd_block0(buf, TRUE);
}
/*
* Update the timestamp or the B0_SAME_DIR flag of the .swp file.
*/
static void
ml_upd_block0(buf, set_fname)
buf_T *buf;
int set_fname;
{
memfile_T *mfp;
bhdr_T *hp;
ZERO_BL *b0p;
mfp = buf->b_ml.ml_mfp;
if (mfp == NULL || (hp = mf_get(mfp, (blocknr_T)0, 1)) == NULL)
return;
b0p = (ZERO_BL *)(hp->bh_data);
if (b0p->b0_id[0] != BLOCK0_ID0 || b0p->b0_id[1] != BLOCK0_ID1)
EMSG(_("E304: ml_upd_block0(): Didn't get block 0??"));
else
{
if (set_fname)
set_b0_fname(b0p, buf);
else
set_b0_dir_flag(b0p, buf);
}
mf_put(mfp, hp, TRUE, FALSE);
}
/*
* Write file name and timestamp into block 0 of a swap file.
* Also set buf->b_mtime.
* Don't use NameBuff[]!!!
*/
static void
set_b0_fname(b0p, buf)
ZERO_BL *b0p;
buf_T *buf;
{
struct stat st;
if (buf->b_ffname == NULL)
b0p->b0_fname[0] = NUL;
else
{
#if defined(MSDOS) || defined(MSWIN) || defined(AMIGA) || defined(RISCOS)
/* Systems that cannot translate "~user" back into a path: copy the
* file name unmodified. Do use slashes instead of backslashes for
* portability. */
vim_strncpy(b0p->b0_fname, buf->b_ffname, B0_FNAME_SIZE - 1);
# ifdef BACKSLASH_IN_FILENAME
forward_slash(b0p->b0_fname);
# endif
#else
size_t flen, ulen;
char_u uname[B0_UNAME_SIZE];
/*
* For a file under the home directory of the current user, we try to
* replace the home directory path with "~user". This helps when
* editing the same file on different machines over a network.
* First replace home dir path with "~/" with home_replace().
* Then insert the user name to get "~user/".
*/
home_replace(NULL, buf->b_ffname, b0p->b0_fname, B0_FNAME_SIZE, TRUE);
if (b0p->b0_fname[0] == '~')
{
flen = STRLEN(b0p->b0_fname);
/* If there is no user name or it is too long, don't use "~/" */
if (get_user_name(uname, B0_UNAME_SIZE) == FAIL
|| (ulen = STRLEN(uname)) + flen > B0_FNAME_SIZE - 1)
vim_strncpy(b0p->b0_fname, buf->b_ffname, B0_FNAME_SIZE - 1);
else
{
mch_memmove(b0p->b0_fname + ulen + 1, b0p->b0_fname + 1, flen);
mch_memmove(b0p->b0_fname + 1, uname, ulen);
}
}
#endif
if (mch_stat((char *)buf->b_ffname, &st) >= 0)
{
long_to_char((long)st.st_mtime, b0p->b0_mtime);
#ifdef CHECK_INODE
long_to_char((long)st.st_ino, b0p->b0_ino);
#endif
buf_store_time(buf, &st, buf->b_ffname);
buf->b_mtime_read = buf->b_mtime;
}
else
{
long_to_char(0L, b0p->b0_mtime);
#ifdef CHECK_INODE
long_to_char(0L, b0p->b0_ino);
#endif
buf->b_mtime = 0;
buf->b_mtime_read = 0;
buf->b_orig_size = 0;
buf->b_orig_mode = 0;
}
}
#ifdef FEAT_MBYTE
/* Also add the 'fileencoding' if there is room. */
add_b0_fenc(b0p, curbuf);
#endif
}
/*
* Update the B0_SAME_DIR flag of the swap file. It's set if the file and the
* swapfile for "buf" are in the same directory.
* This is fail safe: if we are not sure the directories are equal the flag is
* not set.
*/
static void
set_b0_dir_flag(b0p, buf)
ZERO_BL *b0p;
buf_T *buf;
{
if (same_directory(buf->b_ml.ml_mfp->mf_fname, buf->b_ffname))
b0p->b0_flags |= B0_SAME_DIR;
else
b0p->b0_flags &= ~B0_SAME_DIR;
}
#ifdef FEAT_MBYTE
/*
* When there is room, add the 'fileencoding' to block zero.
*/
static void
add_b0_fenc(b0p, buf)
ZERO_BL *b0p;
buf_T *buf;
{
int n;
n = (int)STRLEN(buf->b_p_fenc);
if (STRLEN(b0p->b0_fname) + n + 1 > B0_FNAME_SIZE)
b0p->b0_flags &= ~B0_HAS_FENC;
else
{
mch_memmove((char *)b0p->b0_fname + B0_FNAME_SIZE - n,
(char *)buf->b_p_fenc, (size_t)n);
*(b0p->b0_fname + B0_FNAME_SIZE - n - 1) = NUL;
b0p->b0_flags |= B0_HAS_FENC;
}
}
#endif
/*
* try to recover curbuf from the .swp file
*/
void
ml_recover()
{
buf_T *buf = NULL;
memfile_T *mfp = NULL;
char_u *fname;
bhdr_T *hp = NULL;
ZERO_BL *b0p;
int b0_ff;
char_u *b0_fenc = NULL;
PTR_BL *pp;
DATA_BL *dp;
infoptr_T *ip;
blocknr_T bnum;
int page_count;
struct stat org_stat, swp_stat;
int len;
int directly;
linenr_T lnum;
char_u *p;
int i;
long error;
int cannot_open;
linenr_T line_count;
int has_error;
int idx;
int top;
int txt_start;
off_t size;
int called_from_main;
int serious_error = TRUE;
long mtime;
int attr;
recoverymode = TRUE;
called_from_main = (curbuf->b_ml.ml_mfp == NULL);
attr = hl_attr(HLF_E);
/*
* If the file name ends in ".sw?" we use it directly.
* Otherwise a search is done to find the swap file(s).
*/
fname = curbuf->b_fname;
if (fname == NULL) /* When there is no file name */
fname = (char_u *)"";
len = (int)STRLEN(fname);
if (len >= 4 &&
#if defined(VMS) || defined(RISCOS)
STRNICMP(fname + len - 4, "_sw" , 3)
#else
STRNICMP(fname + len - 4, ".sw" , 3)
#endif
== 0)
{
directly = TRUE;
fname = vim_strsave(fname); /* make a copy for mf_open() */
}
else
{
directly = FALSE;
/* count the number of matching swap files */
len = recover_names(&fname, FALSE, 0);
if (len == 0) /* no swap files found */
{
EMSG2(_("E305: No swap file found for %s"), fname);
goto theend;
}
if (len == 1) /* one swap file found, use it */
i = 1;
else /* several swap files found, choose */
{
/* list the names of the swap files */
(void)recover_names(&fname, TRUE, 0);
msg_putchar('\n');
MSG_PUTS(_("Enter number of swap file to use (0 to quit): "));
i = get_number(FALSE, NULL);
if (i < 1 || i > len)
goto theend;
}
/* get the swap file name that will be used */
(void)recover_names(&fname, FALSE, i);
}
if (fname == NULL)
goto theend; /* out of memory */
/* When called from main() still need to initialize storage structure */
if (called_from_main && ml_open(curbuf) == FAIL)
getout(1);
/*
* allocate a buffer structure (only the memline in it is really used)
*/
buf = (buf_T *)alloc((unsigned)sizeof(buf_T));
if (buf == NULL)
{
vim_free(fname);
goto theend;
}
/*
* init fields in memline struct
*/
buf->b_ml.ml_stack_size = 0; /* no stack yet */
buf->b_ml.ml_stack = NULL; /* no stack yet */
buf->b_ml.ml_stack_top = 0; /* nothing in the stack */
buf->b_ml.ml_line_lnum = 0; /* no cached line */
buf->b_ml.ml_locked = NULL; /* no locked block */
buf->b_ml.ml_flags = 0;
/*
* open the memfile from the old swap file
*/
p = vim_strsave(fname); /* save fname for the message
(mf_open() may free fname) */
mfp = mf_open(fname, O_RDONLY); /* consumes fname! */
if (mfp == NULL || mfp->mf_fd < 0)
{
if (p != NULL)
{
EMSG2(_("E306: Cannot open %s"), p);
vim_free(p);
}
goto theend;
}
vim_free(p);
buf->b_ml.ml_mfp = mfp;
/*
* The page size set in mf_open() might be different from the page size
* used in the swap file, we must get it from block 0. But to read block
* 0 we need a page size. Use the minimal size for block 0 here, it will
* be set to the real value below.
*/
mfp->mf_page_size = MIN_SWAP_PAGE_SIZE;
/*
* try to read block 0
*/
if ((hp = mf_get(mfp, (blocknr_T)0, 1)) == NULL)
{
msg_start();
MSG_PUTS_ATTR(_("Unable to read block 0 from "), attr | MSG_HIST);
msg_outtrans_attr(mfp->mf_fname, attr | MSG_HIST);
MSG_PUTS_ATTR(
_("\nMaybe no changes were made or Vim did not update the swap file."),
attr | MSG_HIST);
msg_end();
goto theend;
}
b0p = (ZERO_BL *)(hp->bh_data);
if (STRNCMP(b0p->b0_version, "VIM 3.0", 7) == 0)
{
msg_start();
msg_outtrans_attr(mfp->mf_fname, MSG_HIST);
MSG_PUTS_ATTR(_(" cannot be used with this version of Vim.\n"),
MSG_HIST);
MSG_PUTS_ATTR(_("Use Vim version 3.0.\n"), MSG_HIST);
msg_end();
goto theend;
}
if (b0p->b0_id[0] != BLOCK0_ID0 || b0p->b0_id[1] != BLOCK0_ID1)
{
EMSG2(_("E307: %s does not look like a Vim swap file"), mfp->mf_fname);
goto theend;
}
if (b0_magic_wrong(b0p))
{
msg_start();
msg_outtrans_attr(mfp->mf_fname, attr | MSG_HIST);
#if defined(MSDOS) || defined(MSWIN)
if (STRNCMP(b0p->b0_hname, "PC ", 3) == 0)