File: data.c

package info (click to toggle)
wily 0.13.33-3
  • links: PTS
  • area: main
  • in suites: slink
  • size: 1,500 kB
  • ctags: 1,720
  • sloc: ansic: 12,830; sh: 245; makefile: 188
file content (163 lines) | stat: -rw-r--r-- 2,788 bytes parent folder | download | duplicates (2)
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
#include "wily.h"
#include "data.h"
#include <errno.h>

Data		*dataroot;

Text*
data_body(Data*d) {
	return d->t;
}

Text*
data_tag(Data*d){
	return d->tag;
}

/* Return the names in directory 'd', or 0 */
char**
data_names(Data*d){
	return d?d->names:0;
}

/* Return true if 'd' should have its resize box filled in */
Bool
data_isdirty(Data *d) {
	return d && NEEDSBACKUP(d);
}

/* Write all dirty windows.  Return 0 for success. */
int
data_putall(void) {
	Data	*d;
	int	retval = 0;

	for(d=dataroot; d; d=d->next)
		if(NEEDSBACKUP(d))
			if (data_put(d,0))
				retval = -1;
	return retval;
}

/* Backup all dirty files.  Return 0 unless we failed to backup something. */
int
data_backupall(void) {
	Data	*d;
	int	retval= 0;

	for(d = dataroot; d; d = d->next) {
		if (data_backup(d)) {
			retval = 1;
		}
	}
	return retval;
}

/*
 * Write the contents of 'd' to 'path'.
 * Return 0 if we've successfully written 'd' to its label.
 */
int
data_put(Data *d, char *label) {
	int		fd;
	Stat		buf;
	Path		path;
	Bool		statfailed, writefailed;
	
	if(!label)
		label = d->label;
	label2path(path, label);
	
	statfailed = stat(path, &buf);

	if((fd = open(path,O_RDWR|O_CREAT|O_TRUNC, 0666))<0){
		diag(path, "couldn't open \"%s\" for write", path);
		return -1;
	}
	writefailed = text_write_range(d->t, text_all(d->t), fd);
	close(fd);
	if(writefailed) {
		diag(path, "write failed: \"%s\"", path);
		return -1;
	}
	
	if (!strcmp(d->label,label) || !statcmp(&buf, &d->stat)) {
		if(!d->names) {
			tag_rmtool(d->tag, "Put");
			undo_mark(d->t);
		}
		return 0;
	}
	return -1;
}

void
data_listremove(Data *d) {
	Data	**ptr;

	for(ptr = &dataroot; *ptr != d; ptr = &((*ptr)->next))
		;
	*ptr = d->next;
}

/* Delete 'd' if we can do so without permanently losing
 * data, and return 0.  If there's a problem, return 1.
 */
int
data_del(Data*d) {
	if(data_backup(d))
		return 1;
	
	data_senddestroy(d);
	data_listremove(d);
	dirnames_free(d->names);
	if(d->backupto)
		free(d->backupto);
	free(d);
	return 0;
}

/* Backup 'd' if necessary.  Return 0 for success. */
int
data_backup(Data *d) {
	Path	fname;

	if (!NEEDSBACKUP(d))
		return 0;

	if( (backup_name(d->backupto, fname)) )
		return -1;
	if(text_write(d->t, fname))
		return -1;

	errno = 0;
	diag(fname, "backup %s %s", mybasename(fname), d->backupto);
	return 0;
}

/* Record that in emergencies, 'd' should be saved, noting
 * that it is a copy of 'bfile'
 */
void
data_setbackup(Data *d, char*bfile) {
	if(d->backupto)
		free(d->backupto);
	if(bfile && !strstr(bfile, "+Errors")) {
		d->backupto = strdup(bfile);
		text_setneedsbackup(d->t, true);
	} else {
		d->backupto = 0;
		text_setneedsbackup(d->t, false);
	};
}

Data *
data_findid(Id id) {
	Data	*d;

	for(d=dataroot; d; d=d->next)
		if(d->id==id)
			break;
	return d;
}