LCOV - code coverage report
Current view: top level - source4/ntvfs/simple - svfs_util.c (source / functions) Hit Total Coverage
Test: coverage report for master 2f515e9b Lines: 13 80 16.2 %
Date: 2024-04-21 15:09:00 Functions: 2 5 40.0 %

          Line data    Source code
       1             : /* 
       2             :    Unix SMB/CIFS implementation.
       3             : 
       4             :    simple NTVFS filesystem backend
       5             : 
       6             :    Copyright (C) Andrew Tridgell 2003
       7             : 
       8             :    This program is free software; you can redistribute it and/or modify
       9             :    it under the terms of the GNU General Public License as published by
      10             :    the Free Software Foundation; either version 3 of the License, or
      11             :    (at your option) any later version.
      12             :    
      13             :    This program is distributed in the hope that it will be useful,
      14             :    but WITHOUT ANY WARRANTY; without even the implied warranty of
      15             :    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      16             :    GNU General Public License for more details.
      17             :    
      18             :    You should have received a copy of the GNU General Public License
      19             :    along with this program.  If not, see <http://www.gnu.org/licenses/>.
      20             : */
      21             : /*
      22             :   utility functions for simple backend
      23             : */
      24             : 
      25             : #include "includes.h"
      26             : #include "system/filesys.h"
      27             : #include "svfs.h"
      28             : #include "system/time.h"
      29             : #include "system/dir.h"
      30             : #include "ntvfs/ntvfs.h"
      31             : #include "ntvfs/simple/proto.h"
      32             : 
      33             : /*
      34             :   convert a windows path to a unix path - don't do any mangling or case sensitive handling
      35             : */
      36           8 : char *svfs_unix_path(struct ntvfs_module_context *ntvfs,
      37             :                      struct ntvfs_request *req, const char *name)
      38             : {
      39           8 :         struct svfs_private *p = ntvfs->private_data;
      40           0 :         char *ret;
      41           8 :         char *name_lower = strlower_talloc(p, name);
      42             : 
      43           8 :         if (*name != '\\') {
      44           0 :                 ret = talloc_asprintf(req, "%s/%s", p->connectpath, name_lower);
      45             :         } else {
      46           8 :                 ret = talloc_asprintf(req, "%s%s", p->connectpath, name_lower);
      47             :         }
      48           8 :         all_string_sub(ret, "\\", "/", 0);
      49           8 :         talloc_free(name_lower);
      50           8 :         return ret;
      51             : }
      52             : 
      53             : 
      54             : /*
      55             :   read a directory and find all matching file names and stat info
      56             :   returned names are separate unix and DOS names. The returned names
      57             :   are relative to the directory
      58             : */
      59           0 : struct svfs_dir *svfs_list_unix(TALLOC_CTX *mem_ctx, struct ntvfs_request *req, const char *unix_path)
      60             : {
      61           0 :         char *p, *mask;
      62           0 :         struct svfs_dir *dir;
      63           0 :         DIR *odir;
      64           0 :         struct dirent *dent;
      65           0 :         unsigned int allocated = 0;
      66           0 :         char *low_mask;
      67             : 
      68           0 :         dir = talloc(mem_ctx, struct svfs_dir);
      69           0 :         if (!dir) { return NULL; }
      70             : 
      71           0 :         dir->count = 0;
      72           0 :         dir->files = 0;
      73             : 
      74             :         /* find the base directory */
      75           0 :         p = strrchr(unix_path, '/');
      76           0 :         if (!p) { return NULL; }
      77             : 
      78           0 :         dir->unix_dir = talloc_strndup(mem_ctx, unix_path, PTR_DIFF(p, unix_path));
      79           0 :         if (!dir->unix_dir) { return NULL; }
      80             : 
      81             :         /* the wildcard pattern is the last part */
      82           0 :         mask = p+1;
      83             : 
      84           0 :         low_mask = strlower_talloc(mem_ctx, mask);
      85           0 :         if (!low_mask) { return NULL; }
      86             : 
      87           0 :         odir = opendir(dir->unix_dir);
      88           0 :         if (!odir) { return NULL; }
      89             : 
      90           0 :         while ((dent = readdir(odir))) {
      91           0 :                 unsigned int i = dir->count;
      92           0 :                 char *full_name;
      93           0 :                 char *low_name;
      94             : 
      95           0 :                 if (strchr(dent->d_name, ':') && !strchr(unix_path, ':')) {
      96             :                         /* don't show streams in dir listing */
      97           0 :                         continue;
      98             :                 }
      99             : 
     100           0 :                 low_name = strlower_talloc(mem_ctx, dent->d_name);
     101           0 :                 if (!low_name) { continue; }
     102             : 
     103             :                 /* check it matches the wildcard pattern */
     104           0 :                 if (ms_fnmatch_protocol(low_mask, low_name, PROTOCOL_NT1,
     105             :                                         false) != 0) {
     106           0 :                         continue;
     107             :                 }
     108             :                 
     109           0 :                 if (dir->count >= allocated) {
     110           0 :                         allocated = (allocated + 100) * 1.2;
     111           0 :                         dir->files = talloc_realloc(dir, dir->files, struct svfs_dirfile, allocated);
     112           0 :                         if (!dir->files) { 
     113           0 :                                 closedir(odir);
     114           0 :                                 return NULL;
     115             :                         }
     116             :                 }
     117             : 
     118           0 :                 dir->files[i].name = low_name;
     119           0 :                 if (!dir->files[i].name) { continue; }
     120             : 
     121           0 :                 full_name = talloc_asprintf(mem_ctx, "%s/%s", dir->unix_dir,
     122           0 :                                             dir->files[i].name);
     123           0 :                 if (!full_name) { continue; }
     124             : 
     125           0 :                 if (stat(full_name, &dir->files[i].st) == 0) { 
     126           0 :                         dir->count++;
     127             :                 }
     128             : 
     129           0 :                 talloc_free(full_name);
     130             :         }
     131             : 
     132           0 :         closedir(odir);
     133             : 
     134           0 :         return dir;
     135             : }
     136             : 
     137             : /*
     138             :   read a directory and find all matching file names and stat info
     139             :   returned names are separate unix and DOS names. The returned names
     140             :   are relative to the directory
     141             : */
     142           0 : struct svfs_dir *svfs_list(struct ntvfs_module_context *ntvfs, struct ntvfs_request *req, const char *pattern)
     143             : {
     144           0 :         struct svfs_private *p = ntvfs->private_data;
     145           0 :         char *unix_path;
     146             : 
     147           0 :         unix_path = svfs_unix_path(ntvfs, req, pattern);
     148           0 :         if (!unix_path) { return NULL; }
     149             : 
     150           0 :         return svfs_list_unix(p, req, unix_path);
     151             : }
     152             : 
     153             : 
     154             : /*******************************************************************
     155             : set the time on a file via file descriptor
     156             : *******************************************************************/
     157           0 : int svfs_file_utime(int fd, struct utimbuf *times)
     158             : {
     159           0 :         char *fd_path = NULL;
     160           0 :         int ret;
     161             : 
     162           0 :         ret = asprintf(&fd_path, "/proc/self/%d", fd);
     163           0 :         if (ret == -1) {
     164           0 :                 errno = ENOMEM;
     165           0 :                 return -1;
     166             :         }
     167             : 
     168           0 :         if (!fd_path) {
     169           0 :                 errno = ENOMEM;
     170           0 :                 return -1;
     171             :         }
     172             :         
     173           0 :         ret = utime(fd_path, times);
     174           0 :         free(fd_path);
     175           0 :         return ret;
     176             : }
     177             : 
     178             : 
     179             : /*
     180             :   map a unix file attrib to a DOS attribute
     181             : */
     182           4 : uint16_t svfs_unix_to_dos_attrib(mode_t mode)
     183             : {
     184           4 :         uint16_t ret = 0;
     185           4 :         if (S_ISDIR(mode)) ret |= FILE_ATTRIBUTE_DIRECTORY;
     186           4 :         if (!(mode & S_IWUSR)) ret |= FILE_ATTRIBUTE_READONLY;
     187           4 :         return ret;
     188             : }
     189             : 

Generated by: LCOV version 1.14