3
/* Synchronize data in buffers with the file system.
4
** If f is nil, all streams are sync-ed
6
** Written by Kiem-Phong Vo.
10
static int _sfall(void)
15
reg Sfpool_t *p, *next;
18
reg int nsync, count, loop;
21
for(loop = 0; loop < MAXLOOP; ++loop)
22
{ rv = nsync = count = 0;
23
for(p = &_Sfpool; p; p = next)
24
{ /* find the next legitimate pool */
25
for(next = p->next; next; next = next->next)
29
/* walk the streams for _Sfpool only */
30
for(n = 0; n < ((p == &_Sfpool) ? p->n_sf : 1); ++n)
34
if(f->flags&SF_STRING )
38
if((f->mode&SF_READ) && (f->mode&SF_SYNCED) )
40
if((f->mode&SF_READ) && !(f->bits&SF_MMAP) &&
43
if((f->mode&SF_WRITE) && !(f->bits&SF_HOLE) &&
62
int sfsync(reg Sfio_t* f)
65
reg Sfio_t* f; /* stream to be synchronized */
76
GETLOCAL(origf,local);
78
if(origf->disc == _Sfudisc) /* throw away ungetc */
79
(void)sfclose((*_Sfstack)(origf,NIL(Sfio_t*)));
83
if((origf->mode&SF_RDWR) != SFMODE(origf,local) && _sfmode(origf,0,local) < 0)
90
if((f->flags&SF_IOCHECK) && f->disc && f->disc->exceptf)
91
(void)(*f->disc->exceptf)(f,SF_SYNC,(Void_t*)((int)1),f->disc);
95
/* pretend that this stream is not on a stack */
96
mode = f->mode&SF_PUSH;
99
/* these streams do not need synchronization */
100
if((f->flags&SF_STRING) || (f->mode&SF_SYNCED))
103
if((f->mode&SF_WRITE) && (f->next > f->data || (f->bits&SF_HOLE)) )
104
{ /* sync the buffer, make sure pool don't move */
105
reg int pool = f->mode&SF_POOL;
107
if(f->next > f->data && (SFWRALL(f), SFFLSBUF(f,-1)) < 0)
109
if(!SFISNULL(f) && (f->bits&SF_HOLE) )
110
{ /* realize a previously created hole of 0's */
111
if(SFSK(f,(Sfoff_t)(-1),SEEK_CUR,f->disc) >= 0)
112
(void)SFWR(f,"",1,f->disc);
118
if((f->mode&SF_READ) && f->extent >= 0 &&
119
((f->bits&SF_MMAP) || f->next < f->endb) )
120
{ /* make sure the file pointer is at the right place */
121
f->here -= (f->endb-f->next);
122
f->endr = f->endw = f->data;
123
f->mode = SF_READ|SF_SYNCED|SF_LOCK;
124
(void)SFSK(f,f->here,SEEK_SET,f->disc);
126
if((f->flags&SF_SHARE) && !(f->flags&SF_PUBLIC) &&
128
{ f->endb = f->next = f->data;
129
f->mode &= ~SF_SYNCED;
137
if((f->flags&SF_IOCHECK) && f->disc && f->disc->exceptf)
138
(void)(*f->disc->exceptf)(f,SF_SYNC,(Void_t*)((int)0),f->disc);
142
if(!local && f && (f->mode&SF_POOL) && f->pool && f != f->pool->sf[0])
143
SFSYNC(f->pool->sf[0]);
145
SFMTXRETURN(origf, rv);