2 #define _GNU_SOURCE /* memrchr(3) is non-standard */
15 #include <sys/types.h>
22 #include <selinux/selinux.h>
26 #include "text-util.h"
27 #include "text-motions.h"
30 /* Allocate blocks holding the actual file content in junks of size: */
32 #define BLOCK_SIZE (1 << 20)
34 /* Files smaller than this value are copied on load, larger ones are mmap(2)-ed
35 * directely. Hence the former can be truncated, while doing so on the latter
36 * results in havoc. */
37 #define BLOCK_MMAP_SIZE (1 << 23)
39 /* Block holding the file content, either readonly mmap(2)-ed from the original
40 * file or heap allocated to store the modifications.
42 typedef struct Block Block
;
44 size_t size
; /* maximal capacity */
45 size_t len
; /* current used length / insertion position */
46 char *data
; /* actual data */
47 enum { /* type of allocation */
48 MMAP_ORIG
, /* mmap(2)-ed from an external file */
49 MMAP
, /* mmap(2)-ed from a temporary file only known to this process */
50 MALLOC
, /* heap allocated block using malloc(3) */
52 Block
*next
; /* next junk */
55 /* A piece holds a reference (but doesn't itself store) a certain amount of data.
56 * All active pieces chained together form the whole content of the document.
57 * At the beginning there exists only one piece, spanning the whole document.
58 * Upon insertion/deletion new pieces will be created to represent the changes.
59 * Generally pieces are never destroyed, but kept around to peform undo/redo
63 Text
*text
; /* text to which this piece belongs */
64 Piece
*prev
, *next
; /* pointers to the logical predecessor/successor */
65 Piece
*global_prev
; /* double linked list in order of allocation, */
66 Piece
*global_next
; /* used to free individual pieces */
67 const char *data
; /* pointer into a Block holding the data */
68 size_t len
; /* the length in number of bytes of the data */
71 /* used to transform a global position (byte offset starting from the beginning
72 * of the text) into an offset relative to a piece.
75 Piece
*piece
; /* piece holding the location */
76 size_t off
; /* offset into the piece in bytes */
79 /* A Span holds a certain range of pieces. Changes to the document are always
80 * performed by swapping out an existing span with a new one.
83 Piece
*start
, *end
; /* start/end of the span */
84 size_t len
; /* the sum of the lengths of the pieces which form this span */
87 /* A Change keeps all needed information to redo/undo an insertion/deletion. */
88 typedef struct Change Change
;
90 Span old
; /* all pieces which are being modified/swapped out by the change */
91 Span
new; /* all pieces which are introduced/swapped in by the change */
92 size_t pos
; /* absolute position at which the change occured */
93 Change
*next
; /* next change which is part of the same revision */
94 Change
*prev
; /* previous change which is part of the same revision */
97 /* A Revision is a list of Changes which are used to undo/redo all modifications
98 * since the last snapshot operation. Revisions are stored in a directed graph structure.
100 typedef struct Revision Revision
;
102 Change
*change
; /* the most recent change */
103 Revision
*next
; /* the next (child) revision in the undo tree */
104 Revision
*prev
; /* the previous (parent) revision in the undo tree */
105 Revision
*earlier
; /* the previous Revision, chronologically */
106 Revision
*later
; /* the next Revision, chronologically */
107 time_t time
; /* when the first change of this revision was performed */
108 size_t seq
; /* a unique, strictly increasing identifier */
112 size_t pos
; /* position in bytes from start of file */
113 size_t lineno
; /* line number in file i.e. number of '\n' in [0, pos) */
116 /* The main struct holding all information of a given file */
118 Block
*block
; /* original file content at the time of load operation */
119 Block
*blocks
; /* all blocks which have been allocated to hold insertion data */
120 Piece
*pieces
; /* all pieces which have been allocated, used to free them */
121 Piece
*cache
; /* most recently modified piece */
122 Piece begin
, end
; /* sentinel nodes which always exists but don't hold any data */
123 Revision
*history
; /* undo tree */
124 Revision
*current_revision
; /* revision holding all file changes until a snapshot is performed */
125 Revision
*last_revision
; /* the last revision added to the tree, chronologically */
126 Revision
*saved_revision
; /* the last revision at the time of the save operation */
127 size_t size
; /* current file content size in bytes */
128 struct stat info
; /* stat as probed at load time */
129 LineCache lines
; /* mapping between absolute pos in bytes and logical line breaks */
132 struct TextSave
{ /* used to hold context between text_save_{begin,commit} calls */
133 Text
*txt
; /* text to operate on */
134 char *filename
; /* filename to save to as given to text_save_begin */
135 char *tmpname
; /* temporary name used for atomic rename(2) */
136 int fd
; /* file descriptor to write data to using text_save_write */
137 enum TextSaveMethod type
; /* method used to save file */
140 /* block management */
141 static Block
*block_alloc(Text
*, size_t size
);
142 static Block
*block_read(Text
*, size_t size
, int fd
);
143 static Block
*block_mmap(Text
*, size_t size
, int fd
, off_t offset
);
144 static void block_free(Block
*);
145 static bool block_capacity(Block
*, size_t len
);
146 static const char *block_append(Block
*, const char *data
, size_t len
);
147 static bool block_insert(Block
*, size_t pos
, const char *data
, size_t len
);
148 static bool block_delete(Block
*, size_t pos
, size_t len
);
149 static const char *block_store(Text
*, const char *data
, size_t len
);
151 static void cache_piece(Text
*txt
, Piece
*p
);
152 static bool cache_contains(Text
*txt
, Piece
*p
);
153 static bool cache_insert(Text
*txt
, Piece
*p
, size_t off
, const char *data
, size_t len
);
154 static bool cache_delete(Text
*txt
, Piece
*p
, size_t off
, size_t len
);
155 /* piece management */
156 static Piece
*piece_alloc(Text
*txt
);
157 static void piece_free(Piece
*p
);
158 static void piece_init(Piece
*p
, Piece
*prev
, Piece
*next
, const char *data
, size_t len
);
159 static Location
piece_get_intern(Text
*txt
, size_t pos
);
160 static Location
piece_get_extern(Text
*txt
, size_t pos
);
161 /* span management */
162 static void span_init(Span
*span
, Piece
*start
, Piece
*end
);
163 static void span_swap(Text
*txt
, Span
*old
, Span
*new);
164 /* change management */
165 static Change
*change_alloc(Text
*txt
, size_t pos
);
166 static void change_free(Change
*c
);
167 /* revision management */
168 static Revision
*revision_alloc(Text
*txt
);
169 static void revision_free(Revision
*rev
);
170 /* logical line counting cache */
171 static void lineno_cache_invalidate(LineCache
*cache
);
172 static size_t lines_skip_forward(Text
*txt
, size_t pos
, size_t lines
, size_t *lines_skiped
);
173 static size_t lines_count(Text
*txt
, size_t pos
, size_t len
);
175 static ssize_t
write_all(int fd
, const char *buf
, size_t count
) {
178 ssize_t written
= write(fd
, buf
, rem
> INT_MAX
? INT_MAX
: rem
);
180 if (errno
== EAGAIN
|| errno
== EINTR
)
183 } else if (written
== 0) {
192 /* allocate a new block of MAX(size, BLOCK_SIZE) bytes */
193 static Block
*block_alloc(Text
*txt
, size_t size
) {
194 Block
*blk
= calloc(1, sizeof *blk
);
197 if (BLOCK_SIZE
> size
)
199 if (!(blk
->data
= malloc(size
))) {
205 blk
->next
= txt
->blocks
;
210 static Block
*block_read(Text
*txt
, size_t size
, int fd
) {
211 Block
*blk
= block_alloc(txt
, size
);
216 ssize_t len
= read(fd
, data
, MIN(sizeof(data
), size
));
218 txt
->blocks
= blk
->next
;
221 } else if (len
== 0) {
224 block_append(blk
, data
, len
);
231 static Block
*block_mmap(Text
*txt
, size_t size
, int fd
, off_t offset
) {
232 Block
*blk
= calloc(1, sizeof *blk
);
236 blk
->data
= mmap(NULL
, size
, PROT_READ
, MAP_SHARED
, fd
, offset
);
237 if (blk
->data
== MAP_FAILED
) {
242 blk
->type
= MMAP_ORIG
;
245 blk
->next
= txt
->blocks
;
250 static void block_free(Block
*blk
) {
253 if (blk
->type
== MALLOC
)
255 else if ((blk
->type
== MMAP_ORIG
|| blk
->type
== MMAP
) && blk
->data
)
256 munmap(blk
->data
, blk
->size
);
260 /* check whether block has enough free space to store len bytes */
261 static bool block_capacity(Block
*blk
, size_t len
) {
262 return blk
->size
- blk
->len
>= len
;
265 /* append data to block, assumes there is enough space available */
266 static const char *block_append(Block
*blk
, const char *data
, size_t len
) {
267 char *dest
= memcpy(blk
->data
+ blk
->len
, data
, len
);
272 /* stores the given data in a block, allocates a new one if necessary. returns
273 * a pointer to the storage location or NULL if allocation failed. */
274 static const char *block_store(Text
*txt
, const char *data
, size_t len
) {
275 Block
*blk
= txt
->blocks
;
276 if ((!blk
|| !block_capacity(blk
, len
)) && !(blk
= block_alloc(txt
, len
)))
278 return block_append(blk
, data
, len
);
281 /* insert data into block at an arbitrary position, this should only be used with
282 * data of the most recently created piece. */
283 static bool block_insert(Block
*blk
, size_t pos
, const char *data
, size_t len
) {
284 if (pos
> blk
->len
|| !block_capacity(blk
, len
))
287 return block_append(blk
, data
, len
);
288 char *insert
= blk
->data
+ pos
;
289 memmove(insert
+ len
, insert
, blk
->len
- pos
);
290 memcpy(insert
, data
, len
);
295 /* delete data from a block at an arbitrary position, this should only be used with
296 * data of the most recently created piece. */
297 static bool block_delete(Block
*blk
, size_t pos
, size_t len
) {
299 if (!addu(pos
, len
, &end
) || end
> blk
->len
)
301 if (blk
->len
== pos
) {
305 char *delete = blk
->data
+ pos
;
306 memmove(delete, delete + len
, blk
->len
- pos
- len
);
311 /* cache the given piece if it is the most recently changed one */
312 static void cache_piece(Text
*txt
, Piece
*p
) {
313 Block
*blk
= txt
->blocks
;
314 if (!blk
|| p
->data
< blk
->data
|| p
->data
+ p
->len
!= blk
->data
+ blk
->len
)
319 /* check whether the given piece was the most recently modified one */
320 static bool cache_contains(Text
*txt
, Piece
*p
) {
321 Block
*blk
= txt
->blocks
;
322 Revision
*rev
= txt
->current_revision
;
323 if (!blk
|| !txt
->cache
|| txt
->cache
!= p
|| !rev
|| !rev
->change
)
326 Piece
*start
= rev
->change
->new.start
;
327 Piece
*end
= rev
->change
->new.end
;
329 for (Piece
*cur
= start
; !found
; cur
= cur
->next
) {
336 return found
&& p
->data
+ p
->len
== blk
->data
+ blk
->len
;
339 /* try to insert a junk of data at a given piece offset. the insertion is only
340 * performed if the piece is the most recenetly changed one. the legnth of the
341 * piece, the span containing it and the whole text is adjusted accordingly */
342 static bool cache_insert(Text
*txt
, Piece
*p
, size_t off
, const char *data
, size_t len
) {
343 if (!cache_contains(txt
, p
))
345 Block
*blk
= txt
->blocks
;
346 size_t bufpos
= p
->data
+ off
- blk
->data
;
347 if (!block_insert(blk
, bufpos
, data
, len
))
350 txt
->current_revision
->change
->new.len
+= len
;
355 /* try to delete a junk of data at a given piece offset. the deletion is only
356 * performed if the piece is the most recenetly changed one and the whole
357 * affected range lies within it. the legnth of the piece, the span containing it
358 * and the whole text is adjusted accordingly */
359 static bool cache_delete(Text
*txt
, Piece
*p
, size_t off
, size_t len
) {
360 if (!cache_contains(txt
, p
))
362 Block
*blk
= txt
->blocks
;
364 size_t bufpos
= p
->data
+ off
- blk
->data
;
365 if (!addu(off
, len
, &end
) || end
> p
->len
|| !block_delete(blk
, bufpos
, len
))
368 txt
->current_revision
->change
->new.len
-= len
;
373 /* initialize a span and calculate its length */
374 static void span_init(Span
*span
, Piece
*start
, Piece
*end
) {
378 for (Piece
*p
= start
; p
; p
= p
->next
) {
386 /* swap out an old span and replace it with a new one.
388 * - if old is an empty span do not remove anything, just insert the new one
389 * - if new is an empty span do not insert anything, just remove the old one
391 * adjusts the document size accordingly.
393 static void span_swap(Text
*txt
, Span
*old
, Span
*new) {
394 if (old
->len
== 0 && new->len
== 0) {
396 } else if (old
->len
== 0) {
397 /* insert new span */
398 new->start
->prev
->next
= new->start
;
399 new->end
->next
->prev
= new->end
;
400 } else if (new->len
== 0) {
401 /* delete old span */
402 old
->start
->prev
->next
= old
->end
->next
;
403 old
->end
->next
->prev
= old
->start
->prev
;
405 /* replace old with new */
406 old
->start
->prev
->next
= new->start
;
407 old
->end
->next
->prev
= new->end
;
409 txt
->size
-= old
->len
;
410 txt
->size
+= new->len
;
413 /* Allocate a new revision and place it in the revision graph.
414 * All further changes will be associated with this revision. */
415 static Revision
*revision_alloc(Text
*txt
) {
416 Revision
*rev
= calloc(1, sizeof *rev
);
419 rev
->time
= time(NULL
);
420 txt
->current_revision
= rev
;
422 /* set sequence number */
423 if (!txt
->last_revision
)
426 rev
->seq
= txt
->last_revision
->seq
+ 1;
428 /* set earlier, later pointers */
429 if (txt
->last_revision
)
430 txt
->last_revision
->later
= rev
;
431 rev
->earlier
= txt
->last_revision
;
438 /* set prev, next pointers */
439 rev
->prev
= txt
->history
;
440 txt
->history
->next
= rev
;
445 static void revision_free(Revision
*rev
) {
448 for (Change
*next
, *c
= rev
->change
; c
; c
= next
) {
455 static Piece
*piece_alloc(Text
*txt
) {
456 Piece
*p
= calloc(1, sizeof *p
);
460 p
->global_next
= txt
->pieces
;
462 txt
->pieces
->global_prev
= p
;
467 static void piece_free(Piece
*p
) {
471 p
->global_prev
->global_next
= p
->global_next
;
473 p
->global_next
->global_prev
= p
->global_prev
;
474 if (p
->text
->pieces
== p
)
475 p
->text
->pieces
= p
->global_next
;
476 if (p
->text
->cache
== p
)
477 p
->text
->cache
= NULL
;
481 static void piece_init(Piece
*p
, Piece
*prev
, Piece
*next
, const char *data
, size_t len
) {
488 /* returns the piece holding the text at byte offset pos. if pos happens to
489 * be at a piece boundry i.e. the first byte of a piece then the previous piece
490 * to the left is returned with an offset of piece->len. this is convenient for
491 * modifications to the piece chain where both pieces (the returned one and the
492 * one following it) are needed, but unsuitable as a public interface.
494 * in particular if pos is zero, the begin sentinel piece is returned.
496 static Location
piece_get_intern(Text
*txt
, size_t pos
) {
498 for (Piece
*p
= &txt
->begin
; p
->next
; p
= p
->next
) {
499 if (cur
<= pos
&& pos
<= cur
+ p
->len
)
500 return (Location
){ .piece
= p
, .off
= pos
- cur
};
504 return (Location
){ 0 };
507 /* similiar to piece_get_intern but usable as a public API. returns the piece
508 * holding the text at byte offset pos. never returns a sentinel piece.
509 * it pos is the end of file (== text_size()) and the file is not empty then
510 * the last piece holding data is returned.
512 static Location
piece_get_extern(Text
*txt
, size_t pos
) {
516 for (p
= txt
->begin
.next
; p
->next
; p
= p
->next
) {
517 if (cur
<= pos
&& pos
< cur
+ p
->len
)
518 return (Location
){ .piece
= p
, .off
= pos
- cur
};
523 return (Location
){ .piece
= p
->prev
, .off
= p
->prev
->len
};
525 return (Location
){ 0 };
528 /* allocate a new change, associate it with current revision or a newly
529 * allocated one if none exists. */
530 static Change
*change_alloc(Text
*txt
, size_t pos
) {
531 Revision
*rev
= txt
->current_revision
;
533 rev
= revision_alloc(txt
);
537 Change
*c
= calloc(1, sizeof *c
);
541 c
->next
= rev
->change
;
543 rev
->change
->prev
= c
;
548 static void change_free(Change
*c
) {
551 /* only free the new part of the span, the old one is still in use */
552 if (c
->new.start
!= c
->new.end
)
553 piece_free(c
->new.end
);
554 piece_free(c
->new.start
);
558 /* When inserting new data there are 2 cases to consider.
560 * - in the first the insertion point falls into the middle of an exisiting
561 * piece which is replaced by three new pieces:
563 * /-+ --> +---------------+ --> +-\
564 * | | | existing text | | |
565 * \-+ <-- +---------------+ <-- +-/
567 * Insertion point for "demo "
569 * /-+ --> +---------+ --> +-----+ --> +-----+ --> +-\
570 * | | | existing| |demo | |text | | |
571 * \-+ <-- +---------+ <-- +-----+ <-- +-----+ <-- +-/
573 * - the second case deals with an insertion point at a piece boundry:
575 * /-+ --> +---------------+ --> +-\
576 * | | | existing text | | |
577 * \-+ <-- +---------------+ <-- +-/
579 * Insertion point for "short"
581 * /-+ --> +-----+ --> +---------------+ --> +-\
582 * | | |short| | existing text | | |
583 * \-+ <-- +-----+ <-- +---------------+ <-- +-/
585 bool text_insert(Text
*txt
, size_t pos
, const char *data
, size_t len
) {
590 if (pos
< txt
->lines
.pos
)
591 lineno_cache_invalidate(&txt
->lines
);
593 Location loc
= piece_get_intern(txt
, pos
);
594 Piece
*p
= loc
.piece
;
597 size_t off
= loc
.off
;
598 if (cache_insert(txt
, p
, off
, data
, len
))
601 Change
*c
= change_alloc(txt
, pos
);
605 if (!(data
= block_store(txt
, data
, len
)))
611 /* insert between two existing pieces, hence there is nothing to
612 * remove, just add a new piece holding the extra text */
613 if (!(new = piece_alloc(txt
)))
615 piece_init(new, p
, p
->next
, data
, len
);
616 span_init(&c
->new, new, new);
617 span_init(&c
->old
, NULL
, NULL
);
619 /* insert into middle of an existing piece, therfore split the old
620 * piece. that is we have 3 new pieces one containing the content
621 * before the insertion point then one holding the newly inserted
622 * text and one holding the content after the insertion point.
624 Piece
*before
= piece_alloc(txt
);
625 new = piece_alloc(txt
);
626 Piece
*after
= piece_alloc(txt
);
627 if (!before
|| !new || !after
)
629 piece_init(before
, p
->prev
, new, p
->data
, off
);
630 piece_init(new, before
, after
, data
, len
);
631 piece_init(after
, new, p
->next
, p
->data
+ off
, p
->len
- off
);
633 span_init(&c
->new, before
, after
);
634 span_init(&c
->old
, p
, p
);
637 cache_piece(txt
, new);
638 span_swap(txt
, &c
->old
, &c
->new);
642 static bool text_vprintf(Text
*txt
, size_t pos
, const char *format
, va_list ap
) {
644 va_copy(ap_save
, ap
);
645 int len
= vsnprintf(NULL
, 0, format
, ap
);
650 char *buf
= malloc(len
+1);
651 bool ret
= buf
&& (vsnprintf(buf
, len
+1, format
, ap_save
) == len
) && text_insert(txt
, pos
, buf
, len
);
657 bool text_appendf(Text
*txt
, const char *format
, ...) {
659 va_start(ap
, format
);
660 bool ret
= text_vprintf(txt
, text_size(txt
), format
, ap
);
665 bool text_printf(Text
*txt
, size_t pos
, const char *format
, ...) {
667 va_start(ap
, format
);
668 bool ret
= text_vprintf(txt
, pos
, format
, ap
);
673 static size_t revision_undo(Text
*txt
, Revision
*rev
) {
675 for (Change
*c
= rev
->change
; c
; c
= c
->next
) {
676 span_swap(txt
, &c
->new, &c
->old
);
682 static size_t revision_redo(Text
*txt
, Revision
*rev
) {
684 Change
*c
= rev
->change
;
687 for ( ; c
; c
= c
->prev
) {
688 span_swap(txt
, &c
->old
, &c
->new);
690 if (c
->new.len
> c
->old
.len
)
691 pos
+= c
->new.len
- c
->old
.len
;
696 size_t text_undo(Text
*txt
) {
698 /* taking rev snapshot makes sure that txt->current_revision is reset */
700 Revision
*rev
= txt
->history
->prev
;
703 pos
= revision_undo(txt
, txt
->history
);
705 lineno_cache_invalidate(&txt
->lines
);
709 size_t text_redo(Text
*txt
) {
711 /* taking a snapshot makes sure that txt->current_revision is reset */
713 Revision
*rev
= txt
->history
->next
;
716 pos
= revision_redo(txt
, rev
);
718 lineno_cache_invalidate(&txt
->lines
);
722 static bool history_change_branch(Revision
*rev
) {
723 bool changed
= false;
725 if (rev
->prev
->next
!= rev
) {
726 rev
->prev
->next
= rev
;
734 static size_t history_traverse_to(Text
*txt
, Revision
*rev
) {
738 bool changed
= history_change_branch(rev
);
740 if (rev
->seq
== txt
->history
->seq
) {
741 return txt
->lines
.pos
;
742 } else if (rev
->seq
> txt
->history
->seq
) {
743 while (txt
->history
!= rev
)
744 pos
= text_redo(txt
);
746 } else if (rev
->seq
< txt
->history
->seq
) {
747 while (txt
->history
!= rev
)
748 pos
= text_undo(txt
);
752 while (txt
->history
->prev
&& txt
->history
->prev
->next
== txt
->history
)
754 pos
= text_undo(txt
);
755 while (txt
->history
!= rev
)
756 pos
= text_redo(txt
);
762 size_t text_earlier(Text
*txt
) {
763 return history_traverse_to(txt
, txt
->history
->earlier
);
766 size_t text_later(Text
*txt
) {
767 return history_traverse_to(txt
, txt
->history
->later
);
770 size_t text_restore(Text
*txt
, time_t time
) {
771 Revision
*rev
= txt
->history
;
772 while (time
< rev
->time
&& rev
->earlier
)
774 while (time
> rev
->time
&& rev
->later
)
776 time_t diff
= labs(rev
->time
- time
);
777 if (rev
->earlier
&& rev
->earlier
!= txt
->history
&& labs(rev
->earlier
->time
- time
) < diff
)
779 if (rev
->later
&& rev
->later
!= txt
->history
&& labs(rev
->later
->time
- time
) < diff
)
781 return history_traverse_to(txt
, rev
);
784 time_t text_state(Text
*txt
) {
785 return txt
->history
->time
;
788 static bool preserve_acl(int src
, int dest
) {
790 acl_t acl
= acl_get_fd(src
);
792 return errno
== ENOTSUP
? true : false;
793 if (acl_set_fd(dest
, acl
) == -1) {
798 #endif /* CONFIG_ACL */
802 static bool preserve_selinux_context(int src
, int dest
) {
804 char *context
= NULL
;
805 if (!is_selinux_enabled())
807 if (fgetfilecon(src
, &context
) == -1)
808 return errno
== ENOTSUP
? true : false;
809 if (fsetfilecon(dest
, context
) == -1) {
814 #endif /* CONFIG_SELINUX */
818 /* Create a new file named `filename~` and try to preserve all important
819 * meta data. After the file content has been written to this temporary
820 * file, text_save_commit_atomic will atomically move it to its final
821 * (possibly already existing) destination using rename(2).
823 * This approach does not work if:
825 * - the file is a symbolic link
826 * - the file is a hard link
827 * - file ownership can not be preserved
828 * - file group can not be preserved
829 * - directory permissions do not allow creation of a new file
830 * - POSXI ACL can not be preserved (if enabled)
831 * - SELinux security context can not be preserved (if enabled)
833 static bool text_save_begin_atomic(TextSave
*ctx
) {
834 int oldfd
, saved_errno
;
835 if ((oldfd
= open(ctx
->filename
, O_RDONLY
)) == -1 && errno
!= ENOENT
)
837 struct stat oldmeta
= { 0 };
838 if (oldfd
!= -1 && lstat(ctx
->filename
, &oldmeta
) == -1)
841 if (S_ISLNK(oldmeta
.st_mode
)) /* symbolic link */
843 if (oldmeta
.st_nlink
> 1) /* hard link */
847 size_t namelen
= strlen(ctx
->filename
) + 1 /* ~ */ + 1 /* \0 */;
848 if (!(ctx
->tmpname
= calloc(1, namelen
)))
850 snprintf(ctx
->tmpname
, namelen
, "%s~", ctx
->filename
);
852 if ((ctx
->fd
= open(ctx
->tmpname
, O_CREAT
|O_WRONLY
|O_TRUNC
, oldfd
== -1 ? 0666 : oldmeta
.st_mode
)) == -1)
855 if (!preserve_acl(oldfd
, ctx
->fd
) || !preserve_selinux_context(oldfd
, ctx
->fd
))
857 /* change owner if necessary */
858 if (oldmeta
.st_uid
!= getuid() && fchown(ctx
->fd
, oldmeta
.st_uid
, (uid_t
)-1) == -1)
860 /* change group if necessary, in case of failure some editors reset
861 * the group permissions to the same as for others */
862 if (oldmeta
.st_gid
!= getgid() && fchown(ctx
->fd
, (uid_t
)-1, oldmeta
.st_gid
) == -1)
867 ctx
->type
= TEXT_SAVE_ATOMIC
;
880 static bool text_save_commit_atomic(TextSave
*ctx
) {
881 if (fsync(ctx
->fd
) == -1)
884 struct stat meta
= { 0 };
885 if (fstat(ctx
->fd
, &meta
) == -1)
888 bool close_failed
= (close(ctx
->fd
) == -1);
893 if (rename(ctx
->tmpname
, ctx
->filename
) == -1)
899 int dir
= open(dirname(ctx
->filename
), O_DIRECTORY
|O_RDONLY
);
903 if (fsync(dir
) == -1) {
908 if (close(dir
) == -1)
912 ctx
->txt
->info
= meta
;
916 static bool text_save_begin_inplace(TextSave
*ctx
) {
917 Text
*txt
= ctx
->txt
;
918 struct stat meta
= { 0 };
919 int newfd
= -1, saved_errno
;
920 if ((ctx
->fd
= open(ctx
->filename
, O_CREAT
|O_WRONLY
, 0666)) == -1)
922 if (fstat(ctx
->fd
, &meta
) == -1)
924 if (meta
.st_dev
== txt
->info
.st_dev
&& meta
.st_ino
== txt
->info
.st_ino
&&
925 txt
->block
&& txt
->block
->type
== MMAP_ORIG
&& txt
->block
->size
) {
926 /* The file we are going to overwrite is currently mmap-ed from
927 * text_load, therefore we copy the mmap-ed block to a temporary
928 * file and remap it at the same position such that all pointers
929 * from the various pieces are still valid.
931 size_t size
= txt
->block
->size
;
932 char tmpname
[32] = "/tmp/vis-XXXXXX";
933 newfd
= mkstemp(tmpname
);
936 if (unlink(tmpname
) == -1)
938 ssize_t written
= write_all(newfd
, txt
->block
->data
, size
);
939 if (written
== -1 || (size_t)written
!= size
)
941 if (munmap(txt
->block
->data
, size
) == -1)
944 void *data
= mmap(txt
->block
->data
, size
, PROT_READ
, MAP_SHARED
, newfd
, 0);
945 if (data
== MAP_FAILED
)
947 if (data
!= txt
->block
->data
) {
951 bool close_failed
= (close(newfd
) == -1);
955 txt
->block
->data
= data
;
956 txt
->block
->type
= MMAP
;
959 /* overwrite the existing file content, if something goes wrong
960 * here we are screwed, TODO: make a backup before? */
961 if (ftruncate(ctx
->fd
, 0) == -1)
963 ctx
->type
= TEXT_SAVE_INPLACE
;
976 static bool text_save_commit_inplace(TextSave
*ctx
) {
977 if (fsync(ctx
->fd
) == -1)
979 struct stat meta
= { 0 };
980 if (fstat(ctx
->fd
, &meta
) == -1)
982 if (close(ctx
->fd
) == -1)
984 ctx
->txt
->info
= meta
;
988 TextSave
*text_save_begin(Text
*txt
, const char *filename
, enum TextSaveMethod type
) {
991 TextSave
*ctx
= calloc(1, sizeof *ctx
);
996 if (!(ctx
->filename
= strdup(filename
)))
999 if ((type
== TEXT_SAVE_AUTO
|| type
== TEXT_SAVE_ATOMIC
) && text_save_begin_atomic(ctx
))
1001 if (errno
== ENOSPC
)
1003 if ((type
== TEXT_SAVE_AUTO
|| type
== TEXT_SAVE_INPLACE
) && text_save_begin_inplace(ctx
))
1006 text_save_cancel(ctx
);
1010 bool text_save_commit(TextSave
*ctx
) {
1014 Text
*txt
= ctx
->txt
;
1015 switch (ctx
->type
) {
1016 case TEXT_SAVE_ATOMIC
:
1017 ret
= text_save_commit_atomic(ctx
);
1019 case TEXT_SAVE_INPLACE
:
1020 ret
= text_save_commit_inplace(ctx
);
1028 txt
->saved_revision
= txt
->history
;
1031 text_save_cancel(ctx
);
1035 void text_save_cancel(TextSave
*ctx
) {
1038 int saved_errno
= errno
;
1041 if (ctx
->tmpname
&& ctx
->tmpname
[0])
1042 unlink(ctx
->tmpname
);
1044 free(ctx
->filename
);
1046 errno
= saved_errno
;
1049 bool text_save(Text
*txt
, const char *filename
) {
1050 Filerange r
= (Filerange
){ .start
= 0, .end
= text_size(txt
) };
1051 return text_save_range(txt
, &r
, filename
);
1054 /* First try to save the file atomically using rename(2) if this does not
1055 * work overwrite the file in place. However if something goes wrong during
1056 * this overwrite the original file is permanently damaged.
1058 bool text_save_range(Text
*txt
, Filerange
*range
, const char *filename
) {
1060 txt
->saved_revision
= txt
->history
;
1064 TextSave
*ctx
= text_save_begin(txt
, filename
, TEXT_SAVE_AUTO
);
1067 ssize_t written
= text_write_range(txt
, range
, ctx
->fd
);
1068 if (written
== -1 || (size_t)written
!= text_range_size(range
)) {
1069 text_save_cancel(ctx
);
1072 return text_save_commit(ctx
);
1075 ssize_t
text_save_write_range(TextSave
*ctx
, Filerange
*range
) {
1076 return text_write_range(ctx
->txt
, range
, ctx
->fd
);
1079 ssize_t
text_write(Text
*txt
, int fd
) {
1080 Filerange r
= (Filerange
){ .start
= 0, .end
= text_size(txt
) };
1081 return text_write_range(txt
, &r
, fd
);
1084 ssize_t
text_write_range(Text
*txt
, Filerange
*range
, int fd
) {
1085 size_t size
= text_range_size(range
), rem
= size
;
1086 for (Iterator it
= text_iterator_get(txt
, range
->start
);
1087 rem
> 0 && text_iterator_valid(&it
);
1088 text_iterator_next(&it
)) {
1089 size_t prem
= it
.end
- it
.text
;
1092 ssize_t written
= write_all(fd
, it
.text
, prem
);
1096 if ((size_t)written
!= prem
)
1102 /* load the given file as starting point for further editing operations.
1103 * to start with an empty document, pass NULL as filename. */
1104 Text
*text_load(const char *filename
) {
1107 Text
*txt
= calloc(1, sizeof *txt
);
1110 Piece
*p
= piece_alloc(txt
);
1113 lineno_cache_invalidate(&txt
->lines
);
1115 if ((fd
= open(filename
, O_RDONLY
)) == -1)
1117 if (fstat(fd
, &txt
->info
) == -1)
1119 if (!S_ISREG(txt
->info
.st_mode
)) {
1120 errno
= S_ISDIR(txt
->info
.st_mode
) ? EISDIR
: ENOTSUP
;
1123 // XXX: use lseek(fd, 0, SEEK_END); instead?
1124 size
= txt
->info
.st_size
;
1126 if (size
< BLOCK_MMAP_SIZE
)
1127 txt
->block
= block_read(txt
, size
, fd
);
1129 txt
->block
= block_mmap(txt
, size
, fd
, 0);
1132 piece_init(p
, &txt
->begin
, &txt
->end
, txt
->block
->data
, txt
->block
->len
);
1137 piece_init(p
, &txt
->begin
, &txt
->end
, "\0", 0);
1139 piece_init(&txt
->begin
, NULL
, p
, NULL
, 0);
1140 piece_init(&txt
->end
, p
, NULL
, NULL
, 0);
1142 /* write an empty revision */
1143 change_alloc(txt
, EPOS
);
1145 txt
->saved_revision
= txt
->history
;
1157 struct stat
text_stat(Text
*txt
) {
1161 /* A delete operation can either start/stop midway through a piece or at
1162 * a boundry. In the former case a new piece is created to represent the
1163 * remaining text before/after the modification point.
1165 * /-+ --> +---------+ --> +-----+ --> +-----+ --> +-\
1166 * | | | existing| |demo | |text | | |
1167 * \-+ <-- +---------+ <-- +-----+ <-- +-----+ <-- +-/
1169 * |------ delete range -----|
1171 * /-+ --> +----+ --> +--+ --> +-\
1172 * | | | exi| |t | | |
1173 * \-+ <-- +----+ <-- +--+ <-- +-/
1175 bool text_delete(Text
*txt
, size_t pos
, size_t len
) {
1179 if (!addu(pos
, len
, &pos_end
) || pos_end
> txt
->size
)
1181 if (pos
< txt
->lines
.pos
)
1182 lineno_cache_invalidate(&txt
->lines
);
1184 Location loc
= piece_get_intern(txt
, pos
);
1185 Piece
*p
= loc
.piece
;
1188 size_t off
= loc
.off
;
1189 if (cache_delete(txt
, p
, off
, len
))
1191 Change
*c
= change_alloc(txt
, pos
);
1195 bool midway_start
= false, midway_end
= false; /* split pieces? */
1196 Piece
*before
, *after
; /* unmodified pieces before/after deletion point */
1197 Piece
*start
, *end
; /* span which is removed */
1198 size_t cur
; /* how much has already been deleted */
1200 if (off
== p
->len
) {
1201 /* deletion starts at a piece boundry */
1206 /* deletion starts midway through a piece */
1207 midway_start
= true;
1210 before
= piece_alloc(txt
);
1215 /* skip all pieces which fall into deletion range */
1222 /* deletion stops at a piece boundry */
1226 /* cur > len: deletion stops midway through a piece */
1229 after
= piece_alloc(txt
);
1232 piece_init(after
, before
, p
->next
, p
->data
+ p
->len
- (cur
- len
), cur
- len
);
1236 /* we finally know which piece follows our newly allocated before piece */
1237 piece_init(before
, start
->prev
, after
, start
->data
, off
);
1240 Piece
*new_start
= NULL
, *new_end
= NULL
;
1252 span_init(&c
->new, new_start
, new_end
);
1253 span_init(&c
->old
, start
, end
);
1254 span_swap(txt
, &c
->old
, &c
->new);
1258 bool text_delete_range(Text
*txt
, Filerange
*r
) {
1259 if (!text_range_valid(r
))
1261 return text_delete(txt
, r
->start
, text_range_size(r
));
1264 /* preserve the current text content such that it can be restored by
1265 * means of undo/redo operations */
1266 void text_snapshot(Text
*txt
) {
1267 if (txt
->current_revision
)
1268 txt
->last_revision
= txt
->current_revision
;
1269 txt
->current_revision
= NULL
;
1274 void text_free(Text
*txt
) {
1279 Revision
*hist
= txt
->history
;
1280 while (hist
&& hist
->prev
)
1283 Revision
*later
= hist
->later
;
1284 revision_free(hist
);
1288 for (Piece
*next
, *p
= txt
->pieces
; p
; p
= next
) {
1289 next
= p
->global_next
;
1293 for (Block
*next
, *blk
= txt
->blocks
; blk
; blk
= next
) {
1301 bool text_modified(Text
*txt
) {
1302 return txt
->saved_revision
!= txt
->history
;
1305 bool text_mmaped(Text
*txt
, const char *ptr
) {
1306 uintptr_t addr
= (uintptr_t)ptr
;
1307 for (Block
*blk
= txt
->blocks
; blk
; blk
= blk
->next
) {
1308 if ((blk
->type
== MMAP_ORIG
|| blk
->type
== MMAP
) &&
1309 (uintptr_t)(blk
->data
) <= addr
&& addr
< (uintptr_t)(blk
->data
+ blk
->size
))
1315 static bool text_iterator_init(Iterator
*it
, size_t pos
, Piece
*p
, size_t off
) {
1316 Iterator iter
= (Iterator
){
1319 .start
= p
? p
->data
: NULL
,
1320 .end
= p
? p
->data
+ p
->len
: NULL
,
1321 .text
= p
? p
->data
+ off
: NULL
,
1324 return text_iterator_valid(it
);
1327 Iterator
text_iterator_get(Text
*txt
, size_t pos
) {
1329 Location loc
= piece_get_extern(txt
, pos
);
1330 text_iterator_init(&it
, pos
, loc
.piece
, loc
.off
);
1334 bool text_iterator_byte_get(Iterator
*it
, char *b
) {
1335 if (text_iterator_valid(it
)) {
1336 if (it
->start
<= it
->text
&& it
->text
< it
->end
) {
1339 } else if (it
->pos
== it
->piece
->text
->size
) { /* EOF */
1347 bool text_iterator_next(Iterator
*it
) {
1348 size_t rem
= it
->end
- it
->text
;
1349 return text_iterator_init(it
, it
->pos
+rem
, it
->piece
? it
->piece
->next
: NULL
, 0);
1352 bool text_iterator_prev(Iterator
*it
) {
1353 size_t off
= it
->text
- it
->start
;
1354 size_t len
= it
->piece
&& it
->piece
->prev
? it
->piece
->prev
->len
: 0;
1355 return text_iterator_init(it
, it
->pos
-off
, it
->piece
? it
->piece
->prev
: NULL
, len
);
1358 bool text_iterator_valid(const Iterator
*it
) {
1359 /* filter out sentinel nodes */
1360 return it
->piece
&& it
->piece
->text
;
1363 bool text_iterator_byte_next(Iterator
*it
, char *b
) {
1364 if (!it
->piece
|| !it
->piece
->next
)
1367 if (it
->text
< it
->end
) {
1371 } else if (!it
->piece
->prev
) {
1375 while (it
->text
== it
->end
) {
1376 if (!text_iterator_next(it
)) {
1381 return text_iterator_prev(it
);
1390 bool text_iterator_byte_prev(Iterator
*it
, char *b
) {
1391 if (!it
->piece
|| !it
->piece
->prev
)
1393 bool eof
= !it
->piece
->next
;
1394 while (it
->text
== it
->start
) {
1395 if (!text_iterator_prev(it
)) {
1400 return text_iterator_next(it
);
1412 bool text_iterator_byte_find_prev(Iterator
*it
, char b
) {
1414 const char *match
= memrchr(it
->start
, b
, it
->text
- it
->start
);
1416 it
->pos
-= it
->text
- match
;
1420 text_iterator_prev(it
);
1422 text_iterator_next(it
);
1426 bool text_iterator_byte_find_next(Iterator
*it
, char b
) {
1428 const char *match
= memchr(it
->text
, b
, it
->end
- it
->text
);
1430 it
->pos
+= match
- it
->text
;
1434 text_iterator_next(it
);
1436 text_iterator_prev(it
);
1440 bool text_iterator_codepoint_next(Iterator
*it
, char *c
) {
1441 while (text_iterator_byte_next(it
, NULL
)) {
1442 if (ISUTF8(*it
->text
)) {
1451 bool text_iterator_codepoint_prev(Iterator
*it
, char *c
) {
1452 while (text_iterator_byte_prev(it
, NULL
)) {
1453 if (ISUTF8(*it
->text
)) {
1462 bool text_iterator_char_next(Iterator
*it
, char *c
) {
1463 if (!text_iterator_codepoint_next(it
, c
))
1465 mbstate_t ps
= { 0 };
1467 char buf
[MB_LEN_MAX
];
1468 size_t len
= text_bytes_get(it
->piece
->text
, it
->pos
, sizeof buf
, buf
);
1470 size_t wclen
= mbrtowc(&wc
, buf
, len
, &ps
);
1471 if (wclen
== (size_t)-1 && errno
== EILSEQ
) {
1473 } else if (wclen
== (size_t)-2) {
1475 } else if (wclen
== 0) {
1478 int width
= wcwidth(wc
);
1481 if (!text_iterator_codepoint_next(it
, c
))
1488 bool text_iterator_char_prev(Iterator
*it
, char *c
) {
1489 if (!text_iterator_codepoint_prev(it
, c
))
1492 char buf
[MB_LEN_MAX
];
1493 size_t len
= text_bytes_get(it
->piece
->text
, it
->pos
, sizeof buf
, buf
);
1495 mbstate_t ps
= { 0 };
1496 size_t wclen
= mbrtowc(&wc
, buf
, len
, &ps
);
1497 if (wclen
== (size_t)-1 && errno
== EILSEQ
) {
1499 } else if (wclen
== (size_t)-2) {
1501 } else if (wclen
== 0) {
1504 int width
= wcwidth(wc
);
1507 if (!text_iterator_codepoint_prev(it
, c
))
1514 bool text_byte_get(Text
*txt
, size_t pos
, char *byte
) {
1515 return text_bytes_get(txt
, pos
, 1, byte
);
1518 size_t text_bytes_get(Text
*txt
, size_t pos
, size_t len
, char *buf
) {
1523 for (Iterator it
= text_iterator_get(txt
, pos
);
1524 text_iterator_valid(&it
);
1525 text_iterator_next(&it
)) {
1528 size_t piece_len
= it
.end
- it
.text
;
1529 if (piece_len
> rem
)
1532 memcpy(cur
, it
.text
, piece_len
);
1540 char *text_bytes_alloc0(Text
*txt
, size_t pos
, size_t len
) {
1541 if (len
== SIZE_MAX
)
1543 char *buf
= malloc(len
+1);
1546 len
= text_bytes_get(txt
, pos
, len
, buf
);
1551 size_t text_size(Text
*txt
) {
1555 /* count the number of new lines '\n' in range [pos, pos+len) */
1556 static size_t lines_count(Text
*txt
, size_t pos
, size_t len
) {
1558 for (Iterator it
= text_iterator_get(txt
, pos
);
1559 text_iterator_valid(&it
);
1560 text_iterator_next(&it
)) {
1561 const char *start
= it
.text
;
1562 while (len
> 0 && start
< it
.end
) {
1563 size_t n
= MIN(len
, (size_t)(it
.end
- start
));
1564 const char *end
= memchr(start
, '\n', n
);
1570 len
-= end
- start
+ 1;
1580 /* skip n lines forward and return position afterwards */
1581 static size_t lines_skip_forward(Text
*txt
, size_t pos
, size_t lines
, size_t *lines_skipped
) {
1582 size_t lines_old
= lines
;
1583 for (Iterator it
= text_iterator_get(txt
, pos
);
1584 text_iterator_valid(&it
);
1585 text_iterator_next(&it
)) {
1586 const char *start
= it
.text
;
1587 while (lines
> 0 && start
< it
.end
) {
1588 size_t n
= it
.end
- start
;
1589 const char *end
= memchr(start
, '\n', n
);
1594 pos
+= end
- start
+ 1;
1603 *lines_skipped
= lines_old
- lines
;
1607 static void lineno_cache_invalidate(LineCache
*cache
) {
1612 size_t text_pos_by_lineno(Text
*txt
, size_t lineno
) {
1613 size_t lines_skipped
;
1614 LineCache
*cache
= &txt
->lines
;
1617 if (lineno
> cache
->lineno
) {
1618 cache
->pos
= lines_skip_forward(txt
, cache
->pos
, lineno
- cache
->lineno
, &lines_skipped
);
1619 cache
->lineno
+= lines_skipped
;
1620 } else if (lineno
< cache
->lineno
) {
1622 // TODO does it make sense to scan memory backwards here?
1623 size_t diff
= cache
->lineno
- lineno
;
1624 if (diff
< lineno
) {
1625 lines_skip_backward(txt
, cache
->pos
, diff
);
1628 cache
->pos
= lines_skip_forward(txt
, 0, lineno
- 1, &lines_skipped
);
1629 cache
->lineno
= lines_skipped
+ 1;
1631 return cache
->lineno
== lineno
? cache
->pos
: EPOS
;
1634 size_t text_lineno_by_pos(Text
*txt
, size_t pos
) {
1635 LineCache
*cache
= &txt
->lines
;
1636 if (pos
> txt
->size
)
1638 if (pos
< cache
->pos
) {
1639 size_t diff
= cache
->pos
- pos
;
1641 cache
->lineno
-= lines_count(txt
, pos
, diff
);
1643 cache
->lineno
= lines_count(txt
, 0, pos
) + 1;
1644 } else if (pos
> cache
->pos
) {
1645 cache
->lineno
+= lines_count(txt
, cache
->pos
, pos
- cache
->pos
);
1647 cache
->pos
= text_line_begin(txt
, pos
);
1648 return cache
->lineno
;
1651 Mark
text_mark_set(Text
*txt
, size_t pos
) {
1652 if (pos
== txt
->size
)
1653 return (Mark
)&txt
->end
;
1654 Location loc
= piece_get_extern(txt
, pos
);
1657 return (Mark
)(loc
.piece
->data
+ loc
.off
);
1660 size_t text_mark_get(Text
*txt
, Mark mark
) {
1665 if (mark
== (Mark
)&txt
->end
)
1668 for (Piece
*p
= txt
->begin
.next
; p
->next
; p
= p
->next
) {
1669 Mark start
= (Mark
)(p
->data
);
1670 Mark end
= start
+ p
->len
;
1671 if (start
<= mark
&& mark
< end
)
1672 return cur
+ (mark
- start
);