File: file.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 (224 lines) | stat: -rw-r--r-- 4,594 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
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
#include "wily.h"
#include "data.h"

static int		data_getstat		(Data*, char*, char*, Stat*);
static void	data_opennew		(Data*, char*, char*path);
static int		data_getdir		(Data*, char*, char*, Stat*);
static int		data_getfile		(Data*, char*, char*, Stat*);
static void	data_settag		(Data*, char*);
static Data *	data_alloc		(void);

/*
 * Open a new 'data' to represent 'label'.  Either read 'label', or if
 * it doesn't exist and 'create' is set, return an empty window to
 * represent where we _will_ write it.
 *
 * Return (View*)0 if 'label' exists but we can't read it, or it doesn't
 * exist and we don't want to create it.
 */
View*
data_open(char*label, Bool create) {
	Data	*d;
	Stat	buf;
	Path	path;
	Bool	failure;
	
	label2path(path, label);

	d = data_alloc();
	failure = false;
	
	if(stat(path,&buf)) { /* doesn't exist -- yet */
		if(create)
			data_opennew(d, label, path);
		else
			failure = true;
	} else {
		failure = data_getstat(d, label, path,&buf);
	}
	
	if(failure){
		data_listremove(d);
		free(d);
		return 0;
	} else {
		if(d->names)
			add_slash(path);
		data_settag(d, path);
		win_new(path, d->tag, d->t); 
		return text_view(d->t);
	}
}

/*
 * Read file or directory 'label' into 'd'. 
 * Return 0 for success.
 */
int
data_get(Data *d, char *label) {
	Stat	buf;
	Path	path;
	
	if(data_backup(d))
		return -1;		/* If we did this, we'd lose data */

	if(!label)
		label = d->label;	/* is strcpy(a,a) bad? */
	label2path(path,label);
	if(stat(path,&buf)){
		diag(0, "Couldn't stat [%s (%s)]", path, label);
		return -1;
	} else {
		return data_getstat(d, label, path, &buf);
	}
}

/* Load 'd' with 'path', which has stat buffer 'buf'.
 * Returns 0 if we loaded succesfully.
 */
static int
data_getstat(Data*d, char* label, char*path, Stat*buf) {
	int	failed;
	
	cursorswitch(&boxcursor);
	bflush();
	undo_reset(d->t);
	failed =  S_ISDIR(buf->st_mode) ?
		data_getdir(d, label, path, buf) :
		data_getfile(d, label, path, buf);

	undo_start(d->t);
	if(!failed)
		undo_mark(d->t);
	
	cursorswitch(cursor);
	return failed;
}

/* Load 'd' with new file 'path'. */
static void
data_opennew(Data*d, char*label, char*path) {
	/* d */
	pathcontract(d->label, label);
	strcpy(d->cachedlabel, d->label);
	d->has_stat = false;
	data_setbackup(d, path);
	dirnames_free(d->names);
	d->names = 0;

	/* d->t */
	text_replace(d->t, text_all(d->t), rstring(0,0));
	undo_start(d->t);
	undo_mark(d->t);
	
	/* d->tag */
	tag_setlabel(d->tag, d->label);
	tag_rmtool(d->tag, "Put");
}

/* Load 'd' with directory 'path', which has stat buffer 'buf'.
 * Returns 0 if we loaded succesfully.
 */
static int
data_getdir(Data*d, char*label, char*path, Stat*buf) {
	DIR*	dirp;
	
	if(!(dirp = opendir(path))) {
		diag(path, "opendir [%s]", path);
		return -1;	/* failure - modify nothing */
	}
	
	/* d */
	pathcontract(d->label,label);
	add_slash(d->label);
	strcpy(d->cachedlabel, d->label);
	
	d->has_stat = true;
	d->stat = *buf;
	data_setbackup(d,0);
	dirnames_free(d->names);
	d->names = dirnames(dirp, path);

	/* d->t */
	text_formatdir(d->t, d->names);
	
	/* d->tag */
	tag_rmtool(d->tag, "Put");
	tag_addtool(d->tag, "Get");
	tag_setlabel(d->tag, d->label);
	return 0;
}

/* Load 'd' with file 'path', which has stat buffer 'buf'.
 * Returns 0 if we loaded succesfully.
 */
static int
data_getfile(Data*d, char*label, char*path, Stat*buf) {
	int	fd;
	extern Bool utfHadNulls;
	if(!(fd = open(path, O_RDONLY))) {
		diag(path, "open [%s]", path);
		return -1;	/* failure - modify nothing */
	}
	
	/* d */
	pathcontract(d->label, label);
	strcpy(d->cachedlabel, d->label);

	d->has_stat = true;
	d->stat = *buf;
	data_setbackup(d,path);
	dirnames_free(d->names);
	d->names = 0;

	/* d->t */
	text_read(d->t, fd, buf->st_size);
	close(fd);
	if (utfHadNulls) {
		diag(path, "removed nulls from [%s]", path);
		data_setbackup(d,0);
	}

	/* d->tag */
	tag_rmtool(d->tag, "Put");
	tag_rmtool(d->tag, "Get");
	tag_setlabel(d->tag, d->label);
	return 0;
}

/* Fill 'buf' with appropriate stuff for d's tag */
static void
data_settag(Data*d, char *path) {
	Path	buf;
	char*common;
	char*specific;
	
	common = d->names? dirtools : filetools;
	specific = tag_match(path);
	
	sprintf (buf, "%s | %s %s ", d->label, common, specific);
	tag_set(d->tag, buf);
}

static Data *
data_alloc(void) {
	Data	*d;
	static Id id;
	
	d = NEW(Data);
	d->id = id++;
	d->next = dataroot;
	dataroot = d;
	d->t = text_alloc(d, true);
	d->tag = text_alloc(d, false);
	d->names = 0;
	d->backupto = 0;
	d->fd = 0;
	d->emask = 0;
	d->has_stat = false;
	
	strcpy(d->label, "");
	strcpy(d->cachedlabel, "");
	
	return d;
}