Browse Source

Adds accessors function for smb_stat[_list]_t objects

Julien 'Lta' BALLET 11 years ago
parent
commit
7dd1c745e7

+ 31 - 30
Makefile.am

@@ -15,45 +15,46 @@ endif
 bdsmdir = $(includedir)/bdsm
 
 bdsm_HEADERS = \
-    include/bdsm.h			\
+    include/bdsm.h      \
     include/bdsm/debug.h    \
-    include/bdsm/netbios_defs.h		\
-    include/bdsm/netbios_query.h	\
-    include/bdsm/netbios_utils.h	\
-    include/bdsm/smb_file.h		\
-    include/bdsm/smb_ntlm.h		\
-    include/bdsm/smb_session.h    \
+    include/bdsm/hmac_md5.h   \
+    include/bdsm/netbios_defs.h   \
+    include/bdsm/netbios_ns.h   \
+    include/bdsm/netbios_query.h  \
+    include/bdsm/netbios_session.h  \
+    include/bdsm/netbios_utils.h  \
+    include/bdsm/smb_defs.h   \
     include/bdsm/smb_fd.h    \
-    include/bdsm/smb_trans2.h		\
-    include/bdsm/hmac_md5.h		\
-    include/bdsm/netbios_ns.h		\
-    include/bdsm/netbios_session.h	\
-    include/bdsm/smb_defs.h		\
+    include/bdsm/smb_file.h   \
+    include/bdsm/smb_message.h    \
+    include/bdsm/smb_ntlm.h   \
+    include/bdsm/smb_packets.h    \
+    include/bdsm/smb_session.h    \
+    include/bdsm/smb_share.h    \
+    include/bdsm/smb_stat.h   \
     include/bdsm/smb_types.h    \
-    include/bdsm/smb_message.h		\
-    include/bdsm/smb_packets.h		\
-    include/bdsm/smb_share.h		\
     include/bdsm/smb_utils.h
 
 lib_LTLIBRARIES = libdsm.la
 
 libdsm_la_SOURCES = \
-    contrib/mdx/md5.c		\
-    contrib/mdx/md4.c		\
-    src/hmac_md5.c		\
-    src/netbios_utils.c		\
-    src/netbios_ns.c		\
-    src/netbios_ns_entry.c	\
-    src/netbios_query.c		\
-    src/netbios_session.c	\
-    src/smb_utils.c		\
-    src/smb_message.c		\
-    src/smb_session.c   \
+    contrib/mdx/md4.c   \
+    contrib/mdx/md5.c   \
+    src/hmac_md5.c    \
+    src/netbios_ns.c    \
+    src/netbios_ns_entry.c  \
+    src/netbios_query.c   \
+    src/netbios_session.c \
+    src/netbios_utils.c   \
     src/smb_fd.c   \
-    src/smb_ntlm.c		\
-    src/smb_share.c		\
-    src/smb_file.c		\
-    src/smb_trans2.c
+    src/smb_file.c    \
+    src/smb_message.c   \
+    src/smb_ntlm.c    \
+    src/smb_session.c   \
+    src/smb_share.c   \
+    src/smb_stat.c    \
+    src/smb_trans2.c  \
+    src/smb_utils.c
 
 pkgconfigdir = $(libdir)/pkgconfig
 pkgconfig_DATA = libdsm.pc

+ 1 - 1
include/bdsm.h

@@ -28,6 +28,6 @@
 #include "bdsm/smb_session.h"
 #include "bdsm/smb_share.h"
 #include "bdsm/smb_file.h"
-#include "bdsm/smb_trans2.h"
+#include "bdsm/smb_stat.h"
 
 #endif

+ 1 - 1
include/bdsm/netbios_ns.h

