File: precachetrack.c

package info (click to toggle)
gtoaster 0.19991130-1
  • links: PTS
  • area: main
  • in suites: potato
  • size: 1,700 kB
  • ctags: 1,318
  • sloc: ansic: 12,739; sh: 353; makefile: 217; sed: 93
file content (280 lines) | stat: -rw-r--r-- 6,679 bytes parent folder | download
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
/* The Type of Track representing a raw cd track coming directly from cdrom */

#include <gtk/gtk.h>

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <stdlib.h>

#include "tracks.h"
#include "precachetrack.h"
#include "preferences.h"
#include "datacopydlg.h"
#include "cddrives.h"
#include "layoutconfig.h"

/* uncomment for debugging */
/* #define DEBUG */

/* information handed to the several functions used via callback to 
 * preread a track */

typedef struct 
{
	tracks_trackinfo *dc;
	precachetrack_info *info;

	int readtrack;
	int writetrack;
	int readcount;
	char buf[BUFSIZE];
	
	void (*callback)(tracks_trackinfo*,int);
	int inputwatch;

	datacopydlg_dlginfo *dlg;
} precachetrack_copyinfo;
	

int precachetrack_openpipe(void*trackinfo)
{	
	precachetrack_info *i;
	
	i=(precachetrack_info*)((tracks_trackinfo*)trackinfo)->data;

	if (i->filedes==-1)
	  {
		  i->filedes=open(i->tempfile,O_RDONLY);
		  if (i->filedes==-1)
		      perror ("error opening data track");	
	  }
	;
	return i->filedes;
}
;

void precachetrack_closepipe(void*trackinfo)
{
	precachetrack_info *i;
	
	i=(precachetrack_info*)((tracks_trackinfo*)trackinfo)->data;
	if (i->filedes!=-1)
	  {		  
		  close(i->filedes);
		  i->filedes=-1;
	  }
	;
	
}
;

int precachetrack_tracksize(void*trackinfo)
{
	precachetrack_info *i;
	struct stat buf;
	
	i=(precachetrack_info*)((tracks_trackinfo*)trackinfo)->data;
	
	stat(i->tempfile,&buf);
		
	return buf.st_size;
}
;

int precachetrack_dataavail(void*trackinfo)
{
	precachetrack_info *i;

	i=(precachetrack_info*)((tracks_trackinfo*)trackinfo)->data;

	return (i->filedes!=-1);
}
;	

/* FIXME: add function tracks_invalidate() to tracks.h to regulate
 * the availability of the tracks located on a cdrom */
int  precachetrack_valid(void*trackinfo) 
{
	return 1;
}
;

void precachetrack_destroy(void*trackinfo)
{
	precachetrack_info *i;
	
	i=(precachetrack_info*)((tracks_trackinfo*)trackinfo)->data;
	
	if (precachetrack_dataavail(trackinfo))
	    close (i->filedes);
	
	remove(i->tempfile);
	
	/* free the client claimed by the precachetrack */
	tracks_unclaim(i->client);
	
	free(i);
}
;


void precachetrack_donecb(precachetrack_copyinfo *copy)
{
	gdk_input_remove(copy->inputwatch);
	
#ifdef DEBUG
	printf ("precachetrack_donecb: closing track %s\n",(char*)&copy->info->client->name);
#endif	
	tracks_closepipe(copy->info->client);
	if (copy->writetrack!=-1)
	    close(copy->writetrack);
	
        layoutconfig_widget_hide(copy->dlg->messagebox,"datacopydialog");
	datacopydlg_destroy(copy->dlg);

   	/* report the new track to the caller,who can now replace the
	 * precachetracks client by the precachetrack itself */
	copy->callback(copy->dc,1);   
   	/* unclaim() our local copy of the precachetrack.
	 * it should have been claimed by the caller by now so this is
	 * safe and simply decreases the users-counter by one...
	 * If the caller did not claim the track,however,it is assumed
	 * that it has thought better of it and does not need that track
	 * any longer - the structure will hence be destroyed,including
	 * the buffer containing the precached data */
	tracks_unclaim(copy->dc);
	free(copy);
}
;

