Musepack speed optimization. Speep up 64 bit precision synthesizer by another 1.5MHz...
[kugel-rb.git] / rbutil / sansapatcher / sansaio-posix.c
blob95677b6b35919372324533e8fad32f923446aa39
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2006-2007 Dave Chapman
12 * All files in this archive are subject to the GNU General Public License.
13 * See the file COPYING in the source tree root for full license agreement.
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
18 ****************************************************************************/
20 #include <stdio.h>
21 #include <unistd.h>
22 #include <fcntl.h>
23 #include <string.h>
24 #include <stdlib.h>
25 #include <sys/types.h>
26 #include <sys/stat.h>
27 #include <sys/ioctl.h>
28 #include <errno.h>
30 #if defined(linux) || defined (__linux)
31 #include <sys/mount.h>
32 #define SANSA_SECTORSIZE_IOCTL BLKSSZGET
33 #elif defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) \
34 || defined(__bsdi__) || defined(__DragonFly__)
35 #include <sys/disk.h>
36 #define SANSA_SECTORSIZE_IOCTL DIOCGSECTORSIZE
37 #elif defined(__APPLE__) && defined(__MACH__)
38 #include <sys/disk.h>
39 #define SANSA_SECTORSIZE_IOCTL DKIOCGETBLOCKSIZE
40 #else
41 #error No sector-size detection implemented for this platform
42 #endif
44 #include "sansaio.h"
46 #if defined(__APPLE__) && defined(__MACH__)
47 static int sansa_unmount(struct sansa_t* sansa)
49 char cmd[4096];
50 int res;
52 sprintf(cmd, "/usr/sbin/diskutil unmount \"%ss1\"",sansa->diskname);
53 fprintf(stderr,"[INFO] ");
54 res = system(cmd);
56 if (res==0) {
57 return 0;
58 } else {
59 perror("Unmount failed");
60 return -1;
63 #endif
66 #ifndef RBUTIL
67 void print_error(char* msg)
69 perror(msg);
71 #endif
73 int sansa_open(struct sansa_t* sansa, int silent)
75 sansa->dh=open(sansa->diskname,O_RDONLY);
76 if (sansa->dh < 0) {
77 if (!silent) perror(sansa->diskname);
78 if(errno == EACCES) return -2;
79 else return -1;
82 if(ioctl(sansa->dh,SANSA_SECTORSIZE_IOCTL,&sansa->sector_size) < 0) {
83 sansa->sector_size=512;
84 if (!silent) {
85 fprintf(stderr,"[ERR] ioctl() call to get sector size failed, defaulting to %d\n"
86 ,sansa->sector_size);
89 return 0;
93 int sansa_reopen_rw(struct sansa_t* sansa)
95 #if defined(__APPLE__) && defined(__MACH__)
96 if (sansa_unmount(sansa) < 0)
97 return -1;
98 #endif
100 close(sansa->dh);
101 sansa->dh=open(sansa->diskname,O_RDWR);
102 if (sansa->dh < 0) {
103 perror(sansa->diskname);
104 return -1;
106 return 0;
109 int sansa_close(struct sansa_t* sansa)
111 close(sansa->dh);
112 return 0;
115 int sansa_alloc_buffer(unsigned char** sectorbuf, int bufsize)
117 *sectorbuf=malloc(bufsize);
118 if (*sectorbuf == NULL) {
119 return -1;
121 return 0;
124 int sansa_seek(struct sansa_t* sansa, loff_t pos)
126 off_t res;
128 res = lseek64(sansa->dh, pos, SEEK_SET);
130 if (res == -1) {
131 return -1;
133 return 0;
136 int sansa_read(struct sansa_t* sansa, unsigned char* buf, int nbytes)
138 return read(sansa->dh, buf, nbytes);
141 int sansa_write(struct sansa_t* sansa, unsigned char* buf, int nbytes)
143 return write(sansa->dh, buf, nbytes);