@@ -82,7 +82,7 @@ typedef struct {
   int                 socket;
   struct sockaddr_in  addr;
   uint16_t            last_trn_id;  // Last transaction id used;
-  netbios_ns_entry_t  *entries;     // Only used during discovery;
+  netbios_ns_entry_t  *entries;     // NS entries cache, mainly used by discover()
 }                   netbios_ns_t;
 
 /**

+ 1 - 0
include/bdsm/smb_defs.h

@@ -41,6 +41,7 @@
 /// Error state, there was an error somewhere
 #define SMB_STATE_ERROR         -1
 
+
 //-----------------------------------------------------------------------------/
 // smb_fseek() operations
 //-----------------------------------------------------------------------------/

+ 124 - 0
include/bdsm/smb_stat.h

@@ -0,0 +1,124 @@
+//---------------------------------------------------------------------------
+//  __________________    _________  _____            _____  .__         ._.
+//  \______   \______ \  /   _____/ /     \          /  _  \ |__| ____   | |
+//   |    |  _/|    |  \ \_____  \ /  \ /  \        /  /_\  \|  _/ __ \  | |
+//   |    |   \|    `   \/        /    Y    \      /    |    |  \  ___/   \|
+//   |______  /_______  /_______  \____|__  / /\   \____|__  |__|\___ |   __
+//          \/        \/        \/        \/  )/           \/        \/   \/
+//
+// This file is part of libdsm. Copyright © 2014 VideoLabs SAS
+//
+// Author: Julien 'Lta' BALLET <contact@lta.io>
+//
+// This program is free software. It comes without any warranty, to the extent
+// permitted by applicable law. You can redistribute it and/or modify it under
+// the terms of the Do What The Fuck You Want To Public License, Version 2, as
+// published by Sam Hocevar. See the COPYING file for more details.
+//----------------------------------------------------------------------------
+
+/**
+ * @file smb_stat.h
+ * @brief File status / listing
+ */
+
+#ifndef __BDSM_SMB_TRANS2_H_
+#define __BDSM_SMB_TRANS2_H_
+
+#include "bdsm/smb_defs.h"
+#include "bdsm/smb_session.h"
+
+/// smb_stat_get() OP: Get file size
+#define SMB_STAT_SIZE         0
+/// smb_stat_get() OP: Get file allocation size (Size on disk)
+#define SMB_STAT_ALLOC_SIZE   1
+/// smb_stat_get() OP: 0 -> not a directory, != 0 -> directory
+#define SMB_STAT_ISDIR        2
+/// smb_stat_get() OP: Get file creation time
+#define SMB_STAT_CTIME        3
+/// smb_stat_get() OP: Get file last access time
+#define SMB_STAT_ATIME        4
+/// smb_stat_get() OP: Get file last write time
+#define SMB_STAT_WTIME        5
+/// smb_stat_get() OP: Get file last moditification time
+#define SMB_STAT_MTIME        6
+
+/**
+ * @brief Returns infos about files matching a pattern
+ * @details This functions uses the FIND_FIRST2 SMB operations to list files
+ * matching a certain pattern. It's basically used to list folder contents
+ *
+ * @param s The session object
+ * @param tid The share inside of which we want to find files obtained by
+ * smb_tree_connect()
+ * @param pattern The pattern to match files. '\\*' will list all the files at
+ * the root of the share. '\\afolder\\*' will list all the files inside of the
+ * 'afolder' directory.
+ * @return An opaque list of smb_stat_t or NULL in case of error
+ */
+smb_stat_list_t   smb_find(smb_session_t *s, smb_tid tid, const char *pattern);
+
+/**
+ * @brief Get the status of a file from it's path inside of a share
+ *
+ * @param s The session object
+ * @param tid The tree id of a share obtained by smb_tree_connect()
+ * @param path The full path of the file relative to the root of the share
+ * (e.g. '\\folder\\file.ext')
+ * @return An opaque smb_stat_t or NULL in case of error
+ */
+smb_stat_t        smb_stat(smb_session_t *s, smb_tid tid, const char *path);
+
+/**
+ * @brief Get the status of an open file from it's file descriptor
+ * @details The file status will be those at the time of open
+ *
+ * @param s The session object
+ * @param fd The smb_fd from which you want infos/status
+ *
+ * @return  An opaque smb_stat_t or NULL in case of error
+ */
+smb_stat_t        smb_stat_fd(smb_session_t *s, smb_fd fd);
+
+/**
+ * @brief Get the number of item in a smb_stat_list_t file info
+ *
+ * @param list The list you want the length of
+ * @return The length of the list. It returns 0 if the list is invalid
+ */
+size_t            smb_stat_list_count(smb_stat_list_t list);
+
+/**
+ * @brief Get the element at the given position.
+ *
+ * @param list A stat list
+ * @param index The position of the element you want.
+ *
+ * @return An opaque smb_stat_t or NULL in case of error
+ */
+smb_stat_t        smb_stat_list_at(smb_stat_list_t list, size_t index);
+
+/**
+ * @brief Get the name of the file from its status
+ *
+ * @param info A file status
+ * @return A null-terminated string in you current locale encoding or NULL.
+ */
+const char        *smb_stat_name(smb_stat_t info);
+
+/**
+ * @brief Get a file attribute
+ * @details This function is a getter that allow you to retrieve various
+ * informations about a file on a smb_stat_t object. You can get its size,
+ * various timestamps, etc.
+ *
+ * @param info The smb_stat_t object to get info from.
+ * @param what This parameter tells the functions which information to get, can
+ * be one of #SMB_STAT_SIZE, #SMB_STAT_ALLOC_SIZE, #SMB_STAT_ISDIR,
+ * #SMB_STAT_CTIME, #SMB_STAT_ATIME, #SMB_STAT_MTIME, #SMB_STAT_WTIME.
+ *
+ * @return The meaning of the returned value depends on the 'what' parameter.
+ * See each item documentation.
+ */
+uint64_t          smb_stat_get(smb_stat_t info, int what);
+
+#endif

