~ubuntu-branches/ubuntu/edgy/libcdio/edgy-updates

« back to all changes in this revision

Viewing changes to src/cd-paranoia/buffering_write.c

  • Committer: Bazaar Package Importer
  • Author(s): Martin Pitt
  • Date: 2005-11-15 16:53:23 UTC
  • mfrom: (3.1.1 etch)
  • Revision ID: james.westby@ubuntu.com-20051115165323-peroku75syl2j36u
Tags: 0.76-1ubuntu1
* Sync to new Debian version, manually apply Ubuntu patches:
  - debian/control: Remove dpkg-awk build dependency.
  - debian/rules: hardcode $LIBCDEV. This keeps the diff small (compared to
    the original patch of changing every ${libcdev} occurence).

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
   $Id: buffering_write.c,v 1.2 2004/12/19 01:43:38 rocky Exp $
 
3
 
 
4
   Copyright (C) 2004 Rocky Bernstein <rocky@panix.com>
 
5
   Copyright (C) 1998, 1999 Monty <xiphmont@mit.edu>
 
6
 
 
7
   This program is free software; you can redistribute it and/or
 
8
   modify it under the terms of the GNU General Public License as
 
9
   published by the Free Software Foundation; either version 2, or (at
 
10
   your option) any later version.
 
11
 
 
12
   This program is distributed in the hope that it will be useful, but
 
13
   WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
15
   General Public License for more details.
 
16
 
 
17
   You should have received a copy of the GNU General Public License
 
18
   along with this program; if not, write to the Free Software
 
19
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
20
 
 
21
 */
 
22
/* Eliminate teeny little writes.  patch submitted by 
 
23
   Rob Ross <rbross@parl.ces.clemson.edu> --Monty 19991008 */
 
24
 
 
25
#include <string.h>
 
26
#include <unistd.h>
 
27
#include <errno.h>
 
28
#include <stdio.h>
 
29
 
 
30
#define OUTBUFSZ 32*1024
 
31
 
 
32
#include "utils.h"
 
33
#include "buffering_write.h"
 
34
 
 
35
 
 
36
/* GLOBALS FOR BUFFERING CALLS */
 
37
static int  bw_fd  = -1;
 
38
static long bw_pos = 0;
 
39
static char bw_outbuf[OUTBUFSZ];
 
40
 
 
41
 
 
42
 
 
43
long int
 
44
blocking_write(int outf, char *buffer, long num){
 
45
  long int words=0,temp;
 
46
 
 
47
  while(words<num){
 
48
    temp=write(outf,buffer+words,num-words);
 
49
    if(temp==-1){
 
50
      if(errno!=EINTR && errno!=EAGAIN)
 
51
        return(-1);
 
52
      temp=0;
 
53
    }
 
54
    words+=temp;
 
55
  }
 
56
  return(0);
 
57
}
 
58
 
 
59
/** buffering_write() - buffers data to a specified size before writing.
 
60
 *
 
61
 * Restrictions:
 
62
 * - MUST CALL BUFFERING_CLOSE() WHEN FINISHED!!!
 
63
 *
 
64
 */
 
65
long int 
 
66
buffering_write(int fd, char *buffer, long num)
 
67
{
 
68
  if (fd != bw_fd) {
 
69
    /* clean up after buffering for some other file */
 
70
    if (bw_fd >= 0 && bw_pos > 0) {
 
71
      if (blocking_write(bw_fd, bw_outbuf, bw_pos)) {
 
72
        perror("write (in buffering_write, flushing)");
 
73
      }
 
74
    }
 
75
    bw_fd  = fd;
 
76
    bw_pos = 0;
 
77
  }
 
78
  
 
79
  if (bw_pos + num > OUTBUFSZ) {
 
80
    /* fill our buffer first, then write, then modify buffer and num */
 
81
    memcpy(&bw_outbuf[bw_pos], buffer, OUTBUFSZ - bw_pos);
 
82
    if (blocking_write(fd, bw_outbuf, OUTBUFSZ)) {
 
83
      perror("write (in buffering_write, full buffer)");
 
84
      return(-1);
 
85
    }
 
86
    num -= (OUTBUFSZ - bw_pos);
 
87
    buffer += (OUTBUFSZ - bw_pos);
 
88
    bw_pos = 0;
 
89
  }
 
90
  /* save data */
 
91
  memcpy(&bw_outbuf[bw_pos], buffer, num);
 
92
  bw_pos += num;
 
93
  
 
94
  return(0);
 
95
}
 
96
 
 
97
/** buffering_close() - writes out remaining buffered data before
 
98
 * closing file.
 
99
 *
 
100
 */
 
101
int 
 
102
buffering_close(int fd)
 
103
{
 
104
  if (fd == bw_fd && bw_pos > 0) {
 
105
    /* write out remaining data and clean up */
 
106
    if (blocking_write(fd, bw_outbuf, bw_pos)) {
 
107
      perror("write (in buffering_close)");
 
108
    }
 
109
    bw_fd  = -1;
 
110
    bw_pos = 0;
 
111
  }
 
112
  return(close(fd));
 
113
}