1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 1991-2003 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
28*7c478bd9Sstevel@tonic-gate #include <unistd.h>
29*7c478bd9Sstevel@tonic-gate #include <sys/stat.h>
30*7c478bd9Sstevel@tonic-gate 
31*7c478bd9Sstevel@tonic-gate #include <AudioRawPipe.h>
32*7c478bd9Sstevel@tonic-gate #include <libaudio.h>
33*7c478bd9Sstevel@tonic-gate #include <audio_hdr.h>
34*7c478bd9Sstevel@tonic-gate 
35*7c478bd9Sstevel@tonic-gate // class AudioPipe methods
36*7c478bd9Sstevel@tonic-gate 
37*7c478bd9Sstevel@tonic-gate // Constructor with file descriptor, mode, and optional name
38*7c478bd9Sstevel@tonic-gate AudioRawPipe::
AudioRawPipe(const int desc,const FileAccess acc,const AudioHdr & hdr_local,const char * name_local,const off_t off)39*7c478bd9Sstevel@tonic-gate AudioRawPipe(
40*7c478bd9Sstevel@tonic-gate 	const int		desc,		// file descriptor
41*7c478bd9Sstevel@tonic-gate 	const FileAccess	acc,		// access mode
42*7c478bd9Sstevel@tonic-gate 	const AudioHdr&		hdr_local,	// header
43*7c478bd9Sstevel@tonic-gate 	const char		*name_local,	// name
44*7c478bd9Sstevel@tonic-gate 	const off_t		off		// offset
45*7c478bd9Sstevel@tonic-gate ):AudioPipe(desc, acc, name_local), offset(off)
46*7c478bd9Sstevel@tonic-gate {
47*7c478bd9Sstevel@tonic-gate 	isopened = FALSE;
48*7c478bd9Sstevel@tonic-gate 	setfd(desc);
49*7c478bd9Sstevel@tonic-gate 	SetHeader(hdr_local);
50*7c478bd9Sstevel@tonic-gate }
51*7c478bd9Sstevel@tonic-gate 
52*7c478bd9Sstevel@tonic-gate // The create routine for pipes writes a file header
53*7c478bd9Sstevel@tonic-gate AudioError AudioRawPipe::
Create()54*7c478bd9Sstevel@tonic-gate Create()
55*7c478bd9Sstevel@tonic-gate {
56*7c478bd9Sstevel@tonic-gate 	AudioError	err;
57*7c478bd9Sstevel@tonic-gate 
58*7c478bd9Sstevel@tonic-gate 	// Was the header properly set?
59*7c478bd9Sstevel@tonic-gate 	err = GetHeader().Validate();
60*7c478bd9Sstevel@tonic-gate 	if (err != AUDIO_SUCCESS)
61*7c478bd9Sstevel@tonic-gate 		return (RaiseError(err));
62*7c478bd9Sstevel@tonic-gate 
63*7c478bd9Sstevel@tonic-gate 	// Open fd supplied by constructor
64*7c478bd9Sstevel@tonic-gate 	if (!isfdset() || opened()) {
65*7c478bd9Sstevel@tonic-gate 		return (RaiseError(AUDIO_ERR_NOEFFECT, Warning));
66*7c478bd9Sstevel@tonic-gate 	}
67*7c478bd9Sstevel@tonic-gate 
68*7c478bd9Sstevel@tonic-gate 	// set flag for opened() test
69*7c478bd9Sstevel@tonic-gate 	isopened = TRUE;
70*7c478bd9Sstevel@tonic-gate 
71*7c478bd9Sstevel@tonic-gate 	// Set the actual output length to zero
72*7c478bd9Sstevel@tonic-gate 	setlength(0.);
73*7c478bd9Sstevel@tonic-gate 
74*7c478bd9Sstevel@tonic-gate 	return (AUDIO_SUCCESS);
75*7c478bd9Sstevel@tonic-gate }
76*7c478bd9Sstevel@tonic-gate 
77*7c478bd9Sstevel@tonic-gate // The open routine for raw pipes validates the header and
78*7c478bd9Sstevel@tonic-gate // init's the read pos to offset and sets the opened flag.
79*7c478bd9Sstevel@tonic-gate AudioError AudioRawPipe::
Open()80*7c478bd9Sstevel@tonic-gate Open()
81*7c478bd9Sstevel@tonic-gate {
82*7c478bd9Sstevel@tonic-gate 	AudioError	err;
83*7c478bd9Sstevel@tonic-gate 	struct stat	st;
84*7c478bd9Sstevel@tonic-gate 
85*7c478bd9Sstevel@tonic-gate 	// The constructor should have supplied a valid fd
86*7c478bd9Sstevel@tonic-gate 	// If fd is not open, or file header already decoded, skip it
87*7c478bd9Sstevel@tonic-gate 	if (!isfdset() || opened())
88*7c478bd9Sstevel@tonic-gate 		return (RaiseError(AUDIO_ERR_NOEFFECT, Warning));
89*7c478bd9Sstevel@tonic-gate 
90*7c478bd9Sstevel@tonic-gate 	// Stat the file, to see if it is a regular file
91*7c478bd9Sstevel@tonic-gate 	if (fstat(getfd(), &st) < 0)
92*7c478bd9Sstevel@tonic-gate 		return (RaiseError(AUDIO_UNIXERROR));
93*7c478bd9Sstevel@tonic-gate 
94*7c478bd9Sstevel@tonic-gate 	// check validity of file header
95*7c478bd9Sstevel@tonic-gate 	err = GetHeader().Validate();
96*7c478bd9Sstevel@tonic-gate 	if (err != AUDIO_SUCCESS) {
97*7c478bd9Sstevel@tonic-gate 		(void) close(getfd());
98*7c478bd9Sstevel@tonic-gate 		setfd(-1);
99*7c478bd9Sstevel@tonic-gate 		return (err);
100*7c478bd9Sstevel@tonic-gate 	}
101*7c478bd9Sstevel@tonic-gate 
102*7c478bd9Sstevel@tonic-gate 	// Only trust the file size for regular files
103*7c478bd9Sstevel@tonic-gate 	if (S_ISREG(st.st_mode)) {
104*7c478bd9Sstevel@tonic-gate 		// for raw files - no hdr, so it's the whole file minus
105*7c478bd9Sstevel@tonic-gate 		// the offset.
106*7c478bd9Sstevel@tonic-gate 		setlength(GetHeader().Bytes_to_Time(st.st_size - offset));
107*7c478bd9Sstevel@tonic-gate 	} else {
108*7c478bd9Sstevel@tonic-gate 		// don't know ...
109*7c478bd9Sstevel@tonic-gate 		setlength(AUDIO_UNKNOWN_TIME);
110*7c478bd9Sstevel@tonic-gate 	}
111*7c478bd9Sstevel@tonic-gate 
112*7c478bd9Sstevel@tonic-gate 	// set flag for opened() test
113*7c478bd9Sstevel@tonic-gate 	isopened = TRUE;
114*7c478bd9Sstevel@tonic-gate 
115*7c478bd9Sstevel@tonic-gate 	err = SetOffset(offset);
116*7c478bd9Sstevel@tonic-gate 
117*7c478bd9Sstevel@tonic-gate 	// reset logical position to 0.0, since this is, in effect,
118*7c478bd9Sstevel@tonic-gate 	// the beginning of the file.
119*7c478bd9Sstevel@tonic-gate 	SetReadPosition(0.0, Absolute);
120*7c478bd9Sstevel@tonic-gate 
121*7c478bd9Sstevel@tonic-gate 	return (err);
122*7c478bd9Sstevel@tonic-gate }
123*7c478bd9Sstevel@tonic-gate 
124*7c478bd9Sstevel@tonic-gate Boolean AudioRawPipe::
opened() const125*7c478bd9Sstevel@tonic-gate opened() const
126*7c478bd9Sstevel@tonic-gate {
127*7c478bd9Sstevel@tonic-gate 	return (isopened);
128*7c478bd9Sstevel@tonic-gate }
129*7c478bd9Sstevel@tonic-gate 
130*7c478bd9Sstevel@tonic-gate AudioError AudioRawPipe::
SetOffset(off_t val)131*7c478bd9Sstevel@tonic-gate SetOffset(off_t val)
132*7c478bd9Sstevel@tonic-gate {
133*7c478bd9Sstevel@tonic-gate 	off_t		setting = 0;
134*7c478bd9Sstevel@tonic-gate 	AudioError	err;
135*7c478bd9Sstevel@tonic-gate 
136*7c478bd9Sstevel@tonic-gate 	// only read only files for now
137*7c478bd9Sstevel@tonic-gate 	if (GetAccess().Writeable()) {
138*7c478bd9Sstevel@tonic-gate 		return (AUDIO_ERR_NOEFFECT);
139*7c478bd9Sstevel@tonic-gate 	}
140*7c478bd9Sstevel@tonic-gate 
141*7c478bd9Sstevel@tonic-gate 	// only allow this if we haven't read anything yet (i.e. current
142*7c478bd9Sstevel@tonic-gate 	// position is 0).
143*7c478bd9Sstevel@tonic-gate 	if (ReadPosition() != 0.) {
144*7c478bd9Sstevel@tonic-gate 		return (AUDIO_ERR_NOEFFECT);
145*7c478bd9Sstevel@tonic-gate 	}
146*7c478bd9Sstevel@tonic-gate 
147*7c478bd9Sstevel@tonic-gate 	if ((err = seekread(GetHeader().Bytes_to_Time(val), setting))
148*7c478bd9Sstevel@tonic-gate 	    != AUDIO_SUCCESS) {
149*7c478bd9Sstevel@tonic-gate 		return (err);
150*7c478bd9Sstevel@tonic-gate 	}
151*7c478bd9Sstevel@tonic-gate 
152*7c478bd9Sstevel@tonic-gate 	// this should *never* happen 'cause seekread just sets setting
153*7c478bd9Sstevel@tonic-gate 	// to GetHeader().Time_to_Bytes....
154*7c478bd9Sstevel@tonic-gate 	if (setting != val) {
155*7c478bd9Sstevel@tonic-gate 		// don't really know what error is apropos for this.
156*7c478bd9Sstevel@tonic-gate 		return (AUDIO_ERR_BADFRAME);
157*7c478bd9Sstevel@tonic-gate 	}
158*7c478bd9Sstevel@tonic-gate 
159*7c478bd9Sstevel@tonic-gate 	offset = val;
160*7c478bd9Sstevel@tonic-gate 	return (AUDIO_SUCCESS);
161*7c478bd9Sstevel@tonic-gate }
162*7c478bd9Sstevel@tonic-gate 
163*7c478bd9Sstevel@tonic-gate off_t AudioRawPipe::
GetOffset() const164*7c478bd9Sstevel@tonic-gate GetOffset() const
165*7c478bd9Sstevel@tonic-gate {
166*7c478bd9Sstevel@tonic-gate 	return (offset);
167*7c478bd9Sstevel@tonic-gate }
168