~ubuntu-branches/ubuntu/karmic/psicode/karmic

« back to all changes in this revision

Viewing changes to src/lib/libpsio/rw.c

  • Committer: Bazaar Package Importer
  • Author(s): Michael Banck, Michael Banck, Daniel Leidert
  • Date: 2009-02-23 00:12:02 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20090223001202-rutldoy3dimfpesc
Tags: 3.4.0-1
* New upstream release.

[ Michael Banck ]
* debian/patches/01_DESTDIR.dpatch: Refreshed.
* debian/patches/02_FHS.dpatch: Removed, applied upstream.
* debian/patches/03_debian_docdir: Likewise.
* debian/patches/04_man.dpatch: Likewise.
* debian/patches/06_466828_fix_gcc_43_ftbfs.dpatch: Likewise.
* debian/patches/07_464867_move_executables: Fixed and refreshed.
* debian/patches/00list: Adjusted.
* debian/control: Improved description.
* debian/patches-held: Removed.
* debian/rules (install/psi3): Do not ship the ruby bindings for now.

[ Daniel Leidert ]
* debian/rules: Fix txtdir via DEB_MAKE_INSTALL_TARGET.
* debian/patches/01_DESTDIR.dpatch: Refreshed.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*!
2
 
   \file rw.c
3
 
   \ingroup (PSIO)
4
 
*/
5
 
 
6
 
#include <stdio.h>
7
 
#include <unistd.h>
8
 
#include "psio.h"
9
 
 
10
 
/*!
11
 
** PSIO_RW(): Central function for all reads and writes on a PSIO unit.
12
 
**
13
 
** \params unit    = The PSI unit number.
14
 
** \params buffer  = The buffer containing the bytes for the read/write event.
15
 
** \params address = the PSIO global address for the start of the read/write.
16
 
** \params size    = The number of bytes to read/write.
17
 
** \params         = Indicates if the call is to read (0) or write (0) the input data.
18
 
**
19
 
** \ingroup (PSIO)
20
 
*/
21
 
int psio_rw(unsigned int unit, char *buffer, psio_address address, ULI size, int wrt)
22
 
{
23
 
  int errcod;
24
 
  unsigned int i;
25
 
  ULI errcod_uli;
26
 
  ULI page, offset;
27
 
  ULI buf_offset;
28
 
  ULI this_page, this_page_max, this_page_total;
29
 
  unsigned int first_vol, this_vol, numvols;
30
 
  ULI bytes_left, num_full_pages;
31
 
  psio_ud *this_unit;
32
 
 
33
 
  this_unit = &(psio_unit[unit]);
34
 
  numvols = this_unit->numvols;
35
 
  page = address.page;
36
 
  offset = address.offset;
37
 
  
38
 
  /* Seek all volumes to correct starting positions */
39
 
  first_vol = page % numvols;
40
 
  errcod = psio_volseek(&(this_unit->vol[first_vol]), page, offset, numvols);
41
 
  if(errcod == -1) psio_error(unit,PSIO_ERROR_LSEEK);
42
 
  for(i=1,this_page=page+1; i < numvols; i++,this_page++) {
43
 
    this_vol = this_page % numvols;
44
 
    errcod = psio_volseek(&(this_unit->vol[this_vol]), this_page, 
45
 
                          (ULI) 0, numvols);
46
 
    if(errcod == -1) psio_error(unit,PSIO_ERROR_LSEEK);
47
 
  }
48
 
 
49
 
  /* Number of bytes left on the first page */
50
 
  this_page_max = PSIO_PAGELEN - offset;
51
 
 
52
 
  /* If we have enough room on this page, use it */
53
 
  if(size <= this_page_max)  this_page_total = size;
54
 
  else this_page_total = this_page_max;
55
 
 
56
 
  buf_offset = 0;
57
 
  if(wrt) {
58
 
    errcod_uli = write(this_unit->vol[first_vol].stream, &(buffer[buf_offset]),
59
 
                       this_page_total);
60
 
    if(errcod_uli != this_page_total) psio_error(unit,PSIO_ERROR_WRITE);
61
 
  }
62
 
  else {
63
 
    errcod_uli = read(this_unit->vol[first_vol].stream, &(buffer[buf_offset]),
64
 
                      this_page_total);
65
 
    if(errcod_uli != this_page_total) psio_error(unit,PSIO_ERROR_READ);
66
 
  }
67
 
 
68
 
  /* Total number of bytes remaining to be read/written */
69
 
  bytes_left = size - this_page_total;
70
 
 
71
 
  /* Read/Write all the full pages */
72
 
  num_full_pages = bytes_left/PSIO_PAGELEN;  
73
 
  buf_offset += this_page_total;
74
 
  for(i=0,this_page=page+1; i < num_full_pages; i++,this_page++) {
75
 
    this_vol = this_page % numvols;
76
 
    this_page_total = PSIO_PAGELEN;
77
 
    if(wrt) {
78
 
      errcod_uli = write(this_unit->vol[this_vol].stream, &(buffer[buf_offset]),
79
 
                         this_page_total);
80
 
      if(errcod_uli != this_page_total) psio_error(unit,PSIO_ERROR_WRITE);
81
 
    }
82
 
    else {
83
 
      errcod_uli = read(this_unit->vol[this_vol].stream, &(buffer[buf_offset]),
84
 
                        this_page_total);
85
 
      if(errcod_uli != this_page_total) psio_error(unit,PSIO_ERROR_READ);
86
 
    }
87
 
    buf_offset += this_page_total;
88
 
  }
89
 
 
90
 
  /* Read/Write the final partial page */
91
 
  bytes_left -= num_full_pages * PSIO_PAGELEN;
92
 
  this_vol = this_page % numvols;
93
 
  if(bytes_left) {
94
 
    if(wrt) {
95
 
      errcod_uli = write(this_unit->vol[this_vol].stream, &(buffer[buf_offset]),
96
 
                         bytes_left);
97
 
      if(errcod_uli != bytes_left) psio_error(unit,PSIO_ERROR_WRITE);
98
 
    }
99
 
    else {
100
 
      errcod_uli = read(this_unit->vol[this_vol].stream, &(buffer[buf_offset]),
101
 
                        bytes_left);
102
 
      if(errcod_uli != bytes_left) psio_error(unit,PSIO_ERROR_READ);
103
 
    }
104
 
  }
105
 
 
106
 
  return(1);
107
 
}