1/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17/*==================================================================================================
18
19    Header Name: SYNCML_DM_TreeMount.cc
20
21    General Description: Implementation of the and SYNCML_DM_TreeMount  classes.
22
23==================================================================================================*/
24
25#include "dm_uri_utils.h"
26#include "SyncML_DM_WBXMLArchive.H"
27#include "dmBufferReader.h"
28#include "xpl_dm_Manager.h"
29#include "dm_tree_class.H"
30#include "SYNCML_DM_TreeMount.H"
31
32SYNCML_DM_TreeMount::SYNCML_DM_TreeMount()
33  : m_pTree( NULL )
34{
35}
36
37SYNCML_DM_RET_STATUS_T
38SYNCML_DM_TreeMount::TreeAddToMountList (CPCHAR pUri, CPCHAR pTreePath)
39{
40    if( !m_pTree ) return SYNCML_DM_FAIL;
41
42    /* Validate the URI */
43    SYNCML_DM_URI_RESULT_T  dm_uri_result = m_pTree->URIValidateAndParse(pUri);
44
45    if (( dm_uri_result == SYNCML_DM_COMMAND_ON_UNKNOWN_PROPERTY ) ||
46        ( dm_uri_result == SYNCML_DM_COMMAND_INVALID_URI) ||
47        ( dm_uri_result == SYNCML_DM_COMMAND_URI_TOO_LONG ))
48    {
49        return (SYNCML_DM_FAIL);
50    }
51
52    /* Check if the entry is existed in the list already */
53    if ( m_astrURIs.find( pUri ) >= 0 )
54        return SYNCML_DM_ENTRY_EXIST;
55
56    CPCHAR loc = DmStrrchr(pTreePath, '.');
57
58    // Compare the last six chars for a ".wbxml" extension
59    if(loc == NULL || DmStrcmp(loc, SyncML_DM_WBXMLArchive::FILE_EXTENSION) != 0)
60        return SYNCML_DM_FAIL;
61
62    BOOLEAN bFeatureEnabled = TRUE;
63    bFeatureEnabled = DmIsEnabledURI(pUri);
64
65    m_astrURIs.push_back( pUri );
66    if ( bFeatureEnabled )
67        m_astrPaths.push_back( pTreePath );
68    else
69        m_astrPaths.push_back( NULL );
70
71    return (SYNCML_DM_SUCCESS);
72}
73
74void
75SYNCML_DM_TreeMount::GetTreeMountEntry(CPCHAR &p_Uri,
76                                                              CPCHAR& p_TreePath,
77                                                              UINT16 index) const
78{
79  /* If the list is empty, set OUTPUT parameters as NULL and return. */
80    if ( index >= m_astrURIs.size() ) {
81        p_Uri = NULL;
82        p_TreePath = NULL;
83        return;
84    }
85    p_Uri = m_astrURIs[index].c_str();
86    p_TreePath = m_astrPaths[index].c_str();
87}
88
89BOOLEAN
90SYNCML_DM_TreeMount::IsMountPointEnabled(CPCHAR pUri) const
91{
92    INT32 size =  m_astrURIs.size();
93
94    for (INT32 index = 0; index < size; index++)
95    {
96        const DMString & str = m_astrURIs[index];
97        if ( DmIsParentURI( str.c_str(), pUri ) )
98        {
99            if ( m_astrPaths[index] == NULL )
100                 return FALSE;
101            else
102                 return TRUE;
103        }
104
105   }
106   return TRUE;
107}
108
109void
110SYNCML_DM_TreeMount::UnMountTree(void)
111{
112    m_astrURIs.clear();
113    m_astrPaths.clear();
114    m_pTree = NULL;
115}
116
117
118#ifndef DM_STATIC_FILES
119SYNCML_DM_RET_STATUS_T
120SYNCML_DM_TreeMount::MountTree( CEnv* env, DMTree* tree )
121{
122    if( !env || !tree ) return SYNCML_DM_FAIL;
123
124    m_pTree = tree;
125
126    DMString strLineBuffer;
127    char *line = strLineBuffer.AllocateBuffer(DM_MAX_CONFIG_LINE);
128    SYNCML_DM_RET_STATUS_T dm_result =  SYNCML_DM_SUCCESS;
129    DMString strFstabFileName;
130
131   if ( !line )
132    return SYNCML_DM_DEVICE_FULL;
133
134    env->GetMainRFSFullPath(DM_FSTAB_FILENAME,strFstabFileName);
135
136    DMFileHandler fp( strFstabFileName );
137
138    dm_result = fp.open(XPL_FS_FILE_READ);
139
140    if ( dm_result != SYNCML_DM_SUCCESS )
141        return dm_result;
142
143    INT32 numMountedFiles =0;
144    char * uri = NULL;
145    char * filepath = NULL;
146    while( !fp.iseof() )
147    {
148        fp.fgets((char *)line, DM_MAX_CONFIG_LINE);
149        if( line[0] == '#' || line[0] == '\0' )
150            continue;
151
152        uri = line;
153        filepath = (char*)DmStrchr(line,' ');
154
155        if ( filepath == NULL )
156            break;
157
158        *filepath ='\0';
159        filepath++;
160
161        while ( *filepath !='\0' && *filepath == ' ' )
162           filepath ++;
163
164        if (filepath == '\0')
165           break;
166
167        //Now add to the mount table
168        dm_result = TreeAddToMountList(uri, filepath);
169        if ( dm_result  == SYNCML_DM_SUCCESS)
170            numMountedFiles++;
171    }
172
173    fp.close();
174
175    //If no files are there, bail out.
176    if (numMountedFiles==0)
177       return SYNCML_DM_IO_FAILURE;
178
179    return dm_result;
180}
181#else
182SYNCML_DM_RET_STATUS_T
183SYNCML_DM_TreeMount::MountTree( CEnv* env, DMTree* tree )
184{
185    SYNCML_DM_RET_STATUS_T dm_result =  SYNCML_DM_SUCCESS;
186
187    UINT8 * pBuffer;
188    UINT32  size;
189    char * line;
190
191    pBuffer = (UINT8*)XPL_DM_GetFstab(&size);
192
193    if ( !pBuffer )
194        return SYNCML_DM_FAIL;
195
196    DMBufferReader fp(pBuffer,size);
197    INT32 numMountedFiles =0;
198    DMString uri;
199    char * filepath = NULL;
200    while( !fp.iseof() )
201    {
202        line = fp.fgets();
203
204        filepath = (char*)DmStrchr(line,' ');
205
206        if ( filepath == NULL)
207           break;
208
209        uri.assign(line,filepath-line);
210        filepath++;
211
212        while ( *filepath !='\0' && *filepath ==' ' )
213           filepath++;
214
215        dm_result = TreeAddToMountList(uri, filepath);
216        if ( dm_result  == SYNCML_DM_SUCCESS)
217           numMountedFiles++;
218    }
219
220    //If no files are there, bail out.
221    if (numMountedFiles==0)
222        return SYNCML_DM_IO_FAILURE;
223
224    return dm_result;
225}
226
227#endif
228