comparison src/smb/smb.c @ 845:71c6cbf25348 trunk

[svn] - Prototype smb:// vfs transport. Needs testing.
author nazca
date Tue, 13 Mar 2007 05:11:48 -0700
parents
children 76983ca8178a
comparison
equal deleted inserted replaced
844:80e7162bd968 845:71c6cbf25348
1 /* Audacious
2 * Copyright (c) 2007 Daniel Bradshaw
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 */
18
19 #include <audacious/vfs.h>
20 #include <audacious/plugin.h>
21 #include <stdio.h>
22
23 #include <unistd.h>
24 #include <sys/stat.h>
25 #include <sys/types.h>
26
27 #include <libsmbclient.h>
28
29 static void smb_auth_fn(const char *srv,
30 const char *shr,
31 char *wg, int wglen,
32 char *un, int unlen,
33 char *pw, int pwlen)
34 {
35 }
36
37 typedef struct smb_file smb_file;
38
39 struct smb_file
40 {
41 int fd;
42 long length;
43 };
44
45 VFSFile *
46 smb_vfs_fopen_impl(const gchar * path,
47 const gchar * mode)
48 {
49 VFSFile *file;
50 smb_file *handle;
51 struct stat st;
52
53 if (!path || !mode)
54 return NULL;
55
56 if (smbc_init(smb_auth_fn, 1) != 0)
57 return NULL;
58
59 file = g_new0(VFSFile, 1);
60 handle = g_new0(smb_file, 1);
61 handle->fd = smbc_open(path, O_RDONLY, 0);
62
63 if (file->handle < 0) {
64 g_free(file);
65 file = NULL;
66 } else {
67 smbc_stat(path,&st);
68 handle->length = st.st_size;
69 file->handle = (void *)handle;
70 }
71
72 return file;
73 }
74
75 gint
76 smb_vfs_fclose_impl(VFSFile * file)
77 {
78 gint ret = 0;
79 smb_file *handle;
80
81 if (file == NULL)
82 return -1;
83
84 if (file->handle)
85 {
86 handle = (smb_file *)file->handle;
87 if (smbc_close(handle->fd) != 0)
88 ret = -1;
89 g_free(file->handle);
90 }
91
92 return ret;
93 }
94
95 size_t
96 smb_vfs_fread_impl(gpointer ptr,
97 size_t size,
98 size_t nmemb,
99 VFSFile * file)
100 {
101 smb_file *handle;
102 if (file == NULL)
103 return 0;
104 handle = (smb_file *)file->handle;
105 return smbc_read(handle->fd, ptr, size * nmemb);
106 }
107
108 size_t
109 smb_vfs_fwrite_impl(gconstpointer ptr,
110 size_t size,
111 size_t nmemb,
112 VFSFile * file)
113 {
114 return 0;
115 }
116
117 gint
118 smb_vfs_getc_impl(VFSFile *file)
119 {
120 smb_file *handle;
121 char temp[2];
122 handle = (smb_file *)file->handle;
123 smbc_read(handle->fd, &temp, 1);
124 return (gint) temp[0];
125 }
126
127 gint
128 smb_vfs_fseek_impl(VFSFile * file,
129 glong offset,
130 gint whence)
131 {
132 smb_file *handle;
133 glong roffset = offset;
134 gint ret = 0;
135 if (file == NULL)
136 return 0;
137
138 handle = (smb_file *)file->handle;
139
140 if (whence == SEEK_END)
141 {
142 roffset = handle->length + offset;
143 if (roffset < 0)
144 roffset = 0;
145
146 ret = smbc_lseek(handle->fd, roffset, SEEK_SET);
147 //printf("%ld -> %ld = %d\n",offset, roffset, ret);
148 }
149 else
150 {
151 if (roffset < 0)
152 roffset = 0;
153 ret = smbc_lseek(handle->fd, roffset, whence);
154 //printf("%ld %d = %d\n",roffset, whence, ret);
155 if (ret == 0) ret = handle->length;
156 }
157
158 return ret;
159 }
160
161 gint
162 smb_vfs_ungetc_impl(gint c, VFSFile *file)
163 {
164 smb_vfs_fseek_impl(file, -1, SEEK_CUR);
165 return c;
166 }
167
168 void
169 smb_vfs_rewind_impl(VFSFile * file)
170 {
171 smb_vfs_fseek_impl(file, 0, SEEK_SET);
172 }
173
174 glong
175 smb_vfs_ftell_impl(VFSFile * file)
176 {
177 smb_file *handle;
178 handle = (smb_file *)file->handle;
179 return smbc_lseek(handle->fd, 0, SEEK_CUR);
180 }
181
182 gboolean
183 smb_vfs_feof_impl(VFSFile * file)
184 {
185 smb_file *handle;
186 off_t at;
187 handle = (smb_file *)file->handle;
188 at = smbc_lseek(handle->fd, 0, SEEK_CUR);
189 //printf("%d %d %ld %ld\n",sizeof(int), sizeof(off_t), at, handle->length);
190 return (gboolean) (at == handle->length) ? TRUE : FALSE;
191 }
192
193 gint
194 smb_vfs_truncate_impl(VFSFile * file, glong size)
195 {
196 return -1;
197 }
198
199 VFSConstructor smb_const = {
200 "smb://",
201 smb_vfs_fopen_impl,
202 smb_vfs_fclose_impl,
203 smb_vfs_fread_impl,
204 smb_vfs_fwrite_impl,
205 smb_vfs_getc_impl,
206 smb_vfs_ungetc_impl,
207 smb_vfs_fseek_impl,
208 smb_vfs_rewind_impl,
209 smb_vfs_ftell_impl,
210 smb_vfs_feof_impl,
211 smb_vfs_truncate_impl
212 };
213
214 static void init(void)
215 {
216 vfs_register_transport(&smb_const);
217 }
218
219 static void cleanup(void)
220 {
221 #if 0
222 vfs_unregister_transport(&smb_const);
223 #endif
224 }
225
226 LowlevelPlugin llp_smb = {
227 NULL,
228 NULL,
229 "smb:// URI Transport",
230 init,
231 cleanup,
232 };
233
234 LowlevelPlugin *get_lplugin_info(void)
235 {
236 return &llp_smb;
237 }
238