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