-
-
Notifications
You must be signed in to change notification settings - Fork 5.3k
/
memline.c
6155 lines (5636 loc) · 162 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 noet:
*
* 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) do { if (c) emsg((s)); } while (0)
#define CHECK(c, s) do { /**/ } while (0)
/*
* 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().
*/
#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
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
#define BLOCK0_ID1_C0 'c' // block 0 id 1 'cm' 0
#define BLOCK0_ID1_C1 'C' // block 0 id 1 'cm' 1
#define BLOCK0_ID1_C2 'd' // block 0 id 1 'cm' 2
// BLOCK0_ID1_C3 and BLOCK0_ID1_C4 are for libsodium encryption. However, for
// these the swapfile is disabled, thus they will not be used. Added for
// consistency anyway.
#define BLOCK0_ID1_C3 'S' // block 0 id 1 'cm' 3
#define BLOCK0_ID1_C4 's' // block 0 id 1 'cm' 4
#if defined(FEAT_CRYPT)
static int id1_codes[] = {
BLOCK0_ID1_C0, // CRYPT_M_ZIP
BLOCK0_ID1_C1, // CRYPT_M_BF
BLOCK0_ID1_C2, // CRYPT_M_BF2
BLOCK0_ID1_C3, // CRYPT_M_SOD - Unused!
BLOCK0_ID1_C4, // CRYPT_M_SOD2 - Unused!
};
#endif
/*
* 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 pointers 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
};
// Value for pb_count_max.
#define PB_COUNT_MAX(mfp) (short_u)(((mfp)->mf_page_size - offsetof(PTR_BL, pb_pointer)) / sizeof(PTR_EN))
/*
* 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 up to 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 (offsetof(DATA_BL, db_index)) // size of data block header
#define B0_FNAME_SIZE_ORG 900 // what it was in older versions
#define B0_FNAME_SIZE_NOCRYPT 898 // 2 bytes used for other things
#define B0_FNAME_SIZE_CRYPT 890 // 10 bytes used for other things
#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,
// BLOCK0_ID1_C0, BLOCK0_ID1_C1, etc.
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]
/*
* Crypt seed goes here, 8 bytes. New in Vim 7.3.
* Without encryption these bytes may be used for 'fenc'.
*/
#define b0_seed b0_fname[B0_FNAME_SIZE_ORG - 2 - MF_SEED_LEN]
// 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
// argument for ml_upd_block0()
typedef enum {
UB_FNAME = 0 // update timestamp and filename
, UB_SAME_DIR // update the B0_SAME_DIR flag
, UB_CRYPT // update crypt key
} upd_block0_T;
#ifdef FEAT_CRYPT
static void ml_set_b0_crypt(buf_T *buf, ZERO_BL *b0p);
#endif
static void ml_upd_block0(buf_T *buf, upd_block0_T what);
static void set_b0_fname(ZERO_BL *, buf_T *buf);
static void set_b0_dir_flag(ZERO_BL *b0p, buf_T *buf);
static void add_b0_fenc(ZERO_BL *b0p, buf_T *buf);
static time_t swapfile_info(char_u *);
static int recov_file_names(char_u **, char_u *, int prepend_dot);
static char_u *findswapname(buf_T *, char_u **, char_u *);
static void ml_flush_line(buf_T *);
static bhdr_T *ml_new_data(memfile_T *, int, int);
static bhdr_T *ml_new_ptr(memfile_T *);
static bhdr_T *ml_find_line(buf_T *, linenr_T, int);
static int ml_add_stack(buf_T *);
static void ml_lineadd(buf_T *, int);
static int b0_magic_wrong(ZERO_BL *);
#ifdef CHECK_INODE
static int fnamecmp_ino(char_u *, char_u *, long);
#endif
static void long_to_char(long, char_u *);
static long char_to_long(char_u *);
#ifdef FEAT_CRYPT
static cryptstate_T *ml_crypt_prepare(memfile_T *mfp, off_T offset, int reading);
#endif
#ifdef FEAT_BYTEOFF
static void ml_updatechunk(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_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;
buf->b_ml.ml_usedchunks = 0;
#endif
if (cmdmod.cmod_flags & CMOD_NOSWAPFILE)
buf->b_p_swf = FALSE;
/*
* 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;
#ifdef FEAT_CRYPT
mfp->mf_buffer = buf;
#endif
buf->b_ml.ml_flags = ML_EMPTY;
buf->b_ml.ml_line_count = 1;
/*
* fill block0 struct and write page 0
*/
if ((hp = mf_new(mfp, FALSE, 1)) == NULL)
goto error;
if (hp->bh_bnum != 0)
{
iemsg(e_didnt_get_block_nr_zero);
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;
mch_memmove(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);
#ifdef FEAT_CRYPT
ml_set_b0_crypt(buf, b0p);
#endif
}
/*
* 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 a help files or
* a 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)
{
iemsg(e_didnt_get_block_nr_one);
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)
{
iemsg(e_didnt_get_block_nr_two);
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; // empty 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;
}
#if defined(FEAT_CRYPT) || defined(PROTO)
/*
* Swapfile encryption is not supported by XChaCha20. If this crypt method is
* used then disable the swapfile, to avoid plain text being written to disk,
* and return TRUE.
* Otherwise return FALSE.
*/
static int
crypt_may_close_swapfile(buf_T *buf, char_u *key, int method)
{
if (crypt_method_is_sodium(method) && *key != NUL)
{
mf_close_file(buf, TRUE);
buf->b_p_swf = FALSE;
return TRUE;
}
return FALSE;
}
/*
* Prepare encryption for "buf" for the current key and method.
*/
static void
ml_set_mfp_crypt(buf_T *buf)
{
if (*buf->b_p_key == NUL)
return;
int method_nr = crypt_get_method_nr(buf);
if (method_nr > CRYPT_M_ZIP && method_nr < CRYPT_M_SOD)
{
// Generate a seed and store it in the memfile.
sha2_seed(buf->b_ml.ml_mfp->mf_seed, MF_SEED_LEN, NULL, 0);
}
# ifdef FEAT_SODIUM
else if (crypt_method_is_sodium(method_nr))
crypt_sodium_randombytes_buf(buf->b_ml.ml_mfp->mf_seed, MF_SEED_LEN);
# endif
}
/*
* Prepare encryption for "buf" with block 0 "b0p".
* Note: should not be called with libsodium encryption, since xchacha20 does
* not support swapfile encryption.
*/
static void
ml_set_b0_crypt(buf_T *buf, ZERO_BL *b0p)
{
if (*buf->b_p_key == NUL)
b0p->b0_id[1] = BLOCK0_ID1;
else
{
int method_nr = crypt_get_method_nr(buf);
b0p->b0_id[1] = id1_codes[method_nr];
if (method_nr > CRYPT_M_ZIP && method_nr < CRYPT_M_SOD)
{
// Generate a seed and store it in block 0 and in the memfile.
sha2_seed(&b0p->b0_seed, MF_SEED_LEN, NULL, 0);
mch_memmove(buf->b_ml.ml_mfp->mf_seed, &b0p->b0_seed, MF_SEED_LEN);
}
}
}
/*
* Called after the crypt key or 'cryptmethod' was changed for "buf".
* Will apply this to the swapfile.
* "old_key" is the previous key. It is equal to buf->b_p_key when
* 'cryptmethod' is changed.
* "old_cm" is the previous 'cryptmethod'. It is equal to the current
* 'cryptmethod' when 'key' is changed.
*/
void
ml_set_crypt_key(
buf_T *buf,
char_u *old_key,
char_u *old_cm)
{
memfile_T *mfp = buf->b_ml.ml_mfp;
bhdr_T *hp;
int page_count;
int idx;
long error;
infoptr_T *ip;
PTR_BL *pp;
DATA_BL *dp;
blocknr_T bnum;
int top;
int old_method;
if (mfp == NULL || mfp->mf_fd < 0)
return; // no memfile yet, nothing to do
old_method = crypt_method_nr_from_name(old_cm);
#ifdef FEAT_CRYPT
if (crypt_may_close_swapfile(buf, buf->b_p_key, crypt_get_method_nr(buf)))
return;
#endif
// First make sure the swapfile is in a consistent state, using the old
// key and method.
{
char_u *new_key = buf->b_p_key;
char_u *new_buf_cm = buf->b_p_cm;
buf->b_p_key = old_key;
buf->b_p_cm = old_cm;
ml_preserve(buf, FALSE);
buf->b_p_key = new_key;
buf->b_p_cm = new_buf_cm;
}
// Set the key, method and seed to be used for reading, these must be the
// old values.
mfp->mf_old_key = old_key;
mfp->mf_old_cm = old_method;
if (old_method > 0 && *old_key != NUL)
mch_memmove(mfp->mf_old_seed, mfp->mf_seed, MF_SEED_LEN);
// Update block 0 with the crypt flag and may set a new seed.
ml_upd_block0(buf, UB_CRYPT);
if (mfp->mf_infile_count > 2)
{
/*
* Need to read back all data blocks from disk, decrypt them with the
* old key/method and mark them to be written. The algorithm is
* similar to what happens in ml_recover(), but we skip negative block
* numbers.
*/
ml_flush_line(buf); // flush buffered line
(void)ml_find_line(buf, (linenr_T)0, ML_FLUSH); // flush locked block
hp = NULL;
bnum = 1; // start with block 1
page_count = 1; // which is 1 page
idx = 0; // start with first index in block 1
error = 0;
buf->b_ml.ml_stack_top = 0;
VIM_CLEAR(buf->b_ml.ml_stack);
buf->b_ml.ml_stack_size = 0; // no stack yet
for ( ; !got_int; line_breakcheck())
{
if (hp != NULL)
mf_put(mfp, hp, FALSE, FALSE); // release previous block
// get the block (pointer or data)
if ((hp = mf_get(mfp, bnum, page_count)) == NULL)
{
if (bnum == 1)
break;
++error;
}
else
{
pp = (PTR_BL *)(hp->bh_data);
if (pp->pb_id == PTR_ID) // it is a pointer block
{
if (pp->pb_count == 0)
{
// empty block?
++error;
}
else if (idx < (int)pp->pb_count) // go a block deeper
{
if (pp->pb_pointer[idx].pe_bnum < 0)
{
// Skip data block with negative block number.
// Should not happen, because of the ml_preserve()
// above. Get same block again for next index.
++idx;
continue;
}
// going one block deeper in the tree, new entry in
// stack
if ((top = ml_add_stack(buf)) < 0)
{
++error;
break; // out of memory
}
ip = &(buf->b_ml.ml_stack[top]);
ip->ip_bnum = bnum;
ip->ip_index = idx;
bnum = pp->pb_pointer[idx].pe_bnum;
page_count = pp->pb_pointer[idx].pe_page_count;
idx = 0;
continue;
}
}
else // not a pointer block
{
dp = (DATA_BL *)(hp->bh_data);
if (dp->db_id != DATA_ID) // block id wrong
++error;
else
{
// It is a data block, need to write it back to disk.
mf_put(mfp, hp, TRUE, FALSE);
hp = NULL;
}
}
}
if (buf->b_ml.ml_stack_top == 0) // finished
break;
// go one block up in the tree
ip = &(buf->b_ml.ml_stack[--(buf->b_ml.ml_stack_top)]);
bnum = ip->ip_bnum;
idx = ip->ip_index + 1; // go to next index
page_count = 1;
}
if (hp != NULL)
mf_put(mfp, hp, FALSE, FALSE); // release previous block
if (error > 0)
emsg(_(e_error_while_updating_swap_file_crypt));
}
mfp->mf_old_key = NULL;
}
#endif
/*
* ml_setname() is called when the file name of "buf" has been changed.
* It may rename the swap file.
*/
void
ml_setname(buf_T *buf)
{
int success = FALSE;
memfile_T *mfp;
char_u *fname;
char_u *dirp;
#if 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 && (cmdmod.cmod_flags & CMOD_NOSWAPFILE) == 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 (dirp == NULL) // out of memory
break;
if (fname == NULL) // no file name found for this dir
continue;
#if 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(MSWIN)
mfp->mf_ffname = NULL; // mf_fname is full pathname already
#else
mf_set_ffname(mfp);
#endif
ml_upd_block0(buf, UB_SAME_DIR);
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(_(e_oops_lost_the_swap_file));
return;
}
#ifdef HAVE_FD_CLOEXEC
{
int fdflags = fcntl(mfp->mf_fd, F_GETFD);
if (fdflags >= 0 && (fdflags & FD_CLOEXEC) == 0)
(void)fcntl(mfp->mf_fd, F_SETFD, fdflags | FD_CLOEXEC);
}
#endif
}
if (!success)
emsg(_(e_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(void)
{
buf_T *buf;
FOR_ALL_BUFFERS(buf)
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_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
|| (cmdmod.cmod_flags & CMOD_NOSWAPFILE))
return; // nothing to do
#ifdef FEAT_SPELL
// For a spell buffer use a temp file name.
if (buf->b_spell)
{
fname = vim_tempname('s', FALSE);
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 choosing 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 (dirp == NULL)
break; // out of memory
if (fname == NULL)
continue;
if (mf_open_file(mfp, fname) == OK) // consumes fname!
{
// don't sync yet in ml_sync_all()
mfp->mf_dirty = MF_DIRTY_YES_NOSYNC;
#if defined(MSWIN)
/*
* 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, UB_SAME_DIR);
// 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 (*p_dir != NUL && mfp->mf_fname == NULL)
{
need_wait_return = TRUE; // call wait_return() later
++no_wait_return;
(void)semsg(_(e_unable_to_open_swap_file_for_str_recovery_impossible),
buf_spname(buf) != NULL ? 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(
int newfile) // reading file into new buffer
{
int old_msg_silent = msg_silent; // might be reset by an E325 message
if (curbuf->b_may_swap && (!curbuf->b_p_ro || !newfile))
ml_open_file(curbuf);
msg_silent = old_msg_silent;
}
/*
* Close memline for buffer 'buf'.
* If 'del_file' is TRUE, delete the swap file
*/
void
ml_close(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 | ML_ALLOCATED)))
vim_free(buf->b_ml.ml_line_ptr);
vim_free(buf->b_ml.ml_stack);
#ifdef FEAT_BYTEOFF
VIM_CLEAR(buf->b_ml.ml_chunksize);
#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(int del_file)
{
buf_T *buf;
FOR_ALL_BUFFERS(buf)
ml_close(buf, del_file && ((buf->b_flags & BF_PRESERVED) == 0
|| vim_strchr(p_cpo, CPO_PRESERVE) == NULL));
#ifdef FEAT_SPELL
spell_delete_wordlist(); // delete the internal wordlist
#endif
#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(void)
{
buf_T *buf;
FOR_ALL_BUFFERS(buf)
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_T *buf)
{
ml_upd_block0(buf, UB_FNAME);
}
/*
* Return FAIL when the ID of "b0p" is wrong.
*/
static int
ml_check_b0_id(ZERO_BL *b0p)
{
if (b0p->b0_id[0] != BLOCK0_ID0
|| (b0p->b0_id[1] != BLOCK0_ID1
&& b0p->b0_id[1] != BLOCK0_ID1_C0
&& b0p->b0_id[1] != BLOCK0_ID1_C1
&& b0p->b0_id[1] != BLOCK0_ID1_C2
&& b0p->b0_id[1] != BLOCK0_ID1_C3
&& b0p->b0_id[1] != BLOCK0_ID1_C4)
)
return FAIL;
return OK;
}
/*
* Update the timestamp or the B0_SAME_DIR flag of the .swp file.
*/
static void
ml_upd_block0(buf_T *buf, upd_block0_T what)
{
memfile_T *mfp;
bhdr_T *hp;
ZERO_BL *b0p;
mfp = buf->b_ml.ml_mfp;
if (mfp == NULL)
return;
hp = mf_get(mfp, (blocknr_T)0, 1);
if (hp == NULL)
{
#ifdef FEAT_CRYPT
// Possibly update the seed in the memfile before there is a block0.
if (what == UB_CRYPT)
ml_set_mfp_crypt(buf);
#endif
return;
}
b0p = (ZERO_BL *)(hp->bh_data);
if (ml_check_b0_id(b0p) == FAIL)
iemsg(e_ml_upd_block0_didnt_get_block_zero);
else
{
if (what == UB_FNAME)
set_b0_fname(b0p, buf);
#ifdef FEAT_CRYPT
else if (what == UB_CRYPT)
ml_set_b0_crypt(buf, b0p);
#endif
else // what == UB_SAME_DIR
set_b0_dir_flag(b0p, buf);
}
mf_put(mfp, hp, TRUE, FALSE);