LCOV - code coverage report
Current view: top level - idlc/source - idlcproduce.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 50 113 44.2 %
Date: 2012-08-25 Functions: 3 4 75.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 60 196 30.6 %

           Branch data     Line data    Source code
       1                 :            : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
       2                 :            : /*
       3                 :            :  * This file is part of the LibreOffice project.
       4                 :            :  *
       5                 :            :  * This Source Code Form is subject to the terms of the Mozilla Public
       6                 :            :  * License, v. 2.0. If a copy of the MPL was not distributed with this
       7                 :            :  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
       8                 :            :  *
       9                 :            :  * This file incorporates work covered by the following license notice:
      10                 :            :  *
      11                 :            :  *   Licensed to the Apache Software Foundation (ASF) under one or more
      12                 :            :  *   contributor license agreements. See the NOTICE file distributed
      13                 :            :  *   with this work for additional information regarding copyright
      14                 :            :  *   ownership. The ASF licenses this file to you under the Apache
      15                 :            :  *   License, Version 2.0 (the "License"); you may not use this file
      16                 :            :  *   except in compliance with the License. You may obtain a copy of
      17                 :            :  *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
      18                 :            :  */
      19                 :            : 
      20                 :            : #include <idlc/idlc.hxx>
      21                 :            : #include <idlc/astmodule.hxx>
      22                 :            : #include <rtl/strbuf.hxx>
      23                 :            : #include <osl/file.hxx>
      24                 :            : #include <osl/thread.h>
      25                 :            : 
      26                 :            : #if defined(SAL_W32)
      27                 :            : #include <io.h>
      28                 :            : #include <direct.h>
      29                 :            : #include <errno.h>
      30                 :            : #endif
      31                 :            : 
      32                 :            : #ifdef SAL_UNX
      33                 :            : #include <unistd.h>
      34                 :            : #include <sys/stat.h>
      35                 :            : #include <errno.h>
      36                 :            : #endif
      37                 :            : 
      38                 :            : #include <string.h>
      39                 :            : 
      40                 :            : using namespace ::rtl;
      41                 :            : using namespace ::osl;
      42                 :            : 
      43                 :            : StringList* pCreatedDirectories = NULL;
      44                 :            : 
      45                 :      11955 : static sal_Bool checkOutputPath(const OString& completeName)
      46                 :            : {
      47         [ +  - ]:      11955 :     OString sysPathName = convertToAbsoluteSystemPath(completeName);
      48                 :      11955 :     OStringBuffer buffer(sysPathName.getLength());
      49                 :            : 
      50         [ -  + ]:      11955 :     if ( sysPathName.indexOf( SEPARATOR ) == -1 )
      51                 :          0 :         return sal_True;
      52                 :            : 
      53                 :      11955 :     sal_Int32 nIndex = 0;
      54                 :      11955 :     OString token(sysPathName.getToken(0, SEPARATOR, nIndex));
      55                 :      11955 :     const sal_Char* p = token.getStr();
      56 [ +  - ][ -  + ]:      11955 :     if (strcmp(p, "..") == 0
                 [ +  - ]
      57                 :      11955 :         || *(p+1) == ':'
      58                 :      11955 :         || strcmp(p, ".") == 0)
      59                 :            :     {
      60         [ #  # ]:          0 :         buffer.append(token);
      61         [ #  # ]:          0 :         buffer.append(SEPARATOR);
      62                 :            :     }
      63                 :            :     else
      64                 :      11955 :         nIndex = 0;
      65                 :            : 
      66         [ +  + ]:     172907 :     do
      67                 :            :     {
      68         [ +  - ]:     172907 :         buffer.append(sysPathName.getToken(0, SEPARATOR, nIndex));
      69                 :            : 
      70 [ +  + ][ +  + ]:     172907 :         if ( buffer.getLength() > 0 && nIndex != -1 )
                 [ +  + ]
      71                 :            :         {
      72                 :            : #if defined(SAL_UNX)
      73         [ +  - ]:     148997 :             if (mkdir((char*)buffer.getStr(), 0777) == -1)
      74                 :            : #else
      75                 :            :             if (mkdir((char*)buffer.getStr()) == -1)
      76                 :            : #endif
      77                 :            :             {
      78         [ -  + ]:     148997 :                 if (errno == ENOENT)
      79                 :            :                 {
      80                 :            :                     fprintf(stderr, "%s: cannot create directory '%s'\n",
      81 [ #  # ][ #  # ]:          0 :                             idlc()->getOptions()->getProgramName().getStr(), buffer.getStr());
                 [ #  # ]
      82                 :          0 :                     return sal_False;
      83                 :            :                 }
      84                 :            :             } else
      85                 :            :             {
      86         [ #  # ]:          0 :                 if ( !pCreatedDirectories )
      87 [ #  # ][ #  # ]:          0 :                     pCreatedDirectories = new StringList();
      88         [ #  # ]:          0 :                 pCreatedDirectories->push_front(buffer.getStr());
      89                 :            :             }
      90                 :            :         }
      91         [ +  - ]:     172907 :         buffer.append(SEPARATOR);
      92                 :            :     } while( nIndex != -1 );
      93                 :      11955 :     return sal_True;
      94                 :            : }
      95                 :            : 
      96                 :          0 : static sal_Bool cleanPath()
      97                 :            : {
      98         [ #  # ]:          0 :     if ( pCreatedDirectories )
      99                 :            :     {
     100                 :          0 :         StringList::iterator iter = pCreatedDirectories->begin();
     101                 :          0 :         StringList::iterator end = pCreatedDirectories->end();
     102         [ #  # ]:          0 :         while ( iter != end )
     103                 :            :         {
     104                 :            : //#ifdef SAL_UNX
     105                 :            : //          if (rmdir((char*)(*iter).getStr(), 0777) == -1)
     106                 :            : //#else
     107         [ #  # ]:          0 :             if (rmdir((char*)(*iter).getStr()) == -1)
     108                 :            : //#endif
     109                 :            :             {
     110                 :            :                 fprintf(stderr, "%s: cannot remove directory '%s'\n",
     111 [ #  # ][ #  # ]:          0 :                         idlc()->getOptions()->getProgramName().getStr(), (*iter).getStr());
                 [ #  # ]
     112                 :          0 :                 return sal_False;
     113                 :            :             }
     114                 :          0 :             ++iter;
     115                 :            :         }
     116         [ #  # ]:          0 :         delete pCreatedDirectories;
     117                 :            :     }
     118                 :          0 :     return sal_True;
     119                 :            : }
     120                 :            : 
     121                 :      35865 : void removeIfExists(const OString& pathname)
     122                 :            : {
     123                 :      35865 :     unlink(pathname.getStr());
     124                 :      35865 : }
     125                 :            : 
     126                 :            : sal_Int32 SAL_CALL
     127                 :       5978 : produceFile(const OString& regFileName, sPair_t const*const pDepFile)
     128                 :            : {
     129         [ +  - ]:       5978 :     Options* pOptions = idlc()->getOptions();
     130                 :            : 
     131                 :       5978 :     OString regTmpName = regFileName.replaceAt(regFileName.getLength() -3, 3, "_idlc_");
     132                 :            : 
     133 [ -  + ][ +  - ]:       5978 :     if ( !checkOutputPath(regFileName) )
     134                 :            :     {
     135                 :            :         fprintf(stderr, "%s: could not create path of registry file '%s'.\n",
     136 [ #  # ][ #  # ]:          0 :                 pOptions->getProgramName().getStr(), regFileName.getStr());
     137                 :          0 :         return 1;
     138                 :            :     }
     139                 :            : 
     140                 :       5978 :     OString depTmpName;
     141         [ +  + ]:       5978 :     if (pDepFile)
     142                 :            :     {
     143                 :            :         depTmpName = pDepFile->first.replaceAt(
     144                 :       5977 :                         regFileName.getLength() -3, 3, "_idlc_");
     145 [ -  + ][ +  - ]:       5977 :         if ( !checkOutputPath(depTmpName) )
     146                 :            :         {
     147                 :            :             fprintf(stderr, "%s: could not create path of dep file '%s'.\n",
     148 [ #  # ][ #  # ]:          0 :                 pOptions->getProgramName().getStr(), pDepFile->first.getStr());
     149                 :          0 :             return 1;
     150                 :            :         }
     151                 :       5977 :         removeIfExists(depTmpName);
     152                 :            :     }
     153                 :            : 
     154                 :       5978 :     removeIfExists(regTmpName);
     155         [ +  - ]:       5978 :     OString urlRegTmpName = convertToFileUrl(regTmpName);
     156                 :            : 
     157         [ +  - ]:       5978 :     Registry regFile;
     158 [ +  - ][ +  - ]:       5978 :     if ( regFile.create(OStringToOUString(urlRegTmpName, RTL_TEXTENCODING_UTF8)) != REG_NO_ERROR )
                 [ -  + ]
     159                 :            :     {
     160                 :            :         fprintf(stderr, "%s: could not create registry file '%s'\n",
     161 [ #  # ][ #  # ]:          0 :                 pOptions->getProgramName().getStr(), regTmpName.getStr());
     162                 :          0 :         removeIfExists(regTmpName);
     163                 :          0 :         removeIfExists(regFileName);
     164         [ #  # ]:          0 :         cleanPath();
     165                 :          0 :         return 1;
     166                 :            :     }
     167                 :            : 
     168         [ +  - ]:       5978 :     RegistryKey rootKey;
     169 [ +  - ][ -  + ]:       5978 :     if ( regFile.openRootKey(rootKey) != REG_NO_ERROR )
     170                 :            :     {
     171                 :            :         fprintf(stderr, "%s: could not open root of registry file '%s'\n",
     172 [ #  # ][ #  # ]:          0 :                 pOptions->getProgramName().getStr(), regFileName.getStr());
     173                 :          0 :         removeIfExists(regTmpName);
     174                 :          0 :         removeIfExists(regFileName);
     175         [ #  # ]:          0 :         cleanPath();
     176                 :          0 :         return 1;
     177                 :            :     }
     178                 :            : 
     179                 :            :     // produce registry file
     180 [ +  - ][ +  - ]:       5978 :     if ( !idlc()->getRoot()->dump(rootKey) )
                 [ -  + ]
     181                 :            :     {
     182         [ #  # ]:          0 :         rootKey.releaseKey();
     183         [ #  # ]:          0 :         regFile.close();
     184 [ #  # ][ #  # ]:          0 :         regFile.destroy(OStringToOUString(regFileName, RTL_TEXTENCODING_UTF8));
     185                 :          0 :         removeIfExists(regFileName);
     186         [ #  # ]:          0 :         cleanPath();
     187                 :          0 :         return 1;
     188                 :            :     }
     189                 :            : 
     190         [ +  - ]:       5978 :     rootKey.releaseKey();
     191 [ +  - ][ -  + ]:       5978 :     if ( regFile.close() != REG_NO_ERROR )
     192                 :            :     {
     193                 :            :         fprintf(stderr, "%s: could not close registry file '%s'\n",
     194 [ #  # ][ #  # ]:          0 :                 pOptions->getProgramName().getStr(), regFileName.getStr());
     195                 :          0 :         removeIfExists(regTmpName);
     196                 :          0 :         removeIfExists(regFileName);
     197         [ #  # ]:          0 :         cleanPath();
     198                 :          0 :         return 1;
     199                 :            :     }
     200                 :            : 
     201 [ +  + ][ +  - ]:       5978 :     if (pDepFile && !idlc()->dumpDeps(depTmpName, pDepFile->second))
         [ +  - ][ -  + ]
                 [ -  + ]
     202                 :            :     {
     203                 :            :         fprintf(stderr, "%s: could not write dep file '%s'\n",
     204 [ #  # ][ #  # ]:          0 :                 pOptions->getProgramName().getStr(), pDepFile->first.getStr());
     205                 :          0 :         removeIfExists(depTmpName);
     206                 :          0 :         removeIfExists(pDepFile->first);
     207                 :          0 :         removeIfExists(regTmpName);
     208                 :          0 :         removeIfExists(regFileName);
     209         [ #  # ]:          0 :         cleanPath();
     210                 :          0 :         return 1;
     211                 :            :     }
     212                 :            : 
     213                 :       5978 :     removeIfExists(regFileName);
     214                 :            : 
     215   [ +  -  -  + ]:      11956 :     if ( File::move(OStringToOUString(regTmpName, osl_getThreadTextEncoding()),
     216 [ +  - ][ +  - ]:      11956 :                     OStringToOUString(regFileName, osl_getThreadTextEncoding())) != FileBase::E_None ) {
         [ +  - ][ +  - ]
     217                 :            :         fprintf(stderr, "%s: cannot rename temporary registry '%s' to '%s'\n",
     218 [ #  # ][ #  # ]:          0 :                 idlc()->getOptions()->getProgramName().getStr(),
     219         [ #  # ]:          0 :                 regTmpName.getStr(), regFileName.getStr());
     220                 :          0 :         removeIfExists(regTmpName);
     221         [ #  # ]:          0 :         cleanPath();
     222                 :          0 :         return 1;
     223                 :            :     }
     224                 :       5978 :     removeIfExists(regTmpName);
     225                 :            : 
     226         [ +  + ]:       5978 :     if (pDepFile)
     227                 :            :     {
     228                 :       5977 :         removeIfExists(pDepFile->first);
     229   [ +  -  -  + ]:      11954 :         if ( File::move(OStringToOUString(depTmpName, osl_getThreadTextEncoding()),
     230 [ +  - ][ +  - ]:      11954 :                         OStringToOUString(pDepFile->first, osl_getThreadTextEncoding())) != FileBase::E_None ) {
         [ +  - ][ +  - ]
     231                 :            :             fprintf(stderr, "%s: cannot rename dep file '%s' to '%s'\n",
     232 [ #  # ][ #  # ]:          0 :                     idlc()->getOptions()->getProgramName().getStr(),
     233         [ #  # ]:          0 :                     depTmpName.getStr(), pDepFile->first.getStr());
     234                 :          0 :             removeIfExists(depTmpName);
     235                 :          0 :             removeIfExists(pDepFile->first);
     236                 :          0 :             removeIfExists(regFileName);
     237         [ #  # ]:          0 :             cleanPath();
     238                 :          0 :             return 1;
     239                 :            :         }
     240                 :       5977 :         removeIfExists(depTmpName);
     241                 :            :     }
     242                 :            : 
     243 [ +  - ][ +  - ]:       5978 :     return 0;
     244                 :            : }
     245                 :            : 
     246                 :            : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10