Home | History | Annotate | Download | only in android
      1 /*
      2  * Copyright 2009, The Android Open Source Project
      3  * Copyright (C) 2007 Holger Hans Peter Freyther
      4  * All rights reserved.
      5  *
      6  * Redistribution and use in source and binary forms, with or without
      7  * modification, are permitted provided that the following conditions
      8  * are met:
      9  * 1. Redistributions of source code must retain the above copyright
     10  *    notice, this list of conditions and the following disclaimer.
     11  * 2. Redistributions in binary form must reproduce the above copyright
     12  *    notice, this list of conditions and the following disclaimer in the
     13  *    documentation and/or other materials provided with the distribution.
     14  *
     15  * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
     16  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     18  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
     19  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
     20  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     21  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
     22  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
     23  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     25  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     26  */
     27 
     28 #include "config.h"
     29 #include "FileSystem.h"
     30 
     31 #include "PlatformBridge.h"
     32 #include "cutils/log.h"
     33 #include <dirent.h>
     34 #include <dlfcn.h>
     35 #include <errno.h>
     36 #include <fnmatch.h>
     37 #include <sys/stat.h>
     38 #include <wtf/text/CString.h>
     39 #include <wtf/text/StringBuilder.h>
     40 
     41 namespace WebCore {
     42 
     43 // Global static used to store the base to the plugin path.
     44 // This is set in WebSettings.cpp
     45 String sPluginPath;
     46 
     47 CString fileSystemRepresentation(const String& path)
     48 {
     49     // If the path is a content:// URI then ask Java to resolve it for us.
     50     if (path.startsWith("content://"))
     51         return PlatformBridge::resolveFilePathForContentUri(path).utf8();
     52     else if (path.startsWith("file://"))
     53         return path.substring(strlen("file://")).utf8();
     54 
     55     return path.utf8();
     56 }
     57 
     58 String openTemporaryFile(const String& prefix, PlatformFileHandle& handle)
     59 {
     60     int number = rand() % 10000 + 1;
     61     String filename;
     62     do {
     63         StringBuilder builder;
     64         builder.append(sPluginPath);
     65         builder.append('/');
     66         builder.append(prefix);
     67         builder.append(String::number(number));
     68         filename = builder.toString();
     69         handle = open(filename.utf8().data(), O_WRONLY | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR);
     70         number++;
     71 
     72         if (handle != -1)
     73             return filename;
     74     } while (errno == EEXIST);
     75 
     76     return String();
     77 }
     78 
     79 bool unloadModule(PlatformModule module)
     80 {
     81     return !dlclose(module);
     82 }
     83 
     84 String homeDirectoryPath()
     85 {
     86     return sPluginPath;
     87 }
     88 
     89 // We define our own pathGetFileName rather than use the POSIX versions as we
     90 // may get passed a content URI representing the path to the file. We pass
     91 // the input through fileSystemRepresentation before using it to resolve it if
     92 // it is a content URI.
     93 String pathGetFileName(const String& path)
     94 {
     95     CString fsRep = fileSystemRepresentation(path);
     96     String fsPath = String(fsRep.data());
     97     return fsPath.substring(fsPath.reverseFind('/') + 1);
     98 }
     99 
    100 
    101 } // namespace WebCore
    102