+ 0 - 56
include/bdsm/smb_trans2.h

@@ -1,56 +0,0 @@
-//---------------------------------------------------------------------------
-//  __________________    _________  _____            _____  .__         ._.
-//  \______   \______ \  /   _____/ /     \          /  _  \ |__| ____   | |
-//   |    |  _/|    |  \ \_____  \ /  \ /  \        /  /_\  \|  _/ __ \  | |
-//   |    |   \|    `   \/        /    Y    \      /    |    |  \  ___/   \|
-//   |______  /_______  /_______  \____|__  / /\   \____|__  |__|\___ |   __
-//          \/        \/        \/        \/  )/           \/        \/   \/
-//
-// This file is part of libdsm. Copyright © 2014 VideoLabs SAS
-//
-// Author: Julien 'Lta' BALLET <contact@lta.io>
-//
-// This program is free software. It comes without any warranty, to the extent
-// permitted by applicable law. You can redistribute it and/or modify it under
-// the terms of the Do What The Fuck You Want To Public License, Version 2, as
-// published by Sam Hocevar. See the COPYING file for more details.
-//----------------------------------------------------------------------------
-
-/**
- * @file smb_trans2.h
- * @brief SMB transactions operations: find(i.e. ls), stat
- */
-
-#ifndef __BDSM_SMB_TRANS2_H_
-#define __BDSM_SMB_TRANS2_H_
-
-#include "bdsm/smb_defs.h"
-#include "bdsm/smb_session.h"
-
-/**
- * @brief Returns infos about files matching a pattern
- * @details This functions uses the FIND_FIRST2 SMB operations to list files
- * matching a certain pattern. It's basically used to list folder contents
- *
- * @param s The session object
- * @param tid The share inside of which we want to find files obtained by
- * smb_tree_connect()
- * @param pattern The pattern to match files. '\\*' will list all the files at
- * the root of the share. '\\afolder\\*' will list all the files inside of the
- * 'afolder' directory.
- * @return A list of stat opaque object containing file status
- */
-smb_stat_list_t   smb_find(smb_session_t *s, smb_tid tid, const char *pattern);
-
-/**
- * @brief Get the status of a file from it's path inside of a share
- *
- * @param s The session object
- * @param tid The tree id of a share obtained by smb_tree_connect()
- * @param path The full path of the file relative to the root of the share
- * (e.g. '\\folder\\file.ext')
- * @return An opaque smb_stat_t file representing a file status (informations)
- */
-smb_stat_t        smb_stat(smb_session_t *s, smb_tid tid, const char *path);
-
-#endif

