llapr.cpp

Go to the documentation of this file.
00001 
00034 #include "linden_common.h"
00035 #include "llapr.h"
00036 
00037 apr_pool_t *gAPRPoolp = NULL; // Global APR memory pool
00038 apr_thread_mutex_t *gLogMutexp = NULL;
00039 
00040 
00041 void ll_init_apr()
00042 {
00043         if (!gAPRPoolp)
00044         {
00045                 // Initialize APR and create the global pool
00046                 apr_initialize();
00047                 apr_pool_create(&gAPRPoolp, NULL);
00048 
00049                 // Initialize the logging mutex
00050                 apr_thread_mutex_create(&gLogMutexp, APR_THREAD_MUTEX_UNNESTED, gAPRPoolp);
00051         }
00052 }
00053 
00054 
00055 void ll_cleanup_apr()
00056 {
00057         llinfos << "Cleaning up APR" << llendl;
00058 
00059         if (gLogMutexp)
00060         {
00061                 // Clean up the logging mutex
00062 
00063                 // All other threads NEED to be done before we clean up APR, so this is okay.
00064                 apr_thread_mutex_destroy(gLogMutexp);
00065                 gLogMutexp = NULL;
00066         }
00067         if (gAPRPoolp)
00068         {
00069                 apr_pool_destroy(gAPRPoolp);
00070                 gAPRPoolp = NULL;
00071         }
00072         apr_terminate();
00073 }
00074 
00075 //
00076 // LLScopedLock
00077 //
00078 LLScopedLock::LLScopedLock(apr_thread_mutex_t* mutex) : mMutex(mutex)
00079 {
00080         if(mutex)
00081         {
00082                 if(ll_apr_warn_status(apr_thread_mutex_lock(mMutex)))
00083                 {
00084                         mLocked = false;
00085                 }
00086                 else
00087                 {
00088                         mLocked = true;
00089                 }
00090         }
00091         else
00092         {
00093                 mLocked = false;
00094         }
00095 }
00096 
00097 LLScopedLock::~LLScopedLock()
00098 {
00099         unlock();
00100 }
00101 
00102 void LLScopedLock::unlock()
00103 {
00104         if(mLocked)
00105         {
00106                 if(!ll_apr_warn_status(apr_thread_mutex_unlock(mMutex)))
00107                 {
00108                         mLocked = false;
00109                 }
00110         }
00111 }
00112 
00113 //
00114 // Misc functions
00115 //
00116 bool ll_apr_warn_status(apr_status_t status)
00117 {
00118         if(APR_SUCCESS == status) return false;
00119         char buf[MAX_STRING];   /* Flawfinder: ignore */
00120         llwarns << "APR: " << apr_strerror(status, buf, MAX_STRING) << llendl;
00121         return true;
00122 }
00123 
00124 void ll_apr_assert_status(apr_status_t status)
00125 {
00126         llassert(ll_apr_warn_status(status) == false);
00127 }
00128 
00129 // File I/O
00130 apr_file_t* ll_apr_file_open(const LLString& filename, apr_int32_t flags, S32* sizep, apr_pool_t* pool)
00131 {
00132         apr_file_t* apr_file;
00133         apr_status_t s;
00134         if (pool == NULL) pool = gAPRPoolp;
00135         s = apr_file_open(&apr_file, filename.c_str(), flags, APR_OS_DEFAULT, pool);
00136         if (s != APR_SUCCESS)
00137         {
00138                 if (sizep)
00139                 {
00140                         *sizep = 0;
00141                 }
00142                 return NULL;
00143         }
00144 
00145         if (sizep)
00146         {
00147                 S32 file_size = 0;
00148                 apr_off_t offset = 0;
00149                 if (apr_file_seek(apr_file, APR_END, &offset) == APR_SUCCESS)
00150                 {
00151                         llassert_always(offset <= 0x7fffffff);
00152                         file_size = (S32)offset;
00153                         offset = 0;
00154                         apr_file_seek(apr_file, APR_SET, &offset);
00155                 }
00156                 *sizep = file_size;
00157         }
00158 
00159         return apr_file;
00160 }
00161 apr_file_t* ll_apr_file_open(const LLString& filename, apr_int32_t flags, S32* sizep)
00162 {
00163         return ll_apr_file_open(filename, flags, sizep, NULL);
00164 }
00165 apr_file_t* ll_apr_file_open(const LLString& filename, apr_int32_t flags, apr_pool_t* pool)
00166 {
00167         return ll_apr_file_open(filename, flags, NULL, pool);
00168 }
00169 apr_file_t* ll_apr_file_open(const LLString& filename, apr_int32_t flags)
00170 {
00171         return ll_apr_file_open(filename, flags, NULL, NULL);
00172 }
00173 
00174 S32 ll_apr_file_read(apr_file_t* apr_file, void *buf, S32 nbytes)
00175 {
00176         apr_size_t sz = nbytes;
00177         apr_status_t s = apr_file_read(apr_file, buf, &sz);
00178         if (s != APR_SUCCESS)
00179         {
00180                 return 0;
00181         }
00182         else
00183         {
00184                 llassert_always(sz <= 0x7fffffff);
00185                 return (S32)sz;
00186         }
00187 }
00188 
00189 S32 ll_apr_file_read_ex(const LLString& filename, apr_pool_t* pool, void *buf, S32 offset, S32 nbytes)
00190 {
00191         if (pool == NULL) pool = gAPRPoolp;
00192         apr_file_t* filep = ll_apr_file_open(filename, APR_READ|APR_BINARY, pool);
00193         if (!filep)
00194         {
00195                 return 0;
00196         }
00197         S32 off;
00198         if (offset < 0)
00199                 off = ll_apr_file_seek(filep, APR_END, 0);
00200         else
00201                 off = ll_apr_file_seek(filep, APR_SET, offset);
00202         S32 bytes_read;
00203         if (off < 0)
00204         {
00205                 bytes_read = 0;
00206         }
00207         else
00208         {
00209                 bytes_read = ll_apr_file_read(filep, buf, nbytes );
00210         }
00211         apr_file_close(filep);
00212 
00213         return bytes_read;
00214 }
00215 
00216 S32 ll_apr_file_write(apr_file_t* apr_file, const void *buf, S32 nbytes)
00217 {
00218         apr_size_t sz = nbytes;
00219         apr_status_t s = apr_file_write(apr_file, buf, &sz);
00220         if (s != APR_SUCCESS)
00221         {
00222                 return 0;
00223         }
00224         else
00225         {
00226                 llassert_always(sz <= 0x7fffffff);
00227                 return (S32)sz;
00228         }
00229 }
00230 
00231 S32 ll_apr_file_write_ex(const LLString& filename, apr_pool_t* pool, void *buf, S32 offset, S32 nbytes)
00232 {
00233         if (pool == NULL) pool = gAPRPoolp;
00234         apr_int32_t flags = APR_CREATE|APR_WRITE|APR_BINARY;
00235         if (offset < 0)
00236         {
00237                 flags |= APR_APPEND;
00238                 offset = 0;
00239         }
00240         apr_file_t* filep = ll_apr_file_open(filename, flags, pool);
00241         if (!filep)
00242         {
00243                 return 0;
00244         }
00245         if (offset > 0)
00246         {
00247                 offset = ll_apr_file_seek(filep, APR_SET, offset);
00248         }
00249         S32 bytes_written;
00250         if (offset < 0)
00251         {
00252                 bytes_written = 0;
00253         }
00254         else
00255         {
00256                 bytes_written = ll_apr_file_write(filep, buf, nbytes );
00257         }
00258         apr_file_close(filep);
00259 
00260         return bytes_written;
00261 }
00262 
00263 S32 ll_apr_file_seek(apr_file_t* apr_file, apr_seek_where_t where, S32 offset)
00264 {
00265         apr_status_t s;
00266         apr_off_t apr_offset;
00267         if (offset >= 0)
00268         {
00269                 apr_offset = (apr_off_t)offset;
00270                 s = apr_file_seek(apr_file, where, &apr_offset);
00271         }
00272         else
00273         {
00274                 apr_offset = 0;
00275                 s = apr_file_seek(apr_file, APR_END, &apr_offset);
00276         }
00277         if (s != APR_SUCCESS)
00278         {
00279                 return -1;
00280         }
00281         else
00282         {
00283                 llassert_always(apr_offset <= 0x7fffffff);
00284                 return (S32)apr_offset;
00285         }
00286 }
00287 
00288 bool ll_apr_file_remove(const LLString& filename, apr_pool_t* pool)
00289 {
00290         apr_status_t s;
00291         if (pool == NULL) pool = gAPRPoolp;
00292         s = apr_file_remove(filename.c_str(), pool);
00293         if (s != APR_SUCCESS)
00294         {
00295                 llwarns << "ll_apr_file_remove failed on file: " << filename << llendl;
00296                 return false;
00297         }
00298         return true;
00299 }
00300 
00301 bool ll_apr_file_rename(const LLString& filename, const LLString& newname, apr_pool_t* pool)
00302 {
00303         apr_status_t s;
00304         if (pool == NULL) pool = gAPRPoolp;
00305         s = apr_file_rename(filename.c_str(), newname.c_str(), pool);
00306         if (s != APR_SUCCESS)
00307         {
00308                 llwarns << "ll_apr_file_rename failed on file: " << filename << llendl;
00309                 return false;
00310         }
00311         return true;
00312 }
00313 
00314 bool ll_apr_file_exists(const LLString& filename, apr_pool_t* pool)
00315 {
00316         apr_file_t* apr_file;
00317         apr_status_t s;
00318         if (pool == NULL) pool = gAPRPoolp;
00319         s = apr_file_open(&apr_file, filename.c_str(), APR_READ, APR_OS_DEFAULT, pool);
00320         if (s != APR_SUCCESS || !apr_file)
00321         {
00322                 return false;
00323         }
00324         else
00325         {
00326                 apr_file_close(apr_file);
00327                 return true;
00328         }
00329 }
00330 
00331 S32 ll_apr_file_size(const LLString& filename, apr_pool_t* pool)
00332 {
00333         apr_file_t* apr_file;
00334         apr_finfo_t info;
00335         apr_status_t s;
00336         if (pool == NULL) pool = gAPRPoolp;
00337         s = apr_file_open(&apr_file, filename.c_str(), APR_READ, APR_OS_DEFAULT, pool);
00338         if (s != APR_SUCCESS || !apr_file)
00339         {
00340                 return 0;
00341         }
00342         else
00343         {
00344                 apr_status_t s = apr_file_info_get(&info, APR_FINFO_SIZE, apr_file);
00345                 apr_file_close(apr_file);
00346                 if (s == APR_SUCCESS)
00347                 {
00348                         return (S32)info.size;
00349                 }
00350                 else
00351                 {
00352                         return 0;
00353                 }
00354         }
00355 }
00356 
00357 bool ll_apr_dir_make(const LLString& dirname, apr_pool_t* pool)
00358 {
00359         apr_status_t s;
00360         if (pool == NULL) pool = gAPRPoolp;
00361         s = apr_dir_make(dirname.c_str(), APR_FPROT_OS_DEFAULT, pool);
00362         if (s != APR_SUCCESS)
00363         {
00364                 llwarns << "ll_apr_file_remove failed on file: " << dirname << llendl;
00365                 return false;
00366         }
00367         return true;
00368 }
00369 
00370 bool ll_apr_dir_remove(const LLString& dirname, apr_pool_t* pool)
00371 {
00372         apr_status_t s;
00373         if (pool == NULL) pool = gAPRPoolp;
00374         s = apr_file_remove(dirname.c_str(), pool);
00375         if (s != APR_SUCCESS)
00376         {
00377                 llwarns << "ll_apr_file_remove failed on file: " << dirname << llendl;
00378                 return false;
00379         }
00380         return true;
00381 }
00382 

Generated on Thu Jul 1 06:08:19 2010 for Second Life Viewer by  doxygen 1.4.7