Changeset 353 for box/trunk/bin/bbackupd/BackupDaemon.cpp
- Timestamp:
- 30/01/2006 20:04:53 (6 years ago)
- File:
-
- 1 edited
-
box/trunk/bin/bbackupd/BackupDaemon.cpp (modified) (7 diffs)
Legend:
- Unmodified
- Added
- Removed
-
box/trunk/bin/bbackupd/BackupDaemon.cpp
r341 r353 11 11 12 12 #include <stdio.h> 13 #include <string.h> 13 14 #include <unistd.h> 14 15 15 #if ndef WIN3216 #ifdef HAVE_SIGNAL_H 16 17 #include <signal.h> 18 #endif 19 #ifdef HAVE_SYSLOG_H 17 20 #include <syslog.h> 21 #endif 22 #ifdef HAVE_SYS_PARAM_H 18 23 #include <sys/param.h> 24 #endif 25 #ifdef HAVE_SYS_WAIT_H 19 26 #include <sys/wait.h> 20 27 #endif … … 62 69 #include "IOStreamGetLine.h" 63 70 #include "Conversion.h" 71 #include "Archive.h" 64 72 65 73 #include "MemLeakFindOn.h" … … 468 476 box_time_t lastSyncTime = 0; 469 477 478 // -------------------------------------------------------------------------------------------- 479 480 // And what's the current client store marker? 481 int64_t clientStoreMarker = 482 BackupClientContext::ClientStoreMarker_NotKnown; 483 // haven't contacted the store yet 484 485 DeserializeStoreObjectInfo(clientStoreMarker, lastSyncTime, 486 nextSyncTime); 487 470 488 // -------------------------------------------------------------------------------------------- 471 489 472 // And what's the current client store marker?473 int64_t clientStoreMarker = BackupClientContext::ClientStoreMarker_NotKnown; // haven't contacted the store yet474 490 475 491 // Set state … … 675 691 // Log 676 692 ::syslog(LOG_INFO, "Finished scan of local files"); 693 694 // -------------------------------------------------------------------------------------------- 695 696 // We had a successful backup, save the store info 697 SerializeStoreObjectInfo(clientStoreMarker, lastSyncTime, nextSyncTime); 698 699 // -------------------------------------------------------------------------------------------- 677 700 } 678 701 catch(BoxException &e) … … 1832 1855 } 1833 1856 1857 // -------------------------------------------------------------------------- 1858 1859 typedef struct 1860 { 1861 int32_t mMagicValue; // also the version number 1862 int32_t mNumEntries; 1863 int64_t mObjectID; // this object ID 1864 int64_t mContainerID; // ID of container 1865 uint64_t mAttributesModTime; 1866 int32_t mOptionsPresent; // bit mask of optional sections / features present 1867 1868 } loc_StreamFormat; 1834 1869 1835 1870 // -------------------------------------------------------------------------- … … 1871 1906 } 1872 1907 1908 // -------------------------------------------------------------------------- 1909 // 1910 // Function 1911 // Name: BackupDaemon::Location::Deserialize(Archive & rArchive) 1912 // Purpose: Deserializes this object instance from a stream of bytes, using an Archive abstraction. 1913 // 1914 // Created: 2005/04/11 1915 // 1916 // -------------------------------------------------------------------------- 1917 void BackupDaemon::Location::Deserialize(Archive &rArchive) 1918 { 1919 // 1920 // 1921 // 1922 mpDirectoryRecord.reset(NULL); 1923 if (mpExcludeFiles) 1924 { 1925 delete mpExcludeFiles; 1926 mpExcludeFiles = NULL; 1927 } 1928 if (mpExcludeDirs) 1929 { 1930 delete mpExcludeDirs; 1931 mpExcludeDirs = NULL; 1932 } 1933 1934 // 1935 // 1936 // 1937 rArchive.Read(mName); 1938 rArchive.Read(mPath); 1939 rArchive.Read(mIDMapIndex); 1940 1941 // 1942 // 1943 // 1944 int64_t aMagicMarker = 0; 1945 rArchive.Read(aMagicMarker); 1946 1947 if (aMagicMarker == ARCHIVE_MAGIC_VALUE_NOOP) 1948 { 1949 // NOOP 1950 } 1951 else if (aMagicMarker == ARCHIVE_MAGIC_VALUE_RECURSE) 1952 { 1953 BackupClientDirectoryRecord *pSubRecord = new BackupClientDirectoryRecord(0, ""); 1954 if (!pSubRecord) 1955 throw std::bad_alloc(); 1956 1957 mpDirectoryRecord.reset(pSubRecord); 1958 mpDirectoryRecord->Deserialize(rArchive); 1959 } 1960 else 1961 { 1962 // there is something going on here 1963 THROW_EXCEPTION(CommonException, Internal) 1964 } 1965 1966 // 1967 // 1968 // 1969 rArchive.Read(aMagicMarker); 1970 1971 if (aMagicMarker == ARCHIVE_MAGIC_VALUE_NOOP) 1972 { 1973 // NOOP 1974 } 1975 else if (aMagicMarker == ARCHIVE_MAGIC_VALUE_RECURSE) 1976 { 1977 mpExcludeFiles = new ExcludeList; 1978 if (!mpExcludeFiles) 1979 throw std::bad_alloc(); 1980 1981 mpExcludeFiles->Deserialize(rArchive); 1982 } 1983 else 1984 { 1985 // there is something going on here 1986 THROW_EXCEPTION(CommonException, Internal) 1987 } 1988 1989 // 1990 // 1991 // 1992 rArchive.Read(aMagicMarker); 1993 1994 if (aMagicMarker == ARCHIVE_MAGIC_VALUE_NOOP) 1995 { 1996 // NOOP 1997 } 1998 else if (aMagicMarker == ARCHIVE_MAGIC_VALUE_RECURSE) 1999 { 2000 mpExcludeDirs = new ExcludeList; 2001 if (!mpExcludeDirs) 2002 throw std::bad_alloc(); 2003 2004 mpExcludeDirs->Deserialize(rArchive); 2005 } 2006 else 2007 { 2008 // there is something going on here 2009 THROW_EXCEPTION(CommonException, Internal) 2010 } 2011 } 2012 2013 // -------------------------------------------------------------------------- 2014 // 2015 // Function 2016 // Name: BackupDaemon::Location::Serialize(Archive & rArchive) 2017 // Purpose: Serializes this object instance into a stream of bytes, using an Archive abstraction. 2018 // 2019 // Created: 2005/04/11 2020 // 2021 // -------------------------------------------------------------------------- 2022 void BackupDaemon::Location::Serialize(Archive & rArchive) const 2023 { 2024 // 2025 // 2026 // 2027 rArchive.Write(mName); 2028 rArchive.Write(mPath); 2029 rArchive.Write(mIDMapIndex); 2030 2031 // 2032 // 2033 // 2034 if (mpDirectoryRecord.get() == NULL) 2035 { 2036 int64_t aMagicMarker = ARCHIVE_MAGIC_VALUE_NOOP; 2037 rArchive.Write(aMagicMarker); 2038 } 2039 else 2040 { 2041 int64_t aMagicMarker = ARCHIVE_MAGIC_VALUE_RECURSE; // be explicit about whether recursion follows 2042 rArchive.Write(aMagicMarker); 2043 2044 mpDirectoryRecord->Serialize(rArchive); 2045 } 2046 2047 // 2048 // 2049 // 2050 if (!mpExcludeFiles) 2051 { 2052 int64_t aMagicMarker = ARCHIVE_MAGIC_VALUE_NOOP; 2053 rArchive.Write(aMagicMarker); 2054 } 2055 else 2056 { 2057 int64_t aMagicMarker = ARCHIVE_MAGIC_VALUE_RECURSE; // be explicit about whether recursion follows 2058 rArchive.Write(aMagicMarker); 2059 2060 mpExcludeFiles->Serialize(rArchive); 2061 } 2062 2063 // 2064 // 2065 // 2066 if (!mpExcludeDirs) 2067 { 2068 int64_t aMagicMarker = ARCHIVE_MAGIC_VALUE_NOOP; 2069 rArchive.Write(aMagicMarker); 2070 } 2071 else 2072 { 2073 int64_t aMagicMarker = ARCHIVE_MAGIC_VALUE_RECURSE; // be explicit about whether recursion follows 2074 rArchive.Write(aMagicMarker); 2075 2076 mpExcludeDirs->Serialize(rArchive); 2077 } 2078 } 1873 2079 1874 2080 // -------------------------------------------------------------------------- … … 1902 2108 } 1903 2109 } 2110 2111 // -------------------------------------------------------------------------- 2112 // 2113 // Function 2114 // Name: BackupDaemon::SerializeStoreObjectInfo(int64_t aClientStoreMarker, box_time_t theLastSyncTime, box_time_t theNextSyncTime) 2115 // Purpose: Serializes remote directory and file information into a stream of bytes, using an Archive abstraction. 2116 // 2117 // Created: 2005/04/11 2118 // 2119 // -------------------------------------------------------------------------- 2120 2121 static const int STOREOBJECTINFO_MAGIC_ID_VALUE = 0x7777525F; 2122 static const std::string STOREOBJECTINFO_MAGIC_ID_STRING = "BBACKUPD-STATE"; 2123 static const int STOREOBJECTINFO_VERSION = 1; 2124 2125 void BackupDaemon::SerializeStoreObjectInfo(int64_t aClientStoreMarker, box_time_t theLastSyncTime, box_time_t theNextSyncTime) const 2126 { 2127 if(!GetConfiguration().KeyExists("StoreObjectInfoFile")) 2128 { 2129 return; 2130 } 2131 2132 std::string StoreObjectInfoFile = 2133 GetConfiguration().GetKeyValue("StoreObjectInfoFile"); 2134 2135 if (StoreObjectInfoFile.size() <= 0) 2136 { 2137 return; 2138 } 2139 2140 try 2141 { 2142 FileStream aFile(StoreObjectInfoFile.c_str(), 2143 O_WRONLY | O_CREAT | O_TRUNC); 2144 Archive anArchive(aFile, 0); 2145 2146 anArchive.Write(STOREOBJECTINFO_MAGIC_ID_VALUE); 2147 anArchive.Write(STOREOBJECTINFO_MAGIC_ID_STRING); 2148 anArchive.Write(STOREOBJECTINFO_VERSION); 2149 anArchive.Write(GetLoadedConfigModifiedTime()); 2150 anArchive.Write(aClientStoreMarker); 2151 anArchive.Write(theLastSyncTime); 2152 anArchive.Write(theNextSyncTime); 2153 2154 // 2155 // 2156 // 2157 int64_t iCount = mLocations.size(); 2158 anArchive.Write(iCount); 2159 2160 for (int v = 0; v < iCount; v++) 2161 { 2162 ASSERT(mLocations[v]); 2163 mLocations[v]->Serialize(anArchive); 2164 } 2165 2166 // 2167 // 2168 // 2169 iCount = mIDMapMounts.size(); 2170 anArchive.Write(iCount); 2171 2172 for (int v = 0; v < iCount; v++) 2173 anArchive.Write(mIDMapMounts[v]); 2174 2175 // 2176 // 2177 // 2178 aFile.Close(); 2179 ::syslog(LOG_INFO, "Saved store object info file '%s'", 2180 StoreObjectInfoFile.c_str()); 2181 } 2182 catch (...) 2183 { 2184 ::syslog(LOG_WARNING, "Requested store object info file '%s' " 2185 "not accessible or could not be created", 2186 StoreObjectInfoFile.c_str()); 2187 } 2188 } 2189 2190 // -------------------------------------------------------------------------- 2191 // 2192 // Function 2193 // Name: BackupDaemon::DeserializeStoreObjectInfo(int64_t & aClientStoreMarker, box_time_t & theLastSyncTime, box_time_t & theNextSyncTime) 2194 // Purpose: Deserializes remote directory and file information from a stream of bytes, using an Archive abstraction. 2195 // 2196 // Created: 2005/04/11 2197 // 2198 // -------------------------------------------------------------------------- 2199 void BackupDaemon::DeserializeStoreObjectInfo(int64_t & aClientStoreMarker, box_time_t & theLastSyncTime, box_time_t & theNextSyncTime) 2200 { 2201 // 2202 // 2203 // 2204 DeleteAllLocations(); 2205 2206 // 2207 // 2208 // 2209 if(!GetConfiguration().KeyExists("StoreObjectInfoFile")) 2210 { 2211 return; 2212 } 2213 2214 std::string StoreObjectInfoFile = 2215 GetConfiguration().GetKeyValue("StoreObjectInfoFile"); 2216 2217 if (StoreObjectInfoFile.size() <= 0) 2218 { 2219 return; 2220 } 2221 2222 try 2223 { 2224 FileStream aFile(StoreObjectInfoFile.c_str(), O_RDONLY); 2225 Archive anArchive(aFile, 0); 2226 2227 // 2228 // see if the content looks like a valid serialised archive 2229 // 2230 int iMagicValue = 0; 2231 anArchive.Read(iMagicValue); 2232 2233 if (iMagicValue != STOREOBJECTINFO_MAGIC_ID_VALUE) 2234 { 2235 ::syslog(LOG_WARNING, "Store object info file '%s' " 2236 "is not a valid or compatible serialised " 2237 "archive. Will re-cache from store.", 2238 StoreObjectInfoFile.c_str()); 2239 return; 2240 } 2241 2242 // 2243 // get a bit optimistic and read in a string identifier 2244 // 2245 std::string strMagicValue; 2246 anArchive.Read(strMagicValue); 2247 2248 if (strMagicValue != STOREOBJECTINFO_MAGIC_ID_STRING) 2249 { 2250 ::syslog(LOG_WARNING, "Store object info file '%s' " 2251 "is not a valid or compatible serialised " 2252 "archive. Will re-cache from store.", 2253 StoreObjectInfoFile.c_str()); 2254 return; 2255 } 2256 2257 // 2258 // check if we are loading some future format 2259 // version by mistake 2260 // 2261 int iVersion = 0; 2262 anArchive.Read(iVersion); 2263 2264 if (iVersion != STOREOBJECTINFO_VERSION) 2265 { 2266 ::syslog(LOG_WARNING, "Store object info file '%s' " 2267 "version [%d] unsupported. " 2268 "Will re-cache from store.", 2269 StoreObjectInfoFile.c_str(), 2270 iVersion); 2271 return; 2272 } 2273 2274 // 2275 // check if this state file is even valid 2276 // for the loaded bbackupd.conf file 2277 // 2278 box_time_t lastKnownConfigModTime; 2279 anArchive.Read(lastKnownConfigModTime); 2280 2281 if (lastKnownConfigModTime != GetLoadedConfigModifiedTime()) 2282 { 2283 ::syslog(LOG_WARNING, "Store object info file '%s' " 2284 "out of date. Will re-cache from store", 2285 StoreObjectInfoFile.c_str()); 2286 return; 2287 } 2288 2289 // 2290 // this is it, go at it 2291 // 2292 anArchive.Read(aClientStoreMarker); 2293 anArchive.Read(theLastSyncTime); 2294 anArchive.Read(theNextSyncTime); 2295 2296 // 2297 // 2298 // 2299 int64_t iCount = 0; 2300 anArchive.Read(iCount); 2301 2302 for (int v = 0; v < iCount; v++) 2303 { 2304 Location* pLocation = new Location; 2305 if (!pLocation) 2306 throw std::bad_alloc(); 2307 2308 pLocation->Deserialize(anArchive); 2309 mLocations.push_back(pLocation); 2310 } 2311 2312 // 2313 // 2314 // 2315 iCount = 0; 2316 anArchive.Read(iCount); 2317 2318 for (int v = 0; v < iCount; v++) 2319 { 2320 std::string strItem; 2321 anArchive.Read(strItem); 2322 2323 mIDMapMounts.push_back(strItem); 2324 } 2325 2326 // 2327 // 2328 // 2329 aFile.Close(); 2330 ::syslog(LOG_INFO, "Loaded store object info file '%s', " 2331 "version [%d]", StoreObjectInfoFile.c_str(), 2332 iVersion); 2333 2334 if (::unlink(StoreObjectInfoFile.c_str()) != 0) 2335 { 2336 ::syslog(LOG_ERR, "Failed to delete the old " 2337 "store object info file '%s': %s", 2338 StoreObjectInfoFile.c_str(), strerror(errno)); 2339 } 2340 } 2341 catch (...) 2342 { 2343 DeleteAllLocations(); 2344 2345 aClientStoreMarker = 2346 BackupClientContext::ClientStoreMarker_NotKnown; 2347 theLastSyncTime = 0; 2348 theNextSyncTime = 0; 2349 2350 ::syslog(LOG_WARNING, "Requested store object info file '%s' " 2351 "does not exist, not accessible, or inconsistent. " 2352 "Will re-cache from store.", 2353 StoreObjectInfoFile.c_str()); 2354 } 2355 }
Note: See TracChangeset
for help on using the changeset viewer.
