-
Notifications
You must be signed in to change notification settings - Fork 2
/
journal.c
892 lines (766 loc) · 19.2 KB
/
journal.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
/*
| journal.c
|
| This file contains the routines for handling the journal file
| for ae.
|
| $Header: /home/hugh/sources/aee/RCS/journal.c,v 1.41 2010/07/18 21:53:50 hugh Exp hugh $
*/
/*
| This file contains routines for performing journalling for aee and
| xae. Journalling is writing lines and information about them to a
| file when the cursor moves away from them (if they have been changed
| during the time the cursor was on the line).
|
| The data structure ae_file_info contains the information about
| locations in the file for where to find the information about the
| current line, the information about the previous line, the info
| about the next line, and the location of the actual text. The
| length of the line (which is important!) is contained in the 'text'
| structure for the line.
|
| The format that the information is written into the file is as
| follows:
|
| +--+--+--+--+
| | | | | | previous line info location
| +--+--+--+--+
|
| +--+--+--+--+
| | | | | | next line info location
| +--+--+--+--+
|
| +--+--+--+--+
| | | | | | text location
| +--+--+--+--+
|
| +--+--+--+--+
| | | | | | text length (in bytes, including terminating NULL)
| +--+--+--+--+
|
*/
/*
| Also in this file are routines for managing information about what
| journal files have been written, and the relationship between the
| journal file and the file being journalled. This helps when
| recovering from a disaster, and could be used to determine duplicate
| edit sessions, or a failed previous session.
|
| The name of the file is ~/.aeeinfo, and there is a lock file named
| ~/.aeeinfo.L. The format of ~/.aeeinfo is as follows:
|
| 32 30 /home/hugh/sources/aee/journal.c /tmp/hugh/journal/journal.c.rv
|
| where the first number is the length (decimal) if the full file name,
| the second number is the length of the journal file name, followed by
| the file name, and the name of the journal file. The fields are
| separated by a single space (ASCII 32).
|
|
*/
/*
|
| Copyright (c) 1994, 1995, 1996, 1998, 2000, 2001, 2002, 2009, 2010 Hugh Mahon.
|
*/
char *jrn_vers_str = "@(#) journal.c $Revision: 1.41 $";
#include "aee.h"
#include <time.h>
#include <unistd.h>
/*
| writes the contents of the line, updates the value stored in
| memory of where the start of the line is stored in the file
*/
void
write_journal(buffer, line)
struct bufr *buffer;
struct text *line;
{
int counter;
int ret_val = 0;
ret_val = line->file_info.line_location =
lseek(buffer->journ_fd, 0, SEEK_END);
for (counter = 0; (counter < line->line_length) && (ret_val != -1);
counter += min(1024, (line->line_length - counter)))
{
ret_val = write(buffer->journ_fd, &(line->line[counter]),
min(1024, (line->line_length - counter)));
}
update_journal_entry(buffer, line);
line->changed = FALSE;
}
/*
| update the information for a line that has already been written, or
| write a line for the first after its values have been initialised
| by journ_info_init
*/
void
update_journal_entry(buffer, line)
struct bufr *buffer;
struct text *line;
{
int ret_val;
if (line->file_info.info_location == NO_FURTHER_LINES)
{
journ_info_init(buffer, line);
return ;
}
ret_val = lseek(buffer->journ_fd, line->file_info.info_location,
SEEK_SET);
ret_val |= write(buffer->journ_fd,
(char *)&(line->file_info.prev_info), sizeof(unsigned long));
ret_val |= write(buffer->journ_fd,
(char *)&(line->file_info.next_info), sizeof(unsigned long));
ret_val |= write(buffer->journ_fd,
(char *)&(line->file_info.line_location),
sizeof(unsigned long));
ret_val |= write(buffer->journ_fd, (char *)&(line->line_length),
sizeof(int));
}
/*
| change journal info at first line if the first line is deleted
| while editing
*/
void
remove_journ_line(buffer, line)
struct bufr *buffer;
struct text *line;
{
if (line->prev_line == NULL)
{
/*
| This was the first line, now the next line is.
*/
line->next_line->file_info.info_location =
line->file_info.info_location;
update_journal_entry(buffer, line->next_line);
if (line->next_line->next_line != NULL)
{
line->next_line->next_line->file_info.prev_info =
line->file_info.info_location;
update_journal_entry(buffer, line->next_line->next_line);
}
}
else if (line->next_line == NULL)
{
/*
| This was the last line, now the prev line is.
*/
line->prev_line->file_info.next_info = NO_FURTHER_LINES;
update_journal_entry(buffer, line->prev_line);
/*
| no point in writing to file since no way to
| access current line now
*/
}
else
{
/*
| Need to allow previous line to see next line,
| vice-versa.
*/
line->prev_line->file_info.next_info =
line->next_line->file_info.info_location;
line->next_line->file_info.prev_info =
line->prev_line->file_info.info_location;
update_journal_entry(buffer, line->next_line);
update_journal_entry(buffer, line->prev_line);
}
}
/*
| initialize the location of where the information is to reside
| (using lseek), initialize information for this line of where the
| previous line is, set the information where to find this line for
| the previous line, next line then
|
| update current line (write for the first time)
| update the previous line (if prev_line != NULL)
| update the next line (if next_line != NULL)
|
*/
void
journ_info_init(buffer, line)
struct bufr *buffer;
struct text *line;
{
int ret_val;
ret_val = line->file_info.info_location =
lseek(buffer->journ_fd, 0, SEEK_END);
if (ret_val == -1)
{
wmove(com_win, 0, 0);
wstandout(com_win);
wprintw(com_win, journal_err_str);
wstandend(com_win);
wrefresh(com_win);
return;
}
if (line->prev_line != NULL)
{
line->file_info.prev_info =
line->prev_line->file_info.info_location;
line->prev_line->file_info.next_info =
line->file_info.info_location;
}
if (line->next_line != NULL)
{
line->file_info.next_info =
line->next_line->file_info.info_location;
line->next_line->file_info.prev_info =
line->file_info.info_location;
}
else
line->file_info.next_info = NO_FURTHER_LINES;
update_journal_entry(buffer, line);
if (line->prev_line != NULL)
update_journal_entry(buffer, line->prev_line);
if ((line->next_line != NULL) &&
(line->file_info.next_info != NO_FURTHER_LINES))
update_journal_entry(buffer, line->next_line);
}
/*
| read the journal entry from the file
*/
void
read_journal_entry(buffer, line)
struct bufr *buffer;
struct text *line;
{
int counter;
lseek(buffer->journ_fd, line->file_info.info_location, SEEK_SET);
read(buffer->journ_fd, (char *)&(line->file_info.prev_info),
sizeof(unsigned long));
read(buffer->journ_fd, (char *)&(line->file_info.next_info),
sizeof(unsigned long));
read(buffer->journ_fd, (char *)&(line->file_info.line_location),
sizeof(unsigned long));
read(buffer->journ_fd, (char *)&(line->line_length),
sizeof(unsigned int));
lseek(buffer->journ_fd, line->file_info.line_location, SEEK_SET);
line->line = malloc(line->line_length);
for (counter = 0; counter < line->line_length;
counter += min(1024, (line->line_length - counter)))
{
read(buffer->journ_fd, &(line->line[counter]),
min(1024, (line->line_length - counter)));
}
}
/*
| read the contents of the edited file from the journal file
| read the file up to the first '\n' (line feed), which is where
| the journal information begins
|
| This reads the contents of the edited file, plus sets the values
| in the data structures to allow continued editing (it is recreated
| within the editor, so why not?).
*/
int
recover_from_journal(buffer, file_name)
struct bufr *buffer;
char *file_name;
{
int counter = 0;
struct text *line;
char temp;
char done = FALSE;
char name[1024]; /* name of file stored in journal file */
ssize_t len = 0;
if ((buffer->journ_fd = open(file_name, O_RDONLY)) == -1)
{
/*
| Unable to open journal file.
*/
buffer->journ_fd = '1';
return(1);
}
memset(name, 0, 1024);
/*
| get to first record in file (skip the file name)
*/
do
{
len = read(buffer->journ_fd, &temp, 1);
if (len == 0)
{
/*
| we've got a zero length file, there's a problem
*/
buffer->journalling = FALSE;
return(1);
}
if ((counter < 1024) && (temp != '\n'))
name[counter] = temp;
counter++;
}
while (temp != '\n');
line = buffer->first_line;
/*
| set up first entry
*/
line->file_info.info_location = counter;
do
{
read_journal_entry(buffer, line);
line->vert_len = (scanline(line, line->line_length) / COLS) + 1;
line->max_length = line->line_length;
if (line->file_info.next_info != NO_FURTHER_LINES)
{
line->next_line = txtalloc();
buffer->num_of_lines++;
line->next_line->prev_line = line;
line->next_line->next_line = NULL;
line->next_line->line_number = line->line_number + 1;
line = line->next_line;
line->file_info.next_info = 0;
line->file_info.info_location =
line->prev_line->file_info.next_info;
}
else
done = TRUE;
}
while (!done);
curr_buff->pointer = buffer->first_line->line;
close(buffer->journ_fd);
change = TRUE;
buffer->changed = TRUE;
/*
| open journal for further changes
*/
if (buffer->journalling)
{
if ((buffer->journ_fd =
open(buffer->journal_file, O_WRONLY)) == -1)
{
wprintw(com_win, cant_opn_rcvr_fil_msg);
buffer->journalling = FALSE;
}
}
/*
| Since the user can specify a journal name without specifying
| the name of the file it journalled, use the name stored in
| the journal file.
*/
if ((buffer->full_name == NULL) || (*buffer->full_name == '\0'))
{
buffer->full_name = strdup(name);
if ((buffer->full_name != NULL) && (*buffer->full_name != '\0'))
{
buffer->file_name = ae_basename(buffer->full_name);
if (strcmp(main_buffer_name, buffer->name))
buffer->name = strdup(buffer->file_name);
}
}
/*
| Success!
*/
return(0);
}
/*
| journal database file routines
*/
static char *lock_file_name = NULL;
static char *db_file_name = NULL;
/*
| write a lock file so that another process doesn't try to open
| the file
|
| This is an atomic action since the open should fail if the file
| already exists, thus it can't be opened if someone else has a
| lock in place.
*/
int
lock_journal_fd()
{
int counter = 0;
int ret_val;
if (lock_file_name == NULL)
lock_file_name = resolve_name("~/.aeeinfo.L");
/*
| At some point may want to put in a check for an old lock file.
*/
while (((ret_val = open(lock_file_name, (O_CREAT | O_EXCL), 0700)) == -1) && (counter < 3))
{
sleep(1);
counter++;
}
if (ret_val != -1)
close(ret_val);
return(ret_val);
}
/*
| remove the lock file
*/
void
unlock_journal_fd()
{
unlink(lock_file_name);
}
/*
| free the memory used in the list of files being edited and their
| journal files
*/
void
free_db_list(list)
struct journal_db *list;
{
if (list->next != NULL)
free_db_list(list->next);
free(list->journal_name);
if (list->file_name != NULL)
free(list->file_name);
free(list);
}
/*
| read the file ~/.aeeinfo (which contains the name of the file
| being edited and the name of the journal file associated with it)
*/
struct journal_db *
read_journal_db()
{
char buffer[4092];
char *tmp;
int file_name_len;
int journ_name_len;
struct journal_db *top_of_list = NULL;
struct journal_db *list = NULL;
FILE *db_fp;
if (lock_journal_fd() == -1)
return(NULL);
if (db_file_name == NULL)
db_file_name = resolve_name("~/.aeeinfo");
db_fp = fopen(db_file_name, "r");
if (db_fp == NULL)
{
unlock_journal_fd();
return(NULL);
}
while ((tmp = fgets(buffer, 4092, db_fp)) != NULL)
{
if (list == NULL)
top_of_list = list =
(struct journal_db *) malloc(sizeof(struct journal_db));
else
{
list->next = (struct journal_db *)
malloc(sizeof(struct journal_db));
list = list->next;
}
list->next = NULL;
if ((*tmp == ' ') || (*tmp == '\t'))
tmp = next_word(tmp);
file_name_len = atoi(tmp);
tmp = next_word(tmp);
journ_name_len = atoi(tmp);
if (file_name_len > 0)
{
tmp = next_word(tmp);
list->file_name = (char *) malloc(file_name_len + 1);
strncpy(list->file_name, tmp, file_name_len);
list->file_name[file_name_len] = '\0';
}
else
list->file_name = NULL;
tmp = next_word(tmp);
list->journal_name = (char *) malloc(journ_name_len + 1);
strncpy(list->journal_name, tmp, journ_name_len);
list->journal_name[journ_name_len] = '\0';
}
fclose(db_fp);
return(top_of_list);
}
/*
| Write the file ~/.aeeinfo .
*/
void
write_db_file(list)
struct journal_db *list;
{
struct journal_db *tmp;
FILE *db_fp;
db_fp = fopen(db_file_name, "w");
if (db_fp == NULL)
return;
for (tmp = list; (db_fp != NULL) && (tmp != NULL); tmp = tmp->next)
{
if (tmp->file_name != NULL)
{
fprintf(db_fp, "%d %d %s %s\n",
(int)strlen(tmp->file_name),
(int)strlen(tmp->journal_name),
tmp->file_name,
tmp->journal_name);
}
else
{
fprintf(db_fp, "%d %d %s\n", 0,
(int)strlen(tmp->journal_name),
tmp->journal_name);
}
}
fclose(db_fp);
unlock_journal_fd();
}
/*
| add an entry to the file ~/.aeeinfo (a new file being edited and
| its associated journal file)
*/
void
add_to_journal_db(buffer)
struct bufr *buffer;
{
struct journal_db *top_of_list;
struct journal_db *list;
top_of_list = list = read_journal_db();
if (list == (struct journal_db *) -1) /* could not open lock file */
return;
if (list == NULL)
{
top_of_list = list =
(struct journal_db *) malloc(sizeof(struct journal_db));
}
else
{
/*
| Could check for duplicate file being edited here.
*/
while (list->next != NULL)
list = list->next;
list->next = (struct journal_db *) malloc(sizeof(struct journal_db));
list = list->next;
}
list->next = NULL;
list->journal_name = strdup(buffer->journal_file);
if (*buffer->full_name != '\0')
list->file_name = strdup(buffer->full_name);
else
list->file_name = NULL;
write_db_file(top_of_list);
free_db_list(top_of_list);
}
/*
| remove an entry from the file ~/.aeeinfo (the editor is being
| exited and it is time to delete the journal file, so remove the
| reference)
*/
void
rm_journal_db_entry(buffer)
struct bufr *buffer;
{
struct journal_db *top_of_list;
struct journal_db *list;
struct journal_db *tmp;
int changed = FALSE;
top_of_list = list = read_journal_db();
if (list == (struct journal_db *) NULL) /* could not open db file */
{
return;
}
if ((!strcmp(buffer->journal_file, top_of_list->journal_name)) &&
((top_of_list->file_name == NULL) ||
((top_of_list->file_name != NULL) &&
(!strcmp(buffer->full_name, top_of_list->file_name)))))
{
top_of_list = list->next;
tmp = list;
changed = TRUE;
}
else
{
while (((list->next != NULL) && (buffer->journal_file != NULL) &&
(buffer->full_name != NULL) &&
(!((list->next->file_name != NULL) &&
(!strcmp(buffer->full_name, list->next->file_name)
&& !strcmp(buffer->journal_file, list->next->journal_name)))))
&& (!((list->next->file_name == NULL) &&
!strcmp(buffer->journal_file, list->next->journal_name))))
{
list = list->next;
}
if (list->next != NULL)
{
tmp = list->next;
list->next = list->next->next;
changed = TRUE;
}
}
if (changed == TRUE)
{
if (top_of_list == NULL) /* no entries for file */
{
unlink(db_file_name);
unlock_journal_fd();
}
else
{
write_db_file(top_of_list);
}
tmp->next = NULL;
free_db_list(tmp);
}
else
unlock_journal_fd();
if (top_of_list != NULL)
free_db_list(top_of_list);
}
/*
| check for the existence of a directory, and if it doesn't exist
| create it (will recursively go through and create the directory
| from the highest location where a directory does exist)
*/
int
create_dir(name)
char *name;
{
char *path;
struct stat buf;
int ret_val;
if ((name == (char *)NULL) || (*name == '\0'))
return(-1);
ret_val = stat(name, &buf);
if (ret_val == -1)
{
path = ae_dirname(name);
ret_val = create_dir(path);
if (ret_val != -1)
ret_val = mkdir(name, 0700);
free(path);
}
return(ret_val);
}
/*
| determine a name for a journal file and make sure no other journal
| file of that name already exists (if one does, create a name based
| on the current date and time)
*/
void
journal_name(buffer, file_name)
struct bufr *buffer;
char *file_name;
{
char *temp, *buff, *name;
int count;
if (*file_name == '\0')
{
struct tm *time_info;
time_t t;
int ret_val;
/*
| create a file name based on the date, e.g.,
| 950913221206.rv
| Not a guarantee of uniqueness, but certainly not
| likely to be repeated.
*/
file_name = (char *)malloc(14);
t = time(NULL);
time_info = gmtime(&t);
ret_val = strftime(file_name, 13, "%y%m%d%H%M%S", time_info);
}
if ((journal_dir != NULL) && (*journal_dir != '\0'))
{
name = ae_basename(file_name);
buffer->journal_file = temp =
xalloc(strlen(name) + 5 + strlen(journal_dir));
strcpy(temp, journal_dir);
strcat(temp, "/");
strcat(temp, name);
}
if (buffer->journal_file == NULL)
{
buffer->journal_file = temp = xalloc(strlen(file_name) + 4);
strcpy(temp, file_name);
}
buff = ae_basename(temp);
temp = buff;
if (strlen(temp) >= (MAX_NAME_LEN - 3))
{
for (count = 1; count < (MAX_NAME_LEN - 3); count++)
temp++;
}
else
{
while(*temp != '\0')
temp++;
}
copy_str(".rv", temp);
}
/*
| create and initialize the journal file
*/
void
open_journal_for_write(buffer)
struct bufr *buffer;
{
int ret_val;
int length;
int counter;
char *temp;
struct tm *time_info;
time_t t;
if (((buffer->journ_fd =
open(buffer->journal_file, (O_CREAT | O_EXCL | O_WRONLY), 0600)) == -1) &&
(errno == EEXIST))
{
/*
| file already exists
*/
buffer->journal_file = realloc(buffer->journal_file,
(strlen(buffer->journal_file) + 17));
/*
| find the last '/'
*/
temp = strrchr(buffer->journal_file, '/');
if (temp != NULL)
temp++;
else
temp = buffer->journal_file;
/*
| create a file name based on the date, e.g.,
| 950913221206.rv
| Not a guarantee of uniqueness, but certainly not
| likely to be repeated.
*/
t = time(NULL);
time_info = gmtime(&t);
ret_val = strftime(temp, 16, "%y%m%d%H%M%S.rv", time_info);
/*
| For some extra paranoia, check this one, and append
| a character (a-z) to get uniqueness.
*/
counter = 'a';
length = strlen (buffer->journal_file);
while (((buffer->journ_fd =
open(buffer->journal_file, (O_CREAT | O_EXCL | O_WRONLY), 0600)) == -1)
&& (errno == EEXIST) && (counter <= 'z'))
{
buffer->journal_file[length] = counter;
buffer->journal_file[length + 1] = '\0';
counter++;
}
}
if (buffer->journ_fd == -1)
{
wprintw(com_win, cant_opn_rcvr_fil_msg);
buffer->journalling = FALSE;
buffer->journ_fd = '1';
return;
}
add_to_journal_db(buffer);
if (*buffer->full_name != '\0')
{
ret_val = write(buffer->journ_fd, buffer->full_name,
strlen(buffer->full_name));
}
ret_val |= write(buffer->journ_fd, "\n", 1);
journ_info_init(buffer, buffer->first_line);
}
/*
| delete a journal file and delete its entry from the file ~/.aeeinfo
*/
void
remove_journal_file(buffer)
struct bufr *buffer;
{
close(buffer->journ_fd);
unlink(buffer->journal_file);
rm_journal_db_entry(buffer);
}