LCOV - code coverage report
Current view: top level - src - PolicyFile.cc (source / functions) Hit Total Coverage
Test: app.info Lines: 3 77 3.9 %
Date: 2010-12-13 Functions: 3 7 42.9 %
Branches: 2 46 4.3 %

           Branch data     Line data    Source code
       1                 :            : // $Id: PolicyFile.cc 1473 2005-10-06 21:32:45Z vern $
       2                 :            : 
       3                 :            : #include "config.h"
       4                 :            : 
       5                 :            : #include <sys/types.h>
       6                 :            : #include <sys/stat.h>
       7                 :            : #include <assert.h>
       8                 :            : 
       9                 :            : #include <map>
      10                 :            : #include <stdio.h>
      11                 :            : #include <string>
      12                 :            : #include <vector>
      13                 :            : 
      14                 :            : using namespace std;
      15                 :            : 
      16                 :            : #include "Debug.h"
      17                 :            : #include "util.h"
      18                 :            : #include "PolicyFile.h"
      19                 :            : 
      20                 :            : struct PolicyFile {
      21                 :          0 :         PolicyFile ()   { filedata = 0; lmtime = 0; }
      22                 :            :         ~PolicyFile ()  { delete [] filedata; filedata = 0; }
      23                 :            : 
      24                 :            :         time_t lmtime;
      25                 :            :         char* filedata;
      26                 :            :         vector<const char*> lines;
      27                 :            : };
      28                 :            : 
      29                 :            : typedef map<string, PolicyFile*> PolicyFileMap;
      30                 :          6 : static PolicyFileMap policy_files;
      31                 :            : 
      32                 :          0 : int how_many_lines_in(const char* policy_filename)
      33                 :            :         {
      34         [ #  # ]:          0 :         if ( ! policy_filename )
      35                 :          0 :                 internal_error("NULL value passed to how_many_lines_in\n");
      36                 :            : 
      37                 :          0 :         FILE* throwaway = fopen(policy_filename, "r");
      38         [ #  # ]:          0 :         if ( ! throwaway )
      39                 :            :                 {
      40                 :          0 :                 debug_msg("No such policy file: %s.\n", policy_filename);
      41                 :          0 :                 return -1;
      42                 :            :                 }
      43                 :            : 
      44                 :          0 :         fclose(throwaway);
      45                 :            : 
      46                 :          0 :         PolicyFileMap::iterator match;
      47                 :          0 :         match = policy_files.find(policy_filename);
      48                 :            : 
      49         [ #  # ]:          0 :         if ( match == policy_files.end() )
      50                 :            :                 {
      51                 :          0 :                 match = policy_files.find(policy_filename);
      52         [ #  # ]:          0 :                 if ( match == policy_files.end() )
      53                 :            :                         {
      54                 :          0 :                         debug_msg("Policy file %s was not loaded.\n", policy_filename);
      55                 :          0 :                         return -1;
      56                 :            :                         }
      57                 :            :                 }
      58                 :            : 
      59                 :          0 :         PolicyFile* pf = match->second;
      60                 :          0 :         return pf->lines.size();
      61                 :            :         }
      62                 :            : 
      63                 :          0 : bool LoadPolicyFileText(const char* policy_filename)
      64                 :            :         {
      65         [ #  # ]:          0 :         if ( ! policy_filename )
      66                 :          0 :                 return true;
      67                 :            : 
      68                 :          0 :         FILE* f = fopen(policy_filename, "r");
      69                 :            : 
      70         [ #  # ]:          0 :         if ( ! f )
      71                 :            :                 {
      72                 :          0 :                 debug_msg("No such policy file: %s.\n", policy_filename);
      73                 :          0 :                 return false;
      74                 :            :                 }
      75                 :            : 
      76                 :          0 :         PolicyFile* pf = new PolicyFile;
      77                 :            : 
      78         [ #  # ]:          0 :         if ( policy_files.find(policy_filename) != policy_files.end() )
      79                 :          0 :                 debug_msg("Policy file %s already loaded\n", policy_filename);
      80                 :            : 
      81                 :          0 :         policy_files.insert(PolicyFileMap::value_type(policy_filename, pf));
      82                 :            : 
      83                 :            :         struct stat st;
      84                 :          0 :         fstat(fileno(f), &st);
      85                 :            : 
      86                 :          0 :         pf->lmtime = st.st_mtime;
      87                 :          0 :         off_t size = st.st_size;
      88                 :            : 
      89                 :            :         // ### This code is not necessarily Unicode safe!
      90                 :            :         // (probably fine with UTF-8)
      91                 :          0 :         pf->filedata = new char[size+1];
      92                 :          0 :         fread(pf->filedata, size, 1, f);
      93                 :          0 :         pf->filedata[size] = 0;
      94                 :          0 :         fclose(f);
      95                 :            : 
      96                 :            :         // Separate the string by newlines.
      97                 :          0 :         pf->lines.push_back(pf->filedata);
      98         [ #  # ]:          0 :         for ( char* iter = pf->filedata; *iter; ++iter )
      99                 :            :                 {
     100         [ #  # ]:          0 :                 if ( *iter == '\n' )
     101                 :            :                         {
     102                 :          0 :                         *iter = 0;
     103         [ #  # ]:          0 :                         if ( *(iter + 1) )
     104                 :          0 :                                 pf->lines.push_back(iter + 1);
     105                 :            :                         }
     106                 :            :                 }
     107                 :            : 
     108         [ #  # ]:          0 :         for ( int i = 0; i < int(pf->lines.size()); ++i )
     109         [ #  # ]:          0 :                 assert(pf->lines[i][0] != '\n');
     110                 :            : 
     111                 :          0 :         return true;
     112                 :            :         }
     113                 :            : 
     114                 :            : // REMEMBER: line number arguments are indexed from 0.
     115                 :            : bool PrintLines(const char* policy_filename, unsigned int start_line,
     116                 :          0 :                 unsigned int how_many_lines, bool show_numbers)
     117                 :            :         {
     118         [ #  # ]:          0 :         if ( ! policy_filename )
     119                 :          0 :                 return true;
     120                 :            : 
     121                 :          0 :         FILE* throwaway = fopen(policy_filename, "r");
     122         [ #  # ]:          0 :         if ( ! throwaway )
     123                 :            :                 {
     124                 :          0 :                 debug_msg("No such policy file: %s.\n", policy_filename);
     125                 :          0 :                 return false;
     126                 :            :                 }
     127                 :            : 
     128                 :          0 :         fclose(throwaway);
     129                 :            : 
     130                 :          0 :         PolicyFileMap::iterator match;
     131                 :          0 :         match = policy_files.find(policy_filename);
     132                 :            : 
     133         [ #  # ]:          0 :         if ( match == policy_files.end() )
     134                 :            :                 {
     135                 :          0 :                 match = policy_files.find(policy_filename);
     136         [ #  # ]:          0 :                 if ( match == policy_files.end() )
     137                 :            :                         {
     138                 :          0 :                         debug_msg("Policy file %s was not loaded.\n", policy_filename);
     139                 :          0 :                         return false;
     140                 :            :                         }
     141                 :            :                 }
     142                 :            : 
     143                 :          0 :         PolicyFile* pf = match->second;
     144                 :            : 
     145         [ #  # ]:          0 :         if ( start_line < 1 )
     146                 :          0 :                 start_line = 1;
     147                 :            : 
     148         [ #  # ]:          0 :         if ( start_line > pf->lines.size() )
     149                 :            :                 {
     150                 :            :                 debug_msg("Line number %d out of range; %s has %d lines\n",
     151                 :          0 :                         start_line, policy_filename, int(pf->lines.size()));
     152                 :          0 :                 return false;
     153                 :            :                 }
     154                 :            : 
     155         [ #  # ]:          0 :         if ( start_line + how_many_lines - 1 > pf->lines.size() )
     156                 :          0 :                 how_many_lines = pf->lines.size() - start_line + 1;
     157                 :            : 
     158         [ #  # ]:          0 :         for ( unsigned int i = 0; i < how_many_lines; ++i )
     159                 :            :                 {
     160         [ #  # ]:          0 :                 if ( show_numbers )
     161                 :          0 :                         debug_msg("%d\t", i + start_line);
     162                 :            : 
     163                 :          0 :                 const char* line = pf->lines[start_line + i - 1];
     164                 :          0 :                 debug_msg("%s\n", line);
     165                 :            :                 }
     166                 :            : 
     167                 :          0 :         return true;
     168 [ +  - ][ +  - ]:          6 :         }

Generated by: LCOV version 1.8