17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
237c478bd9Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #include <stdlib.h>
287c478bd9Sstevel@tonic-gate #include <stdio.h>
297c478bd9Sstevel@tonic-gate #include <unistd.h>
307c478bd9Sstevel@tonic-gate #include <errno.h>
317c478bd9Sstevel@tonic-gate #include <malloc.h>
327c478bd9Sstevel@tonic-gate #include <string.h>
337c478bd9Sstevel@tonic-gate #include <fcntl.h>
347c478bd9Sstevel@tonic-gate #include <sys/stat.h>
357c478bd9Sstevel@tonic-gate #include <sys/types.h>
367c478bd9Sstevel@tonic-gate #include <sys/mman.h>
377c478bd9Sstevel@tonic-gate 
387c478bd9Sstevel@tonic-gate #include <AudioFile.h>
397c478bd9Sstevel@tonic-gate #include <AudioLib.h>
407c478bd9Sstevel@tonic-gate #include <AudioDebug.h>
417c478bd9Sstevel@tonic-gate #include <libaudio.h>
427c478bd9Sstevel@tonic-gate #include <audio_hdr.h>
437c478bd9Sstevel@tonic-gate 
447c478bd9Sstevel@tonic-gate // class AudioFile methods
457c478bd9Sstevel@tonic-gate 
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate // Initialize temporary file params
487c478bd9Sstevel@tonic-gate #define	TMPDIR		"/tmp"
497c478bd9Sstevel@tonic-gate #define	TMPFILE		"/audiotoolXXXXXX"
507c478bd9Sstevel@tonic-gate static char		*tmpdir = NULL;
517c478bd9Sstevel@tonic-gate static const char	*tmpname = "(temporary file)";
527c478bd9Sstevel@tonic-gate static const FileAccess	tmpmode = ReadWrite;
537c478bd9Sstevel@tonic-gate static const VMAccess	defaccess = SequentialAccess;
547c478bd9Sstevel@tonic-gate 
557c478bd9Sstevel@tonic-gate // Initialize default access mode, used when a filename is supplied
567c478bd9Sstevel@tonic-gate const FileAccess	AudioFile::defmode = ReadOnly;
577c478bd9Sstevel@tonic-gate 
587c478bd9Sstevel@tonic-gate // Default audio file path prefix environment variable
597c478bd9Sstevel@tonic-gate const char *AudioFile::AUDIO_PATH = "AUDIOPATH";
607c478bd9Sstevel@tonic-gate 
617c478bd9Sstevel@tonic-gate 
627c478bd9Sstevel@tonic-gate // Constructor with no arguments opens a read/write temporary file
637c478bd9Sstevel@tonic-gate AudioFile::
AudioFile()647c478bd9Sstevel@tonic-gate AudioFile():
657c478bd9Sstevel@tonic-gate 	AudioUnixfile(tmpname, tmpmode),
667c478bd9Sstevel@tonic-gate 	hdrsize(0), seekpos(0), origlen(0.), mapaddr(0), maplen(0),
677c478bd9Sstevel@tonic-gate 	vmaccess(defaccess)
687c478bd9Sstevel@tonic-gate {
697c478bd9Sstevel@tonic-gate }
707c478bd9Sstevel@tonic-gate 
717c478bd9Sstevel@tonic-gate // Constructor with pathname and optional mode arg
727c478bd9Sstevel@tonic-gate AudioFile::
AudioFile(const char * path,const FileAccess acc)737c478bd9Sstevel@tonic-gate AudioFile(
747c478bd9Sstevel@tonic-gate 	const char		*path,		// filename
757c478bd9Sstevel@tonic-gate 	const FileAccess	acc):		// access mode
767c478bd9Sstevel@tonic-gate 	AudioUnixfile(path, acc),
777c478bd9Sstevel@tonic-gate 	hdrsize(0), seekpos(0), origlen(0.), mapaddr(0), maplen(0),
787c478bd9Sstevel@tonic-gate 	vmaccess(defaccess)
797c478bd9Sstevel@tonic-gate {
807c478bd9Sstevel@tonic-gate }
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate // Destructor must call the local Close() routine
837c478bd9Sstevel@tonic-gate AudioFile::
~AudioFile()847c478bd9Sstevel@tonic-gate ~AudioFile()
857c478bd9Sstevel@tonic-gate {
867c478bd9Sstevel@tonic-gate 	// If the file was open, close it
877c478bd9Sstevel@tonic-gate 	if (opened())
887c478bd9Sstevel@tonic-gate 		(void) Close();
897c478bd9Sstevel@tonic-gate }
907c478bd9Sstevel@tonic-gate 
917c478bd9Sstevel@tonic-gate // Set a default temporary file directory
927c478bd9Sstevel@tonic-gate AudioError AudioFile::
SetTempPath(const char * path)937c478bd9Sstevel@tonic-gate SetTempPath(
947c478bd9Sstevel@tonic-gate 	const char	*path)
957c478bd9Sstevel@tonic-gate {
967c478bd9Sstevel@tonic-gate 	struct stat	st;
977c478bd9Sstevel@tonic-gate 
987c478bd9Sstevel@tonic-gate 	// Verify intended path
997c478bd9Sstevel@tonic-gate 	if ((stat(path, &st) < 0) ||
1007c478bd9Sstevel@tonic-gate 	    !S_ISDIR(st.st_mode) ||
1017c478bd9Sstevel@tonic-gate 	    (access(path, W_OK) < 0)) {
1027c478bd9Sstevel@tonic-gate 		errno = ENOTDIR;
1037c478bd9Sstevel@tonic-gate 		return (AUDIO_UNIXERROR);
1047c478bd9Sstevel@tonic-gate 	}
1057c478bd9Sstevel@tonic-gate 
1067c478bd9Sstevel@tonic-gate 	if (tmpdir != NULL)
1077c478bd9Sstevel@tonic-gate 		(void) free(tmpdir);
1087c478bd9Sstevel@tonic-gate 	tmpdir = (char *)malloc(strlen(path) + 1);
1097c478bd9Sstevel@tonic-gate 	(void) strcpy(tmpdir, path);
1107c478bd9Sstevel@tonic-gate 	return (AUDIO_SUCCESS);
1117c478bd9Sstevel@tonic-gate }
1127c478bd9Sstevel@tonic-gate 
1137c478bd9Sstevel@tonic-gate 
1147c478bd9Sstevel@tonic-gate // Create a named file according to the current mode setting
1157c478bd9Sstevel@tonic-gate AudioError AudioFile::
createfile(const char * path)1167c478bd9Sstevel@tonic-gate createfile(
1177c478bd9Sstevel@tonic-gate 	const char	*path)			// pathname or 0
1187c478bd9Sstevel@tonic-gate {
1197c478bd9Sstevel@tonic-gate 	char		*tmpf;
1207c478bd9Sstevel@tonic-gate 	char		*tmpstr;
1217c478bd9Sstevel@tonic-gate 	int		openmode;
1227c478bd9Sstevel@tonic-gate 	int		desc;
1237c478bd9Sstevel@tonic-gate 	AudioError	err;
1247c478bd9Sstevel@tonic-gate 
1257c478bd9Sstevel@tonic-gate 	// Convert the open mode to an int argument for open()
1267c478bd9Sstevel@tonic-gate 	openmode = GetAccess();
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate 	// Was the header properly set?
1297c478bd9Sstevel@tonic-gate 	if (!hdrset())
1307c478bd9Sstevel@tonic-gate 		return (RaiseError(AUDIO_ERR_BADHDR));
1317c478bd9Sstevel@tonic-gate 
1327c478bd9Sstevel@tonic-gate 	// Can't create if already opened or if mode or name not set
1337c478bd9Sstevel@tonic-gate 	if ((openmode == -1) || opened() || (strlen(path) == 0))
1347c478bd9Sstevel@tonic-gate 		return (RaiseError(AUDIO_ERR_NOEFFECT));
1357c478bd9Sstevel@tonic-gate 
1367c478bd9Sstevel@tonic-gate 	// If temporary file, create and unlink it.
1377c478bd9Sstevel@tonic-gate 	if (strcmp(path, tmpname) == 0) {
1387c478bd9Sstevel@tonic-gate 		// Construct the temporary file path
1397c478bd9Sstevel@tonic-gate 		tmpstr = (char *)malloc(1 + strlen(TMPFILE) +
1407c478bd9Sstevel@tonic-gate 		    strlen((tmpdir == NULL) ? TMPDIR : tmpdir));
1417c478bd9Sstevel@tonic-gate 		(void) sprintf(tmpstr, "%s%s",
1427c478bd9Sstevel@tonic-gate 		    (tmpdir == NULL) ? TMPDIR : tmpdir, TMPFILE);
1437c478bd9Sstevel@tonic-gate 		tmpf = mktemp(tmpstr);
1447c478bd9Sstevel@tonic-gate 
1457c478bd9Sstevel@tonic-gate 		// Open the temp file and unlink it
1467c478bd9Sstevel@tonic-gate 		err = createfile(tmpf);
1477c478bd9Sstevel@tonic-gate 		if ((err == AUDIO_SUCCESS) && (unlink(tmpf) < 0)) {
1487c478bd9Sstevel@tonic-gate 			(void) Close();
1497c478bd9Sstevel@tonic-gate 			err = RaiseError(AUDIO_UNIXERROR, Warning);
1507c478bd9Sstevel@tonic-gate 		}
1517c478bd9Sstevel@tonic-gate 		(void) free(tmpstr);
1527c478bd9Sstevel@tonic-gate 		return (err);
1537c478bd9Sstevel@tonic-gate 	}
1547c478bd9Sstevel@tonic-gate 
1557c478bd9Sstevel@tonic-gate 	// Create the file
1567c478bd9Sstevel@tonic-gate 	desc = open(path, openmode | O_CREAT | O_TRUNC, 0666);
1577c478bd9Sstevel@tonic-gate 	if ((desc < 0) && (errno == EOVERFLOW)) {
1587c478bd9Sstevel@tonic-gate 		return (RaiseError(AUDIO_UNIXERROR, Fatal,
1597c478bd9Sstevel@tonic-gate 		    (char *)"Large File"));
1607c478bd9Sstevel@tonic-gate 	} else if (desc < 0) {
1617c478bd9Sstevel@tonic-gate 		return (RaiseError(AUDIO_UNIXERROR));
1627c478bd9Sstevel@tonic-gate 	}
1637c478bd9Sstevel@tonic-gate 
1647c478bd9Sstevel@tonic-gate 	// Set the file descriptor (this marks the file open)
1657c478bd9Sstevel@tonic-gate 	setfd(desc);
1667c478bd9Sstevel@tonic-gate 
1677c478bd9Sstevel@tonic-gate 	// Write the file header with current (usually unknown) size
1687c478bd9Sstevel@tonic-gate 	err = encode_filehdr();
1697c478bd9Sstevel@tonic-gate 	if (err != AUDIO_SUCCESS) {
1707c478bd9Sstevel@tonic-gate 		setfd(-1);
1717c478bd9Sstevel@tonic-gate 		(void) close(desc);		// If error, remove file
1727c478bd9Sstevel@tonic-gate 		(void) unlink(path);
1737c478bd9Sstevel@tonic-gate 		return (err);
1747c478bd9Sstevel@tonic-gate 	}
1757c478bd9Sstevel@tonic-gate 
1767c478bd9Sstevel@tonic-gate 	// Save the length that got written, then set it to zero
1777c478bd9Sstevel@tonic-gate 	origlen = GetLength();
1787c478bd9Sstevel@tonic-gate 	setlength(0.);
1797c478bd9Sstevel@tonic-gate 
1807c478bd9Sstevel@tonic-gate 	// Set the size of the file header
1817c478bd9Sstevel@tonic-gate 	hdrsize = lseek(desc, (off_t)0, SEEK_CUR);
1827c478bd9Sstevel@tonic-gate 	if (hdrsize < 0) {
1837c478bd9Sstevel@tonic-gate 		setfd(-1);
1847c478bd9Sstevel@tonic-gate 		(void) close(desc);		// If error, remove file
1857c478bd9Sstevel@tonic-gate 		(void) unlink(path);
1867c478bd9Sstevel@tonic-gate 		return (err);
1877c478bd9Sstevel@tonic-gate 	}
1887c478bd9Sstevel@tonic-gate 	seekpos = 0;
1897c478bd9Sstevel@tonic-gate 
1907c478bd9Sstevel@tonic-gate 	return (AUDIO_SUCCESS);
1917c478bd9Sstevel@tonic-gate }
1927c478bd9Sstevel@tonic-gate 
1937c478bd9Sstevel@tonic-gate // Create a file whose name is already set, according to the mode setting
1947c478bd9Sstevel@tonic-gate AudioError AudioFile::
Create()1957c478bd9Sstevel@tonic-gate Create()
1967c478bd9Sstevel@tonic-gate {
1977c478bd9Sstevel@tonic-gate 	return (createfile(GetName()));
1987c478bd9Sstevel@tonic-gate }
1997c478bd9Sstevel@tonic-gate 
2007c478bd9Sstevel@tonic-gate // Open a file whose name is set
2017c478bd9Sstevel@tonic-gate AudioError AudioFile::
Open()2027c478bd9Sstevel@tonic-gate Open()
2037c478bd9Sstevel@tonic-gate {
2047c478bd9Sstevel@tonic-gate 	return (OpenPath(NULL));
2057c478bd9Sstevel@tonic-gate }
2067c478bd9Sstevel@tonic-gate 
2077c478bd9Sstevel@tonic-gate // Open a file, using the specified path prefixes
2087c478bd9Sstevel@tonic-gate AudioError AudioFile::
OpenPath(const char * path)2097c478bd9Sstevel@tonic-gate OpenPath(
2107c478bd9Sstevel@tonic-gate 	const char	*path)
2117c478bd9Sstevel@tonic-gate {
2127c478bd9Sstevel@tonic-gate 	char		*filename;
2137c478bd9Sstevel@tonic-gate 	int		flen;
2147c478bd9Sstevel@tonic-gate 	char		*prefix;
2157c478bd9Sstevel@tonic-gate 	char		*str;
2167c478bd9Sstevel@tonic-gate 	char		*wrk;
2177c478bd9Sstevel@tonic-gate 	char		*pathname;
2187c478bd9Sstevel@tonic-gate 	int		openmode;
2197c478bd9Sstevel@tonic-gate 	AudioError	err;
2207c478bd9Sstevel@tonic-gate 
2217c478bd9Sstevel@tonic-gate 	// Convert the open mode to an int argument for open()
2227c478bd9Sstevel@tonic-gate 	openmode = GetAccess();
2237c478bd9Sstevel@tonic-gate 	filename = GetName();
2247c478bd9Sstevel@tonic-gate 	flen = strlen(filename);
2257c478bd9Sstevel@tonic-gate 
2267c478bd9Sstevel@tonic-gate 	// Can't open if already opened or if mode or name not set
2277c478bd9Sstevel@tonic-gate 	if ((openmode == -1) || opened() || (strlen(filename) == 0))
2287c478bd9Sstevel@tonic-gate 		return (RaiseError(AUDIO_ERR_NOEFFECT));
2297c478bd9Sstevel@tonic-gate 
2307c478bd9Sstevel@tonic-gate 	// Search path:
2317c478bd9Sstevel@tonic-gate 	//	1) try name: if not found and not readonly:
2327c478bd9Sstevel@tonic-gate 	//		if Append mode, try creating it
2337c478bd9Sstevel@tonic-gate 	//	2) if name is a relative pathname, and 'path' is not NULL:
2347c478bd9Sstevel@tonic-gate 	//		try every path prefix in 'path'
2357c478bd9Sstevel@tonic-gate 
2367c478bd9Sstevel@tonic-gate 	err = tryopen(filename, openmode);
2377c478bd9Sstevel@tonic-gate 	if (!err)
2387c478bd9Sstevel@tonic-gate 		return (AUDIO_SUCCESS);
2397c478bd9Sstevel@tonic-gate 	if (GetAccess().Writeable() || (filename[0] == '/')) {
2407c478bd9Sstevel@tonic-gate 		// If file is non-existent and Append mode, try creating it.
2417c478bd9Sstevel@tonic-gate 		if ((err == AUDIO_UNIXERROR) && (err.sys == ENOENT) &&
2427c478bd9Sstevel@tonic-gate 		    GetAccess().Append() && hdrset()) {
2437c478bd9Sstevel@tonic-gate 			return (Create());
2447c478bd9Sstevel@tonic-gate 		}
2457c478bd9Sstevel@tonic-gate 		return (RaiseError(err));
2467c478bd9Sstevel@tonic-gate 	}
2477c478bd9Sstevel@tonic-gate 
2487c478bd9Sstevel@tonic-gate 	// Try path as an environment variable name, else assume it is a path
2497c478bd9Sstevel@tonic-gate 	str = (path == NULL) ? NULL : getenv(path);
2507c478bd9Sstevel@tonic-gate 	if (str == NULL)
2517c478bd9Sstevel@tonic-gate 		str = (char *)path;
2527c478bd9Sstevel@tonic-gate 
2537c478bd9Sstevel@tonic-gate 	if (str != NULL) {
2547c478bd9Sstevel@tonic-gate 		// Make a copy of the path, to parse it
2557c478bd9Sstevel@tonic-gate 		wrk = new char[strlen(str) + 1];
2567c478bd9Sstevel@tonic-gate 		(void) strcpy(wrk, str);
2577c478bd9Sstevel@tonic-gate 		str = wrk;
2587c478bd9Sstevel@tonic-gate 
2597c478bd9Sstevel@tonic-gate 		// Try each component as a path prefix
2607c478bd9Sstevel@tonic-gate 		for (prefix = str;
2617c478bd9Sstevel@tonic-gate 		    (prefix != NULL) && (prefix[0] != '\0');
2627c478bd9Sstevel@tonic-gate 		    prefix = str) {
2637c478bd9Sstevel@tonic-gate 			str = strchr(str, ':');
2647c478bd9Sstevel@tonic-gate 			if (str != NULL)
2657c478bd9Sstevel@tonic-gate 				*str++ = '\0';
2667c478bd9Sstevel@tonic-gate 			pathname = new char[strlen(prefix) + flen + 2];
2677c478bd9Sstevel@tonic-gate 			(void) sprintf(pathname, "%s/%s", prefix, filename);
2687c478bd9Sstevel@tonic-gate 			err = tryopen(pathname, openmode);
269*7cad4b84SToomas Soome 			delete[] pathname;
2707c478bd9Sstevel@tonic-gate 			switch (err) {
2717c478bd9Sstevel@tonic-gate 			case AUDIO_SUCCESS:	// found the file
272*7cad4b84SToomas Soome 				delete[] wrk;
2737c478bd9Sstevel@tonic-gate 				return (RaiseError(err));
2747c478bd9Sstevel@tonic-gate 			// XXX - if file found but not audio, stop looking??
2757c478bd9Sstevel@tonic-gate 			}
2767c478bd9Sstevel@tonic-gate 		}
277*7cad4b84SToomas Soome 		delete[] wrk;
2787c478bd9Sstevel@tonic-gate 	}
2797c478bd9Sstevel@tonic-gate 	// Can't find file.  Return the original error condition.
2807c478bd9Sstevel@tonic-gate 	return (RaiseError(tryopen(filename, openmode)));
2817c478bd9Sstevel@tonic-gate }
2827c478bd9Sstevel@tonic-gate 
2837c478bd9Sstevel@tonic-gate // Attempt to open the given audio file
2847c478bd9Sstevel@tonic-gate AudioError AudioFile::
tryopen(const char * pathname,int openmode)2857c478bd9Sstevel@tonic-gate tryopen(
2867c478bd9Sstevel@tonic-gate 	const char	*pathname,
2877c478bd9Sstevel@tonic-gate 	int		openmode)
2887c478bd9Sstevel@tonic-gate {
2897c478bd9Sstevel@tonic-gate 	struct stat	st;
2907c478bd9Sstevel@tonic-gate 	int		desc;
2917c478bd9Sstevel@tonic-gate 	AudioError	err;
2927c478bd9Sstevel@tonic-gate 
2937c478bd9Sstevel@tonic-gate 	// If the name is changing, set the new one
2947c478bd9Sstevel@tonic-gate 	if (pathname != GetName())
2957c478bd9Sstevel@tonic-gate 		SetName(pathname);
2967c478bd9Sstevel@tonic-gate 
2977c478bd9Sstevel@tonic-gate 	// Does the file exist?
2987c478bd9Sstevel@tonic-gate 	if (stat(pathname, &st) < 0)
2997c478bd9Sstevel@tonic-gate 		return (AUDIO_UNIXERROR);
3007c478bd9Sstevel@tonic-gate 
3017c478bd9Sstevel@tonic-gate 	// If not a regular file, stop right there
3027c478bd9Sstevel@tonic-gate 	if (!S_ISREG(st.st_mode))
3037c478bd9Sstevel@tonic-gate 		return (AUDIO_ERR_BADFILEHDR);
3047c478bd9Sstevel@tonic-gate 
3057c478bd9Sstevel@tonic-gate 	// Open the file and check that it's an audio file
3067c478bd9Sstevel@tonic-gate 	desc = open(GetName(), openmode);
3077c478bd9Sstevel@tonic-gate 	if ((desc < 0) && (errno == EOVERFLOW)) {
3087c478bd9Sstevel@tonic-gate 		return (RaiseError(AUDIO_UNIXERROR, Fatal,
3097c478bd9Sstevel@tonic-gate 		    (char *)"Large File"));
3107c478bd9Sstevel@tonic-gate 	} else if (desc < 0) {
3117c478bd9Sstevel@tonic-gate 		return (AUDIO_UNIXERROR);
3127c478bd9Sstevel@tonic-gate 	}
3137c478bd9Sstevel@tonic-gate 
3147c478bd9Sstevel@tonic-gate 	// Set the file descriptor (this marks the file open)
3157c478bd9Sstevel@tonic-gate 	setfd(desc);
3167c478bd9Sstevel@tonic-gate 
3177c478bd9Sstevel@tonic-gate 	err = decode_filehdr();
3187c478bd9Sstevel@tonic-gate 	if (err != AUDIO_SUCCESS) {
3197c478bd9Sstevel@tonic-gate 		(void) close(desc);
3207c478bd9Sstevel@tonic-gate 		setfd(-1);
3217c478bd9Sstevel@tonic-gate 		return (err);
3227c478bd9Sstevel@tonic-gate 	}
3237c478bd9Sstevel@tonic-gate 
3247c478bd9Sstevel@tonic-gate 	// Save the length of the data and the size of the file header
3257c478bd9Sstevel@tonic-gate 	origlen = GetLength();
3267c478bd9Sstevel@tonic-gate 	hdrsize = (off_t)lseek(desc, (off_t)0, SEEK_CUR);
3277c478bd9Sstevel@tonic-gate 	if (hdrsize < 0) {
3287c478bd9Sstevel@tonic-gate 		(void) close(desc);
3297c478bd9Sstevel@tonic-gate 		setfd(-1);
3307c478bd9Sstevel@tonic-gate 		return (err);
3317c478bd9Sstevel@tonic-gate 	}
3327c478bd9Sstevel@tonic-gate 	seekpos = 0;
3337c478bd9Sstevel@tonic-gate 
3347c478bd9Sstevel@tonic-gate 	// If this is ReadOnly file, mmap() it.  Don't worry if mmap() fails.
3357c478bd9Sstevel@tonic-gate 	if (!GetAccess().Writeable()) {
3367c478bd9Sstevel@tonic-gate 		maplen = st.st_size;
3377c478bd9Sstevel@tonic-gate 
3387c478bd9Sstevel@tonic-gate 		/*
3397c478bd9Sstevel@tonic-gate 		 * Can't mmap LITTLE_ENDIAN as they are converted in
3407c478bd9Sstevel@tonic-gate 		 * place.
3417c478bd9Sstevel@tonic-gate 		 */
3427c478bd9Sstevel@tonic-gate 		if (localByteOrder() == BIG_ENDIAN) {
3437c478bd9Sstevel@tonic-gate 			if ((mapaddr = (caddr_t)mmap(0, (int)maplen, PROT_READ,
3447c478bd9Sstevel@tonic-gate 				MAP_SHARED, desc, 0)) != (caddr_t)-1) {
3457c478bd9Sstevel@tonic-gate 				// set default access method
3467c478bd9Sstevel@tonic-gate 				(void) madvise(mapaddr, (unsigned int)maplen,
3477c478bd9Sstevel@tonic-gate 				    (int)GetAccessType());
3487c478bd9Sstevel@tonic-gate 			} else {
3497c478bd9Sstevel@tonic-gate 				(void) RaiseError(AUDIO_UNIXERROR, Warning,
3507c478bd9Sstevel@tonic-gate 				    (char *)"Could not mmap() file");
3517c478bd9Sstevel@tonic-gate 				mapaddr = 0;
3527c478bd9Sstevel@tonic-gate 				maplen = 0;
3537c478bd9Sstevel@tonic-gate 			}
3547c478bd9Sstevel@tonic-gate 		} else {
3557c478bd9Sstevel@tonic-gate 			mapaddr = 0;
3567c478bd9Sstevel@tonic-gate 			maplen = 0;
3577c478bd9Sstevel@tonic-gate 		}
3587c478bd9Sstevel@tonic-gate 	}
3597c478bd9Sstevel@tonic-gate 	return (AUDIO_SUCCESS);
3607c478bd9Sstevel@tonic-gate }
3617c478bd9Sstevel@tonic-gate 
3627c478bd9Sstevel@tonic-gate // set VM access hint for mmapped files
3637c478bd9Sstevel@tonic-gate AudioError AudioFile::
SetAccessType(VMAccess vmacc)3647c478bd9Sstevel@tonic-gate SetAccessType(VMAccess vmacc)
3657c478bd9Sstevel@tonic-gate {
3667c478bd9Sstevel@tonic-gate 	if (!opened()) {
3677c478bd9Sstevel@tonic-gate 		return (AUDIO_ERR_NOEFFECT);
3687c478bd9Sstevel@tonic-gate 	}
3697c478bd9Sstevel@tonic-gate 
3707c478bd9Sstevel@tonic-gate 	if (mapaddr == 0) {
3717c478bd9Sstevel@tonic-gate 		return (AUDIO_ERR_NOEFFECT);
3727c478bd9Sstevel@tonic-gate 	}
3737c478bd9Sstevel@tonic-gate 
3747c478bd9Sstevel@tonic-gate 	(void) madvise(mapaddr, (unsigned int)maplen, (int)vmacc);
3757c478bd9Sstevel@tonic-gate 	vmaccess = vmacc;
3767c478bd9Sstevel@tonic-gate 
3777c478bd9Sstevel@tonic-gate 	return (AUDIO_SUCCESS);
3787c478bd9Sstevel@tonic-gate }
3797c478bd9Sstevel@tonic-gate 
3807c478bd9Sstevel@tonic-gate // Close the file
3817c478bd9Sstevel@tonic-gate AudioError AudioFile::
Close()3827c478bd9Sstevel@tonic-gate Close()
3837c478bd9Sstevel@tonic-gate {
3847c478bd9Sstevel@tonic-gate 	AudioError	err;
3857c478bd9Sstevel@tonic-gate 
3867c478bd9Sstevel@tonic-gate 	if (!opened())
3877c478bd9Sstevel@tonic-gate 		return (RaiseError(AUDIO_ERR_NOEFFECT, Warning));
3887c478bd9Sstevel@tonic-gate 
3897c478bd9Sstevel@tonic-gate 	// Rewind the file and rewrite the header with the correct length
3907c478bd9Sstevel@tonic-gate 	if (GetAccess().Writeable() && (origlen != GetLength())) {
3917c478bd9Sstevel@tonic-gate 
3927c478bd9Sstevel@tonic-gate 		// sanity check
3937c478bd9Sstevel@tonic-gate 		if (GetHeader().Time_to_Bytes(GetLength()) !=
3947c478bd9Sstevel@tonic-gate 		    (lseek(getfd(), (off_t)0, SEEK_END) - hdrsize)) {
3957c478bd9Sstevel@tonic-gate 			PrintMsg(_MGET_(
3967c478bd9Sstevel@tonic-gate 			    "AudioFile:Close()...inconsistent length\n"),
3977c478bd9Sstevel@tonic-gate 			    Fatal);
3987c478bd9Sstevel@tonic-gate 		}
3997c478bd9Sstevel@tonic-gate 
4007c478bd9Sstevel@tonic-gate 		// XXX - should be rewritten in C++
4017c478bd9Sstevel@tonic-gate 		err = (AudioError) audio_rewrite_filesize(getfd(), FILE_AU,
4027c478bd9Sstevel@tonic-gate 		    (uint_t)GetHeader().Time_to_Bytes(GetLength()), 0, 0);
4037c478bd9Sstevel@tonic-gate 	}
4047c478bd9Sstevel@tonic-gate 
4057c478bd9Sstevel@tonic-gate 	// Call the generic file close routine
4067c478bd9Sstevel@tonic-gate 	err = AudioUnixfile::Close();
4077c478bd9Sstevel@tonic-gate 
4087c478bd9Sstevel@tonic-gate 	if (mapaddr) {
4097c478bd9Sstevel@tonic-gate 		munmap(mapaddr, (int)maplen);
4107c478bd9Sstevel@tonic-gate 		mapaddr = 0;
4117c478bd9Sstevel@tonic-gate 		maplen = 0;
4127c478bd9Sstevel@tonic-gate 	}
4137c478bd9Sstevel@tonic-gate 
4147c478bd9Sstevel@tonic-gate 	// Init important values, in case the file is reopened
4157c478bd9Sstevel@tonic-gate 	hdrsize = 0;
4167c478bd9Sstevel@tonic-gate 	seekpos = 0;
4177c478bd9Sstevel@tonic-gate 	return (RaiseError(err));
4187c478bd9Sstevel@tonic-gate }
4197c478bd9Sstevel@tonic-gate 
4207c478bd9Sstevel@tonic-gate // Read data from underlying file into specified buffer.
4217c478bd9Sstevel@tonic-gate // No data format translation takes place.
4227c478bd9Sstevel@tonic-gate // The object's read position pointer is unaffected.
4237c478bd9Sstevel@tonic-gate AudioError AudioFile::
ReadData(void * buf,size_t & len,Double & pos)4247c478bd9Sstevel@tonic-gate ReadData(
4257c478bd9Sstevel@tonic-gate 	void*		buf,		// destination buffer address
4267c478bd9Sstevel@tonic-gate 	size_t&		len,		// buffer length (updated)
4277c478bd9Sstevel@tonic-gate 	Double&		pos)		// start position (updated)
4287c478bd9Sstevel@tonic-gate {
4297c478bd9Sstevel@tonic-gate 	off_t		offset;
4307c478bd9Sstevel@tonic-gate 	size_t		cnt;
4317c478bd9Sstevel@tonic-gate 	caddr_t		cp;
4327c478bd9Sstevel@tonic-gate 	AudioError	err;
4337c478bd9Sstevel@tonic-gate 
4347c478bd9Sstevel@tonic-gate 	// If the file is not mapped, call parent ReadData() and return
4357c478bd9Sstevel@tonic-gate 	if (mapaddr == 0) {
4367c478bd9Sstevel@tonic-gate 		// Call the real routine
4377c478bd9Sstevel@tonic-gate 		err = AudioUnixfile::ReadData(buf, len, pos);
4387c478bd9Sstevel@tonic-gate 		// Update the cached seek pointer
4397c478bd9Sstevel@tonic-gate 		seekpos += len;
4407c478bd9Sstevel@tonic-gate 		return (err);
4417c478bd9Sstevel@tonic-gate 	}
4427c478bd9Sstevel@tonic-gate 
4437c478bd9Sstevel@tonic-gate 	// If the file is mmapped, do a memcpy() from the mapaddr
4447c478bd9Sstevel@tonic-gate 
4457c478bd9Sstevel@tonic-gate 	// Save buffer size and zero transfer count
4467c478bd9Sstevel@tonic-gate 	cnt = (size_t)len;
4477c478bd9Sstevel@tonic-gate 	len = 0;
4487c478bd9Sstevel@tonic-gate 
4497c478bd9Sstevel@tonic-gate 	// Cannot read if file is not open
4507c478bd9Sstevel@tonic-gate 	if (!opened() || !GetAccess().Readable())
4517c478bd9Sstevel@tonic-gate 		return (RaiseError(AUDIO_ERR_NOEFFECT));
4527c478bd9Sstevel@tonic-gate 
4537c478bd9Sstevel@tonic-gate 	// Position must be valid
4547c478bd9Sstevel@tonic-gate 	if (Undefined(pos) || (pos < 0.) || ((int)cnt < 0))
4557c478bd9Sstevel@tonic-gate 		return (RaiseError(AUDIO_ERR_BADARG));
4567c478bd9Sstevel@tonic-gate 
4577c478bd9Sstevel@tonic-gate 	// Make sure we don't read off the end of file
4587c478bd9Sstevel@tonic-gate 	offset = GetHeader().Time_to_Bytes(pos);
4597c478bd9Sstevel@tonic-gate 
4607c478bd9Sstevel@tonic-gate 	if ((offset + hdrsize) >= maplen) {
4617c478bd9Sstevel@tonic-gate 		// trying to read past EOF
4627c478bd9Sstevel@tonic-gate 		err = AUDIO_EOF;
4637c478bd9Sstevel@tonic-gate 		err.sys = AUDIO_COPY_INPUT_EOF;
4647c478bd9Sstevel@tonic-gate 		return (err);
4657c478bd9Sstevel@tonic-gate 	} else if ((offset + hdrsize + cnt) > maplen) {
4667c478bd9Sstevel@tonic-gate 		// re-adjust cnt so it reads up to the end of file
4677c478bd9Sstevel@tonic-gate 		cnt = (size_t)(maplen - (offset + hdrsize));
4687c478bd9Sstevel@tonic-gate 	}
4697c478bd9Sstevel@tonic-gate 
4707c478bd9Sstevel@tonic-gate 	// Zero-length reads are finished
4717c478bd9Sstevel@tonic-gate 	if (GetHeader().Bytes_to_Bytes(cnt) == 0) {
4727c478bd9Sstevel@tonic-gate 		err = AUDIO_SUCCESS;
4737c478bd9Sstevel@tonic-gate 		err.sys = AUDIO_COPY_ZERO_LIMIT;
4747c478bd9Sstevel@tonic-gate 		return (err);
4757c478bd9Sstevel@tonic-gate 	} else {
4767c478bd9Sstevel@tonic-gate 		cp = mapaddr + offset + hdrsize;
4777c478bd9Sstevel@tonic-gate 		memcpy((void*)buf, (void*)cp, cnt);
4787c478bd9Sstevel@tonic-gate 	}
4797c478bd9Sstevel@tonic-gate 
4807c478bd9Sstevel@tonic-gate 	// Return the updated byte count and position
4817c478bd9Sstevel@tonic-gate 	len = cnt;
4827c478bd9Sstevel@tonic-gate 	pos = GetHeader().Bytes_to_Time(offset + len);
4837c478bd9Sstevel@tonic-gate 
4847c478bd9Sstevel@tonic-gate 	// Check to see if the endian is right. Note that special care
4857c478bd9Sstevel@tonic-gate 	// doesn't need to be taken because of the mmap, since the data
4867c478bd9Sstevel@tonic-gate 	// is copied into a separate buffer anyway.
4877c478bd9Sstevel@tonic-gate 	coerceEndian((unsigned char *)buf, len, localByteOrder());
4887c478bd9Sstevel@tonic-gate 
4897c478bd9Sstevel@tonic-gate 	return (AUDIO_SUCCESS);
4907c478bd9Sstevel@tonic-gate }
4917c478bd9Sstevel@tonic-gate 
4927c478bd9Sstevel@tonic-gate // Write data to underlying file from specified buffer.
4937c478bd9Sstevel@tonic-gate // No data format translation takes place.
4947c478bd9Sstevel@tonic-gate // The object's write position pointer is unaffected.
4957c478bd9Sstevel@tonic-gate AudioError AudioFile::
WriteData(void * buf,size_t & len,Double & pos)4967c478bd9Sstevel@tonic-gate WriteData(
4977c478bd9Sstevel@tonic-gate 	void*		buf, // source buffer address
4987c478bd9Sstevel@tonic-gate 	size_t&		len, // buffer length (updated)
4997c478bd9Sstevel@tonic-gate 	Double&		pos) // start position (updated)
5007c478bd9Sstevel@tonic-gate {
5017c478bd9Sstevel@tonic-gate 	AudioError	err;
5027c478bd9Sstevel@tonic-gate 
5037c478bd9Sstevel@tonic-gate 	// Call the real routine
5047c478bd9Sstevel@tonic-gate 	err = AudioUnixfile::WriteData(buf, len, pos);
5057c478bd9Sstevel@tonic-gate 
5067c478bd9Sstevel@tonic-gate 	// Update the cached seek pointer
5077c478bd9Sstevel@tonic-gate 	seekpos += len;
5087c478bd9Sstevel@tonic-gate 	return (err);
5097c478bd9Sstevel@tonic-gate }
5107c478bd9Sstevel@tonic-gate 
5117c478bd9Sstevel@tonic-gate // Set the Unix file pointer to match a given file position.
5127c478bd9Sstevel@tonic-gate AudioError AudioFile::
seekread(Double pos,off_t & offset)5137c478bd9Sstevel@tonic-gate seekread(
5147c478bd9Sstevel@tonic-gate 	Double		pos,	// position to seek to
5157c478bd9Sstevel@tonic-gate 	off_t&		offset)	// returned byte offset
5167c478bd9Sstevel@tonic-gate {
5177c478bd9Sstevel@tonic-gate 	offset = GetHeader().Time_to_Bytes(pos);
5187c478bd9Sstevel@tonic-gate 	if (offset != seekpos) {
5197c478bd9Sstevel@tonic-gate 		if (lseek(getfd(), (off_t)(hdrsize + offset), SEEK_SET) < 0)
5207c478bd9Sstevel@tonic-gate 			return (RaiseError(AUDIO_UNIXERROR, Warning));
5217c478bd9Sstevel@tonic-gate 		seekpos = offset;
5227c478bd9Sstevel@tonic-gate 	}
5237c478bd9Sstevel@tonic-gate 	return (AUDIO_SUCCESS);
5247c478bd9Sstevel@tonic-gate }
5257c478bd9Sstevel@tonic-gate 
5267c478bd9Sstevel@tonic-gate // Set the Unix file pointer to match a given file position.
5277c478bd9Sstevel@tonic-gate // If seek beyond end-of-file, NULL out intervening data.
5287c478bd9Sstevel@tonic-gate AudioError AudioFile::
seekwrite(Double pos,off_t & offset)5297c478bd9Sstevel@tonic-gate seekwrite(
5307c478bd9Sstevel@tonic-gate 	Double		pos,	// position to seek to
5317c478bd9Sstevel@tonic-gate 	off_t&		offset)	// returned byte offset
5327c478bd9Sstevel@tonic-gate {
5337c478bd9Sstevel@tonic-gate 	// If append-only, can't seek backwards into file
5347c478bd9Sstevel@tonic-gate 	if (GetAccess().Append() && (pos < GetLength()))
5357c478bd9Sstevel@tonic-gate 		return (RaiseError(AUDIO_ERR_NOEFFECT, Warning));
5367c478bd9Sstevel@tonic-gate 
5377c478bd9Sstevel@tonic-gate 	// If seek beyond eof, fill data
5387c478bd9Sstevel@tonic-gate 	if (pos > GetLength()) {
5397c478bd9Sstevel@tonic-gate 		seekwrite(GetLength(), offset);	// seek to eof
5407c478bd9Sstevel@tonic-gate 
5417c478bd9Sstevel@tonic-gate 		// XXX - not implemented yet
5427c478bd9Sstevel@tonic-gate 
5437c478bd9Sstevel@tonic-gate 		return (AUDIO_SUCCESS);
5447c478bd9Sstevel@tonic-gate 	}
5457c478bd9Sstevel@tonic-gate 
5467c478bd9Sstevel@tonic-gate 	offset = GetHeader().Time_to_Bytes(pos);
5477c478bd9Sstevel@tonic-gate 	if (offset != seekpos) {
5487c478bd9Sstevel@tonic-gate 		if (lseek(getfd(), (off_t)(hdrsize + offset), SEEK_SET) < 0)
5497c478bd9Sstevel@tonic-gate 			return (RaiseError(AUDIO_UNIXERROR, Warning));
5507c478bd9Sstevel@tonic-gate 		seekpos = offset;
5517c478bd9Sstevel@tonic-gate 	}
5527c478bd9Sstevel@tonic-gate 	return (AUDIO_SUCCESS);
5537c478bd9Sstevel@tonic-gate }
5547c478bd9Sstevel@tonic-gate 
5557c478bd9Sstevel@tonic-gate // Copy routine that handles mapped files
5567c478bd9Sstevel@tonic-gate AudioError AudioFile::
AsyncCopy(Audio * to,Double & frompos,Double & topos,Double & limit)5577c478bd9Sstevel@tonic-gate AsyncCopy(
5587c478bd9Sstevel@tonic-gate 	Audio*		to,			// audio object to copy to
5597c478bd9Sstevel@tonic-gate 	Double&		frompos,
5607c478bd9Sstevel@tonic-gate 	Double&		topos,
5617c478bd9Sstevel@tonic-gate 	Double&		limit)
5627c478bd9Sstevel@tonic-gate {
5637c478bd9Sstevel@tonic-gate 	caddr_t		bptr;
5647c478bd9Sstevel@tonic-gate 	size_t		offset;
5657c478bd9Sstevel@tonic-gate 	size_t		cnt;
5667c478bd9Sstevel@tonic-gate 	size_t		svlim;
5677c478bd9Sstevel@tonic-gate 	Double		svfrom;
5687c478bd9Sstevel@tonic-gate 	Double		svto;
5697c478bd9Sstevel@tonic-gate 	AudioHdr	tohdr;
5707c478bd9Sstevel@tonic-gate 	AudioError	err;
5717c478bd9Sstevel@tonic-gate 
5727c478bd9Sstevel@tonic-gate 	// If this is NOT mmapped, or the destination is an AudioBuffer,
5737c478bd9Sstevel@tonic-gate 	// use the default routine
5747c478bd9Sstevel@tonic-gate 	if ((mapaddr == 0) || to->isBuffer()) {
5757c478bd9Sstevel@tonic-gate 		return (Audio::AsyncCopy(to, frompos, topos, limit));
5767c478bd9Sstevel@tonic-gate 	}
5777c478bd9Sstevel@tonic-gate 
5787c478bd9Sstevel@tonic-gate 	tohdr = to->GetHeader();
5797c478bd9Sstevel@tonic-gate 	if (err = tohdr.Validate())
5807c478bd9Sstevel@tonic-gate 		return (err);
5817c478bd9Sstevel@tonic-gate 	if (limit < 0.)
5827c478bd9Sstevel@tonic-gate 		return (RaiseError(AUDIO_ERR_BADARG));
5837c478bd9Sstevel@tonic-gate 	svlim = (size_t)tohdr.Time_to_Bytes(limit);
5847c478bd9Sstevel@tonic-gate 
5857c478bd9Sstevel@tonic-gate 	// Get maximum possible copy length
5867c478bd9Sstevel@tonic-gate 	svfrom = GetLength();
5877c478bd9Sstevel@tonic-gate 	if ((frompos >= svfrom) || ((cnt = (size_t)
5887c478bd9Sstevel@tonic-gate 	    GetHeader().Time_to_Bytes(svfrom - frompos)) == 0)) {
5897c478bd9Sstevel@tonic-gate 		limit = 0.;
5907c478bd9Sstevel@tonic-gate 		err = AUDIO_EOF;
5917c478bd9Sstevel@tonic-gate 		err.sys = AUDIO_COPY_INPUT_EOF;
5927c478bd9Sstevel@tonic-gate 		return (err);
5937c478bd9Sstevel@tonic-gate 	}
5947c478bd9Sstevel@tonic-gate 	if (!Undefined(limit) && (svlim < cnt))
5957c478bd9Sstevel@tonic-gate 		cnt = svlim;
5967c478bd9Sstevel@tonic-gate 
5977c478bd9Sstevel@tonic-gate 	limit = 0.;
5987c478bd9Sstevel@tonic-gate 
5997c478bd9Sstevel@tonic-gate 	offset = (size_t)GetHeader().Time_to_Bytes(frompos);
6007c478bd9Sstevel@tonic-gate 	if ((offset + hdrsize) >= maplen) {
6017c478bd9Sstevel@tonic-gate 		// trying to read past EOF
6027c478bd9Sstevel@tonic-gate 		err = AUDIO_EOF;
6037c478bd9Sstevel@tonic-gate 		err.sys = AUDIO_COPY_INPUT_EOF;
6047c478bd9Sstevel@tonic-gate 		return (err);
6057c478bd9Sstevel@tonic-gate 	} else if ((offset + hdrsize + cnt) > maplen) {
6067c478bd9Sstevel@tonic-gate 		// re-adjust cnt so it reads up to the end of file
6077c478bd9Sstevel@tonic-gate 		cnt = (size_t)(maplen - (offset + hdrsize));
6087c478bd9Sstevel@tonic-gate 	}
6097c478bd9Sstevel@tonic-gate 
6107c478bd9Sstevel@tonic-gate 	// Zero-length reads are done
6117c478bd9Sstevel@tonic-gate 	if (GetHeader().Bytes_to_Bytes(cnt) == 0) {
6127c478bd9Sstevel@tonic-gate 		err = AUDIO_SUCCESS;
6137c478bd9Sstevel@tonic-gate 		err.sys = AUDIO_COPY_ZERO_LIMIT;
6147c478bd9Sstevel@tonic-gate 		return (err);
6157c478bd9Sstevel@tonic-gate 	}
6167c478bd9Sstevel@tonic-gate 
6177c478bd9Sstevel@tonic-gate 	// Write the data to the destination and update pointers/ctrs
6187c478bd9Sstevel@tonic-gate 	svfrom = frompos;
6197c478bd9Sstevel@tonic-gate 	svto = topos;
6207c478bd9Sstevel@tonic-gate 	svlim = cnt;
6217c478bd9Sstevel@tonic-gate 	bptr = mapaddr + hdrsize + offset;
6227c478bd9Sstevel@tonic-gate 	err = to->WriteData(bptr, cnt, topos);
6237c478bd9Sstevel@tonic-gate 	limit = topos - svto;
6247c478bd9Sstevel@tonic-gate 	frompos = svfrom + limit;
6257c478bd9Sstevel@tonic-gate 
6267c478bd9Sstevel@tonic-gate 	// Report short writes
6277c478bd9Sstevel@tonic-gate 	if (!err && (cnt < svlim))
6287c478bd9Sstevel@tonic-gate 		err.sys = AUDIO_COPY_SHORT_OUTPUT;
6297c478bd9Sstevel@tonic-gate 	return (err);
6307c478bd9Sstevel@tonic-gate }
631