+ 2 - 2
include/bdsm/smb_types.h

@@ -17,8 +17,8 @@
 //----------------------------------------------------------------------------
 
 /**
- * @file smb_trans2.h
- * @brief SMB transactions operations: find(i.e. ls), stat
+ * @file smb_types.h
+ * @brief liBDSM types and structures
  */
 
 #ifndef __BDSM_SMB_TYPES_H_

+ 87 - 0
src/smb_stat.c

@@ -0,0 +1,87 @@
+//---------------------------------------------------------------------------
+//  __________________    _________  _____            _____  .__         ._.
+//  \______   \______ \  /   _____/ /     \          /  _  \ |__| ____   | |
+//   |    |  _/|    |  \ \_____  \ /  \ /  \        /  /_\  \|  _/ __ \  | |
+//   |    |   \|    `   \/        /    Y    \      /    |    |  \  ___/   \|
+//   |______  /_______  /_______  \____|__  / /\   \____|__  |__|\___ |   __
+//          \/        \/        \/        \/  )/           \/        \/   \/
+//
+// This file is part of libdsm. Copyright © 2014 VideoLabs SAS
+//
+// Author: Julien 'Lta' BALLET <contact@lta.io>
+//
+// This program is free software. It comes without any warranty, to the extent
+// permitted by applicable law. You can redistribute it and/or modify it under
+// the terms of the Do What The Fuck You Want To Public License, Version 2, as
+// published by Sam Hocevar. See the COPYING file for more details.
+//----------------------------------------------------------------------------
+
+#include <assert.h>
+
+#include "bdsm/smb_stat.h"
+#include "bdsm/smb_fd.h"
+
+smb_stat_t        smb_stat_fd(smb_session_t *s, smb_fd fd)
+{
+  assert (s != NULL && fd);
+
+  return (smb_session_file_get(s, fd));
+}
+
+size_t            smb_stat_list_count(smb_stat_list_t list)
+{
+  size_t          count = 0;
+
+  while(list != NULL)
+  {
+    list = list->next;
+    ++count;
+  }
+
+  return (count);
+}
+
+smb_stat_t        smb_stat_list_at(smb_stat_list_t list, size_t index)
+{
+  size_t          pos;
+
+  while(list != NULL && pos < index)
+  {
+    list = list->next;
+    pos++;
+  }
+
+  return (list);
+}
+
+const char        *smb_stat_name(smb_stat_t info)
+{
+  if (info == NULL)
+    return (NULL);
+  else
+    return (info->name);
+}
+
+uint64_t          smb_stat_get(smb_stat_t info, int what)
+{
+  if (info == NULL)
+    return (0);
+
+  switch (what)
+  {
+    case SMB_STAT_SIZE:
+      return(info->size);
+    case SMB_STAT_ALLOC_SIZE:
+      return(info->alloc_size);
+    case SMB_STAT_CTIME:
+      return(info->created);
+    case SMB_STAT_ATIME:
+      return(info->accessed);
+    case SMB_STAT_WTIME:
+      return(info->written);
+    case SMB_STAT_MTIME:
+      return(info->changed);
+    default:
+      return (0);
+  }
+}

+ 1 - 1
src/smb_trans2.c

@@ -23,7 +23,7 @@
 
 #include "bdsm/debug.h"
 #include "bdsm/smb_utils.h"
-#include "bdsm/smb_trans2.h"
+#include "bdsm/smb_stat.h"
 
 static smb_file_t *smb_find_parse(smb_message_t *msg)
 {