Bug Summary

File:libsynthesis/src/platform_adapters/linux/configfiles.cpp
Warning:line 173, column 7
Value stored to 'rc' is never read

Annotated Source Code

1/*
2 * File: configfiles.cpp
3 *
4 * Author: Lukas Zeller (luz@plan44.ch)
5 *
6 * Linux Platform adaptor for accessing config files
7 *
8 * Copyright (c) 2002-2011 by Synthesis AG + plan44.ch
9 *
10 * 2002-11-18 : luz : created
11 *
12 *
13 */
14
15#include "sysync.h"
16
17#ifdef ANDROID
18#include <unistd.h>
19#endif
20
21#include <sys/types.h>
22#include <pwd.h>
23
24
25// determine OS version
26static bool getOSVersion(string &aOSVersion)
27{
28 // Obtain OS Version
29 //%%%% for now
30 aOSVersion="unknown";
31 return false;
32} // getOSVersion
33
34
35// determine hardware name
36static bool getHardwareName(string &aHardwareName)
37{
38 // Obtain Device name
39 #ifdef ANDROID
40 aHardwareName="Android Device";
41 #else
42 aHardwareName="Linux PC";
43 #endif
44 return true;
45} // getHardwareName
46
47
48/// @brief get platform specific string by ID
49/// @return false if string is not available for this platform
50/// @param aStringID string selector ID
51/// @param aString will receive the requested string
52bool getPlatformString(TPlatformStringID aStringID, string &aString)
53{
54 const sInt16 bufsiz=1024;
55 char buffer[bufsiz];
56 buffer[0]=0; // terminate for safety
57 string str;
58 string aSub;
59 struct passwd *userInfoP=NULL__null;
60
61 switch (aStringID) {
62 case pfs_platformvers:
63 // Platform OS version string
64 getOSVersion(aString);
65 break;
66 case pfs_device_uri:
67 getLocalDeviceID(aString);
68 break;
69 case pfs_device_name:
70 getHardwareName(aString);
71 break;
72 case pfs_loccfg_path:
73 #ifdef STANDALONE_APP1
74 // default path for local config for XPT standalones is the current dir
75 aString = ".";
76 break;
77 #endif
78 // for server modules, it is same as the global config path
79 #ifndef STANDALONE_APP1
80 case pfs_globcfg_path:
81 // global config directory path
82 aString = "/etc";
83 break;
84 #endif
85 case pfs_defout_path:
86 // default output path (formerly: default write path)
87 #ifdef STANDALONE_APP1
88 // default path for local config for XPT standalones is the current dir
89 aString = ".";
90 #else
91 // for server modules, default log path is /var/log
92 aString = "/var/log";
93 #endif
94 break;
95 case pfs_temp_path:
96 // temp path
97 aString = "/temp";
98 break;
99 case pfs_userdir_path:
100 // user's home dir for user-visible documents and files
101 userInfoP = getpwuid(getuid());
102 aString = userInfoP->pw_dir; // home dir
103 break;
104 #ifdef APPDATA_SUBDIR"synthesis.ch/SySyncLib_uni"
105 case pfs_appdata_path:
106 // My specific subdirectory for storing my app data/prefs
107 userInfoP = getpwuid(getuid());
108 aString = userInfoP->pw_dir; // user home dir
109 aSub = APPDATA_SUBDIR"synthesis.ch/SySyncLib_uni";
110 #ifdef ANDROID
111 aString += "/data/com.sysync"; // application specific subdir for android
112 if (!aSub.empty()) aString+= "/"; // slash only if subdir is really there
113 #else
114 aString += "/.sysync/"; // application specific subdir
115 // don't adapt it here to avoid potential problems on other platforms
116 #endif
117 aString += aSub;
118 break;
119 #endif
120 /*
121 case pfs_prefs_path:
122 // general directory where all applications store data & prefs (for current user)
123 aString = %%%
124 break;
125 */
126 /*
127 case pfs_exedir_path:
128 exedir:
129 // path where module file resides
130 aString = %%%
131 break;
132 */
133 default:
134 // unknown ID
135 return false;
136 }
137 // ok
138 return true;
139} // getPlatformString
140
141
142extern "C" {
143 #ifndef ANDROID
144 #include <ctime>
145 #endif
146
147 #include <sys/stat.h>
148}
149
150/// @brief update string such that it can be used as target OS directory path
151/// (filename can be appended without any separator)
152/// @param aPath path to be updated
153/// @param aMakeDirs if set, directories along path are created if not existing
154/// @return false if directory does not exist or could not be created (when aMakeDirs is set)
155bool makeOSDirPath(string &aPath, bool aMakeDirs)
156{
157 // make sure path ends with backslash
158 string::size_type n=aPath.size();
159 if (n>=1 && aPath[n-1]!='/')
160 aPath+='/';
161 // now make sure path exists if requested
162 if (aMakeDirs) {
163 // no slash at the end
164 string tmppath;
165 tmppath.assign(aPath,0,aPath.size()-1);
166 // optimization check if entire path exists
167 struct stat statinfo;
168 int rc=stat(aPath.c_str(),&statinfo);
169 if(rc!=0 || !S_ISDIR(statinfo.st_mode)((((statinfo.st_mode)) & 0170000) == (0040000))) {
170 #ifdef ANDROID
171 rc = 0; // BFO_INCOMPLETE
172 #else
173 rc = errno(*__errno_location ());
Value stored to 'rc' is never read
174 #endif
175 // path does not exist yet - start from beginning to create it
176 n=0;
177 bool knownmissing=false;
178 // skip first slash for absolute paths
179 if (aPath[0]=='/') n+=1; // skip
180 do {
181 // find first directory in path
182 n=aPath.find("/",n);
183 // if no more separators to find, all dirs exist now
184 if (n==string::npos) break;
185 tmppath.assign(aPath,0,n);
186 n+=1; // skip separator
187 if (!knownmissing) {
188 // test if this dir exists
189 rc=stat(tmppath.c_str(),&statinfo);
190 if(rc!=0 || !S_ISDIR(statinfo.st_mode)((((statinfo.st_mode)) & 0170000) == (0040000))) {
191 #ifdef ANDROID
192 rc = 0; // BFO_INCOMPLETE
193 #else
194 rc = errno(*__errno_location ());
195 #endif
196 knownmissing=true;
197 }
198 }
199 if (knownmissing) {
200 // create the subdir (might fail if part of path already exists)
201 if (mkdir(tmppath.c_str(),S_IRWXU(0400|0200|0100))!=0)
202 return false; // failure to create directory
203 }
204 } while(true);
205 } // path does not exist yet entirely
206 } // make path existing
207 return true;
208} // makeOSDirPath
209
210
211// returns timestamp (UTC) of last file modification or 0 if none known
212lineartime_t getFileModificationDate(const char *aFileName)
213{
214 struct stat st;
215 lineartime_t res;
216
217 if (stat(aFileName,&st)!=0) res=0;
218 else {
219 // stat ok, get modification date
220 res= ((lineartime_t)st.st_mtimest_mtim.tv_sec * secondToLinearTimeFactor) + UnixToLineartimeOffset;
221 }
222 // return timestamp
223 return res;
224} // getFileModificationDate
225
226
227#include <netdb.h>
228#include <unistd.h>
229
230// get local device URI/name info
231bool getLocalDeviceID(string &aURI)
232{
233 char szHostname[100];
234 string hostName;
235
236 // get name of own machine
237 if (gethostname(szHostname, sizeof(szHostname))!=0) {
238 hostName="_unknown_";
239 }
240 else {
241 // A network lookup of the domain name is not likely
242 // to yield any good result on most Linux consumer
243 // devices. It just causes a slowdown, in particular
244 // when the device is not currently connected (which
245 // does not necessarily prevent syncing, for example
246 // when using Bluetooth); 10 second delays have
247 // been observed while the network stack waits for
248 // a timeout (FDO #70771).
249 //
250 // To avoid that timeout, disable this code unconditionally.
251#if 0
252 struct hostent *pHostEnt=NULL__null;
253 // get host entry
254 pHostEnt = gethostbyname(szHostname);
255 // return fully qualified name of machine as ID
256 if (pHostEnt)
257 hostName=pHostEnt->h_name; // DNS name of machine
258 else
259#endif
260 hostName=szHostname; // just name of machine
261 }
262 // generate URI from name
263 #ifdef ANDROID
264 aURI="android:";
265 #else
266 aURI="linux:"; // %%% SCTS does not like http:// here, so we take os:xxxx
267 #endif
268 // add name of this machine (fully qualified if possible)
269 aURI+=hostName;
270 // this is more or less unique
271 return true;
272} // getLocalDeviceID
273
274
275// write to platform's "console", whatever that is
276void PlatformConsolePuts(const char *aText)
277{
278 // generic output
279 #ifdef ANDROID
280 //__android_log_write( ANDROID_LOG_DEBUG, "ConsolePuts", aText );
281 #else
282 puts(aText); // appends newline itself
283 #endif
284} // PlatformConsolePuts
285
286
287
288/* eof */