Mercurial > audlegacy-plugins
annotate src/smb/smb.c @ 1751:dc83901850df
- Add license texts to the neon source
author | Ralf Ertzinger <ralf@skytale.net> |
---|---|
date | Wed, 19 Sep 2007 17:44:13 +0200 |
parents | 7d0062c2e2a4 |
children | 6acf1bda788b |
rev | line source |
---|---|
845 | 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 { | |
846
76983ca8178a
[svn] - Adjust indenting cause the previous indenting was painful
nazca
parents:
845
diff
changeset
|
35 /* Does absolutely nothing :P */ |
845 | 36 } |
37 | |
871 | 38 typedef struct _SMBFile { |
845 | 39 int fd; |
40 long length; | |
871 | 41 } SMBFile; |
845 | 42 |
871 | 43 /* TODO: make writing work. */ |
846
76983ca8178a
[svn] - Adjust indenting cause the previous indenting was painful
nazca
parents:
845
diff
changeset
|
44 VFSFile *smb_vfs_fopen_impl(const gchar * path, const gchar * mode) |
845 | 45 { |
846
76983ca8178a
[svn] - Adjust indenting cause the previous indenting was painful
nazca
parents:
845
diff
changeset
|
46 VFSFile *file; |
871 | 47 SMBFile *handle; |
846
76983ca8178a
[svn] - Adjust indenting cause the previous indenting was painful
nazca
parents:
845
diff
changeset
|
48 struct stat st; |
845 | 49 |
846
76983ca8178a
[svn] - Adjust indenting cause the previous indenting was painful
nazca
parents:
845
diff
changeset
|
50 if (!path || !mode) |
76983ca8178a
[svn] - Adjust indenting cause the previous indenting was painful
nazca
parents:
845
diff
changeset
|
51 return NULL; |
845 | 52 |
846
76983ca8178a
[svn] - Adjust indenting cause the previous indenting was painful
nazca
parents:
845
diff
changeset
|
53 file = g_new0(VFSFile, 1); |
871 | 54 handle = g_new0(SMBFile, 1); |
846
76983ca8178a
[svn] - Adjust indenting cause the previous indenting was painful
nazca
parents:
845
diff
changeset
|
55 handle->fd = smbc_open(path, O_RDONLY, 0); |
845 | 56 |
871 | 57 if (handle->fd < 0) { |
846
76983ca8178a
[svn] - Adjust indenting cause the previous indenting was painful
nazca
parents:
845
diff
changeset
|
58 g_free(file); |
76983ca8178a
[svn] - Adjust indenting cause the previous indenting was painful
nazca
parents:
845
diff
changeset
|
59 file = NULL; |
76983ca8178a
[svn] - Adjust indenting cause the previous indenting was painful
nazca
parents:
845
diff
changeset
|
60 } else { |
76983ca8178a
[svn] - Adjust indenting cause the previous indenting was painful
nazca
parents:
845
diff
changeset
|
61 smbc_stat(path,&st); |
76983ca8178a
[svn] - Adjust indenting cause the previous indenting was painful
nazca
parents:
845
diff
changeset
|
62 handle->length = st.st_size; |
76983ca8178a
[svn] - Adjust indenting cause the previous indenting was painful
nazca
parents:
845
diff
changeset
|
63 file->handle = (void *)handle; |
76983ca8178a
[svn] - Adjust indenting cause the previous indenting was painful
nazca
parents:
845
diff
changeset
|
64 } |
845 | 65 |
846
76983ca8178a
[svn] - Adjust indenting cause the previous indenting was painful
nazca
parents:
845
diff
changeset
|
66 return file; |
845 | 67 } |
68 | |
846
76983ca8178a
[svn] - Adjust indenting cause the previous indenting was painful
nazca
parents:
845
diff
changeset
|
69 gint smb_vfs_fclose_impl(VFSFile * file) |
845 | 70 { |
846
76983ca8178a
[svn] - Adjust indenting cause the previous indenting was painful
nazca
parents:
845
diff
changeset
|
71 gint ret = 0; |
871 | 72 SMBFile *handle; |
845 | 73 |
846
76983ca8178a
[svn] - Adjust indenting cause the previous indenting was painful
nazca
parents:
845
diff
changeset
|
74 if (file == NULL) |
76983ca8178a
[svn] - Adjust indenting cause the previous indenting was painful
nazca
parents:
845
diff
changeset
|
75 return -1; |
76983ca8178a
[svn] - Adjust indenting cause the previous indenting was painful
nazca
parents:
845
diff
changeset
|
76 |
76983ca8178a
[svn] - Adjust indenting cause the previous indenting was painful
nazca
parents:
845
diff
changeset
|
77 if (file->handle) |
76983ca8178a
[svn] - Adjust indenting cause the previous indenting was painful
nazca
parents:
845
diff
changeset
|
78 { |
871 | 79 handle = (SMBFile *)file->handle; |
846
76983ca8178a
[svn] - Adjust indenting cause the previous indenting was painful
nazca
parents:
845
diff
changeset
|
80 if (smbc_close(handle->fd) != 0) |
76983ca8178a
[svn] - Adjust indenting cause the previous indenting was painful
nazca
parents:
845
diff
changeset
|
81 ret = -1; |
76983ca8178a
[svn] - Adjust indenting cause the previous indenting was painful
nazca
parents:
845
diff
changeset
|
82 g_free(file->handle); |
76983ca8178a
[svn] - Adjust indenting cause the previous indenting was painful
nazca
parents:
845
diff
changeset
|
83 } |
845 | 84 |
846
76983ca8178a
[svn] - Adjust indenting cause the previous indenting was painful
nazca
parents:
845
diff
changeset
|
85 return ret; |
845 | 86 } |
87 | |
846
76983ca8178a
[svn] - Adjust indenting cause the previous indenting was painful
nazca
parents:
845
diff
changeset
|
88 size_t smb_vfs_fread_impl(gpointer ptr, size_t size, size_t nmemb, VFSFile * file) |
845 | 89 { |
871 | 90 SMBFile *handle; |
846
76983ca8178a
[svn] - Adjust indenting cause the previous indenting was painful
nazca
parents:
845
diff
changeset
|
91 if (file == NULL) |
76983ca8178a
[svn] - Adjust indenting cause the previous indenting was painful
nazca
parents:
845
diff
changeset
|
92 return 0; |
871 | 93 handle = (SMBFile *)file->handle; |
846
76983ca8178a
[svn] - Adjust indenting cause the previous indenting was painful
nazca
parents:
845
diff
changeset
|
94 return smbc_read(handle->fd, ptr, size * nmemb); |
845 | 95 } |
96 | |
846
76983ca8178a
[svn] - Adjust indenting cause the previous indenting was painful
nazca
parents:
845
diff
changeset
|
97 size_t smb_vfs_fwrite_impl(gconstpointer ptr, size_t size, size_t nmemb, VFSFile * file) |
845 | 98 { |
99 return 0; | |
100 } | |
101 | |
846
76983ca8178a
[svn] - Adjust indenting cause the previous indenting was painful
nazca
parents:
845
diff
changeset
|
102 gint smb_vfs_getc_impl(VFSFile *file) |
845 | 103 { |
871 | 104 SMBFile *handle; |
105 char temp; | |
106 handle = (SMBFile *)file->handle; | |
846
76983ca8178a
[svn] - Adjust indenting cause the previous indenting was painful
nazca
parents:
845
diff
changeset
|
107 smbc_read(handle->fd, &temp, 1); |
871 | 108 return (gint) temp; |
845 | 109 } |
110 | |
846
76983ca8178a
[svn] - Adjust indenting cause the previous indenting was painful
nazca
parents:
845
diff
changeset
|
111 gint smb_vfs_fseek_impl(VFSFile * file, glong offset, gint whence) |
845 | 112 { |
871 | 113 SMBFile *handle; |
846
76983ca8178a
[svn] - Adjust indenting cause the previous indenting was painful
nazca
parents:
845
diff
changeset
|
114 glong roffset = offset; |
76983ca8178a
[svn] - Adjust indenting cause the previous indenting was painful
nazca
parents:
845
diff
changeset
|
115 gint ret = 0; |
845 | 116 if (file == NULL) |
846
76983ca8178a
[svn] - Adjust indenting cause the previous indenting was painful
nazca
parents:
845
diff
changeset
|
117 return 0; |
845 | 118 |
871 | 119 handle = (SMBFile *)file->handle; |
845 | 120 |
846
76983ca8178a
[svn] - Adjust indenting cause the previous indenting was painful
nazca
parents:
845
diff
changeset
|
121 if (whence == SEEK_END) |
76983ca8178a
[svn] - Adjust indenting cause the previous indenting was painful
nazca
parents:
845
diff
changeset
|
122 { |
76983ca8178a
[svn] - Adjust indenting cause the previous indenting was painful
nazca
parents:
845
diff
changeset
|
123 roffset = handle->length + offset; |
76983ca8178a
[svn] - Adjust indenting cause the previous indenting was painful
nazca
parents:
845
diff
changeset
|
124 if (roffset < 0) |
76983ca8178a
[svn] - Adjust indenting cause the previous indenting was painful
nazca
parents:
845
diff
changeset
|
125 roffset = 0; |
76983ca8178a
[svn] - Adjust indenting cause the previous indenting was painful
nazca
parents:
845
diff
changeset
|
126 |
76983ca8178a
[svn] - Adjust indenting cause the previous indenting was painful
nazca
parents:
845
diff
changeset
|
127 ret = smbc_lseek(handle->fd, roffset, SEEK_SET); |
76983ca8178a
[svn] - Adjust indenting cause the previous indenting was painful
nazca
parents:
845
diff
changeset
|
128 //printf("%ld -> %ld = %d\n",offset, roffset, ret); |
76983ca8178a
[svn] - Adjust indenting cause the previous indenting was painful
nazca
parents:
845
diff
changeset
|
129 } |
76983ca8178a
[svn] - Adjust indenting cause the previous indenting was painful
nazca
parents:
845
diff
changeset
|
130 else |
76983ca8178a
[svn] - Adjust indenting cause the previous indenting was painful
nazca
parents:
845
diff
changeset
|
131 { |
76983ca8178a
[svn] - Adjust indenting cause the previous indenting was painful
nazca
parents:
845
diff
changeset
|
132 if (roffset < 0) |
76983ca8178a
[svn] - Adjust indenting cause the previous indenting was painful
nazca
parents:
845
diff
changeset
|
133 roffset = 0; |
76983ca8178a
[svn] - Adjust indenting cause the previous indenting was painful
nazca
parents:
845
diff
changeset
|
134 ret = smbc_lseek(handle->fd, roffset, whence); |
76983ca8178a
[svn] - Adjust indenting cause the previous indenting was painful
nazca
parents:
845
diff
changeset
|
135 //printf("%ld %d = %d\n",roffset, whence, ret); |
76983ca8178a
[svn] - Adjust indenting cause the previous indenting was painful
nazca
parents:
845
diff
changeset
|
136 } |
845 | 137 |
846
76983ca8178a
[svn] - Adjust indenting cause the previous indenting was painful
nazca
parents:
845
diff
changeset
|
138 return ret; |
845 | 139 } |
140 | |
846
76983ca8178a
[svn] - Adjust indenting cause the previous indenting was painful
nazca
parents:
845
diff
changeset
|
141 gint smb_vfs_ungetc_impl(gint c, VFSFile *file) |
845 | 142 { |
846
76983ca8178a
[svn] - Adjust indenting cause the previous indenting was painful
nazca
parents:
845
diff
changeset
|
143 smb_vfs_fseek_impl(file, -1, SEEK_CUR); |
845 | 144 return c; |
145 } | |
146 | |
846
76983ca8178a
[svn] - Adjust indenting cause the previous indenting was painful
nazca
parents:
845
diff
changeset
|
147 void smb_vfs_rewind_impl(VFSFile * file) |
845 | 148 { |
846
76983ca8178a
[svn] - Adjust indenting cause the previous indenting was painful
nazca
parents:
845
diff
changeset
|
149 smb_vfs_fseek_impl(file, 0, SEEK_SET); |
845 | 150 } |
151 | |
152 glong | |
153 smb_vfs_ftell_impl(VFSFile * file) | |
154 { | |
871 | 155 SMBFile *handle; |
156 handle = (SMBFile *)file->handle; | |
846
76983ca8178a
[svn] - Adjust indenting cause the previous indenting was painful
nazca
parents:
845
diff
changeset
|
157 return smbc_lseek(handle->fd, 0, SEEK_CUR); |
845 | 158 } |
159 | |
160 gboolean | |
161 smb_vfs_feof_impl(VFSFile * file) | |
162 { | |
871 | 163 SMBFile *handle; |
845 | 164 off_t at; |
871 | 165 |
166 at = smb_vfs_ftell_impl(file); | |
167 | |
846
76983ca8178a
[svn] - Adjust indenting cause the previous indenting was painful
nazca
parents:
845
diff
changeset
|
168 //printf("%d %d %ld %ld\n",sizeof(int), sizeof(off_t), at, handle->length); |
845 | 169 return (gboolean) (at == handle->length) ? TRUE : FALSE; |
170 } | |
171 | |
172 gint | |
173 smb_vfs_truncate_impl(VFSFile * file, glong size) | |
174 { | |
175 return -1; | |
176 } | |
177 | |
965 | 178 off_t |
179 smb_vfs_fsize_impl(VFSFile * file) | |
180 { | |
971 | 181 SMBFile *handle = (SMBFile *)file->handle; |
182 | |
183 return handle->length; | |
965 | 184 } |
185 | |
845 | 186 VFSConstructor smb_const = { |
187 "smb://", | |
188 smb_vfs_fopen_impl, | |
189 smb_vfs_fclose_impl, | |
190 smb_vfs_fread_impl, | |
191 smb_vfs_fwrite_impl, | |
192 smb_vfs_getc_impl, | |
193 smb_vfs_ungetc_impl, | |
194 smb_vfs_fseek_impl, | |
195 smb_vfs_rewind_impl, | |
196 smb_vfs_ftell_impl, | |
197 smb_vfs_feof_impl, | |
965 | 198 smb_vfs_truncate_impl, |
199 smb_vfs_fsize_impl | |
845 | 200 }; |
201 | |
202 static void init(void) | |
203 { | |
871 | 204 int err; |
205 | |
206 err = smbc_init(smb_auth_fn, 1); | |
872
ad00b785dec0
[svn] - Slight tweak ... smbc_init returns 0 on sucess, not fail
nazca
parents:
871
diff
changeset
|
207 if (err < 0) |
871 | 208 { |
209 g_message("[smb] not starting samba support due to error code %d", err); | |
210 return; | |
211 } | |
212 | |
845 | 213 vfs_register_transport(&smb_const); |
214 } | |
215 | |
216 static void cleanup(void) | |
217 { | |
218 #if 0 | |
219 vfs_unregister_transport(&smb_const); | |
220 #endif | |
221 } | |
222 | |
223 LowlevelPlugin llp_smb = { | |
224 NULL, | |
225 NULL, | |
226 "smb:// URI Transport", | |
227 init, | |
228 cleanup, | |
229 }; | |
230 | |
231 LowlevelPlugin *get_lplugin_info(void) | |
232 { | |
233 return &llp_smb; | |
234 } | |
235 |