blob: d0dd8deef3fb2aa6d621642a9db1bf5f78d6a06e [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
35#if defined MSDOS || defined(WIN32) || defined(_WIN64)
Bram Moolenaar362e1a32006-03-06 23:29:24 +000036# include "vimio.h" /* for lseek(), must be before vim.h */
Bram Moolenaar071d4272004-06-13 20:20:40 +000037#endif
38
39#include "vim.h"
40
41#ifdef HAVE_FCNTL_H
42# include <fcntl.h>
43#endif
44
45/*
46 * Some systems have the page size in statfs.f_bsize, some in stat.st_blksize
47 */
48#ifdef HAVE_ST_BLKSIZE
49# define STATFS stat
50# define F_BSIZE st_blksize
51# define fstatfs(fd, buf, len, nul) mch_fstat((fd), (buf))
52#else
53# ifdef HAVE_SYS_STATFS_H
54# include <sys/statfs.h>
55# define STATFS statfs
56# define F_BSIZE f_bsize
57# ifdef __MINT__ /* do we still need this? */
58# define fstatfs(fd, buf, len, nul) mch_fstat((fd), (buf))
59# endif
60# endif
61#endif
62
63/*
64 * for Amiga Dos 2.0x we use Flush
65 */
66#ifdef AMIGA
67# ifdef FEAT_ARP
68extern int dos2; /* this is in os_amiga.c */
69# endif
70# ifdef SASC
71# include <proto/dos.h>
72# include <ios1.h> /* for chkufb() */
73# endif
74#endif
75
76#define MEMFILE_PAGE_SIZE 4096 /* default page size */
77
78static long_u total_mem_used = 0; /* total memory used for memfiles */
Bram Moolenaar071d4272004-06-13 20:20:40 +000079
80static void mf_ins_hash __ARGS((memfile_T *, bhdr_T *));
81static void mf_rem_hash __ARGS((memfile_T *, bhdr_T *));
82static bhdr_T *mf_find_hash __ARGS((memfile_T *, blocknr_T));
83static void mf_ins_used __ARGS((memfile_T *, bhdr_T *));
84static void mf_rem_used __ARGS((memfile_T *, bhdr_T *));
85static bhdr_T *mf_release __ARGS((memfile_T *, int));
86static bhdr_T *mf_alloc_bhdr __ARGS((memfile_T *, int));
87static void mf_free_bhdr __ARGS((bhdr_T *));
88static void mf_ins_free __ARGS((memfile_T *, bhdr_T *));
89static bhdr_T *mf_rem_free __ARGS((memfile_T *));
90static int mf_read __ARGS((memfile_T *, bhdr_T *));
91static int mf_write __ARGS((memfile_T *, bhdr_T *));
92static int mf_trans_add __ARGS((memfile_T *, bhdr_T *));
93static void mf_do_open __ARGS((memfile_T *, char_u *, int));
94
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;
129 int i;
130 off_t size;
131#if defined(STATFS) && defined(UNIX) && !defined(__QNX__)
132# define USE_FSTATFS
133 struct STATFS stf;
134#endif
135
136 if ((mfp = (memfile_T *)alloc((unsigned)sizeof(memfile_T))) == NULL)
137 return NULL;
138
139 if (fname == NULL) /* no file for this memfile, use memory only */
140 {
141 mfp->mf_fname = NULL;
142 mfp->mf_ffname = NULL;
143 mfp->mf_fd = -1;
144 }
145 else
146 {
147 mf_do_open(mfp, fname, flags); /* try to open the file */
148
149 /* if the file cannot be opened, return here */
150 if (mfp->mf_fd < 0)
151 {
152 vim_free(mfp);
153 return NULL;
154 }
155 }
156
157 mfp->mf_free_first = NULL; /* free list is empty */
158 mfp->mf_used_first = NULL; /* used list is empty */
159 mfp->mf_used_last = NULL;
160 mfp->mf_dirty = FALSE;
161 mfp->mf_used_count = 0;
162 for (i = 0; i < MEMHASHSIZE; ++i)
163 {
164 mfp->mf_hash[i] = NULL; /* hash lists are empty */
165 mfp->mf_trans[i] = NULL; /* trans lists are empty */
166 }
167 mfp->mf_page_size = MEMFILE_PAGE_SIZE;
168
169#ifdef USE_FSTATFS
170 /*
171 * Try to set the page size equal to the block size of the device.
172 * Speeds up I/O a lot.
173 * When recovering, the actual block size will be retrieved from block 0
174 * in ml_recover(). The size used here may be wrong, therefore
175 * mf_blocknr_max must be rounded up.
176 */
177 if (mfp->mf_fd >= 0
178 && fstatfs(mfp->mf_fd, &stf, sizeof(struct statfs), 0) == 0
179 && stf.F_BSIZE >= MIN_SWAP_PAGE_SIZE
180 && stf.F_BSIZE <= MAX_SWAP_PAGE_SIZE)
181 mfp->mf_page_size = stf.F_BSIZE;
182#endif
183
184 if (mfp->mf_fd < 0 || (flags & (O_TRUNC|O_EXCL))
185 || (size = lseek(mfp->mf_fd, (off_t)0L, SEEK_END)) <= 0)
186 mfp->mf_blocknr_max = 0; /* no file or empty file */
187 else
188 mfp->mf_blocknr_max = (blocknr_T)((size + mfp->mf_page_size - 1)
189 / mfp->mf_page_size);
190 mfp->mf_blocknr_min = -1;
191 mfp->mf_neg_count = 0;
192 mfp->mf_infile_count = mfp->mf_blocknr_max;
Bram Moolenaarfe265ff2007-05-11 18:15:45 +0000193
194 /*
195 * Compute maximum number of pages ('maxmem' is in Kbyte):
196 * 'mammem' * 1Kbyte / page-size-in-bytes.
197 * Avoid overflow by first reducing page size as much as possible.
198 */
199 {
200 int shift = 10;
201 unsigned page_size = mfp->mf_page_size;
202
203 while (shift > 0 && (page_size & 1) == 0)
204 {
205 page_size = page_size >> 1;
206 --shift;
207 }
208 mfp->mf_used_count_max = (p_mm << shift) / page_size;
209 if (mfp->mf_used_count_max < 10)
210 mfp->mf_used_count_max = 10;
211 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000212
213 return mfp;
214}
215
216/*
217 * Open a file for an existing memfile. Used when updatecount set from 0 to
218 * some value.
219 * If the file already exists, this fails.
220 * "fname" is the name of file to use (NULL means no file at all)
221 * Note: "fname" must have been allocated, it is not copied! If opening the
222 * file fails, "fname" is freed.
223 *
224 * return value: FAIL if file could not be opened, OK otherwise
225 */
226 int
227mf_open_file(mfp, fname)
228 memfile_T *mfp;
229 char_u *fname;
230{
231 mf_do_open(mfp, fname, O_RDWR|O_CREAT|O_EXCL); /* try to open the file */
232
233 if (mfp->mf_fd < 0)
234 return FAIL;
235
236 mfp->mf_dirty = TRUE;
237 return OK;
238}
239
240/*
241 * close a memory file and delete the associated file if 'del_file' is TRUE
242 */
243 void
244mf_close(mfp, del_file)
245 memfile_T *mfp;
246 int del_file;
247{
248 bhdr_T *hp, *nextp;
249 NR_TRANS *tp, *tpnext;
250 int i;
251
252 if (mfp == NULL) /* safety check */
253 return;
254 if (mfp->mf_fd >= 0)
255 {
256 if (close(mfp->mf_fd) < 0)
257 EMSG(_(e_swapclose));
258 }
259 if (del_file && mfp->mf_fname != NULL)
260 mch_remove(mfp->mf_fname);
261 /* free entries in used list */
262 for (hp = mfp->mf_used_first; hp != NULL; hp = nextp)
263 {
264 total_mem_used -= hp->bh_page_count * mfp->mf_page_size;
265 nextp = hp->bh_next;
266 mf_free_bhdr(hp);
267 }
268 while (mfp->mf_free_first != NULL) /* free entries in free list */
269 vim_free(mf_rem_free(mfp));
270 for (i = 0; i < MEMHASHSIZE; ++i) /* free entries in trans lists */
271 for (tp = mfp->mf_trans[i]; tp != NULL; tp = tpnext)
272 {
273 tpnext = tp->nt_next;
274 vim_free(tp);
275 }
276 vim_free(mfp->mf_fname);
277 vim_free(mfp->mf_ffname);
278 vim_free(mfp);
279}
280
281/*
282 * Close the swap file for a memfile. Used when 'swapfile' is reset.
283 */
284 void
285mf_close_file(buf, getlines)
286 buf_T *buf;
287 int getlines; /* get all lines into memory? */
288{
289 memfile_T *mfp;
290 linenr_T lnum;
291
292 mfp = buf->b_ml.ml_mfp;
293 if (mfp == NULL || mfp->mf_fd < 0) /* nothing to close */
294 return;
295
296 if (getlines)
297 {
298 /* get all blocks in memory by accessing all lines (clumsy!) */
Bram Moolenaar47b8b152007-02-07 02:41:57 +0000299 mf_dont_release = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000300 for (lnum = 1; lnum <= buf->b_ml.ml_line_count; ++lnum)
301 (void)ml_get_buf(buf, lnum, FALSE);
Bram Moolenaar47b8b152007-02-07 02:41:57 +0000302 mf_dont_release = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000303 /* TODO: should check if all blocks are really in core */
304 }
305
306 if (close(mfp->mf_fd) < 0) /* close the file */
307 EMSG(_(e_swapclose));
308 mfp->mf_fd = -1;
309
310 if (mfp->mf_fname != NULL)
311 {
312 mch_remove(mfp->mf_fname); /* delete the swap file */
313 vim_free(mfp->mf_fname);
314 vim_free(mfp->mf_ffname);
315 mfp->mf_fname = NULL;
316 mfp->mf_ffname = NULL;
317 }
318}
319
320/*
321 * Set new size for a memfile. Used when block 0 of a swapfile has been read
322 * and the size it indicates differs from what was guessed.
323 */
324 void
325mf_new_page_size(mfp, new_size)
326 memfile_T *mfp;
327 unsigned new_size;
328{
329 /* Correct the memory used for block 0 to the new size, because it will be
330 * freed with that size later on. */
331 total_mem_used += new_size - mfp->mf_page_size;
332 mfp->mf_page_size = new_size;
333}
334
335/*
336 * get a new block
337 *
338 * negative: TRUE if negative block number desired (data block)
339 */
340 bhdr_T *
341mf_new(mfp, negative, page_count)
342 memfile_T *mfp;
343 int negative;
344 int page_count;
345{
Bram Moolenaar17e79192007-05-06 14:15:36 +0000346 bhdr_T *hp; /* new bhdr_T */
347 bhdr_T *freep; /* first block in free list */
348 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000349
350 /*
351 * If we reached the maximum size for the used memory blocks, release one
352 * If a bhdr_T is returned, use it and adjust the page_count if necessary.
353 */
354 hp = mf_release(mfp, page_count);
355
356/*
357 * Decide on the number to use:
358 * If there is a free block, use its number.
359 * Otherwise use mf_block_min for a negative number, mf_block_max for
360 * a positive number.
361 */
362 freep = mfp->mf_free_first;
363 if (!negative && freep != NULL && freep->bh_page_count >= page_count)
364 {
365 /*
366 * If the block in the free list has more pages, take only the number
367 * of pages needed and allocate a new bhdr_T with data
368 *
Bram Moolenaar17e79192007-05-06 14:15:36 +0000369 * If the number of pages matches and mf_release() did not return a
370 * bhdr_T, use the bhdr_T from the free list and allocate the data
Bram Moolenaar071d4272004-06-13 20:20:40 +0000371 *
Bram Moolenaar17e79192007-05-06 14:15:36 +0000372 * If the number of pages matches and mf_release() returned a bhdr_T,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000373 * just use the number and free the bhdr_T from the free list
374 */
375 if (freep->bh_page_count > page_count)
376 {
377 if (hp == NULL && (hp = mf_alloc_bhdr(mfp, page_count)) == NULL)
378 return NULL;
379 hp->bh_bnum = freep->bh_bnum;
380 freep->bh_bnum += page_count;
381 freep->bh_page_count -= page_count;
382 }
383 else if (hp == NULL) /* need to allocate memory for this block */
384 {
385 if ((p = (char_u *)alloc(mfp->mf_page_size * page_count)) == NULL)
386 return NULL;
387 hp = mf_rem_free(mfp);
388 hp->bh_data = p;
389 }
390 else /* use the number, remove entry from free list */
391 {
392 freep = mf_rem_free(mfp);
393 hp->bh_bnum = freep->bh_bnum;
394 vim_free(freep);
395 }
396 }
397 else /* get a new number */
398 {
399 if (hp == NULL && (hp = mf_alloc_bhdr(mfp, page_count)) == NULL)
400 return NULL;
401 if (negative)
402 {
403 hp->bh_bnum = mfp->mf_blocknr_min--;
404 mfp->mf_neg_count++;
405 }
406 else
407 {
408 hp->bh_bnum = mfp->mf_blocknr_max;
409 mfp->mf_blocknr_max += page_count;
410 }
411 }
412 hp->bh_flags = BH_LOCKED | BH_DIRTY; /* new block is always dirty */
413 mfp->mf_dirty = TRUE;
414 hp->bh_page_count = page_count;
415 mf_ins_used(mfp, hp);
416 mf_ins_hash(mfp, hp);
417
418 /*
419 * Init the data to all zero, to avoid reading uninitialized data.
420 * This also avoids that the passwd file ends up in the swap file!
421 */
422 (void)vim_memset((char *)(hp->bh_data), 0, (size_t)mfp->mf_page_size);
423
424 return hp;
425}
426
427/*
428 * get existing block 'nr' with 'page_count' pages
429 *
430 * Note: The caller should first check a negative nr with mf_trans_del()
431 */
432 bhdr_T *
433mf_get(mfp, nr, page_count)
434 memfile_T *mfp;
435 blocknr_T nr;
436 int page_count;
437{
438 bhdr_T *hp;
439 /* doesn't exist */
440 if (nr >= mfp->mf_blocknr_max || nr <= mfp->mf_blocknr_min)
441 return NULL;
442
443 /*
444 * see if it is in the cache
445 */
446 hp = mf_find_hash(mfp, nr);
447 if (hp == NULL) /* not in the hash list */
448 {
449 if (nr < 0 || nr >= mfp->mf_infile_count) /* can't be in the file */
450 return NULL;
451
452 /* could check here if the block is in the free list */
453
454 /*
455 * Check if we need to flush an existing block.
456 * If so, use that block.
457 * If not, allocate a new block.
458 */
459 hp = mf_release(mfp, page_count);
460 if (hp == NULL && (hp = mf_alloc_bhdr(mfp, page_count)) == NULL)
461 return NULL;
462
463 hp->bh_bnum = nr;
464 hp->bh_flags = 0;
465 hp->bh_page_count = page_count;
466 if (mf_read(mfp, hp) == FAIL) /* cannot read the block! */
467 {
468 mf_free_bhdr(hp);
469 return NULL;
470 }
471 }
472 else
473 {
474 mf_rem_used(mfp, hp); /* remove from list, insert in front below */
475 mf_rem_hash(mfp, hp);
476 }
477
478 hp->bh_flags |= BH_LOCKED;
479 mf_ins_used(mfp, hp); /* put in front of used list */
480 mf_ins_hash(mfp, hp); /* put in front of hash list */
481
482 return hp;
483}
484
485/*
486 * release the block *hp
487 *
488 * dirty: Block must be written to file later
489 * infile: Block should be in file (needed for recovery)
490 *
491 * no return value, function cannot fail
492 */
493 void
494mf_put(mfp, hp, dirty, infile)
495 memfile_T *mfp;
496 bhdr_T *hp;
497 int dirty;
498 int infile;
499{
500 int flags;
501
502 flags = hp->bh_flags;
503
504 if ((flags & BH_LOCKED) == 0)
505 EMSG(_("E293: block was not locked"));
506 flags &= ~BH_LOCKED;
507 if (dirty)
508 {
509 flags |= BH_DIRTY;
510 mfp->mf_dirty = TRUE;
511 }
512 hp->bh_flags = flags;
513 if (infile)
514 mf_trans_add(mfp, hp); /* may translate negative in positive nr */
515}
516
517/*
518 * block *hp is no longer in used, may put it in the free list of memfile *mfp
519 */
520 void
521mf_free(mfp, hp)
522 memfile_T *mfp;
523 bhdr_T *hp;
524{
525 vim_free(hp->bh_data); /* free the memory */
526 mf_rem_hash(mfp, hp); /* get *hp out of the hash list */
527 mf_rem_used(mfp, hp); /* get *hp out of the used list */
528 if (hp->bh_bnum < 0)
529 {
530 vim_free(hp); /* don't want negative numbers in free list */
531 mfp->mf_neg_count--;
532 }
533 else
534 mf_ins_free(mfp, hp); /* put *hp in the free list */
535}
536
Bram Moolenaar89f37272006-09-26 11:48:34 +0000537#if defined(__MORPHOS__) && defined(__libnix__)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000538/* function is missing in MorphOS libnix version */
539extern unsigned long *__stdfiledes;
540
541 static unsigned long
542fdtofh(int filedescriptor)
543{
544 return __stdfiledes[filedescriptor];
545}
546#endif
547
548/*
549 * Sync the memory file *mfp to disk.
550 * Flags:
551 * MFS_ALL If not given, blocks with negative numbers are not synced,
552 * even when they are dirty!
553 * MFS_STOP Stop syncing when a character becomes available, but sync at
554 * least one block.
555 * MFS_FLUSH Make sure buffers are flushed to disk, so they will survive a
556 * system crash.
557 * MFS_ZERO Only write block 0.
558 *
559 * Return FAIL for failure, OK otherwise
560 */
561 int
562mf_sync(mfp, flags)
563 memfile_T *mfp;
564 int flags;
565{
566 int status;
567 bhdr_T *hp;
568#if defined(SYNC_DUP_CLOSE) && !defined(MSDOS)
569 int fd;
570#endif
571 int got_int_save = got_int;
572
573 if (mfp->mf_fd < 0) /* there is no file, nothing to do */
574 {
575 mfp->mf_dirty = FALSE;
576 return FAIL;
577 }
578
579 /* Only a CTRL-C while writing will break us here, not one typed
580 * previously. */
581 got_int = FALSE;
582
583 /*
584 * sync from last to first (may reduce the probability of an inconsistent
585 * file) If a write fails, it is very likely caused by a full filesystem.
586 * Then we only try to write blocks within the existing file. If that also
587 * fails then we give up.
588 */
589 status = OK;
590 for (hp = mfp->mf_used_last; hp != NULL; hp = hp->bh_prev)
591 if (((flags & MFS_ALL) || hp->bh_bnum >= 0)
592 && (hp->bh_flags & BH_DIRTY)
593 && (status == OK || (hp->bh_bnum >= 0
594 && hp->bh_bnum < mfp->mf_infile_count)))
595 {
596 if ((flags & MFS_ZERO) && hp->bh_bnum != 0)
597 continue;
598 if (mf_write(mfp, hp) == FAIL)
599 {
600 if (status == FAIL) /* double error: quit syncing */
601 break;
602 status = FAIL;
603 }
604 if (flags & MFS_STOP)
605 {
606 /* Stop when char available now. */
607 if (ui_char_avail())
608 break;
609 }
610 else
611 ui_breakcheck();
612 if (got_int)
613 break;
614 }
615
616 /*
617 * If the whole list is flushed, the memfile is not dirty anymore.
618 * In case of an error this flag is also set, to avoid trying all the time.
619 */
620 if (hp == NULL || status == FAIL)
621 mfp->mf_dirty = FALSE;
622
623 if ((flags & MFS_FLUSH) && *p_sws != NUL)
624 {
625#if defined(UNIX)
626# ifdef HAVE_FSYNC
627 /*
628 * most Unixes have the very useful fsync() function, just what we need.
629 * However, with OS/2 and EMX it is also available, but there are
630 * reports of bad problems with it (a bug in HPFS.IFS).
631 * So we disable use of it here in case someone tries to be smart
632 * and changes os_os2_cfg.h... (even though there is no __EMX__ test
633 * in the #if, as __EMX__ does not have sync(); we hope for a timely
634 * sync from the system itself).
635 */
636# if defined(__EMX__)
637 error "Dont use fsync with EMX! Read emxdoc.doc or emxfix01.doc for info."
638# endif
639 if (STRCMP(p_sws, "fsync") == 0)
640 {
641 if (fsync(mfp->mf_fd))
642 status = FAIL;
643 }
644 else
645# endif
646 /* OpenNT is strictly POSIX (Benzinger) */
647 /* Tandem/Himalaya NSK-OSS doesn't have sync() */
648# if defined(__OPENNT) || defined(__TANDEM)
649 fflush(NULL);
650# else
651 sync();
652# endif
653#endif
654#ifdef VMS
655 if (STRCMP(p_sws, "fsync") == 0)
656 {
657 if (fsync(mfp->mf_fd))
658 status = FAIL;
659 }
660#endif
661#ifdef MSDOS
662 if (_dos_commit(mfp->mf_fd))
663 status = FAIL;
664#else
665# ifdef SYNC_DUP_CLOSE
666 /*
667 * Win32 is a bit more work: Duplicate the file handle and close it.
668 * This should flush the file to disk.
669 */
670 if ((fd = dup(mfp->mf_fd)) >= 0)
671 close(fd);
672# endif
673#endif
674#ifdef AMIGA
Bram Moolenaar5a6404c2006-11-01 17:12:57 +0000675# if defined(__AROS__) || defined(__amigaos4__)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000676 if (fsync(mfp->mf_fd) != 0)
677 status = FAIL;
678# else
679 /*
680 * Flush() only exists for AmigaDos 2.0.
681 * For 1.3 it should be done with close() + open(), but then the risk
682 * is that the open() may fail and lose the file....
683 */
684# ifdef FEAT_ARP
685 if (dos2)
686# endif
687# ifdef SASC
688 {
689 struct UFB *fp = chkufb(mfp->mf_fd);
690
691 if (fp != NULL)
692 Flush(fp->ufbfh);
693 }
694# else
695# if defined(_DCC) || defined(__GNUC__) || defined(__MORPHOS__)
696 {
Bram Moolenaar89f37272006-09-26 11:48:34 +0000697# if defined(__GNUC__) && !defined(__MORPHOS__) && defined(__libnix__)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000698 /* Have function (in libnix at least),
699 * but ain't got no prototype anywhere. */
700 extern unsigned long fdtofh(int filedescriptor);
701# endif
Bram Moolenaar89f37272006-09-26 11:48:34 +0000702# if !defined(__libnix__)
703 fflush(NULL);
704# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000705 BPTR fh = (BPTR)fdtofh(mfp->mf_fd);
706
707 if (fh != 0)
708 Flush(fh);
Bram Moolenaar89f37272006-09-26 11:48:34 +0000709# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000710 }
711# else /* assume Manx */
712 Flush(_devtab[mfp->mf_fd].fd);
713# endif
714# endif
715# endif
716#endif /* AMIGA */
717 }
718
719 got_int |= got_int_save;
720
721 return status;
722}
723
724/*
Bram Moolenaarc32840f2006-01-14 21:23:38 +0000725 * For all blocks in memory file *mfp that have a positive block number set
726 * the dirty flag. These are blocks that need to be written to a newly
727 * created swapfile.
728 */
729 void
730mf_set_dirty(mfp)
731 memfile_T *mfp;
732{
733 bhdr_T *hp;
734
735 for (hp = mfp->mf_used_last; hp != NULL; hp = hp->bh_prev)
736 if (hp->bh_bnum > 0)
737 hp->bh_flags |= BH_DIRTY;
738 mfp->mf_dirty = TRUE;
739}
740
741/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000742 * insert block *hp in front of hashlist of memfile *mfp
743 */
744 static void
745mf_ins_hash(mfp, hp)
746 memfile_T *mfp;
747 bhdr_T *hp;
748{
749 bhdr_T *hhp;
750 int hash;
751
752 hash = MEMHASH(hp->bh_bnum);
753 hhp = mfp->mf_hash[hash];
754 hp->bh_hash_next = hhp;
755 hp->bh_hash_prev = NULL;
756 if (hhp != NULL)
757 hhp->bh_hash_prev = hp;
758 mfp->mf_hash[hash] = hp;
759}
760
761/*
762 * remove block *hp from hashlist of memfile list *mfp
763 */
764 static void
765mf_rem_hash(mfp, hp)
766 memfile_T *mfp;
767 bhdr_T *hp;
768{
769 if (hp->bh_hash_prev == NULL)
770 mfp->mf_hash[MEMHASH(hp->bh_bnum)] = hp->bh_hash_next;
771 else
772 hp->bh_hash_prev->bh_hash_next = hp->bh_hash_next;
773
774 if (hp->bh_hash_next)
775 hp->bh_hash_next->bh_hash_prev = hp->bh_hash_prev;
776}
777
778/*
779 * look in hash lists of memfile *mfp for block header with number 'nr'
780 */
781 static bhdr_T *
782mf_find_hash(mfp, nr)
783 memfile_T *mfp;
784 blocknr_T nr;
785{
786 bhdr_T *hp;
787
788 for (hp = mfp->mf_hash[MEMHASH(nr)]; hp != NULL; hp = hp->bh_hash_next)
789 if (hp->bh_bnum == nr)
790 break;
791 return hp;
792}
793
794/*
795 * insert block *hp in front of used list of memfile *mfp
796 */
797 static void
798mf_ins_used(mfp, hp)
799 memfile_T *mfp;
800 bhdr_T *hp;
801{
802 hp->bh_next = mfp->mf_used_first;
803 mfp->mf_used_first = hp;
804 hp->bh_prev = NULL;
805 if (hp->bh_next == NULL) /* list was empty, adjust last pointer */
806 mfp->mf_used_last = hp;
807 else
808 hp->bh_next->bh_prev = hp;
809 mfp->mf_used_count += hp->bh_page_count;
810 total_mem_used += hp->bh_page_count * mfp->mf_page_size;
811}
812
813/*
814 * remove block *hp from used list of memfile *mfp
815 */
816 static void
817mf_rem_used(mfp, hp)
818 memfile_T *mfp;
819 bhdr_T *hp;
820{
821 if (hp->bh_next == NULL) /* last block in used list */
822 mfp->mf_used_last = hp->bh_prev;
823 else
824 hp->bh_next->bh_prev = hp->bh_prev;
825 if (hp->bh_prev == NULL) /* first block in used list */
826 mfp->mf_used_first = hp->bh_next;
827 else
828 hp->bh_prev->bh_next = hp->bh_next;
829 mfp->mf_used_count -= hp->bh_page_count;
830 total_mem_used -= hp->bh_page_count * mfp->mf_page_size;
831}
832
833/*
834 * Release the least recently used block from the used list if the number
835 * of used memory blocks gets to big.
836 *
837 * Return the block header to the caller, including the memory block, so
838 * it can be re-used. Make sure the page_count is right.
839 */
840 static bhdr_T *
841mf_release(mfp, page_count)
842 memfile_T *mfp;
843 int page_count;
844{
845 bhdr_T *hp;
846 int need_release;
847 buf_T *buf;
848
849 /* don't release while in mf_close_file() */
Bram Moolenaar47b8b152007-02-07 02:41:57 +0000850 if (mf_dont_release)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000851 return NULL;
852
853 /*
854 * Need to release a block if the number of blocks for this memfile is
855 * higher than the maximum or total memory used is over 'maxmemtot'
856 */
857 need_release = ((mfp->mf_used_count >= mfp->mf_used_count_max)
858 || (total_mem_used >> 10) >= (long_u)p_mmt);
859
860 /*
861 * Try to create a swap file if the amount of memory used is getting too
862 * high.
863 */
864 if (mfp->mf_fd < 0 && need_release && p_uc)
865 {
866 /* find for which buffer this memfile is */
867 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
868 if (buf->b_ml.ml_mfp == mfp)
869 break;
870 if (buf != NULL && buf->b_may_swap)
871 ml_open_file(buf);
872 }
873
874 /*
875 * don't release a block if
876 * there is no file for this memfile
877 * or
878 * the number of blocks for this memfile is lower than the maximum
879 * and
880 * total memory used is not up to 'maxmemtot'
881 */
882 if (mfp->mf_fd < 0 || !need_release)
883 return NULL;
884
885 for (hp = mfp->mf_used_last; hp != NULL; hp = hp->bh_prev)
886 if (!(hp->bh_flags & BH_LOCKED))
887 break;
888 if (hp == NULL) /* not a single one that can be released */
889 return NULL;
890
891 /*
892 * If the block is dirty, write it.
893 * If the write fails we don't free it.
894 */
895 if ((hp->bh_flags & BH_DIRTY) && mf_write(mfp, hp) == FAIL)
896 return NULL;
897
898 mf_rem_used(mfp, hp);
899 mf_rem_hash(mfp, hp);
900
901 /*
902 * If a bhdr_T is returned, make sure that the page_count of bh_data is
903 * right
904 */
905 if (hp->bh_page_count != page_count)
906 {
907 vim_free(hp->bh_data);
908 if ((hp->bh_data = alloc(mfp->mf_page_size * page_count)) == NULL)
909 {
910 vim_free(hp);
911 return NULL;
912 }
913 hp->bh_page_count = page_count;
914 }
915 return hp;
916}
917
918/*
919 * release as many blocks as possible
920 * Used in case of out of memory
921 *
922 * return TRUE if any memory was released
923 */
924 int
925mf_release_all()
926{
927 buf_T *buf;
928 memfile_T *mfp;
929 bhdr_T *hp;
930 int retval = FALSE;
931
932 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
933 {
934 mfp = buf->b_ml.ml_mfp;
935 if (mfp != NULL)
936 {
937 /* If no swap file yet, may open one */
938 if (mfp->mf_fd < 0 && buf->b_may_swap)
939 ml_open_file(buf);
940
941 /* only if there is a swapfile */
942 if (mfp->mf_fd >= 0)
943 {
944 for (hp = mfp->mf_used_last; hp != NULL; )
945 {
946 if (!(hp->bh_flags & BH_LOCKED)
947 && (!(hp->bh_flags & BH_DIRTY)
948 || mf_write(mfp, hp) != FAIL))
949 {
950 mf_rem_used(mfp, hp);
951 mf_rem_hash(mfp, hp);
952 mf_free_bhdr(hp);
953 hp = mfp->mf_used_last; /* re-start, list was changed */
954 retval = TRUE;
955 }
956 else
957 hp = hp->bh_prev;
958 }
959 }
960 }
961 }
962 return retval;
963}
964
965/*
966 * Allocate a block header and a block of memory for it
967 */
968 static bhdr_T *
969mf_alloc_bhdr(mfp, page_count)
970 memfile_T *mfp;
971 int page_count;
972{
973 bhdr_T *hp;
974
975 if ((hp = (bhdr_T *)alloc((unsigned)sizeof(bhdr_T))) != NULL)
976 {
977 if ((hp->bh_data = (char_u *)alloc(mfp->mf_page_size * page_count))
978 == NULL)
979 {
980 vim_free(hp); /* not enough memory */
981 return NULL;
982 }
983 hp->bh_page_count = page_count;
984 }
985 return hp;
986}
987
988/*
989 * Free a block header and the block of memory for it
990 */
991 static void
992mf_free_bhdr(hp)
993 bhdr_T *hp;
994{
995 vim_free(hp->bh_data);
996 vim_free(hp);
997}
998
999/*
1000 * insert entry *hp in the free list
1001 */
1002 static void
1003mf_ins_free(mfp, hp)
1004 memfile_T *mfp;
1005 bhdr_T *hp;
1006{
1007 hp->bh_next = mfp->mf_free_first;
1008 mfp->mf_free_first = hp;
1009}
1010
1011/*
1012 * remove the first entry from the free list and return a pointer to it
1013 * Note: caller must check that mfp->mf_free_first is not NULL!
1014 */
1015 static bhdr_T *
1016mf_rem_free(mfp)
1017 memfile_T *mfp;
1018{
1019 bhdr_T *hp;
1020
1021 hp = mfp->mf_free_first;
1022 mfp->mf_free_first = hp->bh_next;
1023 return hp;
1024}
1025
1026/*
1027 * read a block from disk
1028 *
1029 * Return FAIL for failure, OK otherwise
1030 */
1031 static int
1032mf_read(mfp, hp)
1033 memfile_T *mfp;
1034 bhdr_T *hp;
1035{
1036 off_t offset;
1037 unsigned page_size;
1038 unsigned size;
1039
1040 if (mfp->mf_fd < 0) /* there is no file, can't read */
1041 return FAIL;
1042
1043 page_size = mfp->mf_page_size;
1044 offset = (off_t)page_size * hp->bh_bnum;
1045 size = page_size * hp->bh_page_count;
1046 if (lseek(mfp->mf_fd, offset, SEEK_SET) != offset)
1047 {
Bram Moolenaar3f2d9812006-11-07 17:02:30 +00001048 PERROR(_("E294: Seek error in swap file read"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001049 return FAIL;
1050 }
1051 if ((unsigned)vim_read(mfp->mf_fd, hp->bh_data, size) != size)
1052 {
Bram Moolenaar3f2d9812006-11-07 17:02:30 +00001053 PERROR(_("E295: Read error in swap file"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001054 return FAIL;
1055 }
1056 return OK;
1057}
1058
1059/*
1060 * write a block to disk
1061 *
1062 * Return FAIL for failure, OK otherwise
1063 */
1064 static int
1065mf_write(mfp, hp)
1066 memfile_T *mfp;
1067 bhdr_T *hp;
1068{
1069 off_t offset; /* offset in the file */
1070 blocknr_T nr; /* block nr which is being written */
1071 bhdr_T *hp2;
1072 unsigned page_size; /* number of bytes in a page */
1073 unsigned page_count; /* number of pages written */
1074 unsigned size; /* number of bytes written */
1075
1076 if (mfp->mf_fd < 0) /* there is no file, can't write */
1077 return FAIL;
1078
1079 if (hp->bh_bnum < 0) /* must assign file block number */
1080 if (mf_trans_add(mfp, hp) == FAIL)
1081 return FAIL;
1082
1083 page_size = mfp->mf_page_size;
1084
1085 /*
1086 * We don't want gaps in the file. Write the blocks in front of *hp
1087 * to extend the file.
1088 * If block 'mf_infile_count' is not in the hash list, it has been
1089 * freed. Fill the space in the file with data from the current block.
1090 */
1091 for (;;)
1092 {
1093 nr = hp->bh_bnum;
1094 if (nr > mfp->mf_infile_count) /* beyond end of file */
1095 {
1096 nr = mfp->mf_infile_count;
1097 hp2 = mf_find_hash(mfp, nr); /* NULL catched below */
1098 }
1099 else
1100 hp2 = hp;
1101
1102 offset = (off_t)page_size * nr;
1103 if (lseek(mfp->mf_fd, offset, SEEK_SET) != offset)
1104 {
Bram Moolenaar3f2d9812006-11-07 17:02:30 +00001105 PERROR(_("E296: Seek error in swap file write"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001106 return FAIL;
1107 }
1108 if (hp2 == NULL) /* freed block, fill with dummy data */
1109 page_count = 1;
1110 else
1111 page_count = hp2->bh_page_count;
1112 size = page_size * page_count;
1113 if ((unsigned)vim_write(mfp->mf_fd,
1114 (hp2 == NULL ? hp : hp2)->bh_data, size) != size)
1115 {
1116 /*
1117 * Avoid repeating the error message, this mostly happens when the
Bram Moolenaar49325942007-05-10 19:19:59 +00001118 * disk is full. We give the message again only after a successful
Bram Moolenaar071d4272004-06-13 20:20:40 +00001119 * write or when hitting a key. We keep on trying, in case some
1120 * space becomes available.
1121 */
1122 if (!did_swapwrite_msg)
1123 EMSG(_("E297: Write error in swap file"));
1124 did_swapwrite_msg = TRUE;
1125 return FAIL;
1126 }
1127 did_swapwrite_msg = FALSE;
1128 if (hp2 != NULL) /* written a non-dummy block */
1129 hp2->bh_flags &= ~BH_DIRTY;
1130 /* appended to the file */
1131 if (nr + (blocknr_T)page_count > mfp->mf_infile_count)
1132 mfp->mf_infile_count = nr + page_count;
1133 if (nr == hp->bh_bnum) /* written the desired block */
1134 break;
1135 }
1136 return OK;
1137}
1138
1139/*
1140 * Make block number for *hp positive and add it to the translation list
1141 *
1142 * Return FAIL for failure, OK otherwise
1143 */
1144 static int
1145mf_trans_add(mfp, hp)
1146 memfile_T *mfp;
1147 bhdr_T *hp;
1148{
1149 bhdr_T *freep;
1150 blocknr_T new_bnum;
1151 int hash;
1152 NR_TRANS *np;
1153 int page_count;
1154
1155 if (hp->bh_bnum >= 0) /* it's already positive */
1156 return OK;
1157
1158 if ((np = (NR_TRANS *)alloc((unsigned)sizeof(NR_TRANS))) == NULL)
1159 return FAIL;
1160
1161/*
1162 * get a new number for the block.
1163 * If the first item in the free list has sufficient pages, use its number
1164 * Otherwise use mf_blocknr_max.
1165 */
1166 freep = mfp->mf_free_first;
1167 page_count = hp->bh_page_count;
1168 if (freep != NULL && freep->bh_page_count >= page_count)
1169 {
1170 new_bnum = freep->bh_bnum;
1171 /*
1172 * If the page count of the free block was larger, recude it.
1173 * If the page count matches, remove the block from the free list
1174 */
1175 if (freep->bh_page_count > page_count)
1176 {
1177 freep->bh_bnum += page_count;
1178 freep->bh_page_count -= page_count;
1179 }
1180 else
1181 {
1182 freep = mf_rem_free(mfp);
1183 vim_free(freep);
1184 }
1185 }
1186 else
1187 {
1188 new_bnum = mfp->mf_blocknr_max;
1189 mfp->mf_blocknr_max += page_count;
1190 }
1191
1192 np->nt_old_bnum = hp->bh_bnum; /* adjust number */
1193 np->nt_new_bnum = new_bnum;
1194
1195 mf_rem_hash(mfp, hp); /* remove from old hash list */
1196 hp->bh_bnum = new_bnum;
1197 mf_ins_hash(mfp, hp); /* insert in new hash list */
1198
1199 hash = MEMHASH(np->nt_old_bnum); /* insert in trans list */
1200 np->nt_next = mfp->mf_trans[hash];
1201 mfp->mf_trans[hash] = np;
1202 if (np->nt_next != NULL)
1203 np->nt_next->nt_prev = np;
1204 np->nt_prev = NULL;
1205
1206 return OK;
1207}
1208
1209/*
Bram Moolenaar49325942007-05-10 19:19:59 +00001210 * Lookup a translation from the trans lists and delete the entry
Bram Moolenaar071d4272004-06-13 20:20:40 +00001211 *
1212 * Return the positive new number when found, the old number when not found
1213 */
1214 blocknr_T
1215mf_trans_del(mfp, old_nr)
1216 memfile_T *mfp;
1217 blocknr_T old_nr;
1218{
1219 int hash;
1220 NR_TRANS *np;
1221 blocknr_T new_bnum;
1222
1223 hash = MEMHASH(old_nr);
1224 for (np = mfp->mf_trans[hash]; np != NULL; np = np->nt_next)
1225 if (np->nt_old_bnum == old_nr)
1226 break;
1227 if (np == NULL) /* not found */
1228 return old_nr;
1229
1230 mfp->mf_neg_count--;
1231 new_bnum = np->nt_new_bnum;
1232 if (np->nt_prev != NULL) /* remove entry from the trans list */
1233 np->nt_prev->nt_next = np->nt_next;
1234 else
1235 mfp->mf_trans[hash] = np->nt_next;
1236 if (np->nt_next != NULL)
1237 np->nt_next->nt_prev = np->nt_prev;
1238 vim_free(np);
1239
1240 return new_bnum;
1241}
1242
1243/*
1244 * Set mfp->mf_ffname according to mfp->mf_fname and some other things.
1245 * Only called when creating or renaming the swapfile. Either way it's a new
1246 * name so we must work out the full path name.
1247 */
1248 void
1249mf_set_ffname(mfp)
1250 memfile_T *mfp;
1251{
1252 mfp->mf_ffname = FullName_save(mfp->mf_fname, FALSE);
1253}
1254
1255/*
1256 * Make the name of the file used for the memfile a full path.
1257 * Used before doing a :cd
1258 */
1259 void
1260mf_fullname(mfp)
1261 memfile_T *mfp;
1262{
1263 if (mfp != NULL && mfp->mf_fname != NULL && mfp->mf_ffname != NULL)
1264 {
1265 vim_free(mfp->mf_fname);
1266 mfp->mf_fname = mfp->mf_ffname;
1267 mfp->mf_ffname = NULL;
1268 }
1269}
1270
1271/*
1272 * return TRUE if there are any translations pending for 'mfp'
1273 */
1274 int
1275mf_need_trans(mfp)
1276 memfile_T *mfp;
1277{
1278 return (mfp->mf_fname != NULL && mfp->mf_neg_count > 0);
1279}
1280
1281/*
1282 * Open a swap file for a memfile.
1283 * The "fname" must be in allocated memory, and is consumed (also when an
1284 * error occurs).
1285 */
1286 static void
1287mf_do_open(mfp, fname, flags)
1288 memfile_T *mfp;
1289 char_u *fname;
1290 int flags; /* flags for open() */
1291{
1292#ifdef HAVE_LSTAT
1293 struct stat sb;
1294#endif
1295
1296 mfp->mf_fname = fname;
1297
1298 /*
1299 * Get the full path name before the open, because this is
1300 * not possible after the open on the Amiga.
1301 * fname cannot be NameBuff, because it must have been allocated.
1302 */
1303 mf_set_ffname(mfp);
1304#if defined(MSDOS) || defined(MSWIN) || defined(RISCOS)
1305 /*
1306 * A ":!cd e:xxx" may change the directory without us knowning, use the
1307 * full pathname always. Careful: This frees fname!
1308 */
1309 mf_fullname(mfp);
1310#endif
1311
1312#ifdef HAVE_LSTAT
1313 /*
1314 * Extra security check: When creating a swap file it really shouldn't
1315 * exist yet. If there is a symbolic link, this is most likely an attack.
1316 */
1317 if ((flags & O_CREAT) && mch_lstat((char *)mfp->mf_fname, &sb) >= 0)
1318 {
1319 mfp->mf_fd = -1;
1320 EMSG(_("E300: Swap file already exists (symlink attack?)"));
1321 }
1322 else
1323#endif
1324 {
1325 /*
1326 * try to open the file
1327 */
Bram Moolenaara5792f52005-11-23 21:25:05 +00001328 flags |= O_EXTRA | O_NOFOLLOW;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001329#ifdef WIN32
1330 /* Prevent handle inheritance that cause problems with Cscope
1331 * (swap file may not be deleted if cscope connection was open after
1332 * the file) */
1333 flags |= O_NOINHERIT;
1334#endif
1335 mfp->mf_fd = mch_open_rw((char *)mfp->mf_fname, flags);
1336 }
1337
1338 /*
1339 * If the file cannot be opened, use memory only
1340 */
1341 if (mfp->mf_fd < 0)
1342 {
1343 vim_free(mfp->mf_fname);
1344 vim_free(mfp->mf_ffname);
1345 mfp->mf_fname = NULL;
1346 mfp->mf_ffname = NULL;
1347 }
1348 else
1349 mch_hide(mfp->mf_fname); /* try setting the 'hidden' flag */
1350}