blob: 57c9d92fd2222c7b287d5b8a7b6c9167e3a83891 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * memfile.c: Contains the functions for handling blocks of memory which can
12 * be stored in a file. This is the implementation of a sort of virtual memory.
13 *
14 * A memfile consists of a sequence of blocks. The blocks numbered from 0
15 * upwards have been assigned a place in the actual file. The block number
16 * is equal to the page number in the file. The
17 * blocks with negative numbers are currently in memory only. They can be
18 * assigned a place in the file when too much memory is being used. At that
19 * moment they get a new, positive, number. A list is used for translation of
20 * negative to positive numbers.
21 *
22 * The size of a block is a multiple of a page size, normally the page size of
23 * the device the file is on. Most blocks are 1 page long. A Block of multiple
24 * pages is used for a line that does not fit in a single page.
25 *
26 * Each block can be in memory and/or in a file. The block stays in memory
27 * as long as it is locked. If it is no longer locked it can be swapped out to
28 * the file. It is only written to the file if it has been changed.
29 *
30 * Under normal operation the file is created when opening the memory file and
31 * deleted when closing the memory file. Only with recovery an existing memory
32 * file is opened.
33 */
34
Bram Moolenaar071d4272004-06-13 20:20:40 +000035#include "vim.h"
36
Bram Moolenaar071d4272004-06-13 20:20:40 +000037/*
38 * Some systems have the page size in statfs.f_bsize, some in stat.st_blksize
39 */
40#ifdef HAVE_ST_BLKSIZE
41# define STATFS stat
42# define F_BSIZE st_blksize
43# define fstatfs(fd, buf, len, nul) mch_fstat((fd), (buf))
44#else
45# ifdef HAVE_SYS_STATFS_H
46# include <sys/statfs.h>
47# define STATFS statfs
48# define F_BSIZE f_bsize
49# ifdef __MINT__ /* do we still need this? */
50# define fstatfs(fd, buf, len, nul) mch_fstat((fd), (buf))
51# endif
52# endif
53#endif
54
55/*
56 * for Amiga Dos 2.0x we use Flush
57 */
58#ifdef AMIGA
59# ifdef FEAT_ARP
60extern int dos2; /* this is in os_amiga.c */
61# endif
62# ifdef SASC
63# include <proto/dos.h>
64# include <ios1.h> /* for chkufb() */
65# endif
66#endif
67
68#define MEMFILE_PAGE_SIZE 4096 /* default page size */
69
70static long_u total_mem_used = 0; /* total memory used for memfiles */
Bram Moolenaar071d4272004-06-13 20:20:40 +000071
72static void mf_ins_hash __ARGS((memfile_T *, bhdr_T *));
73static void mf_rem_hash __ARGS((memfile_T *, bhdr_T *));
74static bhdr_T *mf_find_hash __ARGS((memfile_T *, blocknr_T));
75static void mf_ins_used __ARGS((memfile_T *, bhdr_T *));
76static void mf_rem_used __ARGS((memfile_T *, bhdr_T *));
77static bhdr_T *mf_release __ARGS((memfile_T *, int));
78static bhdr_T *mf_alloc_bhdr __ARGS((memfile_T *, int));
79static void mf_free_bhdr __ARGS((bhdr_T *));
80static void mf_ins_free __ARGS((memfile_T *, bhdr_T *));
81static bhdr_T *mf_rem_free __ARGS((memfile_T *));
82static int mf_read __ARGS((memfile_T *, bhdr_T *));
83static int mf_write __ARGS((memfile_T *, bhdr_T *));
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +020084static int mf_write_block __ARGS((memfile_T *mfp, bhdr_T *hp, off_t offset, unsigned size));
Bram Moolenaar071d4272004-06-13 20:20:40 +000085static int mf_trans_add __ARGS((memfile_T *, bhdr_T *));
86static void mf_do_open __ARGS((memfile_T *, char_u *, int));
Bram Moolenaarb05b10a2011-03-22 18:10:45 +010087static void mf_hash_init __ARGS((mf_hashtab_T *));
88static void mf_hash_free __ARGS((mf_hashtab_T *));
89static void mf_hash_free_all __ARGS((mf_hashtab_T *));
90static mf_hashitem_T *mf_hash_find __ARGS((mf_hashtab_T *, blocknr_T));
91static void mf_hash_add_item __ARGS((mf_hashtab_T *, mf_hashitem_T *));
92static void mf_hash_rem_item __ARGS((mf_hashtab_T *, mf_hashitem_T *));
93static int mf_hash_grow __ARGS((mf_hashtab_T *));
Bram Moolenaar071d4272004-06-13 20:20:40 +000094
95/*
96 * The functions for using a memfile:
97 *
98 * mf_open() open a new or existing memfile
99 * mf_open_file() open a swap file for an existing memfile
100 * mf_close() close (and delete) a memfile
101 * mf_new() create a new block in a memfile and lock it
102 * mf_get() get an existing block and lock it
103 * mf_put() unlock a block, may be marked for writing
104 * mf_free() remove a block
105 * mf_sync() sync changed parts of memfile to disk
106 * mf_release_all() release as much memory as possible
107 * mf_trans_del() may translate negative to positive block number
108 * mf_fullname() make file name full path (use before first :cd)
109 */
110
111/*
112 * Open an existing or new memory block file.
113 *
114 * fname: name of file to use (NULL means no file at all)
115 * Note: fname must have been allocated, it is not copied!
116 * If opening the file fails, fname is freed.
117 * flags: flags for open() call
118 *
119 * If fname != NULL and file cannot be opened, fail.
120 *
121 * return value: identifier for this memory block file.
122 */
123 memfile_T *
124mf_open(fname, flags)
125 char_u *fname;
126 int flags;
127{
128 memfile_T *mfp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000129 off_t size;
Bram Moolenaara03dbed2013-05-23 22:27:03 +0200130#if defined(STATFS) && defined(UNIX) && !defined(__QNX__) && !defined(__minix)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000131# define USE_FSTATFS
132 struct STATFS stf;
133#endif
134
135 if ((mfp = (memfile_T *)alloc((unsigned)sizeof(memfile_T))) == NULL)
136 return NULL;
137
138 if (fname == NULL) /* no file for this memfile, use memory only */
139 {
140 mfp->mf_fname = NULL;
141 mfp->mf_ffname = NULL;
142 mfp->mf_fd = -1;
143 }
144 else
145 {
146 mf_do_open(mfp, fname, flags); /* try to open the file */
147
148 /* if the file cannot be opened, return here */
149 if (mfp->mf_fd < 0)
150 {
151 vim_free(mfp);
152 return NULL;
153 }
154 }
155
156 mfp->mf_free_first = NULL; /* free list is empty */
157 mfp->mf_used_first = NULL; /* used list is empty */
158 mfp->mf_used_last = NULL;
159 mfp->mf_dirty = FALSE;
160 mfp->mf_used_count = 0;
Bram Moolenaarb05b10a2011-03-22 18:10:45 +0100161 mf_hash_init(&mfp->mf_hash);
162 mf_hash_init(&mfp->mf_trans);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000163 mfp->mf_page_size = MEMFILE_PAGE_SIZE;
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +0200164#ifdef FEAT_CRYPT
165 mfp->mf_old_key = NULL;
166#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000167
168#ifdef USE_FSTATFS
169 /*
170 * Try to set the page size equal to the block size of the device.
171 * Speeds up I/O a lot.
172 * When recovering, the actual block size will be retrieved from block 0
173 * in ml_recover(). The size used here may be wrong, therefore
174 * mf_blocknr_max must be rounded up.
175 */
176 if (mfp->mf_fd >= 0
177 && fstatfs(mfp->mf_fd, &stf, sizeof(struct statfs), 0) == 0
178 && stf.F_BSIZE >= MIN_SWAP_PAGE_SIZE
179 && stf.F_BSIZE <= MAX_SWAP_PAGE_SIZE)
180 mfp->mf_page_size = stf.F_BSIZE;
181#endif
182
183 if (mfp->mf_fd < 0 || (flags & (O_TRUNC|O_EXCL))
184 || (size = lseek(mfp->mf_fd, (off_t)0L, SEEK_END)) <= 0)
185 mfp->mf_blocknr_max = 0; /* no file or empty file */
186 else
187 mfp->mf_blocknr_max = (blocknr_T)((size + mfp->mf_page_size - 1)
188 / mfp->mf_page_size);
189 mfp->mf_blocknr_min = -1;
190 mfp->mf_neg_count = 0;
191 mfp->mf_infile_count = mfp->mf_blocknr_max;
Bram Moolenaarfe265ff2007-05-11 18:15:45 +0000192
193 /*
194 * Compute maximum number of pages ('maxmem' is in Kbyte):
195 * 'mammem' * 1Kbyte / page-size-in-bytes.
196 * Avoid overflow by first reducing page size as much as possible.
197 */
198 {
199 int shift = 10;
200 unsigned page_size = mfp->mf_page_size;
201
202 while (shift > 0 && (page_size & 1) == 0)
203 {
204 page_size = page_size >> 1;
205 --shift;
206 }
207 mfp->mf_used_count_max = (p_mm << shift) / page_size;
208 if (mfp->mf_used_count_max < 10)
209 mfp->mf_used_count_max = 10;
210 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000211
212 return mfp;
213}
214
215/*
216 * Open a file for an existing memfile. Used when updatecount set from 0 to
217 * some value.
218 * If the file already exists, this fails.
219 * "fname" is the name of file to use (NULL means no file at all)
220 * Note: "fname" must have been allocated, it is not copied! If opening the
221 * file fails, "fname" is freed.
222 *
223 * return value: FAIL if file could not be opened, OK otherwise
224 */
225 int
226mf_open_file(mfp, fname)
227 memfile_T *mfp;
228 char_u *fname;
229{
230 mf_do_open(mfp, fname, O_RDWR|O_CREAT|O_EXCL); /* try to open the file */
231
232 if (mfp->mf_fd < 0)
233 return FAIL;
234
235 mfp->mf_dirty = TRUE;
236 return OK;
237}
238
239/*
Bram Moolenaar945e2db2010-06-05 17:43:32 +0200240 * Close a memory file and delete the associated file if 'del_file' is TRUE.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000241 */
242 void
243mf_close(mfp, del_file)
244 memfile_T *mfp;
245 int del_file;
246{
247 bhdr_T *hp, *nextp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000248
249 if (mfp == NULL) /* safety check */
250 return;
251 if (mfp->mf_fd >= 0)
252 {
253 if (close(mfp->mf_fd) < 0)
254 EMSG(_(e_swapclose));
255 }
256 if (del_file && mfp->mf_fname != NULL)
257 mch_remove(mfp->mf_fname);
258 /* free entries in used list */
259 for (hp = mfp->mf_used_first; hp != NULL; hp = nextp)
260 {
261 total_mem_used -= hp->bh_page_count * mfp->mf_page_size;
262 nextp = hp->bh_next;
263 mf_free_bhdr(hp);
264 }
265 while (mfp->mf_free_first != NULL) /* free entries in free list */
266 vim_free(mf_rem_free(mfp));
Bram Moolenaarb05b10a2011-03-22 18:10:45 +0100267 mf_hash_free(&mfp->mf_hash);
268 mf_hash_free_all(&mfp->mf_trans); /* free hashtable and its items */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000269 vim_free(mfp->mf_fname);
270 vim_free(mfp->mf_ffname);
271 vim_free(mfp);
272}
273
274/*
275 * Close the swap file for a memfile. Used when 'swapfile' is reset.
276 */
277 void
278mf_close_file(buf, getlines)
279 buf_T *buf;
280 int getlines; /* get all lines into memory? */
281{
282 memfile_T *mfp;
283 linenr_T lnum;
284
285 mfp = buf->b_ml.ml_mfp;
286 if (mfp == NULL || mfp->mf_fd < 0) /* nothing to close */
287 return;
288
289 if (getlines)
290 {
291 /* get all blocks in memory by accessing all lines (clumsy!) */
Bram Moolenaar47b8b152007-02-07 02:41:57 +0000292 mf_dont_release = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000293 for (lnum = 1; lnum <= buf->b_ml.ml_line_count; ++lnum)
294 (void)ml_get_buf(buf, lnum, FALSE);
Bram Moolenaar47b8b152007-02-07 02:41:57 +0000295 mf_dont_release = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000296 /* TODO: should check if all blocks are really in core */
297 }
298
299 if (close(mfp->mf_fd) < 0) /* close the file */
300 EMSG(_(e_swapclose));
301 mfp->mf_fd = -1;
302
303 if (mfp->mf_fname != NULL)
304 {
305 mch_remove(mfp->mf_fname); /* delete the swap file */
306 vim_free(mfp->mf_fname);
307 vim_free(mfp->mf_ffname);
308 mfp->mf_fname = NULL;
309 mfp->mf_ffname = NULL;
310 }
311}
312
313/*
314 * Set new size for a memfile. Used when block 0 of a swapfile has been read
315 * and the size it indicates differs from what was guessed.
316 */
317 void
318mf_new_page_size(mfp, new_size)
319 memfile_T *mfp;
320 unsigned new_size;
321{
322 /* Correct the memory used for block 0 to the new size, because it will be
323 * freed with that size later on. */
324 total_mem_used += new_size - mfp->mf_page_size;
325 mfp->mf_page_size = new_size;
326}
327
328/*
329 * get a new block
330 *
331 * negative: TRUE if negative block number desired (data block)
332 */
333 bhdr_T *
334mf_new(mfp, negative, page_count)
335 memfile_T *mfp;
336 int negative;
337 int page_count;
338{
Bram Moolenaar17e79192007-05-06 14:15:36 +0000339 bhdr_T *hp; /* new bhdr_T */
340 bhdr_T *freep; /* first block in free list */
341 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000342
343 /*
344 * If we reached the maximum size for the used memory blocks, release one
345 * If a bhdr_T is returned, use it and adjust the page_count if necessary.
346 */
347 hp = mf_release(mfp, page_count);
348
349/*
350 * Decide on the number to use:
351 * If there is a free block, use its number.
352 * Otherwise use mf_block_min for a negative number, mf_block_max for
353 * a positive number.
354 */
355 freep = mfp->mf_free_first;
356 if (!negative && freep != NULL && freep->bh_page_count >= page_count)
357 {
358 /*
359 * If the block in the free list has more pages, take only the number
360 * of pages needed and allocate a new bhdr_T with data
361 *
Bram Moolenaar17e79192007-05-06 14:15:36 +0000362 * If the number of pages matches and mf_release() did not return a
363 * bhdr_T, use the bhdr_T from the free list and allocate the data
Bram Moolenaar071d4272004-06-13 20:20:40 +0000364 *
Bram Moolenaar17e79192007-05-06 14:15:36 +0000365 * If the number of pages matches and mf_release() returned a bhdr_T,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000366 * just use the number and free the bhdr_T from the free list
367 */
368 if (freep->bh_page_count > page_count)
369 {
370 if (hp == NULL && (hp = mf_alloc_bhdr(mfp, page_count)) == NULL)
371 return NULL;
372 hp->bh_bnum = freep->bh_bnum;
373 freep->bh_bnum += page_count;
374 freep->bh_page_count -= page_count;
375 }
376 else if (hp == NULL) /* need to allocate memory for this block */
377 {
378 if ((p = (char_u *)alloc(mfp->mf_page_size * page_count)) == NULL)
379 return NULL;
380 hp = mf_rem_free(mfp);
381 hp->bh_data = p;
382 }
383 else /* use the number, remove entry from free list */
384 {
385 freep = mf_rem_free(mfp);
386 hp->bh_bnum = freep->bh_bnum;
387 vim_free(freep);
388 }
389 }
390 else /* get a new number */
391 {
392 if (hp == NULL && (hp = mf_alloc_bhdr(mfp, page_count)) == NULL)
393 return NULL;
394 if (negative)
395 {
396 hp->bh_bnum = mfp->mf_blocknr_min--;
397 mfp->mf_neg_count++;
398 }
399 else
400 {
401 hp->bh_bnum = mfp->mf_blocknr_max;
402 mfp->mf_blocknr_max += page_count;
403 }
404 }
405 hp->bh_flags = BH_LOCKED | BH_DIRTY; /* new block is always dirty */
406 mfp->mf_dirty = TRUE;
407 hp->bh_page_count = page_count;
408 mf_ins_used(mfp, hp);
409 mf_ins_hash(mfp, hp);
410
411 /*
412 * Init the data to all zero, to avoid reading uninitialized data.
413 * This also avoids that the passwd file ends up in the swap file!
414 */
Bram Moolenaar945e2db2010-06-05 17:43:32 +0200415 (void)vim_memset((char *)(hp->bh_data), 0,
416 (size_t)mfp->mf_page_size * page_count);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000417
418 return hp;
419}
420
421/*
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +0200422 * Get existing block "nr" with "page_count" pages.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000423 *
424 * Note: The caller should first check a negative nr with mf_trans_del()
425 */
426 bhdr_T *
427mf_get(mfp, nr, page_count)
428 memfile_T *mfp;
429 blocknr_T nr;
430 int page_count;
431{
432 bhdr_T *hp;
433 /* doesn't exist */
434 if (nr >= mfp->mf_blocknr_max || nr <= mfp->mf_blocknr_min)
435 return NULL;
436
437 /*
438 * see if it is in the cache
439 */
440 hp = mf_find_hash(mfp, nr);
441 if (hp == NULL) /* not in the hash list */
442 {
443 if (nr < 0 || nr >= mfp->mf_infile_count) /* can't be in the file */
444 return NULL;
445
446 /* could check here if the block is in the free list */
447
448 /*
449 * Check if we need to flush an existing block.
450 * If so, use that block.
451 * If not, allocate a new block.
452 */
453 hp = mf_release(mfp, page_count);
454 if (hp == NULL && (hp = mf_alloc_bhdr(mfp, page_count)) == NULL)
455 return NULL;
456
457 hp->bh_bnum = nr;
458 hp->bh_flags = 0;
459 hp->bh_page_count = page_count;
460 if (mf_read(mfp, hp) == FAIL) /* cannot read the block! */
461 {
462 mf_free_bhdr(hp);
463 return NULL;
464 }
465 }
466 else
467 {
468 mf_rem_used(mfp, hp); /* remove from list, insert in front below */
469 mf_rem_hash(mfp, hp);
470 }
471
472 hp->bh_flags |= BH_LOCKED;
473 mf_ins_used(mfp, hp); /* put in front of used list */
474 mf_ins_hash(mfp, hp); /* put in front of hash list */
475
476 return hp;
477}
478
479/*
480 * release the block *hp
481 *
482 * dirty: Block must be written to file later
483 * infile: Block should be in file (needed for recovery)
484 *
485 * no return value, function cannot fail
486 */
487 void
488mf_put(mfp, hp, dirty, infile)
489 memfile_T *mfp;
490 bhdr_T *hp;
491 int dirty;
492 int infile;
493{
494 int flags;
495
496 flags = hp->bh_flags;
497
498 if ((flags & BH_LOCKED) == 0)
499 EMSG(_("E293: block was not locked"));
500 flags &= ~BH_LOCKED;
501 if (dirty)
502 {
503 flags |= BH_DIRTY;
504 mfp->mf_dirty = TRUE;
505 }
506 hp->bh_flags = flags;
507 if (infile)
508 mf_trans_add(mfp, hp); /* may translate negative in positive nr */
509}
510
511/*
512 * block *hp is no longer in used, may put it in the free list of memfile *mfp
513 */
514 void
515mf_free(mfp, hp)
516 memfile_T *mfp;
517 bhdr_T *hp;
518{
519 vim_free(hp->bh_data); /* free the memory */
520 mf_rem_hash(mfp, hp); /* get *hp out of the hash list */
521 mf_rem_used(mfp, hp); /* get *hp out of the used list */
522 if (hp->bh_bnum < 0)
523 {
524 vim_free(hp); /* don't want negative numbers in free list */
525 mfp->mf_neg_count--;
526 }
527 else
528 mf_ins_free(mfp, hp); /* put *hp in the free list */
529}
530
Bram Moolenaar89f37272006-09-26 11:48:34 +0000531#if defined(__MORPHOS__) && defined(__libnix__)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000532/* function is missing in MorphOS libnix version */
533extern unsigned long *__stdfiledes;
534
535 static unsigned long
536fdtofh(int filedescriptor)
537{
538 return __stdfiledes[filedescriptor];
539}
540#endif
541
542/*
543 * Sync the memory file *mfp to disk.
544 * Flags:
545 * MFS_ALL If not given, blocks with negative numbers are not synced,
546 * even when they are dirty!
547 * MFS_STOP Stop syncing when a character becomes available, but sync at
548 * least one block.
549 * MFS_FLUSH Make sure buffers are flushed to disk, so they will survive a
550 * system crash.
551 * MFS_ZERO Only write block 0.
552 *
553 * Return FAIL for failure, OK otherwise
554 */
555 int
556mf_sync(mfp, flags)
557 memfile_T *mfp;
558 int flags;
559{
560 int status;
561 bhdr_T *hp;
562#if defined(SYNC_DUP_CLOSE) && !defined(MSDOS)
563 int fd;
564#endif
565 int got_int_save = got_int;
566
567 if (mfp->mf_fd < 0) /* there is no file, nothing to do */
568 {
569 mfp->mf_dirty = FALSE;
570 return FAIL;
571 }
572
573 /* Only a CTRL-C while writing will break us here, not one typed
574 * previously. */
575 got_int = FALSE;
576
577 /*
578 * sync from last to first (may reduce the probability of an inconsistent
579 * file) If a write fails, it is very likely caused by a full filesystem.
580 * Then we only try to write blocks within the existing file. If that also
581 * fails then we give up.
582 */
583 status = OK;
584 for (hp = mfp->mf_used_last; hp != NULL; hp = hp->bh_prev)
585 if (((flags & MFS_ALL) || hp->bh_bnum >= 0)
586 && (hp->bh_flags & BH_DIRTY)
587 && (status == OK || (hp->bh_bnum >= 0
588 && hp->bh_bnum < mfp->mf_infile_count)))
589 {
590 if ((flags & MFS_ZERO) && hp->bh_bnum != 0)
591 continue;
592 if (mf_write(mfp, hp) == FAIL)
593 {
594 if (status == FAIL) /* double error: quit syncing */
595 break;
596 status = FAIL;
597 }
598 if (flags & MFS_STOP)
599 {
600 /* Stop when char available now. */
601 if (ui_char_avail())
602 break;
603 }
604 else
605 ui_breakcheck();
606 if (got_int)
607 break;
608 }
609
610 /*
611 * If the whole list is flushed, the memfile is not dirty anymore.
612 * In case of an error this flag is also set, to avoid trying all the time.
613 */
614 if (hp == NULL || status == FAIL)
615 mfp->mf_dirty = FALSE;
616
617 if ((flags & MFS_FLUSH) && *p_sws != NUL)
618 {
619#if defined(UNIX)
620# ifdef HAVE_FSYNC
621 /*
622 * most Unixes have the very useful fsync() function, just what we need.
623 * However, with OS/2 and EMX it is also available, but there are
624 * reports of bad problems with it (a bug in HPFS.IFS).
625 * So we disable use of it here in case someone tries to be smart
626 * and changes os_os2_cfg.h... (even though there is no __EMX__ test
627 * in the #if, as __EMX__ does not have sync(); we hope for a timely
628 * sync from the system itself).
629 */
630# if defined(__EMX__)
Bram Moolenaar84a05ac2013-05-06 04:24:17 +0200631 error "Don't use fsync with EMX! Read emxdoc.doc or emxfix01.doc for info."
Bram Moolenaar071d4272004-06-13 20:20:40 +0000632# endif
633 if (STRCMP(p_sws, "fsync") == 0)
634 {
635 if (fsync(mfp->mf_fd))
636 status = FAIL;
637 }
638 else
639# endif
640 /* OpenNT is strictly POSIX (Benzinger) */
641 /* Tandem/Himalaya NSK-OSS doesn't have sync() */
Bram Moolenaarba17ed62015-02-27 18:25:16 +0100642 /* No sync() on Stratus VOS */
643# if defined(__OPENNT) || defined(__TANDEM) || defined(__VOS__)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000644 fflush(NULL);
645# else
646 sync();
647# endif
648#endif
649#ifdef VMS
650 if (STRCMP(p_sws, "fsync") == 0)
651 {
652 if (fsync(mfp->mf_fd))
653 status = FAIL;
654 }
655#endif
656#ifdef MSDOS
657 if (_dos_commit(mfp->mf_fd))
658 status = FAIL;
659#else
660# ifdef SYNC_DUP_CLOSE
661 /*
662 * Win32 is a bit more work: Duplicate the file handle and close it.
663 * This should flush the file to disk.
664 */
665 if ((fd = dup(mfp->mf_fd)) >= 0)
666 close(fd);
667# endif
668#endif
669#ifdef AMIGA
Bram Moolenaar5a6404c2006-11-01 17:12:57 +0000670# if defined(__AROS__) || defined(__amigaos4__)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000671 if (fsync(mfp->mf_fd) != 0)
672 status = FAIL;
673# else
674 /*
675 * Flush() only exists for AmigaDos 2.0.
676 * For 1.3 it should be done with close() + open(), but then the risk
677 * is that the open() may fail and lose the file....
678 */
679# ifdef FEAT_ARP
680 if (dos2)
681# endif
682# ifdef SASC
683 {
684 struct UFB *fp = chkufb(mfp->mf_fd);
685
686 if (fp != NULL)
687 Flush(fp->ufbfh);
688 }
689# else
690# if defined(_DCC) || defined(__GNUC__) || defined(__MORPHOS__)
691 {
Bram Moolenaar89f37272006-09-26 11:48:34 +0000692# if defined(__GNUC__) && !defined(__MORPHOS__) && defined(__libnix__)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000693 /* Have function (in libnix at least),
694 * but ain't got no prototype anywhere. */
695 extern unsigned long fdtofh(int filedescriptor);
696# endif
Bram Moolenaar89f37272006-09-26 11:48:34 +0000697# if !defined(__libnix__)
698 fflush(NULL);
699# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000700 BPTR fh = (BPTR)fdtofh(mfp->mf_fd);
701
702 if (fh != 0)
703 Flush(fh);
Bram Moolenaar89f37272006-09-26 11:48:34 +0000704# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000705 }
706# else /* assume Manx */
707 Flush(_devtab[mfp->mf_fd].fd);
708# endif
709# endif
710# endif
711#endif /* AMIGA */
712 }
713
714 got_int |= got_int_save;
715
716 return status;
717}
718
719/*
Bram Moolenaarc32840f2006-01-14 21:23:38 +0000720 * For all blocks in memory file *mfp that have a positive block number set
721 * the dirty flag. These are blocks that need to be written to a newly
722 * created swapfile.
723 */
724 void
725mf_set_dirty(mfp)
726 memfile_T *mfp;
727{
728 bhdr_T *hp;
729
730 for (hp = mfp->mf_used_last; hp != NULL; hp = hp->bh_prev)
731 if (hp->bh_bnum > 0)
732 hp->bh_flags |= BH_DIRTY;
733 mfp->mf_dirty = TRUE;
734}
735
736/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000737 * insert block *hp in front of hashlist of memfile *mfp
738 */
739 static void
740mf_ins_hash(mfp, hp)
741 memfile_T *mfp;
742 bhdr_T *hp;
743{
Bram Moolenaarb05b10a2011-03-22 18:10:45 +0100744 mf_hash_add_item(&mfp->mf_hash, (mf_hashitem_T *)hp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000745}
746
747/*
748 * remove block *hp from hashlist of memfile list *mfp
749 */
750 static void
751mf_rem_hash(mfp, hp)
752 memfile_T *mfp;
753 bhdr_T *hp;
754{
Bram Moolenaarb05b10a2011-03-22 18:10:45 +0100755 mf_hash_rem_item(&mfp->mf_hash, (mf_hashitem_T *)hp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000756}
757
758/*
759 * look in hash lists of memfile *mfp for block header with number 'nr'
760 */
761 static bhdr_T *
762mf_find_hash(mfp, nr)
763 memfile_T *mfp;
764 blocknr_T nr;
765{
Bram Moolenaarb05b10a2011-03-22 18:10:45 +0100766 return (bhdr_T *)mf_hash_find(&mfp->mf_hash, nr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000767}
768
769/*
770 * insert block *hp in front of used list of memfile *mfp
771 */
772 static void
773mf_ins_used(mfp, hp)
774 memfile_T *mfp;
775 bhdr_T *hp;
776{
777 hp->bh_next = mfp->mf_used_first;
778 mfp->mf_used_first = hp;
779 hp->bh_prev = NULL;
780 if (hp->bh_next == NULL) /* list was empty, adjust last pointer */
781 mfp->mf_used_last = hp;
782 else
783 hp->bh_next->bh_prev = hp;
784 mfp->mf_used_count += hp->bh_page_count;
785 total_mem_used += hp->bh_page_count * mfp->mf_page_size;
786}
787
788/*
789 * remove block *hp from used list of memfile *mfp
790 */
791 static void
792mf_rem_used(mfp, hp)
793 memfile_T *mfp;
794 bhdr_T *hp;
795{
796 if (hp->bh_next == NULL) /* last block in used list */
797 mfp->mf_used_last = hp->bh_prev;
798 else
799 hp->bh_next->bh_prev = hp->bh_prev;
800 if (hp->bh_prev == NULL) /* first block in used list */
801 mfp->mf_used_first = hp->bh_next;
802 else
803 hp->bh_prev->bh_next = hp->bh_next;
804 mfp->mf_used_count -= hp->bh_page_count;
805 total_mem_used -= hp->bh_page_count * mfp->mf_page_size;
806}
807
808/*
809 * Release the least recently used block from the used list if the number
810 * of used memory blocks gets to big.
811 *
812 * Return the block header to the caller, including the memory block, so
813 * it can be re-used. Make sure the page_count is right.
814 */
815 static bhdr_T *
816mf_release(mfp, page_count)
817 memfile_T *mfp;
818 int page_count;
819{
820 bhdr_T *hp;
821 int need_release;
822 buf_T *buf;
823
824 /* don't release while in mf_close_file() */
Bram Moolenaar47b8b152007-02-07 02:41:57 +0000825 if (mf_dont_release)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000826 return NULL;
827
828 /*
829 * Need to release a block if the number of blocks for this memfile is
830 * higher than the maximum or total memory used is over 'maxmemtot'
831 */
832 need_release = ((mfp->mf_used_count >= mfp->mf_used_count_max)
833 || (total_mem_used >> 10) >= (long_u)p_mmt);
834
835 /*
836 * Try to create a swap file if the amount of memory used is getting too
837 * high.
838 */
839 if (mfp->mf_fd < 0 && need_release && p_uc)
840 {
841 /* find for which buffer this memfile is */
842 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
843 if (buf->b_ml.ml_mfp == mfp)
844 break;
845 if (buf != NULL && buf->b_may_swap)
846 ml_open_file(buf);
847 }
848
849 /*
850 * don't release a block if
851 * there is no file for this memfile
852 * or
853 * the number of blocks for this memfile is lower than the maximum
854 * and
855 * total memory used is not up to 'maxmemtot'
856 */
857 if (mfp->mf_fd < 0 || !need_release)
858 return NULL;
859
860 for (hp = mfp->mf_used_last; hp != NULL; hp = hp->bh_prev)
861 if (!(hp->bh_flags & BH_LOCKED))
862 break;
863 if (hp == NULL) /* not a single one that can be released */
864 return NULL;
865
866 /*
867 * If the block is dirty, write it.
868 * If the write fails we don't free it.
869 */
870 if ((hp->bh_flags & BH_DIRTY) && mf_write(mfp, hp) == FAIL)
871 return NULL;
872
873 mf_rem_used(mfp, hp);
874 mf_rem_hash(mfp, hp);
875
876 /*
877 * If a bhdr_T is returned, make sure that the page_count of bh_data is
878 * right
879 */
880 if (hp->bh_page_count != page_count)
881 {
882 vim_free(hp->bh_data);
883 if ((hp->bh_data = alloc(mfp->mf_page_size * page_count)) == NULL)
884 {
885 vim_free(hp);
886 return NULL;
887 }
888 hp->bh_page_count = page_count;
889 }
890 return hp;
891}
892
893/*
894 * release as many blocks as possible
895 * Used in case of out of memory
896 *
897 * return TRUE if any memory was released
898 */
899 int
900mf_release_all()
901{
902 buf_T *buf;
903 memfile_T *mfp;
904 bhdr_T *hp;
905 int retval = FALSE;
906
907 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
908 {
909 mfp = buf->b_ml.ml_mfp;
910 if (mfp != NULL)
911 {
912 /* If no swap file yet, may open one */
913 if (mfp->mf_fd < 0 && buf->b_may_swap)
914 ml_open_file(buf);
915
916 /* only if there is a swapfile */
917 if (mfp->mf_fd >= 0)
918 {
919 for (hp = mfp->mf_used_last; hp != NULL; )
920 {
921 if (!(hp->bh_flags & BH_LOCKED)
922 && (!(hp->bh_flags & BH_DIRTY)
923 || mf_write(mfp, hp) != FAIL))
924 {
925 mf_rem_used(mfp, hp);
926 mf_rem_hash(mfp, hp);
927 mf_free_bhdr(hp);
928 hp = mfp->mf_used_last; /* re-start, list was changed */
929 retval = TRUE;
930 }
931 else
932 hp = hp->bh_prev;
933 }
934 }
935 }
936 }
937 return retval;
938}
939
940/*
941 * Allocate a block header and a block of memory for it
942 */
943 static bhdr_T *
944mf_alloc_bhdr(mfp, page_count)
945 memfile_T *mfp;
946 int page_count;
947{
948 bhdr_T *hp;
949
950 if ((hp = (bhdr_T *)alloc((unsigned)sizeof(bhdr_T))) != NULL)
951 {
952 if ((hp->bh_data = (char_u *)alloc(mfp->mf_page_size * page_count))
953 == NULL)
954 {
955 vim_free(hp); /* not enough memory */
956 return NULL;
957 }
958 hp->bh_page_count = page_count;
959 }
960 return hp;
961}
962
963/*
964 * Free a block header and the block of memory for it
965 */
966 static void
967mf_free_bhdr(hp)
968 bhdr_T *hp;
969{
970 vim_free(hp->bh_data);
971 vim_free(hp);
972}
973
974/*
975 * insert entry *hp in the free list
976 */
977 static void
978mf_ins_free(mfp, hp)
979 memfile_T *mfp;
980 bhdr_T *hp;
981{
982 hp->bh_next = mfp->mf_free_first;
983 mfp->mf_free_first = hp;
984}
985
986/*
987 * remove the first entry from the free list and return a pointer to it
988 * Note: caller must check that mfp->mf_free_first is not NULL!
989 */
990 static bhdr_T *
991mf_rem_free(mfp)
992 memfile_T *mfp;
993{
994 bhdr_T *hp;
995
996 hp = mfp->mf_free_first;
997 mfp->mf_free_first = hp->bh_next;
998 return hp;
999}
1000
1001/*
1002 * read a block from disk
1003 *
1004 * Return FAIL for failure, OK otherwise
1005 */
1006 static int
1007mf_read(mfp, hp)
1008 memfile_T *mfp;
1009 bhdr_T *hp;
1010{
1011 off_t offset;
1012 unsigned page_size;
1013 unsigned size;
1014
1015 if (mfp->mf_fd < 0) /* there is no file, can't read */
1016 return FAIL;
1017
1018 page_size = mfp->mf_page_size;
1019 offset = (off_t)page_size * hp->bh_bnum;
1020 size = page_size * hp->bh_page_count;
1021 if (lseek(mfp->mf_fd, offset, SEEK_SET) != offset)
1022 {
Bram Moolenaar3f2d9812006-11-07 17:02:30 +00001023 PERROR(_("E294: Seek error in swap file read"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001024 return FAIL;
1025 }
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01001026 if ((unsigned)read_eintr(mfp->mf_fd, hp->bh_data, size) != size)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001027 {
Bram Moolenaar3f2d9812006-11-07 17:02:30 +00001028 PERROR(_("E295: Read error in swap file"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001029 return FAIL;
1030 }
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02001031
1032#ifdef FEAT_CRYPT
1033 /* Decrypt if 'key' is set and this is a data block. */
1034 if (*mfp->mf_buffer->b_p_key != NUL)
1035 ml_decrypt_data(mfp, hp->bh_data, offset, size);
1036#endif
1037
Bram Moolenaar071d4272004-06-13 20:20:40 +00001038 return OK;
1039}
1040
1041/*
1042 * write a block to disk
1043 *
1044 * Return FAIL for failure, OK otherwise
1045 */
1046 static int
1047mf_write(mfp, hp)
1048 memfile_T *mfp;
1049 bhdr_T *hp;
1050{
1051 off_t offset; /* offset in the file */
1052 blocknr_T nr; /* block nr which is being written */
1053 bhdr_T *hp2;
1054 unsigned page_size; /* number of bytes in a page */
1055 unsigned page_count; /* number of pages written */
1056 unsigned size; /* number of bytes written */
1057
1058 if (mfp->mf_fd < 0) /* there is no file, can't write */
1059 return FAIL;
1060
1061 if (hp->bh_bnum < 0) /* must assign file block number */
1062 if (mf_trans_add(mfp, hp) == FAIL)
1063 return FAIL;
1064
1065 page_size = mfp->mf_page_size;
1066
1067 /*
1068 * We don't want gaps in the file. Write the blocks in front of *hp
1069 * to extend the file.
1070 * If block 'mf_infile_count' is not in the hash list, it has been
1071 * freed. Fill the space in the file with data from the current block.
1072 */
1073 for (;;)
1074 {
1075 nr = hp->bh_bnum;
1076 if (nr > mfp->mf_infile_count) /* beyond end of file */
1077 {
1078 nr = mfp->mf_infile_count;
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001079 hp2 = mf_find_hash(mfp, nr); /* NULL caught below */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001080 }
1081 else
1082 hp2 = hp;
1083
1084 offset = (off_t)page_size * nr;
1085 if (lseek(mfp->mf_fd, offset, SEEK_SET) != offset)
1086 {
Bram Moolenaar3f2d9812006-11-07 17:02:30 +00001087 PERROR(_("E296: Seek error in swap file write"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001088 return FAIL;
1089 }
1090 if (hp2 == NULL) /* freed block, fill with dummy data */
1091 page_count = 1;
1092 else
1093 page_count = hp2->bh_page_count;
1094 size = page_size * page_count;
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02001095 if (mf_write_block(mfp, hp2 == NULL ? hp : hp2, offset, size) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001096 {
1097 /*
1098 * Avoid repeating the error message, this mostly happens when the
Bram Moolenaar49325942007-05-10 19:19:59 +00001099 * disk is full. We give the message again only after a successful
Bram Moolenaar071d4272004-06-13 20:20:40 +00001100 * write or when hitting a key. We keep on trying, in case some
1101 * space becomes available.
1102 */
1103 if (!did_swapwrite_msg)
1104 EMSG(_("E297: Write error in swap file"));
1105 did_swapwrite_msg = TRUE;
1106 return FAIL;
1107 }
1108 did_swapwrite_msg = FALSE;
1109 if (hp2 != NULL) /* written a non-dummy block */
1110 hp2->bh_flags &= ~BH_DIRTY;
1111 /* appended to the file */
1112 if (nr + (blocknr_T)page_count > mfp->mf_infile_count)
1113 mfp->mf_infile_count = nr + page_count;
1114 if (nr == hp->bh_bnum) /* written the desired block */
1115 break;
1116 }
1117 return OK;
1118}
1119
1120/*
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02001121 * Write block "hp" with data size "size" to file "mfp->mf_fd".
1122 * Takes care of encryption.
1123 * Return FAIL or OK.
1124 */
1125 static int
1126mf_write_block(mfp, hp, offset, size)
1127 memfile_T *mfp;
1128 bhdr_T *hp;
1129 off_t offset UNUSED;
1130 unsigned size;
1131{
1132 char_u *data = hp->bh_data;
1133 int result = OK;
1134
1135#ifdef FEAT_CRYPT
1136 /* Encrypt if 'key' is set and this is a data block. */
1137 if (*mfp->mf_buffer->b_p_key != NUL)
1138 {
1139 data = ml_encrypt_data(mfp, data, offset, size);
1140 if (data == NULL)
1141 return FAIL;
1142 }
1143#endif
1144
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01001145 if ((unsigned)write_eintr(mfp->mf_fd, data, size) != size)
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02001146 result = FAIL;
1147
1148#ifdef FEAT_CRYPT
1149 if (data != hp->bh_data)
1150 vim_free(data);
1151#endif
1152
1153 return result;
1154}
1155
1156/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001157 * Make block number for *hp positive and add it to the translation list
1158 *
1159 * Return FAIL for failure, OK otherwise
1160 */
1161 static int
1162mf_trans_add(mfp, hp)
1163 memfile_T *mfp;
1164 bhdr_T *hp;
1165{
1166 bhdr_T *freep;
1167 blocknr_T new_bnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001168 NR_TRANS *np;
1169 int page_count;
1170
1171 if (hp->bh_bnum >= 0) /* it's already positive */
1172 return OK;
1173
1174 if ((np = (NR_TRANS *)alloc((unsigned)sizeof(NR_TRANS))) == NULL)
1175 return FAIL;
1176
1177/*
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02001178 * Get a new number for the block.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001179 * If the first item in the free list has sufficient pages, use its number
1180 * Otherwise use mf_blocknr_max.
1181 */
1182 freep = mfp->mf_free_first;
1183 page_count = hp->bh_page_count;
1184 if (freep != NULL && freep->bh_page_count >= page_count)
1185 {
1186 new_bnum = freep->bh_bnum;
1187 /*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001188 * If the page count of the free block was larger, reduce it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001189 * If the page count matches, remove the block from the free list
1190 */
1191 if (freep->bh_page_count > page_count)
1192 {
1193 freep->bh_bnum += page_count;
1194 freep->bh_page_count -= page_count;
1195 }
1196 else
1197 {
1198 freep = mf_rem_free(mfp);
1199 vim_free(freep);
1200 }
1201 }
1202 else
1203 {
1204 new_bnum = mfp->mf_blocknr_max;
1205 mfp->mf_blocknr_max += page_count;
1206 }
1207
1208 np->nt_old_bnum = hp->bh_bnum; /* adjust number */
1209 np->nt_new_bnum = new_bnum;
1210
1211 mf_rem_hash(mfp, hp); /* remove from old hash list */
1212 hp->bh_bnum = new_bnum;
1213 mf_ins_hash(mfp, hp); /* insert in new hash list */
1214
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01001215 /* Insert "np" into "mf_trans" hashtable with key "np->nt_old_bnum" */
1216 mf_hash_add_item(&mfp->mf_trans, (mf_hashitem_T *)np);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001217
1218 return OK;
1219}
1220
1221/*
Bram Moolenaar49325942007-05-10 19:19:59 +00001222 * Lookup a translation from the trans lists and delete the entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00001223 *
1224 * Return the positive new number when found, the old number when not found
1225 */
1226 blocknr_T
1227mf_trans_del(mfp, old_nr)
1228 memfile_T *mfp;
1229 blocknr_T old_nr;
1230{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001231 NR_TRANS *np;
1232 blocknr_T new_bnum;
1233
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01001234 np = (NR_TRANS *)mf_hash_find(&mfp->mf_trans, old_nr);
1235
Bram Moolenaar071d4272004-06-13 20:20:40 +00001236 if (np == NULL) /* not found */
1237 return old_nr;
1238
1239 mfp->mf_neg_count--;
1240 new_bnum = np->nt_new_bnum;
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01001241
1242 /* remove entry from the trans list */
1243 mf_hash_rem_item(&mfp->mf_trans, (mf_hashitem_T *)np);
1244
Bram Moolenaar071d4272004-06-13 20:20:40 +00001245 vim_free(np);
1246
1247 return new_bnum;
1248}
1249
1250/*
1251 * Set mfp->mf_ffname according to mfp->mf_fname and some other things.
1252 * Only called when creating or renaming the swapfile. Either way it's a new
1253 * name so we must work out the full path name.
1254 */
1255 void
1256mf_set_ffname(mfp)
1257 memfile_T *mfp;
1258{
1259 mfp->mf_ffname = FullName_save(mfp->mf_fname, FALSE);
1260}
1261
1262/*
1263 * Make the name of the file used for the memfile a full path.
1264 * Used before doing a :cd
1265 */
1266 void
1267mf_fullname(mfp)
1268 memfile_T *mfp;
1269{
1270 if (mfp != NULL && mfp->mf_fname != NULL && mfp->mf_ffname != NULL)
1271 {
1272 vim_free(mfp->mf_fname);
1273 mfp->mf_fname = mfp->mf_ffname;
1274 mfp->mf_ffname = NULL;
1275 }
1276}
1277
1278/*
1279 * return TRUE if there are any translations pending for 'mfp'
1280 */
1281 int
1282mf_need_trans(mfp)
1283 memfile_T *mfp;
1284{
1285 return (mfp->mf_fname != NULL && mfp->mf_neg_count > 0);
1286}
1287
1288/*
1289 * Open a swap file for a memfile.
1290 * The "fname" must be in allocated memory, and is consumed (also when an
1291 * error occurs).
1292 */
1293 static void
1294mf_do_open(mfp, fname, flags)
1295 memfile_T *mfp;
1296 char_u *fname;
1297 int flags; /* flags for open() */
1298{
1299#ifdef HAVE_LSTAT
1300 struct stat sb;
1301#endif
1302
1303 mfp->mf_fname = fname;
1304
1305 /*
1306 * Get the full path name before the open, because this is
1307 * not possible after the open on the Amiga.
1308 * fname cannot be NameBuff, because it must have been allocated.
1309 */
1310 mf_set_ffname(mfp);
Bram Moolenaare60acc12011-05-10 16:41:25 +02001311#if defined(MSDOS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001312 /*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001313 * A ":!cd e:xxx" may change the directory without us knowing, use the
Bram Moolenaar071d4272004-06-13 20:20:40 +00001314 * full pathname always. Careful: This frees fname!
1315 */
1316 mf_fullname(mfp);
1317#endif
1318
1319#ifdef HAVE_LSTAT
1320 /*
1321 * Extra security check: When creating a swap file it really shouldn't
1322 * exist yet. If there is a symbolic link, this is most likely an attack.
1323 */
1324 if ((flags & O_CREAT) && mch_lstat((char *)mfp->mf_fname, &sb) >= 0)
1325 {
1326 mfp->mf_fd = -1;
1327 EMSG(_("E300: Swap file already exists (symlink attack?)"));
1328 }
1329 else
1330#endif
1331 {
1332 /*
1333 * try to open the file
1334 */
Bram Moolenaara5792f52005-11-23 21:25:05 +00001335 flags |= O_EXTRA | O_NOFOLLOW;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001336#ifdef WIN32
1337 /* Prevent handle inheritance that cause problems with Cscope
1338 * (swap file may not be deleted if cscope connection was open after
1339 * the file) */
1340 flags |= O_NOINHERIT;
1341#endif
1342 mfp->mf_fd = mch_open_rw((char *)mfp->mf_fname, flags);
1343 }
1344
1345 /*
1346 * If the file cannot be opened, use memory only
1347 */
1348 if (mfp->mf_fd < 0)
1349 {
1350 vim_free(mfp->mf_fname);
1351 vim_free(mfp->mf_ffname);
1352 mfp->mf_fname = NULL;
1353 mfp->mf_ffname = NULL;
1354 }
1355 else
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00001356 {
Bram Moolenaarf05da212009-11-17 16:13:15 +00001357#ifdef HAVE_FD_CLOEXEC
1358 int fdflags = fcntl(mfp->mf_fd, F_GETFD);
1359 if (fdflags >= 0 && (fdflags & FD_CLOEXEC) == 0)
1360 fcntl(mfp->mf_fd, F_SETFD, fdflags | FD_CLOEXEC);
1361#endif
Bram Moolenaar5bd32f42014-04-02 14:05:38 +02001362#if defined(HAVE_SELINUX) || defined(HAVE_SMACK)
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00001363 mch_copy_sec(fname, mfp->mf_fname);
1364#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001365 mch_hide(mfp->mf_fname); /* try setting the 'hidden' flag */
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00001366 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001367}
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01001368
1369/*
1370 * Implementation of mf_hashtab_T follows.
1371 */
1372
1373/*
1374 * The number of buckets in the hashtable is increased by a factor of
1375 * MHT_GROWTH_FACTOR when the average number of items per bucket
1376 * exceeds 2 ^ MHT_LOG_LOAD_FACTOR.
1377 */
1378#define MHT_LOG_LOAD_FACTOR 6
1379#define MHT_GROWTH_FACTOR 2 /* must be a power of two */
1380
1381/*
1382 * Initialize an empty hash table.
1383 */
1384 static void
1385mf_hash_init(mht)
1386 mf_hashtab_T *mht;
1387{
1388 vim_memset(mht, 0, sizeof(mf_hashtab_T));
1389 mht->mht_buckets = mht->mht_small_buckets;
1390 mht->mht_mask = MHT_INIT_SIZE - 1;
1391}
1392
1393/*
1394 * Free the array of a hash table. Does not free the items it contains!
1395 * The hash table must not be used again without another mf_hash_init() call.
1396 */
1397 static void
1398mf_hash_free(mht)
1399 mf_hashtab_T *mht;
1400{
1401 if (mht->mht_buckets != mht->mht_small_buckets)
1402 vim_free(mht->mht_buckets);
1403}
1404
1405/*
1406 * Free the array of a hash table and all the items it contains.
1407 */
1408 static void
1409mf_hash_free_all(mht)
1410 mf_hashtab_T *mht;
1411{
1412 long_u idx;
1413 mf_hashitem_T *mhi;
1414 mf_hashitem_T *next;
1415
1416 for (idx = 0; idx <= mht->mht_mask; idx++)
1417 for (mhi = mht->mht_buckets[idx]; mhi != NULL; mhi = next)
1418 {
1419 next = mhi->mhi_next;
1420 vim_free(mhi);
1421 }
1422
1423 mf_hash_free(mht);
1424}
1425
1426/*
1427 * Find "key" in hashtable "mht".
1428 * Returns a pointer to a mf_hashitem_T or NULL if the item was not found.
1429 */
1430 static mf_hashitem_T *
1431mf_hash_find(mht, key)
1432 mf_hashtab_T *mht;
1433 blocknr_T key;
1434{
1435 mf_hashitem_T *mhi;
1436
1437 mhi = mht->mht_buckets[key & mht->mht_mask];
1438 while (mhi != NULL && mhi->mhi_key != key)
1439 mhi = mhi->mhi_next;
1440
1441 return mhi;
1442}
1443
1444/*
1445 * Add item "mhi" to hashtable "mht".
1446 * "mhi" must not be NULL.
1447 */
1448 static void
1449mf_hash_add_item(mht, mhi)
1450 mf_hashtab_T *mht;
1451 mf_hashitem_T *mhi;
1452{
1453 long_u idx;
1454
1455 idx = mhi->mhi_key & mht->mht_mask;
1456 mhi->mhi_next = mht->mht_buckets[idx];
1457 mhi->mhi_prev = NULL;
1458 if (mhi->mhi_next != NULL)
1459 mhi->mhi_next->mhi_prev = mhi;
1460 mht->mht_buckets[idx] = mhi;
1461
1462 mht->mht_count++;
1463
1464 /*
1465 * Grow hashtable when we have more thank 2^MHT_LOG_LOAD_FACTOR
1466 * items per bucket on average
1467 */
1468 if (mht->mht_fixed == 0
1469 && (mht->mht_count >> MHT_LOG_LOAD_FACTOR) > mht->mht_mask)
1470 {
1471 if (mf_hash_grow(mht) == FAIL)
1472 {
1473 /* stop trying to grow after first failure to allocate memory */
1474 mht->mht_fixed = 1;
1475 }
1476 }
1477}
1478
1479/*
1480 * Remove item "mhi" from hashtable "mht".
1481 * "mhi" must not be NULL and must have been inserted into "mht".
1482 */
1483 static void
1484mf_hash_rem_item(mht, mhi)
1485 mf_hashtab_T *mht;
1486 mf_hashitem_T *mhi;
1487{
1488 if (mhi->mhi_prev == NULL)
1489 mht->mht_buckets[mhi->mhi_key & mht->mht_mask] = mhi->mhi_next;
1490 else
1491 mhi->mhi_prev->mhi_next = mhi->mhi_next;
1492
1493 if (mhi->mhi_next != NULL)
1494 mhi->mhi_next->mhi_prev = mhi->mhi_prev;
1495
1496 mht->mht_count--;
1497
1498 /* We could shrink the table here, but it typically takes little memory,
1499 * so why bother? */
1500}
1501
1502/*
1503 * Increase number of buckets in the hashtable by MHT_GROWTH_FACTOR and
1504 * rehash items.
1505 * Returns FAIL when out of memory.
1506 */
1507 static int
1508mf_hash_grow(mht)
1509 mf_hashtab_T *mht;
1510{
1511 long_u i, j;
1512 int shift;
1513 mf_hashitem_T *mhi;
1514 mf_hashitem_T *tails[MHT_GROWTH_FACTOR];
1515 mf_hashitem_T **buckets;
1516 size_t size;
1517
1518 size = (mht->mht_mask + 1) * MHT_GROWTH_FACTOR * sizeof(void *);
1519 buckets = (mf_hashitem_T **)lalloc_clear(size, FALSE);
1520 if (buckets == NULL)
1521 return FAIL;
1522
1523 shift = 0;
1524 while ((mht->mht_mask >> shift) != 0)
1525 shift++;
1526
1527 for (i = 0; i <= mht->mht_mask; i++)
1528 {
1529 /*
1530 * Traverse the items in the i-th original bucket and move them into
1531 * MHT_GROWTH_FACTOR new buckets, preserving their relative order
1532 * within each new bucket. Preserving the order is important because
1533 * mf_get() tries to keep most recently used items at the front of
1534 * each bucket.
1535 *
1536 * Here we strongly rely on the fact the hashes are computed modulo
1537 * a power of two.
1538 */
1539
1540 vim_memset(tails, 0, sizeof(tails));
1541
1542 for (mhi = mht->mht_buckets[i]; mhi != NULL; mhi = mhi->mhi_next)
1543 {
1544 j = (mhi->mhi_key >> shift) & (MHT_GROWTH_FACTOR - 1);
1545 if (tails[j] == NULL)
1546 {
1547 buckets[i + (j << shift)] = mhi;
1548 tails[j] = mhi;
1549 mhi->mhi_prev = NULL;
1550 }
1551 else
1552 {
1553 tails[j]->mhi_next = mhi;
1554 mhi->mhi_prev = tails[j];
1555 tails[j] = mhi;
1556 }
1557 }
1558
1559 for (j = 0; j < MHT_GROWTH_FACTOR; j++)
1560 if (tails[j] != NULL)
1561 tails[j]->mhi_next = NULL;
1562 }
1563
1564 if (mht->mht_buckets != mht->mht_small_buckets)
1565 vim_free(mht->mht_buckets);
1566
1567 mht->mht_buckets = buckets;
1568 mht->mht_mask = (mht->mht_mask + 1) * MHT_GROWTH_FACTOR - 1;
1569
1570 return OK;
1571}