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