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