mpegplayer loose ends: Move gray_release to the exit routine where it should be....
[Rockbox.git] / rbutil / sansapatcher / sansaio-posix.c
blob22abc883d26f155074744c18afb53a6b294f012c
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>
29 #if defined(linux) || defined (__linux)
30 #include <sys/mount.h>
31 #define SANSA_SECTORSIZE_IOCTL BLKSSZGET
32 #elif defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) \
33 || defined(__bsdi__) || defined(__DragonFly__)
34 #include <sys/disk.h>
35 #define SANSA_SECTORSIZE_IOCTL DIOCGSECTORSIZE
36 #elif defined(__APPLE__) && defined(__MACH__)
37 #include <sys/disk.h>
38 #define SANSA_SECTORSIZE_IOCTL DKIOCGETBLOCKSIZE
39 #else
40 #error No sector-size detection implemented for this platform
41 #endif
43 #include "sansaio.h"
45 #if defined(__APPLE__) && defined(__MACH__)
46 static int sansa_unmount(struct sansa_t* sansa)
48 char cmd[4096];
49 int res;
51 sprintf(cmd, "/usr/sbin/diskutil unmount \"%ss1\"",sansa->diskname);
52 fprintf(stderr,"[INFO] ");
53 res = system(cmd);
55 if (res==0) {
56 return 0;
57 } else {
58 perror("Unmount failed");
59 return -1;
62 #endif
65 #ifndef RBUTIL
66 void print_error(char* msg)
68 perror(msg);
70 #endif
72 int sansa_open(struct sansa_t* sansa, int silent)
74 sansa->dh=open(sansa->diskname,O_RDONLY);
75 if (sansa->dh < 0) {
76 if (!silent) perror(sansa->diskname);
77 return -1;
80 if(ioctl(sansa->dh,SANSA_SECTORSIZE_IOCTL,&sansa->sector_size) < 0) {
81 sansa->sector_size=512;
82 if (!silent) {
83 fprintf(stderr,"[ERR] ioctl() call to get sector size failed, defaulting to %d\n"
84 ,sansa->sector_size);
87 return 0;
91 int sansa_reopen_rw(struct sansa_t* sansa)
93 #if defined(__APPLE__) && defined(__MACH__)
94 if (sansa_unmount(sansa) < 0)
95 return -1;
96 #endif
98 close(sansa->dh);
99 sansa->dh=open(sansa->diskname,O_RDWR);
100 if (sansa->dh < 0) {
101 perror(sansa->diskname);
102 return -1;
104 return 0;
107 int sansa_close(struct sansa_t* sansa)
109 close(sansa->dh);
110 return 0;
113 int sansa_alloc_buffer(unsigned char** sectorbuf, int bufsize)
115 *sectorbuf=malloc(bufsize);
116 if (*sectorbuf == NULL) {
117 return -1;
119 return 0;
122 int sansa_seek(struct sansa_t* sansa, loff_t pos)
124 off_t res;
126 res = lseek64(sansa->dh, pos, SEEK_SET);
128 if (res == -1) {
129 return -1;
131 return 0;
134 int sansa_read(struct sansa_t* sansa, unsigned char* buf, int nbytes)
136 return read(sansa->dh, buf, nbytes);
139 int sansa_write(struct sansa_t* sansa, unsigned char* buf, int nbytes)
141 return write(sansa->dh, buf, nbytes);