void precachetrack_stopcb(GtkWidget *w,precachetrack_copyinfo *copy)
{
	gdk_input_remove(copy->inputwatch);

#ifdef DEBUG
	printf ("precachetrack_stopcb: closing track %s\n",(char*)&copy->info->client->name);
#endif
	tracks_closepipe(copy->info->client);
	if (copy->writetrack!=-1)
	    close(copy->writetrack);

        layoutconfig_widget_hide(copy->dlg->messagebox,"datacopydialog");
	datacopydlg_destroy(copy->dlg);
	
	/* degrade precache requirements of current track */
	if (copy->info->client->precacherequired>0)
	    copy->info->client->precacherequired--;

   	/* return NULL instead of the new precachetrack and 0 to
	 * show that the operation was not successful */
	copy->callback(NULL,0);
	
	/* free the incomplete precache track,
	 * this does also unclaim() the client track for the precaching
	 * routines */
	tracks_unclaim(copy->dc);

	free(copy);
}
;	

void precachetrack_newdatacb(gpointer data,gint source,
			     GdkInputCondition condition)
{
	precachetrack_copyinfo *copy;
	
	copy=(precachetrack_copyinfo*)data;

	
	copy->readcount=read(copy->readtrack,&copy->buf,BUFSIZE);
	
	/* finish precaching when no more data could be read */
	if (copy->readcount>0)
	  {
		  write(copy->writetrack,&copy->buf,copy->readcount);
		  
		  datacopydlg_newdatablock(copy->dlg,copy->readcount);		  			    
	  }
	else
	    precachetrack_donecb(copy);
};		   
	


tracks_trackinfo *precachetrack_create(tracks_trackinfo *client,
				       void (*callback)(tracks_trackinfo*,int)
				       )
{
	char copyname[1024];
	
	precachetrack_copyinfo *copyinfo;
	copyinfo=(precachetrack_copyinfo*)malloc(sizeof(precachetrack_copyinfo));
	#ifdef DEBUG
	    printf ("precachetrack: allocated preachetrack_copyinfo at %p\n",
		    copyinfo);
	#endif
	
	copyinfo->info=(precachetrack_info*)malloc(sizeof(precachetrack_info));

	copyinfo->info->tempfile=tempnam(TEMPDIR,"tcache");	
	copyinfo->info->client=client;	
	copyinfo->info->filedes=-1;
	copyinfo->callback=callback;

	/* claim the client */
	tracks_claim(client);

	strcpy((char*)&copyname,(char*)&copyinfo->info->client->name);
	strcat((char*)&copyname," (precached)");	
	copyinfo->dc=tracks_create((char*)&copyname,
				   copyinfo->info->client->tracktype,none,
				   precachetrack_openpipe,
				   precachetrack_closepipe,
				   precachetrack_tracksize,
				   precachetrack_dataavail,
				   precachetrack_valid,
				   precachetrack_destroy,
				   copyinfo->info);

	copyinfo->dlg=datacopydlg_create("precaching file...",
					 GTK_SIGNAL_FUNC(precachetrack_stopcb),
					 copyinfo,
					 1, /* only one "thread" */
					 
					 DATACOPYDLG_SHOWTHROUGHPUT|
					 DATACOPYDLG_SHOWTIME_REMAINING|
					 DATACOPYDLG_SHOWTIME_ELAPSED|
					 DATACOPYDLG_SHOWPROGRESS|
					 DATACOPYDLG_SHOWPROGRESSINTITLE,
					 "",
					 "",					 
					 tracks_tracksize(client)

					 );
        layoutconfig_widget_show(copyinfo->dlg->messagebox,"datacopydialog");
   
	
 	copyinfo->writetrack=open(copyinfo->info->tempfile,O_CREAT|O_RDWR|O_TRUNC,0600);
#ifdef DEBUG
	printf ("opening track %s for precaching,mem location %p.\n",
		(char*)&copyinfo->info->client->name,
		copyinfo->info->client);
#endif
	copyinfo->readtrack=tracks_openpipe(copyinfo->info->client);
	/* wait for the permission to read at first */
	copyinfo->inputwatch=gdk_input_add(copyinfo->readtrack,
					   GDK_INPUT_READ,
					   precachetrack_newdatacb,
					   copyinfo);
		  	
	return copyinfo->dc;

}
;