Savarese Software Research Corporation
util.cc
Go to the documentation of this file.
00001 /*
00002  * Copyright 2006-2009 Savarese Software Research Corporation
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     https://www.savarese.com/software/ApacheLicense-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 
00017 #include <sys/stat.h>
00018 #include <fcntl.h>
00019 #include <unistd.h>
00020 
00021 #include <ssrc/wispers/renderer/util.h>
00022 
00023 #include <boost/filesystem/convenience.hpp>
00024 
00025 __BEGIN_NS_SSRC_WSPR_RENDERER
00026 
00027 void cache_to_file(const char* buf, const unsigned int content_length,
00028                    const path & cached_path)
00029 {
00030   const path dir = cached_path.parent_path();
00031 
00032   if(!boost::filesystem::exists(dir)) {
00033     boost::filesystem::create_directories(dir);
00034   }
00035 
00036   // TODO: how about O_NONBLOCK and handling I/O in event loop?
00037   // Probably not doable.
00038   int fd(::open(cached_path.string().c_str(),
00039                 O_CREAT | O_WRONLY | O_EXCL, S_IRUSR | S_IRGRP));
00040   
00041   if(fd != -1) {
00042     struct ::flock lock;
00043 
00044     lock.l_type = F_WRLCK;
00045     lock.l_whence = SEEK_SET;
00046     lock.l_start = lock.l_len = 0;
00047 
00048     // If we can't get the lock, assume other process is rendering the file.
00049     if(::fcntl(fd, F_SETLK, &lock) == 0) {
00050       ::ssize_t written(0);
00051       // TODO: handle error where written == -1.
00052       // encapsulate this in a utility function or other.  See LAF.
00053       for(unsigned int length = content_length; length > 0 && written > -1;
00054           length-=written, buf+=written)
00055         written = ::write(fd, buf, length);
00056     }
00057     ::close(fd);
00058   }
00059 }
00060 
00064 void init_path_pattern(boost::regex & path_pattern,
00065                        const std::vector<string> & directories,
00066                        const string & leaf_pattern)
00067 {
00068   unsigned int i = directories.size();
00069   string pattern;
00070 
00071   if(i > 0) {
00072     pattern.append("\\A(?:");
00073     while(i-- > 1)
00074       pattern.append("\\Q").append(directories[i])
00075         .append("\\E|");
00076     pattern.append("\\Q").append(directories[0]).append("\\E)/")
00077       .append(leaf_pattern).append("\\Z");
00078 
00079     path_pattern = pattern;
00080   }
00081 }
00082 
00083 __END_NS_SSRC_WSPR_RENDERER

Savarese Software Research Corporation
Copyright © 2006-2011 Savarese Software Research Corporation. All rights reserved.