Home | History | Annotate | Download | only in efl
      1 /*
      2  * Copyright (C) 2007, 2008 Apple Inc. All rights reserved.
      3  * Copyright (C) 2008 Collabora, Ltd. All rights reserved.
      4  * Copyright (C) 2008 Kenneth Rohde Christiansen.
      5  * Copyright (C) 2009-2010 ProFUSION embedded systems
      6  * Copyright (C) 2009-2010 Samsung Electronics
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  *
     12  * 1.  Redistributions of source code must retain the above copyright
     13  *     notice, this list of conditions and the following disclaimer.
     14  * 2.  Redistributions in binary form must reproduce the above copyright
     15  *     notice, this list of conditions and the following disclaimer in the
     16  *     documentation and/or other materials provided with the distribution.
     17  * 3.  Neither the name of Apple Computer, Inc. ("Apple") nor the names of
     18  *     its contributors may be used to endorse or promote products derived
     19  *     from this software without specific prior written permission.
     20  *
     21  * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
     22  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     23  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     24  * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
     25  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     26  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     27  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
     28  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     30  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     31  */
     32 
     33 #include "config.h"
     34 #include "FileSystem.h"
     35 
     36 #include "NotImplemented.h"
     37 
     38 #include <Ecore.h>
     39 #include <Ecore_File.h>
     40 #include <Eina.h>
     41 #include <dirent.h>
     42 #include <dlfcn.h>
     43 #include <errno.h>
     44 #include <fnmatch.h>
     45 #if ENABLE(GLIB_SUPPORT)
     46 #include <glib.h> // TODO: remove me after following TODO is solved.
     47 #endif
     48 #include <limits.h>
     49 #include <stdio.h>
     50 #include <sys/stat.h>
     51 #include <unistd.h>
     52 #include <wtf/text/CString.h>
     53 
     54 namespace WebCore {
     55 
     56 CString fileSystemRepresentation(const String& path)
     57 {
     58 // WARNING: this is just used by platform/network/soup, thus must be GLIB!!!
     59 // TODO: move this to CString and use it instead in both, being more standard
     60 #if !PLATFORM(WIN_OS) && defined(WTF_USE_SOUP)
     61     char* filename = g_uri_unescape_string(path.utf8().data(), 0);
     62     CString cfilename(filename);
     63     g_free(filename);
     64     return cfilename;
     65 #else
     66     return path.utf8();
     67 #endif
     68 }
     69 
     70 String openTemporaryFile(const String& prefix, PlatformFileHandle& handle)
     71 {
     72     char buffer[PATH_MAX];
     73     const char* tmpDir = getenv("TMPDIR");
     74 
     75     if (!tmpDir)
     76         tmpDir = "/tmp";
     77 
     78     if (snprintf(buffer, PATH_MAX, "%s/%sXXXXXX", tmpDir, prefix.utf8().data()) >= PATH_MAX)
     79         goto end;
     80 
     81     handle = mkstemp(buffer);
     82     if (handle < 0)
     83         goto end;
     84 
     85     return String::fromUTF8(buffer);
     86 
     87 end:
     88     handle = invalidPlatformFileHandle;
     89     return String();
     90 }
     91 
     92 bool unloadModule(PlatformModule module)
     93 {
     94     // caution, closing handle will make memory vanish and any remaining
     95     // timer, idler, threads or any other left-over will crash,
     96     // maybe just ignore this is a safer solution?
     97     return !dlclose(module);
     98 }
     99 
    100 String homeDirectoryPath()
    101 {
    102     const char *home = getenv("HOME");
    103     if (!home) {
    104         home = getenv("TMPDIR");
    105         if (!home)
    106             home = "/tmp";
    107     }
    108     return String::fromUTF8(home);
    109 }
    110 
    111 Vector<String> listDirectory(const String& path, const String& filter)
    112 {
    113     Vector<String> entries;
    114     CString cpath = path.utf8();
    115     CString cfilter = filter.utf8();
    116     char filePath[PATH_MAX];
    117     char* fileName;
    118     size_t fileNameSpace;
    119     DIR* dir;
    120 
    121     if (cpath.length() + NAME_MAX >= sizeof(filePath))
    122         return entries;
    123     // loop invariant: directory part + '/'
    124     memcpy(filePath, cpath.data(), cpath.length());
    125     fileName = filePath + cpath.length();
    126     if (cpath.length() > 0 && filePath[cpath.length() - 1] != '/') {
    127         fileName[0] = '/';
    128         fileName++;
    129     }
    130     fileNameSpace = sizeof(filePath) - (fileName - filePath) - 1;
    131 
    132     dir = opendir(cpath.data());
    133     if (!dir)
    134         return entries;
    135 
    136     struct dirent* de;
    137     while (de = readdir(dir)) {
    138         size_t nameLen;
    139         if (de->d_name[0] == '.') {
    140             if (de->d_name[1] == '\0')
    141                 continue;
    142             if (de->d_name[1] == '.' && de->d_name[2] == '\0')
    143                 continue;
    144         }
    145         if (fnmatch(cfilter.data(), de->d_name, 0))
    146             continue;
    147 
    148         nameLen = strlen(de->d_name);
    149         if (nameLen >= fileNameSpace)
    150             continue; // maybe assert? it should never happen anyway...
    151 
    152         memcpy(fileName, de->d_name, nameLen + 1);
    153         entries.append(filePath);
    154     }
    155     closedir(dir);
    156     return entries;
    157 }
    158 
    159 }
    160