3 * Copyright 2004--2006, Google Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 #include "talk/base/unixfilesystem.h"
37 #include <Carbon/Carbon.h>
38 #include <IOKit/IOCFBundle.h>
39 #include <sys/statvfs.h>
40 #include "talk/base/macutils.h"
43 #if defined(POSIX) && !defined(OSX)
44 #include <sys/types.h>
46 #include <sys/statfs.h>
47 #elif !defined(__native_client__)
48 #include <sys/statvfs.h>
49 #endif // !defined(__native_client__)
53 #endif // POSIX && !OSX
60 #if defined(__native_client__) && !defined(__GLIBC__)
61 #include <sys/syslimits.h>
64 #include "talk/base/fileutils.h"
65 #include "talk/base/pathutils.h"
66 #include "talk/base/stream.h"
67 #include "talk/base/stringutils.h"
70 // Defined in iosfilesystem.mm. No header file to discourage use
71 // elsewhere; other places should use GetApp{Data,Temp}Folder() in
72 // this file. Don't copy/paste. I mean it.
73 char* IOSDataDirectory();
74 char* IOSTempDirectory();
75 void IOSAppName(talk_base::Pathname* path);
80 #if !defined(ANDROID) && !defined(IOS)
81 char* UnixFilesystem::app_temp_path_ = NULL;
83 char* UnixFilesystem::provided_app_data_folder_ = NULL;
84 char* UnixFilesystem::provided_app_temp_folder_ = NULL;
86 void UnixFilesystem::SetAppDataFolder(const std::string& folder) {
87 delete [] provided_app_data_folder_;
88 provided_app_data_folder_ = CopyString(folder);
91 void UnixFilesystem::SetAppTempFolder(const std::string& folder) {
92 delete [] provided_app_temp_folder_;
93 provided_app_temp_folder_ = CopyString(folder);
97 UnixFilesystem::UnixFilesystem() {
99 if (!provided_app_data_folder_)
100 provided_app_data_folder_ = IOSDataDirectory();
101 if (!provided_app_temp_folder_)
102 provided_app_temp_folder_ = IOSTempDirectory();
106 UnixFilesystem::~UnixFilesystem() {}
108 bool UnixFilesystem::CreateFolder(const Pathname &path, mode_t mode) {
109 std::string pathname(path.pathname());
110 int len = pathname.length();
111 if ((len == 0) || (pathname[len - 1] != '/'))
115 int res = ::stat(pathname.c_str(), &st);
117 // Something exists at this location, check if it is a directory
118 return S_ISDIR(st.st_mode) != 0;
119 } else if (errno != ENOENT) {
124 // Directory doesn't exist, look up one directory level
127 } while ((len > 0) && (pathname[len - 1] != '/'));
129 if (!CreateFolder(Pathname(pathname.substr(0, len)), mode)) {
133 LOG(LS_INFO) << "Creating folder: " << pathname;
134 return (0 == ::mkdir(pathname.c_str(), mode));
137 bool UnixFilesystem::CreateFolder(const Pathname &path) {
138 return CreateFolder(path, 0755);
141 FileStream *UnixFilesystem::OpenFile(const Pathname &filename,
142 const std::string &mode) {
143 FileStream *fs = new FileStream();
144 if (fs && !fs->Open(filename.pathname().c_str(), mode.c_str(), NULL)) {
151 bool UnixFilesystem::CreatePrivateFile(const Pathname &filename) {
152 int fd = open(filename.pathname().c_str(),
153 O_RDWR | O_CREAT | O_EXCL,
156 LOG_ERR(LS_ERROR) << "open() failed.";
159 // Don't need to keep the file descriptor.
161 LOG_ERR(LS_ERROR) << "close() failed.";
167 bool UnixFilesystem::DeleteFile(const Pathname &filename) {
168 LOG(LS_INFO) << "Deleting file:" << filename.pathname();
170 if (!IsFile(filename)) {
171 ASSERT(IsFile(filename));
174 return ::unlink(filename.pathname().c_str()) == 0;
177 bool UnixFilesystem::DeleteEmptyFolder(const Pathname &folder) {
178 LOG(LS_INFO) << "Deleting folder" << folder.pathname();
180 if (!IsFolder(folder)) {
181 ASSERT(IsFolder(folder));
184 std::string no_slash(folder.pathname(), 0, folder.pathname().length()-1);
185 return ::rmdir(no_slash.c_str()) == 0;
188 bool UnixFilesystem::GetTemporaryFolder(Pathname &pathname, bool create,
189 const std::string *append) {
192 if (0 != FSFindFolder(kOnAppropriateDisk, kTemporaryFolderType,
195 unsigned char buffer[NAME_MAX+1];
196 if (0 != FSRefMakePath(&fr, buffer, ARRAY_SIZE(buffer)))
198 pathname.SetPathname(reinterpret_cast<char*>(buffer), "");
199 #elif defined(ANDROID) || defined(IOS)
200 ASSERT(provided_app_temp_folder_ != NULL);
201 pathname.SetPathname(provided_app_temp_folder_, "");
202 #else // !OSX && !ANDROID
203 if (const char* tmpdir = getenv("TMPDIR")) {
204 pathname.SetPathname(tmpdir, "");
205 } else if (const char* tmp = getenv("TMP")) {
206 pathname.SetPathname(tmp, "");
209 pathname.SetPathname(P_tmpdir, "");
211 pathname.SetPathname("/tmp/", "");
214 #endif // !OSX && !ANDROID
216 ASSERT(!append->empty());
217 pathname.AppendFolder(*append);
219 return !create || CreateFolder(pathname);
222 std::string UnixFilesystem::TempFilename(const Pathname &dir,
223 const std::string &prefix) {
224 int len = dir.pathname().size() + prefix.size() + 2 + 6;
225 char *tempname = new char[len];
227 snprintf(tempname, len, "%s/%sXXXXXX", dir.pathname().c_str(),
229 int fd = ::mkstemp(tempname);
232 std::string ret(tempname);
238 bool UnixFilesystem::MoveFile(const Pathname &old_path,
239 const Pathname &new_path) {
240 if (!IsFile(old_path)) {
241 ASSERT(IsFile(old_path));
244 LOG(LS_VERBOSE) << "Moving " << old_path.pathname()
245 << " to " << new_path.pathname();
246 if (rename(old_path.pathname().c_str(), new_path.pathname().c_str()) != 0) {
249 if (!CopyFile(old_path, new_path))
251 if (!DeleteFile(old_path))
257 bool UnixFilesystem::MoveFolder(const Pathname &old_path,
258 const Pathname &new_path) {
259 if (!IsFolder(old_path)) {
260 ASSERT(IsFolder(old_path));
263 LOG(LS_VERBOSE) << "Moving " << old_path.pathname()
264 << " to " << new_path.pathname();
265 if (rename(old_path.pathname().c_str(), new_path.pathname().c_str()) != 0) {
268 if (!CopyFolder(old_path, new_path))
270 if (!DeleteFolderAndContents(old_path))
276 bool UnixFilesystem::IsFolder(const Pathname &path) {
278 if (stat(path.pathname().c_str(), &st) < 0)
280 return S_ISDIR(st.st_mode);
283 bool UnixFilesystem::CopyFile(const Pathname &old_path,
284 const Pathname &new_path) {
285 LOG(LS_VERBOSE) << "Copying " << old_path.pathname()
286 << " to " << new_path.pathname();
290 StreamInterface *source = OpenFile(old_path, "rb");
294 StreamInterface *dest = OpenFile(new_path, "wb");
300 while (source->Read(buf, sizeof(buf), &len, NULL) == SR_SUCCESS)
301 dest->Write(buf, len, NULL, NULL);
308 bool UnixFilesystem::IsTemporaryPath(const Pathname& pathname) {
309 #if defined(ANDROID) || defined(IOS)
310 ASSERT(provided_app_temp_folder_ != NULL);
313 const char* const kTempPrefixes[] = {
314 #if defined(ANDROID) || defined(IOS)
315 provided_app_temp_folder_,
317 "/tmp/", "/var/tmp/",
319 "/private/tmp/", "/private/var/tmp/", "/private/var/folders/",
321 #endif // ANDROID || IOS
323 for (size_t i = 0; i < ARRAY_SIZE(kTempPrefixes); ++i) {
324 if (0 == strncmp(pathname.pathname().c_str(), kTempPrefixes[i],
325 strlen(kTempPrefixes[i])))
331 bool UnixFilesystem::IsFile(const Pathname& pathname) {
333 int res = ::stat(pathname.pathname().c_str(), &st);
334 // Treat symlinks, named pipes, etc. all as files.
335 return res == 0 && !S_ISDIR(st.st_mode);
338 bool UnixFilesystem::IsAbsent(const Pathname& pathname) {
340 int res = ::stat(pathname.pathname().c_str(), &st);
341 // Note: we specifically maintain ENOTDIR as an error, because that implies
342 // that you could not call CreateFolder(pathname).
343 return res != 0 && ENOENT == errno;
346 bool UnixFilesystem::GetFileSize(const Pathname& pathname, size_t *size) {
348 if (::stat(pathname.pathname().c_str(), &st) != 0)
354 bool UnixFilesystem::GetFileTime(const Pathname& path, FileTimeType which,
357 if (::stat(path.pathname().c_str(), &st) != 0)
375 bool UnixFilesystem::GetAppPathname(Pathname* path) {
377 ProcessSerialNumber psn = { 0, kCurrentProcess };
378 CFDictionaryRef procinfo = ProcessInformationCopyDictionary(&psn,
379 kProcessDictionaryIncludeAllInformationMask);
380 if (NULL == procinfo)
382 CFStringRef cfpath = (CFStringRef) CFDictionaryGetValue(procinfo,
383 kIOBundleExecutableKey);
385 bool success = ToUtf8(cfpath, &path8);
388 path->SetPathname(path8);
390 #elif defined(__native_client__)
396 char buffer[PATH_MAX + 2];
397 ssize_t len = readlink("/proc/self/exe", buffer, ARRAY_SIZE(buffer) - 1);
398 if ((len <= 0) || (len == PATH_MAX + 1))
401 path->SetPathname(buffer);
406 bool UnixFilesystem::GetAppDataFolder(Pathname* path, bool per_user) {
407 ASSERT(!organization_name_.empty());
408 ASSERT(!application_name_.empty());
410 // First get the base directory for app data.
413 // Use ~/Library/Application Support/<orgname>/<appname>/
415 if (0 != FSFindFolder(kUserDomain, kApplicationSupportFolderType,
418 unsigned char buffer[NAME_MAX+1];
419 if (0 != FSRefMakePath(&fr, buffer, ARRAY_SIZE(buffer)))
421 path->SetPathname(reinterpret_cast<char*>(buffer), "");
426 #elif defined(ANDROID) || defined(IOS) // && !OSX
427 ASSERT(provided_app_data_folder_ != NULL);
428 path->SetPathname(provided_app_data_folder_, "");
429 #elif defined(LINUX) // && !OSX && !defined(ANDROID) && !defined(IOS)
431 // We follow the recommendations in
432 // http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html
433 // It specifies separate directories for data and config files, but
434 // GetAppDataFolder() does not distinguish. We just return the config dir
436 const char* xdg_config_home = getenv("XDG_CONFIG_HOME");
437 if (xdg_config_home) {
438 path->SetPathname(xdg_config_home, "");
440 // XDG says to default to $HOME/.config. We also support falling back to
441 // other synonyms for HOME if for some reason it is not defined.
443 if (const char* home = getenv("HOME")) {
445 } else if (const char* dotdir = getenv("DOTDIR")) {
447 } else if (passwd* pw = getpwuid(geteuid())) {
448 homedir = pw->pw_dir;
452 path->SetPathname(homedir, "");
453 path->AppendFolder(".config");
456 // XDG does not define a standard directory for writable global data. Let's
458 path->SetPathname("/var/cache/", "");
460 #endif // !OSX && !defined(ANDROID) && !defined(LINUX)
462 // Now add on a sub-path for our app.
463 #if defined(OSX) || defined(ANDROID) || defined(IOS)
464 path->AppendFolder(organization_name_);
465 path->AppendFolder(application_name_);
467 // XDG says to use a single directory level, so we concatenate the org and app
468 // name with a hyphen. We also do the Linuxy thing and convert to all
469 // lowercase with no spaces.
470 std::string subdir(organization_name_);
472 subdir.append(application_name_);
473 replace_substrs(" ", 1, "", 0, &subdir);
474 std::transform(subdir.begin(), subdir.end(), subdir.begin(), ::tolower);
475 path->AppendFolder(subdir);
477 if (!CreateFolder(*path, 0700)) {
480 #if !defined(__native_client__)
481 // If the folder already exists, it may have the wrong mode or be owned by
482 // someone else, both of which are security problems. Setting the mode
483 // avoids both issues since it will fail if the path is not owned by us.
484 if (0 != ::chmod(path->pathname().c_str(), 0700)) {
485 LOG_ERR(LS_ERROR) << "Can't set mode on " << path;
492 bool UnixFilesystem::GetAppTempFolder(Pathname* path) {
493 #if defined(ANDROID) || defined(IOS)
494 ASSERT(provided_app_temp_folder_ != NULL);
495 path->SetPathname(provided_app_temp_folder_);
498 ASSERT(!application_name_.empty());
499 // TODO: Consider whether we are worried about thread safety.
500 if (app_temp_path_ != NULL && strlen(app_temp_path_) > 0) {
501 path->SetPathname(app_temp_path_);
505 // Create a random directory as /tmp/<appname>-<pid>-<timestamp>
507 sprintfn(buffer, ARRAY_SIZE(buffer), "-%d-%d",
508 static_cast<int>(getpid()),
509 static_cast<int>(time(0)));
510 std::string folder(application_name_);
511 folder.append(buffer);
512 if (!GetTemporaryFolder(*path, true, &folder))
515 delete [] app_temp_path_;
516 app_temp_path_ = CopyString(path->pathname());
517 // TODO: atexit(DeleteFolderAndContents(app_temp_path_));
522 bool UnixFilesystem::GetDiskFreeSpace(const Pathname& path, int64 *freebytes) {
523 #ifdef __native_client__
525 #else // __native_client__
526 ASSERT(NULL != freebytes);
527 // TODO: Consider making relative paths absolute using cwd.
528 // TODO: When popping off a symlink, push back on the components of the
529 // symlink, so we don't jump out of the target disk inadvertently.
530 Pathname existing_path(path.folder(), "");
531 while (!existing_path.folder().empty() && IsAbsent(existing_path)) {
532 existing_path.SetFolder(existing_path.parent_folder());
536 memset(&vfs, 0, sizeof(vfs));
537 if (0 != statfs(existing_path.pathname().c_str(), &vfs))
541 memset(&vfs, 0, sizeof(vfs));
542 if (0 != statvfs(existing_path.pathname().c_str(), &vfs))
545 #if defined(LINUX) || defined(ANDROID)
546 *freebytes = static_cast<int64>(vfs.f_bsize) * vfs.f_bavail;
547 #elif defined(OSX) || defined(IOS)
548 *freebytes = static_cast<int64>(vfs.f_frsize) * vfs.f_bavail;
552 #endif // !__native_client__
555 Pathname UnixFilesystem::GetCurrentDirectory() {
557 char buffer[PATH_MAX];
558 char *path = getcwd(buffer, PATH_MAX);
561 LOG_ERR(LS_ERROR) << "getcwd() failed";
562 return cwd; // returns empty pathname
564 cwd.SetFolder(std::string(path));
569 char* UnixFilesystem::CopyString(const std::string& str) {
570 size_t size = str.length() + 1;
572 char* buf = new char[size];
577 strcpyn(buf, size, str.c_str());
581 } // namespace talk_base
583 #if defined(__native_client__)
584 extern "C" int __attribute__((weak))
585 link(const char* oldpath, const char* newpath) {