Implemented Samba contexts. Added -Werror to CFLAGS. Forced -f in debug build. - susmb - mounting of SMB/CIFS shares via FUSE
(HTM) git clone git://git.codemadness.org/susmb
(DIR) Log
(DIR) Files
(DIR) Refs
(DIR) README
(DIR) LICENSE
---
(DIR) commit 23d458c666083432ecb49a694ded6372b131e03b
(DIR) parent c8dd66a8ee51aaa97ad616e5a0b619fad341b9dd
(HTM) Author: geoff <devnull@localhost>
Date: Sat, 8 Jul 2006 20:15:57 +0000
Implemented Samba contexts.
Added -Werror to CFLAGS.
Forced -f in debug build.
Diffstat:
M Makefile | 2 +-
M options.c | 3 +++
M usmb.c | 40 +++++++++++++++++++++++++------
M usmb.h | 16 ++++++++++++++++
M usmb_dir.c | 26 +++++++++++++++-----------
M usmb_file.c | 73 +++++++++++++++++++------------
6 files changed, 112 insertions(+), 48 deletions(-)
---
(DIR) diff --git a/Makefile b/Makefile
@@ -23,7 +23,7 @@ INSTALL = install
PREFIX = /usr/local
BINDIR = $(PREFIX)/bin
-CFLAGS = -Wall -W -std=c99 -pedantic -O \
+CFLAGS = -Wall -W -Werror -std=c99 -pedantic -O \
-I$(SAMBA)/include -D_BSD_SOURCE -DFUSE_USE_VERSION=25 \
-DHAVE_UTIME_H
(DIR) diff --git a/options.c b/options.c
@@ -147,7 +147,10 @@ void build_fuse_args (char *options, char *mountpoint,
if (debug)
argv[argc++] = "-d";
+ // force -f in debug mode
+#ifndef DEBUG
if (nofork)
+#endif
argv[argc++] = "-f";
argv[argc++] = "-o";
(DIR) diff --git a/usmb.c b/usmb.c
@@ -40,6 +40,7 @@
#include "version.h"
+SMBCCTX *ctx;
static const char *share = NULL;
@@ -61,9 +62,9 @@ static inline void do_strncpy (char *to, const char *from, int tolen)
}
-static const char *domain = NULL;
-static const char *username = NULL;
-static const char *password = NULL;
+static char *domain = NULL;
+static char *username = NULL;
+static char *password = NULL;
static void auth_fn (const char *srv, const char *shr, char *wg, int wglen,
char *un, int unlen, char *pw, int pwlen)
@@ -82,6 +83,32 @@ static void auth_fn (const char *srv, const char *shr, char *wg, int wglen,
}
+static bool create_smb_context (char *domain, char *username, SMBCCTX **pctx)
+{
+ *pctx = smbc_new_context();
+
+ if (NULL == *pctx)
+ {
+ perror ("Cannot create SMB context");
+ return false;
+ }
+
+ (*pctx)->workgroup = domain;
+ (*pctx)->user = username;
+ (*pctx)->timeout = 5000;
+ (*pctx)->callbacks.auth_fn = auth_fn;
+
+ if (NULL == smbc_init_context (*pctx))
+ {
+ perror ("Cannot initialise SMB context");
+ smbc_free_context (*pctx, 1);
+ return false;
+ }
+
+ return true;
+}
+
+
static void * usmb_init (void)
{
DEBUG (fputs ("usmb_init()\n", stderr));
@@ -270,11 +297,8 @@ int main (int argc, char **argv)
return EXIT_FAILURE;
}
- if (0 != smbc_init (auth_fn, 0))
- {
- perror ("Cannot initialise SMB library");
+ if (!create_smb_context (domain, username, &ctx))
return EXIT_FAILURE;
- }
show_about (stdout);
@@ -283,6 +307,8 @@ int main (int argc, char **argv)
build_fuse_args (options, mountpoint, &fuse_argc, &fuse_argv);
int ret = fuse_main (fuse_argc, fuse_argv, &fuse_ops);
+ smbc_free_context (ctx, 1);
+
xfree (mountpoint);
xfree (options);
xfree (domain);
(DIR) diff --git a/usmb.h b/usmb.h
@@ -19,6 +19,22 @@
#ifndef USMB_H
#define USMB_H
+ #include <libsmbclient.h>
+
+ extern SMBCCTX *ctx;
+
char * make_url (const char *path);
+
+ static inline int smbcfile_to_fd (SMBCFILE *file)
+ {
+ return (NULL == file) ? -1 : (int)file;
+ }
+
+
+ static inline SMBCFILE * fd_to_smbcfile (int fd)
+ {
+ return (SMBCFILE *)fd;
+ }
+
#endif
(DIR) diff --git a/usmb_dir.c b/usmb_dir.c
@@ -38,7 +38,7 @@ int usmb_mkdir (const char *dirname, mode_t mode)
return -ENOMEM;
DEBUG (fprintf (stderr, "mkdir (%s)\n", url));
- int ret = smbc_mkdir (url, mode) ? -errno : 0;
+ int ret = ctx->mkdir (ctx, url, mode) ? -errno : 0;
free (url);
return ret;
}
@@ -51,7 +51,7 @@ int usmb_rmdir (const char *dirname)
return -ENOMEM;
DEBUG (fprintf (stderr, "rmdir (%s)\n", url));
- int ret = smbc_rmdir (url) ? -errno : 0;
+ int ret = ctx->rmdir (ctx, url) ? -errno : 0;
free (url);
return ret;
}
@@ -64,12 +64,13 @@ int usmb_opendir (const char *dirname, struct fuse_file_info *fi)
return -ENOMEM;
DEBUG (fprintf (stderr, "opendir (%s)", url));
- int fd = smbc_opendir (url);
- fi->fh = fd;
- DEBUG (fprintf (stderr, " = %d\n", fd));
+ SMBCFILE *file = ctx->opendir (ctx, url);
+ DEBUG (fprintf (stderr, " = %p\n", (void *)file));
- int ret = (fd < 0) ? -errno : 0;
+ int ret = (file == NULL) ? -errno : 0;
free (url);
+ fi->fh = smbcfile_to_fd (file);
+
return ret;
}
@@ -80,11 +81,13 @@ int usmb_readdir (const char *path, void *h, fuse_fill_dir_t filler,
(void)path;
(void)offset;
- DEBUG (fprintf (stderr, "readdir (%s, %llu)\n", path, fi->fh));
-
struct smbc_dirent *dirent;
errno = 0;
- while (NULL != (dirent = smbc_readdir (fi->fh)))
+ SMBCFILE *file = fd_to_smbcfile (fi->fh);
+
+ DEBUG (fprintf (stderr, "readdir (%s, %p)\n", path, (void *)file));
+
+ while (NULL != (dirent = ctx->readdir (ctx, file)))
{
struct stat stbuf;
@@ -118,7 +121,8 @@ int usmb_releasedir (const char *path, struct fuse_file_info *fi)
{
(void)path;
- DEBUG (fprintf (stderr, "releasedir (%s, %llu)\n", path, fi->fh));
- return (smbc_closedir (fi->fh) < 0) ? -errno : 0;
+ SMBCFILE *file = fd_to_smbcfile (fi->fh);
+ DEBUG (fprintf (stderr, "releasedir (%s, %p)\n", path, (void *)file));
+ return (ctx->closedir (ctx, file) < 0) ? -errno : 0;
}
(DIR) diff --git a/usmb_file.c b/usmb_file.c
@@ -35,7 +35,7 @@ int usmb_getattr (const char *filename, struct stat *st)
char *url = make_url (filename);
DEBUG (fprintf (stderr, "stat (%s)\n", url));
- int ret = smbc_stat (url, st);
+ int ret = ctx->stat (ctx, url, st);
free (url);
return (ret < 0) ? -errno : 0;
}
@@ -45,8 +45,9 @@ int usmb_fgetattr (const char *filename, struct stat *st,
struct fuse_file_info *fi)
{
(void)filename;
- DEBUG (fprintf (stderr, "fgetattr (%s, %llu)\n", filename, fi->fh));
- return (smbc_fstat (fi->fh, st) < 0) ? -errno : 0;
+ SMBCFILE *file = fd_to_smbcfile (fi->fh);
+ DEBUG (fprintf (stderr, "fgetattr (%s, %p)\n", filename, (void *)file));
+ return (ctx->fstat (ctx, file, st) < 0) ? -errno : 0;
}
@@ -57,7 +58,7 @@ int usmb_unlink (const char *filename)
return -ENOMEM;
DEBUG (fprintf (stderr, "unlink (%s)\n", url));
- int ret = (smbc_unlink (url) < 0) ? -errno : 0;
+ int ret = (ctx->unlink (ctx, url) < 0) ? -errno : 0;
free (url);
return ret;
}
@@ -70,13 +71,13 @@ int usmb_open (const char *filename, struct fuse_file_info *fi)
return -ENOMEM;
DEBUG (fprintf (stderr, "open (%s, %d)", url, fi->flags));
-
- int fd = smbc_open (url, fi->flags, 0);
- int ret = (fd < 0) ? -errno : 0;
- fi->fh = fd;
- DEBUG (fprintf (stderr, " = %d\n", fd));
+ SMBCFILE *file = ctx->open (ctx, url, fi->flags, 0);
+ DEBUG (fprintf (stderr, " = %p\n", (void *)file));
free (url);
+ int ret = (NULL == file) ? -errno : 0;
+ fi->fh = smbcfile_to_fd (file);
+
return ret;
}
@@ -84,8 +85,9 @@ int usmb_open (const char *filename, struct fuse_file_info *fi)
int usmb_release (const char *filename, struct fuse_file_info *fi)
{
(void)filename;
- DEBUG (fprintf (stderr, "release (%s, %llu)\n", filename, fi->fh));
- return (smbc_close (fi->fh) < 0) ? -errno : 0;
+ SMBCFILE *file = fd_to_smbcfile (fi->fh);
+ DEBUG (fprintf (stderr, "release (%s, %p)\n", filename, (void *)file));
+ return (ctx->close_fn (ctx, file) < 0) ? -errno : 0;
}
@@ -97,15 +99,18 @@ int usmb_read (const char *filename, char *buff, size_t len, off_t off,
assert (len <= 32768);
- DEBUG (fprintf (stderr, "read (%p, %u, %lld) ", buff, len, off));
+ SMBCFILE *file = fd_to_smbcfile (fi->fh);
+ DEBUG (fprintf (stderr, "read (%p, %p, %u, %lld) ",
+ (void *)file, buff, len, off));
- if (smbc_lseek (fi->fh, off, SEEK_SET) < 0)
+ if (ctx->lseek (ctx, file, off, SEEK_SET) < 0)
{
fprintf (stderr, "- seek failed: %d\n", -errno);
return -errno;
}
- int bytes = smbc_read (fi->fh, buff, len);
+ int bytes = ctx->read (ctx, file, buff, len);
+ DEBUG (fprintf (stderr, "= %d\n", bytes));
return (bytes < 0) ? -errno : (int)bytes;
}
@@ -116,10 +121,15 @@ int usmb_write (const char *filename, const char *buff, size_t len, off_t off,
(void)filename;
(void)off;
- DEBUG (fprintf (stderr, "write (%p, len=%u, off=%lld)\n", buff, len, off));
+ SMBCFILE *file = fd_to_smbcfile (fi->fh);
+ DEBUG (fprintf (stderr, "write (%p, %p, len=%u, off=%lld) ",
+ (void *)file, buff, len, off));
- if (smbc_lseek (fi->fh, off, SEEK_SET) < 0)
+ if (ctx->lseek (ctx, file, off, SEEK_SET) < 0)
+ {
+ fprintf (stderr, "- seek failed: %d\n", -errno);
return -errno;
+ }
size_t written = 0;
int bytes = 0;
@@ -128,7 +138,7 @@ int usmb_write (const char *filename, const char *buff, size_t len, off_t off,
// (cf. usmb_read), but taking no chances...
while (written < len)
{
- bytes = smbc_write (fi->fh, (char *)buff, (len > 32768) ? 32768 : len);
+ bytes = ctx->write (ctx, file, (char *)buff, (len > 32768) ? 32768 : len);
if (bytes < 0)
break;
@@ -141,7 +151,7 @@ int usmb_write (const char *filename, const char *buff, size_t len, off_t off,
break;
}
- DEBUG (fprintf (stderr, " = %d\n", (bytes < 0) ? -errno : (int)written));
+ DEBUG (fprintf (stderr, "= %d\n", (bytes < 0) ? -errno : (int)written));
return (bytes < 0) ? -errno : (int)written;
}
@@ -154,10 +164,10 @@ int usmb_create (const char *filename, mode_t mode, struct fuse_file_info *fi)
DEBUG (fprintf (stderr, "creat (%s)", url));
- int fd = smbc_creat (url, mode);
- int ret = (fd < 0) ? -errno : 0;
- fi->fh = fd;
- DEBUG (fprintf (stderr, " = %d\n", fd));
+ SMBCFILE *file = ctx->creat (ctx, url, mode);
+ DEBUG (fprintf (stderr, " = %p\n", (void *)file));
+ int ret = (NULL == file) ? -errno : 0;
+ fi->fh = smbcfile_to_fd (file);
free (url);
return ret;
@@ -178,7 +188,7 @@ int usmb_rename (const char *from, const char *to)
}
DEBUG (fprintf (stderr, "rename (%s, %s)\n", fromurl, tourl));
- int ret = (smbc_rename (fromurl, tourl) < 0) ? -errno : 0;
+ int ret = (ctx->rename (ctx, fromurl, ctx, tourl) < 0) ? -errno : 0;
free (tourl);
free (fromurl);
return ret;
@@ -191,8 +201,13 @@ int usmb_utime (const char *filename, struct utimbuf *utb)
if (NULL == url)
return -ENOMEM;
+ struct timeval tv = {
+ .tv_sec = utb->modtime,
+ .tv_usec = 0
+ };
+
DEBUG (fprintf (stderr, "utime (%s)\n", url));
- int ret = (smbc_utime (url, utb) < 0) ? -errno : 0;
+ int ret = (ctx->utimes (ctx, url, &tv) < 0) ? -errno : 0;
free (url);
return ret;
}
@@ -210,11 +225,11 @@ int usmb_truncate (const char *filename, off_t newsize)
return -ENOMEM;
DEBUG (fprintf (stderr, "truncate (%s)\n", url));
- int fd = smbc_open (url, O_WRONLY | O_TRUNC, 0);
- if (fd < 0)
+ SMBCFILE *file = ctx->open (ctx, url, O_WRONLY | O_TRUNC, 0);
+ if (NULL == file)
return -errno;
- smbc_close (fd);
+ ctx->close_fn (ctx, file);
return 0;
}
@@ -226,7 +241,7 @@ int usmb_chmod (const char *filename, mode_t mode)
return -ENOMEM;
DEBUG (fprintf (stderr, "chmod (%s, %u)\n", url, mode));
- int ret = (smbc_chmod (url, mode) < 0) ? -errno : 0;
+ int ret = (ctx->chmod (ctx, url, mode) < 0) ? -errno : 0;
free (url);
return ret;
}
@@ -241,7 +256,7 @@ int usmb_chown (const char *filename, uid_t owner, uid_t group)
return -ENOMEM;
DEBUG (fprintf (stderr, "chown (%s, %d, %d)\n", url, owner, group));
- int ret = (smbc_chown (url, owner, group) < 0) ? -errno : 0;
+ int ret = (ctx->chown (ctx, url, owner, group) < 0) ? -errno : 0;
free (url);
return ret;
}