projects
/
ruby-vorbistagger.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
Code cleanup.
[ruby-vorbistagger.git]
/
ext
/
vcedit.c
diff --git
a/ext/vcedit.c
b/ext/vcedit.c
index 832f417b2b30519cba6661b3dfb063c52a43f925..f3ad2c95a1a1557468947739d46ec08838a70928 100644
(file)
--- a/
ext/vcedit.c
+++ b/
ext/vcedit.c
@@
-23,6
+23,8
@@
#include <errno.h>
#include <limits.h>
#include <unistd.h>
#include <errno.h>
#include <limits.h>
#include <unistd.h>
+#include <sys/types.h>
+#include <sys/stat.h>
#include <ogg/ogg.h>
#include <vorbis/codec.h>
#include <assert.h>
#include <ogg/ogg.h>
#include <vorbis/codec.h>
#include <assert.h>
@@
-40,9
+42,9
@@
struct vcedit_state_St {
vorbis_comment vc;
vorbis_info vi;
vorbis_comment vc;
vorbis_info vi;
- char filename[PATH_MAX];
-
FILE *in;
FILE *in;
+ mode_t file_mode;
+
bool opened;
long serial;
unsigned char *mainbuf;
bool opened;
long serial;
unsigned char *mainbuf;
@@
-53,27
+55,31
@@
struct vcedit_state_St {
int prevW;
int extrapage;
int eosin;
int prevW;
int extrapage;
int eosin;
+
+ char filename[0];
};
static void
};
static void
-vcedit_state_free (vcedit_state *s
tate
)
+vcedit_state_free (vcedit_state *s)
{
{
- free (s
tate
->mainbuf);
- free (s
tate
->bookbuf);
- free (s
tate
->vendor);
+ free (s->mainbuf);
+ free (s->bookbuf);
+ free (s->vendor);
- if (s
tate
->in) {
- fclose (s
tate
->in);
- s
tate
->in = NULL;
+ if (s->in) {
+ fclose (s->in);
+ s->in = NULL;
}
}
- free (s
tate
);
+ free (s);
}
static bool
}
static bool
-vcedit_state_init (vcedit_state *s
tat
e)
+vcedit_state_init (vcedit_state *s
, const char *filenam
e)
{
{
- state->refcount = 1;
+ s->refcount = 1;
+
+ strcpy (s->filename, filename);
return true;
}
return true;
}
@@
-81,71
+87,73
@@
vcedit_state_init (vcedit_state *state)
vcedit_state *
vcedit_state_new (const char *filename)
{
vcedit_state *
vcedit_state_new (const char *filename)
{
- vcedit_state *state;
+ vcedit_state *s;
+ size_t len;
-
state = malloc (sizeof (vcedit_state)
);
- if (
!state
)
+
len = strlen (filename
);
+ if (
len > PATH_MAX
)
return NULL;
return NULL;
- memset (state, 0, sizeof (vcedit_state));
+ s= malloc (sizeof (vcedit_state) + len + 1);
+ if (!s)
+ return NULL;
+
+ memset (s, 0, sizeof (vcedit_state));
- if (!vcedit_state_init (s
tat
e)) {
- vcedit_state_free (s
tate
);
+ if (!vcedit_state_init (s
, filenam
e)) {
+ vcedit_state_free (s);
return NULL;
}
return NULL;
}
- snprintf (state->filename, sizeof (state->filename),
- "%s", filename);
-
- return state;
+ return s;
}
vorbis_comment *
}
vorbis_comment *
-vcedit_comments (vcedit_state *s
tate
)
+vcedit_comments (vcedit_state *s)
{
{
- return s
tate->opened ? &state
->vc : NULL;
+ return s
->opened ? &s
->vc : NULL;
}
static void
}
static void
-vcedit_clear_internals (vcedit_state *s
tate
)
+vcedit_clear_internals (vcedit_state *s)
{
{
- ogg_stream_clear (&s
tate
->os);
- ogg_sync_clear (&s
tate
->oy);
+ ogg_stream_clear (&s->os);
+ ogg_sync_clear (&s->oy);
- vorbis_info_clear (&s
tate
->vi);
- vorbis_comment_clear (&s
tate
->vc);
+ vorbis_info_clear (&s->vi);
+ vorbis_comment_clear (&s->vc);
- free (s
tate
->vendor);
- s
tate
->vendor = NULL;
+ free (s->vendor);
+ s->vendor = NULL;
- free (s
tate
->mainbuf);
- s
tate
->mainbuf = NULL;
- s
tate
->mainlen = 0;
+ free (s->mainbuf);
+ s->mainbuf = NULL;
+ s->mainlen = 0;
- free (s
tate
->bookbuf);
- s
tate
->bookbuf = NULL;
- s
tate
->booklen = 0;
+ free (s->bookbuf);
+ s->bookbuf = NULL;
+ s->booklen = 0;
- s
tate
->serial = 0;
- s
tate
->opened = false;
+ s->serial = 0;
+ s->opened = false;
}
void
}
void
-vcedit_state_ref (vcedit_state *s
tate
)
+vcedit_state_ref (vcedit_state *s)
{
{
- s
tate
->refcount++;
+ s->refcount++;
}
void
}
void
-vcedit_state_unref (vcedit_state *s
tate
)
+vcedit_state_unref (vcedit_state *s)
{
{
- if (--s
tate
->refcount)
+ if (--s->refcount)
return;
return;
- if (s
tate
->opened)
- vcedit_clear_internals (s
tate
);
+ if (s->opened)
+ vcedit_clear_internals (s);
- vcedit_state_free (s
tate
);
+ vcedit_state_free (s);
}
/* Next two functions pulled straight from libvorbis, apart from one change
}
/* Next two functions pulled straight from libvorbis, apart from one change
@@
-154,17
+162,16
@@
vcedit_state_unref (vcedit_state *state)
static void
_v_writestring (oggpack_buffer *o, char *s, int len)
{
static void
_v_writestring (oggpack_buffer *o, char *s, int len)
{
- while (len--)
{
+ while (len--)
oggpack_write (o, *s++, 8);
oggpack_write (o, *s++, 8);
- }
}
static int
}
static int
-_commentheader_out (v
orbis_comment *vc, char *vendor
, ogg_packet *op)
+_commentheader_out (v
cedit_state *s
, ogg_packet *op)
{
{
- int i;
-
oggpack_buffer opb;
oggpack_buffer opb;
+ size_t len;
+ int i;
oggpack_writeinit (&opb);
oggpack_writeinit (&opb);
@@
-173,30
+180,31
@@
_commentheader_out (vorbis_comment *vc, char *vendor, ogg_packet *op)
_v_writestring (&opb, "vorbis", 6);
/* vendor */
_v_writestring (&opb, "vorbis", 6);
/* vendor */
- oggpack_write (&opb, strlen (vendor), 32);
- _v_writestring (&opb, vendor, strlen (vendor));
+ len = strlen (s->vendor);
+ oggpack_write (&opb, len, 32);
+ _v_writestring (&opb, s->vendor, len);
/* comments */
/* comments */
- oggpack_write (&opb,
vc->
comments, 32);
+ oggpack_write (&opb,
s->vc.
comments, 32);
- for (i = 0; i <
vc->
comments; i++) {
- if (!
vc->
user_comments[i])
+ for (i = 0; i <
s->vc.
comments; i++) {
+ if (!
s->vc.
user_comments[i])
oggpack_write (&opb, 0, 32);
else {
oggpack_write (&opb, 0, 32);
else {
- oggpack_write (&opb,
vc->
comment_lengths[i], 32);
- _v_writestring (&opb,
vc->
user_comments[i],
-
vc->
comment_lengths[i]);
+ oggpack_write (&opb,
s->vc.
comment_lengths[i], 32);
+ _v_writestring (&opb,
s->vc.
user_comments[i],
+
s->vc.
comment_lengths[i]);
}
}
oggpack_write (&opb, 1, 1);
}
}
oggpack_write (&opb, 1, 1);
- op->packet = _ogg_malloc (oggpack_bytes (&opb));
- memcpy (op->packet, opb.buffer, oggpack_bytes (&opb));
-
op->bytes = oggpack_bytes (&opb);
op->bytes = oggpack_bytes (&opb);
- op->b_o_s = 0;
- op->e_o_s = 0;
+ op->packet = _ogg_malloc (op->bytes);
+
+ memcpy (op->packet, opb.buffer, op->bytes);
+
+ op->b_o_s = op->e_o_s = 0;
op->granulepos = 0;
oggpack_writeclear (&opb);
op->granulepos = 0;
oggpack_writeclear (&opb);
@@
-226,19
+234,18
@@
_fetch_next_packet (vcedit_state *s, ogg_packet *p, ogg_page *page)
int result, bytes;
result = ogg_stream_packetout (&s->os, p);
int result, bytes;
result = ogg_stream_packetout (&s->os, p);
-
- if (result > 0)
+ if (result == 1)
return 1;
if (s->eosin)
return 0;
return 1;
if (s->eosin)
return 0;
- while (ogg_sync_pageout (&s->oy, page)
<= 0
) {
+ while (ogg_sync_pageout (&s->oy, page)
!= 1
) {
buffer = ogg_sync_buffer (&s->oy, CHUNKSIZE);
bytes = fread (buffer, 1, CHUNKSIZE, s->in);
ogg_sync_wrote (&s->oy, bytes);
buffer = ogg_sync_buffer (&s->oy, CHUNKSIZE);
bytes = fread (buffer, 1, CHUNKSIZE, s->in);
ogg_sync_wrote (&s->oy, bytes);
- if (!bytes)
+ if (!bytes
&& (feof (s->in) || ferror (s->in))
)
return 0;
}
return 0;
}
@@
-256,129
+263,147
@@
_fetch_next_packet (vcedit_state *s, ogg_packet *p, ogg_page *page)
}
vcedit_error
}
vcedit_error
-vcedit_open (vcedit_state *s
tate
)
+vcedit_open (vcedit_state *s)
{
vcedit_error ret;
{
vcedit_error ret;
- char *buffer;
- int bytes, i;
- int chunks = 0;
ogg_packet *header;
ogg_packet header_main, header_comments, header_codebooks;
ogg_page og;
ogg_packet *header;
ogg_packet header_main, header_comments, header_codebooks;
ogg_page og;
+ struct stat st;
+ char *buffer;
+ size_t bytes, total = 0;
+ int i = 0;
- s
tate->in = fopen (state
->filename, "rb");
- if (!s
tate
->in)
+ s
->in = fopen (s
->filename, "rb");
+ if (!s->in)
return VCEDIT_ERR_OPEN;
return VCEDIT_ERR_OPEN;
- ogg_sync_init (&state->oy);
-
- while (1) {
- buffer = ogg_sync_buffer (&state->oy, CHUNKSIZE);
- bytes = fread (buffer, 1, CHUNKSIZE, state->in);
+ s->file_mode = stat (s->filename, &st) ? 0664 : st.st_mode;
- ogg_sync_wrote (&state->oy, bytes);
-
- if (ogg_sync_pageout (&state->oy, &og) == 1)
- break;
+ ogg_sync_init (&s->oy);
+ do {
/* Bail if we don't find data in the first 40 kB */
/* Bail if we don't find data in the first 40 kB */
- if (
chunks++ >= 10
) {
- ogg_sync_clear (&s
tate
->oy);
+ if (
feof (s->in) || ferror (s->in) || total >= (CHUNKSIZE * 10)
) {
+ ogg_sync_clear (&s->oy);
return VCEDIT_ERR_INVAL;
}
return VCEDIT_ERR_INVAL;
}
- }
- state->serial = ogg_page_serialno (&og);
+ buffer = ogg_sync_buffer (&s->oy, CHUNKSIZE);
+
+ bytes = fread (buffer, 1, CHUNKSIZE, s->in);
+ total += bytes;
+
+ ogg_sync_wrote (&s->oy, bytes);
+ } while (ogg_sync_pageout (&s->oy, &og) != 1);
+
+ s->serial = ogg_page_serialno (&og);
- ogg_stream_init (&s
tate->os, state
->serial);
- vorbis_info_init (&s
tate
->vi);
- vorbis_comment_init (&s
tate
->vc);
+ ogg_stream_init (&s
->os, s
->serial);
+ vorbis_info_init (&s->vi);
+ vorbis_comment_init (&s->vc);
- if (ogg_stream_pagein (&s
tate
->os, &og) < 0) {
+ if (ogg_stream_pagein (&s->os, &og) < 0) {
ret = VCEDIT_ERR_INVAL;
goto err;
}
ret = VCEDIT_ERR_INVAL;
goto err;
}
- if (ogg_stream_packetout (&s
tate
->os, &header_main) != 1) {
+ if (ogg_stream_packetout (&s->os, &header_main) != 1) {
ret = VCEDIT_ERR_INVAL;
goto err;
}
ret = VCEDIT_ERR_INVAL;
goto err;
}
- if (vorbis_synthesis_headerin (&s
tate->vi, &state
->vc, &header_main) < 0) {
+ if (vorbis_synthesis_headerin (&s
->vi, &s
->vc, &header_main) < 0) {
ret = VCEDIT_ERR_INVAL;
goto err;
}
ret = VCEDIT_ERR_INVAL;
goto err;
}
- s
tate
->mainlen = header_main.bytes;
- s
tate->mainbuf = malloc (state
->mainlen);
- memcpy (s
tate
->mainbuf, header_main.packet, header_main.bytes);
+ s->mainlen = header_main.bytes;
+ s
->mainbuf = malloc (s
->mainlen);
+ memcpy (s->mainbuf, header_main.packet, header_main.bytes);
- i = 0;
header = &header_comments;
while (i < 2) {
header = &header_comments;
while (i < 2) {
+ if (feof (s->in) || ferror (s->in)) {
+ ret = VCEDIT_ERR_INVAL;
+ goto err;
+ }
+
while (i < 2) {
while (i < 2) {
- int result
= ogg_sync_pageout (&state->oy, &og)
;
+ int result;
+ result = ogg_sync_pageout (&s->oy, &og);
if (!result)
break; /* Too little data so far */
if (!result)
break; /* Too little data so far */
- if (result
== 1) {
-
ogg_stream_pagein (&state->os, &og)
;
+ if (result
!= 1)
+
continue
;
- while (i < 2) {
- result = ogg_stream_packetout (&state->os, header);
+ ogg_stream_pagein (&s->os, &og);
- if (!result)
- break;
+ while (i < 2) {
+ result = ogg_stream_packetout (&s->os, header);
+ if (!result)
+ break;
- if (result == -1) {
- ret = VCEDIT_ERR_INVAL;
- goto err;
- }
-
- vorbis_synthesis_headerin (&state->vi, &state->vc, header);
+ if (result != 1) {
+ ret = VCEDIT_ERR_INVAL;
+ goto err;
+ }
- if (i == 1) {
- state->booklen = header->bytes;
- state->bookbuf = malloc (state->booklen);
- memcpy (state->bookbuf, header->packet, header->bytes);
- }
+ vorbis_synthesis_headerin (&s->vi, &s->vc, header);
- i++;
- header = &header_codebooks;
+ if (i++ == 1) {
+ s->booklen = header->bytes;
+ s->bookbuf = malloc (s->booklen);
+ memcpy (s->bookbuf, header->packet, header->bytes);
}
}
+
+ header = &header_codebooks;
}
}
}
}
- buffer = ogg_sync_buffer (&state->oy, CHUNKSIZE);
- bytes = fread (buffer, 1, CHUNKSIZE, state->in);
-
- if (bytes == 0 && i < 2) {
- ret = VCEDIT_ERR_INVAL;
- goto err;
- }
+ buffer = ogg_sync_buffer (&s->oy, CHUNKSIZE);
- ogg_sync_wrote (&state->oy, bytes);
+ bytes = fread (buffer, 1, CHUNKSIZE, s->in);
+ ogg_sync_wrote (&s->oy, bytes);
}
/* Copy the vendor tag */
}
/* Copy the vendor tag */
- s
tate->vendor = strdup (state
->vc.vendor);
+ s
->vendor = strdup (s
->vc.vendor);
/* Headers are done! */
/* Headers are done! */
- s
tate
->opened = true;
+ s->opened = true;
return VCEDIT_ERR_SUCCESS;
err:
return VCEDIT_ERR_SUCCESS;
err:
- vcedit_clear_internals (s
tate
);
+ vcedit_clear_internals (s);
return ret;
}
return ret;
}
+static int
+write_data (const void *buf, size_t size, size_t nmemb, FILE *stream)
+{
+ while (nmemb > 0) {
+ size_t w;
+
+ w = fwrite (buf, size, nmemb, stream);
+ if (!w && ferror (stream))
+ return 0;
+
+ nmemb -= w;
+ buf += size * w;
+ }
+
+ return 1;
+}
+
vcedit_error
vcedit_error
-vcedit_write (vcedit_state *s
tate
)
+vcedit_write (vcedit_state *s)
{
ogg_stream_state streamout;
ogg_packet header_main, header_comments, header_codebooks, op;
{
ogg_stream_state streamout;
ogg_packet header_main, header_comments, header_codebooks, op;
@@
-386,83
+411,77
@@
vcedit_write (vcedit_state *state)
ogg_int64_t granpos = 0;
FILE *out;
char *buffer, tmpfile[PATH_MAX];
ogg_int64_t granpos = 0;
FILE *out;
char *buffer, tmpfile[PATH_MAX];
-
int s, result, bytes, needflush = 0, needout = 0
;
-
size_t tmp
;
+
bool success = false
;
+
int fd, result, bytes, needflush = 0, needout = 0
;
- if (!s
tate
->opened)
+ if (!s->opened)
return VCEDIT_ERR_INVAL;
return VCEDIT_ERR_INVAL;
- strcpy (tmpfile, s
tate
->filename);
+ strcpy (tmpfile, s->filename);
strcat (tmpfile, ".XXXXXX");
strcat (tmpfile, ".XXXXXX");
-
s
= mkstemp (tmpfile);
- if (
s
== -1)
+
fd
= mkstemp (tmpfile);
+ if (
fd
== -1)
return VCEDIT_ERR_TMPFILE;
return VCEDIT_ERR_TMPFILE;
- out = fdopen (
s
, "wb");
+ out = fdopen (
fd
, "wb");
if (!out) {
unlink (tmpfile);
if (!out) {
unlink (tmpfile);
- close (
s
);
+ close (
fd
);
return VCEDIT_ERR_TMPFILE;
}
return VCEDIT_ERR_TMPFILE;
}
- s
tate->prevW = state->extrapage = state
->eosin = 0;
+ s
->prevW = s->extrapage = s
->eosin = 0;
- header_main.bytes = s
tate
->mainlen;
- header_main.packet = s
tate
->mainbuf;
+ header_main.bytes = s->mainlen;
+ header_main.packet = s->mainbuf;
header_main.b_o_s = 1;
header_main.e_o_s = 0;
header_main.granulepos = 0;
header_main.b_o_s = 1;
header_main.e_o_s = 0;
header_main.granulepos = 0;
- header_codebooks.bytes = s
tate
->booklen;
- header_codebooks.packet = s
tate
->bookbuf;
+ header_codebooks.bytes = s->booklen;
+ header_codebooks.packet = s->bookbuf;
header_codebooks.b_o_s = 0;
header_codebooks.e_o_s = 0;
header_codebooks.granulepos = 0;
header_codebooks.b_o_s = 0;
header_codebooks.e_o_s = 0;
header_codebooks.granulepos = 0;
- ogg_stream_init (&streamout, s
tate
->serial);
+ ogg_stream_init (&streamout, s->serial);
- _commentheader_out (
&state->vc, state->vendor
, &header_comments);
+ _commentheader_out (
s
, &header_comments);
ogg_stream_packetin (&streamout, &header_main);
ogg_stream_packetin (&streamout, &header_comments);
ogg_stream_packetin (&streamout, &header_codebooks);
while ((result = ogg_stream_flush (&streamout, &ogout))) {
ogg_stream_packetin (&streamout, &header_main);
ogg_stream_packetin (&streamout, &header_comments);
ogg_stream_packetin (&streamout, &header_codebooks);
while ((result = ogg_stream_flush (&streamout, &ogout))) {
- tmp = fwrite (ogout.header, 1, ogout.header_len, out);
- if (tmp != (size_t) ogout.header_len)
+ if (!write_data (ogout.header, 1, ogout.header_len, out))
goto cleanup;
goto cleanup;
- tmp = fwrite (ogout.body, 1, ogout.body_len, out);
- if (tmp != (size_t) ogout.body_len)
+ if (!write_data (ogout.body, 1, ogout.body_len, out))
goto cleanup;
}
goto cleanup;
}
- while (_fetch_next_packet (s
tate
, &op, &ogin)) {
+ while (_fetch_next_packet (s, &op, &ogin)) {
int size;
int size;
- size = _blocksize (s
tate
, &op);
+ size = _blocksize (s, &op);
granpos += size;
if (needflush) {
if (ogg_stream_flush (&streamout, &ogout)) {
granpos += size;
if (needflush) {
if (ogg_stream_flush (&streamout, &ogout)) {
- tmp = fwrite (ogout.header, 1, ogout.header_len, out);
- if (tmp != (size_t) ogout.header_len)
+ if (!write_data (ogout.header, 1, ogout.header_len, out))
goto cleanup;
goto cleanup;
- tmp = fwrite (ogout.body, 1, ogout.body_len, out);
- if (tmp != (size_t) ogout.body_len)
+ if (!write_data (ogout.body, 1, ogout.body_len, out))
goto cleanup;
}
} else if (needout) {
if (ogg_stream_pageout (&streamout, &ogout)) {
goto cleanup;
}
} else if (needout) {
if (ogg_stream_pageout (&streamout, &ogout)) {
- tmp = fwrite (ogout.header, 1, ogout.header_len, out);
- if (tmp != (size_t) ogout.header_len)
+ if (!write_data (ogout.header, 1, ogout.header_len, out))
goto cleanup;
goto cleanup;
- tmp = fwrite (ogout.body, 1, ogout.body_len, out);
- if (tmp != (size_t) ogout.body_len)
+ if (!write_data (ogout.body, 1, ogout.body_len, out))
goto cleanup;
}
}
goto cleanup;
}
}
@@
-490,69
+509,67
@@
vcedit_write (vcedit_state *state)
streamout.e_o_s = 1;
while (ogg_stream_flush (&streamout, &ogout)) {
streamout.e_o_s = 1;
while (ogg_stream_flush (&streamout, &ogout)) {
- tmp = fwrite (ogout.header, 1, ogout.header_len, out);
- if (tmp != (size_t) ogout.header_len)
+ if (!write_data (ogout.header, 1, ogout.header_len, out))
goto cleanup;
goto cleanup;
- tmp = fwrite (ogout.body, 1, ogout.body_len, out);
- if (tmp != (size_t) ogout.body_len)
+ if (!write_data (ogout.body, 1, ogout.body_len, out))
goto cleanup;
}
goto cleanup;
}
- if (state->extrapage) {
- tmp = fwrite (ogin.header, 1, ogin.header_len, out);
- if (tmp != (size_t) ogin.header_len)
+ if (s->extrapage) {
+ if (!write_data (ogin.header, 1, ogin.header_len, out))
goto cleanup;
goto cleanup;
- tmp = fwrite (ogin.body, 1, ogin.body_len, out);
- if (tmp != (size_t) ogin.body_len)
+ if (!write_data (ogin.body, 1, ogin.body_len, out))
goto cleanup;
}
/* clear it, because not all paths to here do */
goto cleanup;
}
/* clear it, because not all paths to here do */
- s
tate
->eosin = 0;
+ s->eosin = 0;
- while (!state->eosin) { /* We reached eos, not eof */
+ do {
/* We copy the rest of the stream (other logical streams)
* through, a page at a time.
*/
/* We copy the rest of the stream (other logical streams)
* through, a page at a time.
*/
- while (1) {
- result = ogg_sync_pageout (&state->oy, &ogout);
+ while ((result = ogg_sync_pageout (&s->oy, &ogout))) {
+ if (result != 1)
+ continue;
- if (!result)
- break;
-
- if (result >= 0) {
- /* Don't bother going through the rest, we can just
- * write the page out now
- */
- tmp = fwrite (ogout.header, 1, ogout.header_len, out);
- if (tmp != (size_t) ogout.header_len)
- goto cleanup;
+ /* Don't bother going through the rest, we can just
+ * write the page out now
+ */
+ if (!write_data (ogout.header, 1, ogout.header_len, out))
+ goto cleanup;
- tmp = fwrite (ogout.body, 1, ogout.body_len, out);
- if (tmp != (size_t) ogout.body_len)
- goto cleanup;
- }
+ if (!write_data (ogout.body, 1, ogout.body_len, out))
+ goto cleanup;
}
}
- buffer = ogg_sync_buffer (&s
tate
->oy, CHUNKSIZE);
- bytes = fread (buffer, 1, CHUNKSIZE, s
tate
->in);
- ogg_sync_wrote (&s
tate
->oy, bytes);
+ buffer = ogg_sync_buffer (&s->oy, CHUNKSIZE);
+ bytes = fread (buffer, 1, CHUNKSIZE, s->in);
+ ogg_sync_wrote (&s->oy, bytes);
- if (!bytes) {
- state->eosin = 1;
- break;
- }
- }
+ if (ferror (s->in))
+ goto cleanup;
-
fclose (out
);
-
fclose (state->
in);
+
s->eosin = !bytes && feof (s->in
);
+
} while (!s->eos
in);
- unlink (state->filename);
- rename (tmpfile, state->filename);
+ success = true;
cleanup:
cleanup:
+ fclose (s->in);
+
+ if (!success) {
+ unlink (tmpfile);
+ fclose (out);
+ } else {
+ fclose (out);
+ unlink (s->filename);
+ rename (tmpfile, s->filename);
+ chmod (s->filename, s->file_mode);
+ }
+
ogg_stream_clear (&streamout);
/* We don't ogg_packet_clear() this, because the memory was
ogg_stream_clear (&streamout);
/* We don't ogg_packet_clear() this, because the memory was
@@
-560,16
+577,16
@@
cleanup:
*/
_ogg_free (header_comments.packet);
*/
_ogg_free (header_comments.packet);
- free (s
tate
->mainbuf);
- free (s
tate
->bookbuf);
+ free (s->mainbuf);
+ free (s->bookbuf);
- s
tate->mainbuf = state
->bookbuf = NULL;
+ s
->mainbuf = s
->bookbuf = NULL;
- if (!s
tate
->eosin)
+ if (!s->eosin)
return VCEDIT_ERR_INVAL;
return VCEDIT_ERR_INVAL;
- vcedit_clear_internals (s
tate
);
+ vcedit_clear_internals (s);
- return (vcedit_open (s
tate
) == VCEDIT_ERR_SUCCESS) ?
+ return (vcedit_open (s) == VCEDIT_ERR_SUCCESS) ?
VCEDIT_ERR_SUCCESS : VCEDIT_ERR_REOPEN;
}
VCEDIT_ERR_SUCCESS : VCEDIT_ERR_REOPEN;
}