2 // Open Service Platform
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
9 // http://www.apache.org/licenses/LICENSE-2.0
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
19 * @file FIo_FileImpl.cpp
20 * @brief This is the implementation file for %_FileImpl class.
27 #include <sys/types.h>
28 #include <sys/mount.h>
32 #include <unique_ptr.h>
35 #include <FBaseSysLog.h>
36 #include <FAppPkgPackageInfo.h>
37 #include <FIoDirectory.h>
40 #include <FSysEnvironment.h>
42 #include <FBase_StringConverter.h>
43 #include <FApp_AppInfo.h>
44 #include <FAppPkg_PackageInfoImpl.h>
45 #include <FSys_EnvironmentImpl.h>
46 #include <FBase_NativeError.h>
48 #include "FIo_FileImpl.h"
49 #include "FIo_NormalFile.h"
50 #include "FIo_DirectoryImpl.h"
51 #include "FIo_SecureFile.h"
52 #include "FIo_SecureIoUtil.h"
53 #include "FIo_IFileCore.h"
54 #include "FIo_FileUtil.h"
55 #include "FIo_FileLockImpl.h"
58 using namespace Tizen::Base;
59 using namespace Tizen::App;
60 using namespace Tizen::System;
62 namespace Tizen { namespace Io
65 static const int _MAX_PATH_LENGTH = 128;
66 static const int _APP_UID = 5000;
67 static const size_t _MAX_FILE_OPENMODE_LENGTH = 3;
68 static const char _INTERNAL_MOUNT_FLAG[] = "/tmp/osp-compat/mount/internal";
69 static const char _EXTERNAL_MOUNT_FLAG[] = "/tmp/osp-compat/mount/external";
73 char path[_MAX_PATH_LENGTH];
75 bool appPrivilege; // false: root privilege
80 char srcPath[_MAX_PATH_LENGTH];
81 char destPath[_MAX_PATH_LENGTH];
86 char destPath[_MAX_PATH_LENGTH];
89 _FileImpl::_FileImpl(void)
95 , __pFileLockImpl(null)
99 _FileImpl::~_FileImpl(void)
102 if (__pFileLockImpl != null)
104 __pFileLockImpl->__pFileImpl = null;
108 _FileImpl::_FileImpl(const _FileImpl& fileImpl)
115 SysAssertf(false, "_FileImpl class does not support copy constructor.\n");
119 _FileImpl::operator =(const _FileImpl& fileImpl)
121 SysAssertf(false, "_FileImpl class does not support '=' operator.\n");
123 if (&fileImpl == this)
132 _FileImpl::VerifyFileOpenMode(const char* pOpenMode)
134 if (pOpenMode == null)
136 SysLog(NID_IO, "[E_INVALID_ARG] The specified openMode is null.");
140 if (strlen(pOpenMode) > _MAX_FILE_OPENMODE_LENGTH)
142 SysLog(NID_IO, "[E_INVALID_ARG] The specified openMode (%s) is invalid.", pOpenMode);
146 switch (pOpenMode[0])
160 SysLog(NID_IO, "[E_INVALID_ARG] The specified openMode (%s) is invalid.", pOpenMode);
164 switch (pOpenMode[1])
169 if (pOpenMode[2] == '\0' || pOpenMode[2] == 'b')
177 SysLog(NID_IO, "[E_INVALID_ARG] The specified openMode (%s) is invalid.", pOpenMode);
181 if (pOpenMode[2] == '\0')
185 else if (pOpenMode[2] == '+')
193 SysLog(NID_IO, "[E_INVALID_ARG] The specified openMode (%s) is invalid.", pOpenMode);
197 SysLog(NID_IO, "[E_INVALID_ARG] The specified openMode (%s) is invalid.", pOpenMode);
205 _FileImpl::Construct(const String& filePath, const String& openMode, bool createParentDirsToo, const ByteBuffer* pSecretKey)
207 SysAssertf(__pCore == null, "Already constructed. Calling Construct() twice or more on a same instance is not allowed for this class\n");
208 result r = E_SUCCESS;
210 if (openMode.Contains(L'r') == true)
212 SysTryReturnResult(NID_IO, !createParentDirsToo, E_INVALID_ARG,
213 "The specified createParentDirsToo cannot be used without file creation mode.");
216 if (createParentDirsToo == true)
221 r = filePath.LastIndexOf(L'/', filePath.GetLength() - 1, position);
222 SysTryReturnResult(NID_IO, r != E_OBJ_NOT_FOUND, E_INVALID_ARG, "The specified filePath is invalid.");
223 SysTryReturnResult(NID_IO, !(position == 0), E_INVALID_ARG, "The specified filePath is invalid.");
225 r = filePath.SubString(0, position, dirPath);
226 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Failed to extract dir path.", GetErrorMessage(r));
228 r = Directory::Create(dirPath, true);
231 if (r == E_FILE_ALREADY_EXIST)
237 SysPropagate(NID_IO, r);
243 unique_ptr<char[]> pOpenMode(_StringConverter::CopyToCharArrayN(openMode));
244 SysTryReturnResult(NID_IO, pOpenMode != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
246 return Construct(filePath, pOpenMode.get(), pSecretKey);
250 _FileImpl::Construct(const String& filePath, const char* pOpenMode, const ByteBuffer* pSecretKey)
252 SysAssertf(__pCore == null, "Already constructed. Calling Construct() twice or more on a same instance is not allowed for this class\n");
253 result r = E_SUCCESS;
255 bool isValidOpenMode = VerifyFileOpenMode(pOpenMode);
256 SysTryReturnResult(NID_IO, isValidOpenMode == true, E_INVALID_ARG, "The specified openMode is invalid. (%s)", pOpenMode);
258 SysTryReturnResult(NID_IO, VerifyFilePathCompatibility(filePath, _AppInfo::IsOspCompat()) == true,
259 E_INVALID_ARG, " [%ls] is not compatible.", filePath.GetPointer());
261 if (!__truncate && IsFileExist(filePath))
263 r = _SecureIoUtil::CheckSecureFileHeader(filePath, pSecretKey);
264 if (r == E_END_OF_FILE)
266 r = E_IO; //for security error
268 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Propagated.", GetErrorMessage(r));
271 if (pSecretKey == null)
273 unique_ptr<_NormalFile> pNormalFile(new (std::nothrow) _NormalFile());
274 SysTryReturnResult(NID_IO, pNormalFile, E_OUT_OF_MEMORY, "The memory is insufficient.");
276 r = pNormalFile->Construct(filePath, pOpenMode);
277 SysTryReturn(NID_IO, !IsFailed(r), r , r, "[%s] Propagated.", GetErrorMessage(r));
278 __pCore = pNormalFile.release();
282 unique_ptr<_SecureFile> pSecureFile(new (std::nothrow) _SecureFile(__read, __write, __truncate, __append));
283 SysTryReturnResult(NID_IO, pSecureFile, E_OUT_OF_MEMORY, "The memory is insufficient.");
285 r = pSecureFile->Construct(filePath, pOpenMode, pSecretKey);
286 SysTryReturn(NID_IO, !IsFailed(r), r , r, "[%s] Propagated.", GetErrorMessage(r));
287 __pCore = pSecureFile.release();
294 _FileImpl::ReadN(char** buffer, int& length)
296 result r = E_SUCCESS;
299 _NormalFile* pNormalFile = dynamic_cast< _NormalFile* > (__pCore);
300 if (pNormalFile != null)
302 r = pNormalFile->ReadN(buffer, length);
307 r = E_INVALID_OPERATION;
314 _FileImpl::Read(ByteBuffer& buffer)
316 SysAssertf(__pCore != null, "Not yet constructed. Construct() should be called before use.\n");
317 SysTryReturnResult(NID_IO, __read == true, E_ILLEGAL_ACCESS, "File is not opened for reading.");
318 return __pCore->Read(buffer);
322 _FileImpl::Read(void* buffer, int length)
324 SysAssertf(__pCore != null, "Not yet constructed. Construct() should be called before use.\n");
325 SysTryReturn(NID_IO, __read == true, 0, E_ILLEGAL_ACCESS, "[E_ILLEGAL_ACCESS] File is not opened for reading.");
326 return __pCore->Read(buffer, length);
330 _FileImpl::Read(String& buffer)
332 SysAssertf(__pCore != null, "Not yet constructed. Construct() should be called before use.\n");
333 SysTryReturnResult(NID_IO, __read == true, E_ILLEGAL_ACCESS, "File is not opened for reading.");
334 return __pCore->Read(buffer);
338 _FileImpl::Write(const ByteBuffer& buffer)
340 SysAssertf(__pCore != null, "Not yet constructed. Construct() should be called before use.\n");
341 SysTryReturnResult(NID_IO, __write == true, E_ILLEGAL_ACCESS, "File is not opened for writing.");
342 return __pCore->Write(buffer);
346 _FileImpl::Write(const void* buffer, int length)
348 SysAssertf(__pCore != null, "Not yet constructed. Construct() should be called before use.\n");
349 SysTryReturnResult(NID_IO, __write == true, E_ILLEGAL_ACCESS, "File is not opened for writing.");
350 return __pCore->Write(buffer, length);
354 _FileImpl::Write(const String& buffer)
356 SysAssertf(__pCore != null, "Not yet constructed. Construct() should be called before use.\n");
357 SysTryReturnResult(NID_IO, __write == true, E_ILLEGAL_ACCESS, "File is not opened for writing.");
358 return __pCore->Write(buffer);
362 _FileImpl::Flush(void)
364 SysAssertf(__pCore != null, "Not yet constructed. Construct() should be called before use.\n");
365 return __pCore->Flush();
369 _FileImpl::Tell(void) const
371 SysTryReturnResult(NID_IO, __pCore != null, -1, "File is not constructed! Construct destined file first! ");
372 return __pCore->Tell();
376 _FileImpl::Seek(FileSeekPosition position, long offset)
378 SysAssertf(__pCore != null, "Not yet constructed. Construct() should be called before use.\n");
379 return __pCore->Seek(position, offset);
383 _FileImpl::Truncate(int length)
385 SysAssertf(__pCore != null, "Not yet constructed. Construct() should be called before use.\n");
386 SysTryReturnResult(NID_IO, __write == true, E_ILLEGAL_ACCESS, "File is not opened for writing.");
387 return __pCore->Truncate(length);
391 _FileImpl::GetName(void) const
393 SysAssertf(__pCore != null, "Not yet constructed. Construct() should be called before use.\n");
394 return __pCore->GetName();
398 _FileImpl::GetFilePointer(void) const
400 SysAssertf(__pCore != null, "Not yet constructed. Construct() should be called before use.\n");
401 return __pCore->GetFilePointer();
405 _FileImpl::LockN(FileLockType lockType)
407 SysAssertf(__pCore != null, "Not yet constructed. Construct() should be called before use.\n");
408 return LockN(lockType, FILE_LOCK_MODE_BLOCKING, 0, 0);
412 _FileImpl::LockN(FileLockType lockType, int offset, int length)
414 SysAssertf(__pCore != null, "Not yet constructed. Construct() should be called before use.\n");
415 return LockN(lockType, FILE_LOCK_MODE_BLOCKING, offset, length);
419 _FileImpl::TryToLockN(FileLockType lockType)
421 SysAssertf(__pCore != null, "Not yet constructed. Construct() should be called before use.\n");
422 return LockN(lockType, FILE_LOCK_MODE_NON_BLOCKING, 0, 0);
426 _FileImpl::TryToLockN(FileLockType lockType, int offset, int length)
428 SysAssertf(__pCore != null, "Not yet constructed. Construct() should be called before use.\n");
429 return LockN(lockType, FILE_LOCK_MODE_NON_BLOCKING, offset, length);
433 _FileImpl::LockN(FileLockType lockType, _FileLockMode lockMode, int offset, int length)
435 SysAssertf(__pCore != null, "Not yet constructed. Construct() should be called before use.\n");
436 SysTryReturn(NID_IO, offset >= 0, null, E_INVALID_ARG, "[E_INVALID_ARG] The specified offset is negative.");
437 SysTryReturn(NID_IO, length >= 0, null, E_INVALID_ARG, "[E_INVALID_ARG] The specified length is negative.");
442 case FILE_LOCK_SHARED:
443 SysTryReturn(NID_IO, __read == true, null, E_INVALID_OPERATION, "[E_INVALID_OPERATION] File is not opened for reading.");
444 lock.l_type = F_RDLCK;
446 case FILE_LOCK_EXCLUSIVE:
447 SysTryReturn(NID_IO, __write == true, null, E_INVALID_OPERATION, "[E_INVALID_OPERATION] File is not opened for writing.");
448 lock.l_type = F_WRLCK;
451 SysLogException(NID_IO, E_INVALID_ARG, "[E_INVALID_ARG] The specified lock type is invalid.");
455 int fd = fileno(this->GetFilePointer());
456 int lockCommand = -1;
460 case FILE_LOCK_MODE_BLOCKING:
461 lockCommand = F_SETLKW;
463 case FILE_LOCK_MODE_NON_BLOCKING:
465 lockCommand = F_SETLK;
469 SysLogException(NID_IO, E_INVALID_ARG, "[E_INVALID_ARG] The specified lock mode is invalid.");
473 lock.l_whence = SEEK_SET;
474 lock.l_start = offset;
476 lock.l_pid = getpid();
478 ret = fcntl(fd, lockCommand, &lock);
481 result r = E_SUCCESS;
489 if (lockMode == FILE_LOCK_MODE_BLOCKING)
491 r = E_WOULD_DEADLOCK;
502 if (lockMode == FILE_LOCK_MODE_NON_BLOCKING)
514 r = _NativeError::ConvertNativeErrorToResult(errno, true);
518 SysLogException(NID_IO, r, "[%s] Aquiring file lock of type (%d) is failed, errno: %d (%s)", GetErrorMessage(r), lockType, errno, strerror(errno));
522 unique_ptr<FileLock> pFileLock(_FileLockImpl::CreateFileLockInstanceN(this, lockType, lock.l_start, lock.l_len, lock.l_pid));
523 SysTryReturn(NID_IO, pFileLock != null, null, GetLastResult(), "[%s] Propagating to caller....", GetErrorMessage(GetLastResult()));
524 __pFileLockImpl = _FileLockImpl::GetInstance(*pFileLock);
525 SetLastResult(E_SUCCESS);
526 return pFileLock.release();
530 _FileImpl::Remove(const String& filePath)
532 SysTryReturnResult(NID_IO, VerifyFilePathCompatibility(filePath, _AppInfo::IsOspCompat()) == true,
533 E_INVALID_ARG, " [%ls] is not compatible.", filePath.GetPointer());
534 return _FileUtil::Remove(filePath);
538 _FileImpl::Move(const String& oldFilePath, const String& newFilePath)
540 SysTryReturnResult(NID_IO, VerifyFilePathCompatibility(oldFilePath, _AppInfo::IsOspCompat()) == true,
541 E_INVALID_ARG, " [%ls] is not compatible.", oldFilePath.GetPointer());
543 SysTryReturnResult(NID_IO, VerifyFilePathCompatibility(newFilePath, _AppInfo::IsOspCompat()) == true,
544 E_INVALID_ARG, " [%ls] is not compatible.", newFilePath.GetPointer());
545 return _FileUtil::Move(oldFilePath, newFilePath);
549 _FileImpl::Copy(const String& srcFilePath, const String& destFilePath, bool failIfExist)
551 SysTryReturnResult(NID_IO, VerifyFilePathCompatibility(srcFilePath, _AppInfo::IsOspCompat()) == true,
552 E_INVALID_ARG, " [%ls] is not compatible.", srcFilePath.GetPointer());
554 SysTryReturnResult(NID_IO, VerifyFilePathCompatibility(destFilePath, _AppInfo::IsOspCompat()) == true,
555 E_INVALID_ARG, " [%ls] is not compatible.", destFilePath.GetPointer());
556 return _FileUtil::Copy(srcFilePath, destFilePath, failIfExist);
560 _FileImpl::GetAttributes(const String& filePath, FileAttributes& attribute)
562 SysTryReturnResult(NID_IO, VerifyFilePathCompatibility(filePath, _AppInfo::IsOspCompat()) == true,
563 E_INVALID_ARG, " [%ls] is not compatible.", filePath.GetPointer());
564 return _FileUtil::GetAttributes(filePath, attribute);
568 _FileImpl::GetFileName(const String& filePath)
570 return _FileUtil::GetFileName(filePath);
574 _FileImpl::GetFileExtension(const String& filePath)
576 return _FileUtil::GetFileExtension(filePath);
580 _FileImpl::IsFileExist(const String& filePath)
582 SysTryReturn(NID_IO, VerifyFilePathCompatibility(filePath, _AppInfo::IsOspCompat()) == true,
583 false, E_INVALID_ARG, "[E_INVALID_ARG] The specified filePath (%ls) is invalid.", filePath.GetPointer());
585 return _FileUtil::IsFileExist(filePath);
589 _FileImpl::ConvertToSecureFile(const String& plainFilePath, const String& secureFilePath,
590 const ByteBuffer& key)
592 return _FileUtil::ConvertToSecureFile(plainFilePath, secureFilePath, &key);
596 _FileImpl::IsAppPath(const String& filePath)
598 SysTryReturn(NID_IO, VerifyFilePathCompatibility(filePath, _AppInfo::IsOspCompat()) == true,
599 false, E_INVALID_ARG, "[E_INVALID_ARG] The specified filePath (%ls) is invalid.", filePath.GetPointer());
601 return _FileUtil::IsAppPath(filePath);
605 _FileImpl::IsMediaPath(const String& filePath)
607 SysTryReturn(NID_IO, VerifyFilePathCompatibility(filePath, _AppInfo::IsOspCompat()) == true,
608 false, E_INVALID_ARG, "[E_INVALID_ARG] The specified filePath (%ls) is invalid.", filePath.GetPointer());
610 return _FileUtil::IsMediaPath(filePath);
614 _FileImpl::IsSystemPath(const String& filePath)
616 SysTryReturn(NID_IO, VerifyFilePathCompatibility(filePath, _AppInfo::IsOspCompat()) == true,
617 false, E_INVALID_ARG, "[E_INVALID_ARG] The specified filePath (%ls) is invalid.", filePath.GetPointer());
619 return _FileUtil::IsSystemPath(filePath);
623 _FileImpl::VerifyFilePath(const String& filePath, _FilePathType pathType)
625 return _FileUtil::VerifyFilePath(filePath, pathType);
629 _FileImpl::CleanDirectories(const String& appRootPath, const String& pkgId)
631 char removeCmd[PATH_MAX] = {0, };
634 String ospSharePkgIdPath = _EnvironmentImpl::GetOspCompatSharedPath();
635 ospSharePkgIdPath.Append(L"share/");
636 ospSharePkgIdPath.Append(pkgId);
638 String ospShare2PkgIdPath = _EnvironmentImpl::GetOspCompatSharedPath();
639 ospShare2PkgIdPath.Append(L"share2/");
640 ospShare2PkgIdPath.Append(pkgId);
643 r = Directory::Remove(ospSharePkgIdPath, true);
644 SysTryReturn(NID_IO, !IsFailed(r), false, E_SYSTEM, "[%s] Failed to remove directory (%ls)",
645 GetErrorMessage(r), ospSharePkgIdPath.GetPointer());
647 r = Directory::Remove(ospShare2PkgIdPath, true);
648 SysTryReturn(NID_IO, !IsFailed(r), false, E_SYSTEM, "[%s] Failed to remove directory (%ls)",
649 GetErrorMessage(r), ospShare2PkgIdPath.GetPointer());
651 sprintf(removeCmd, "rm -rf %ls", ospSharePkgIdPath.GetPointer());
652 ret = system(removeCmd);
653 SysTryReturn(NID_IO, ret != -1, false, E_SYSTEM, "Failed to remove directory (%ls)",
654 ospSharePkgIdPath.GetPointer());
656 memset(removeCmd, 0, PATH_MAX);
658 sprintf(removeCmd, "rm -rf %ls", ospShare2PkgIdPath.GetPointer());
659 ret = system(removeCmd);
660 SysTryReturn(NID_IO, ret != -1, false, E_SYSTEM, "Failed to remove directory (%ls)",
661 ospShare2PkgIdPath.GetPointer());
667 // This method is called by package installer backend.
669 _FileImpl::PrepareDataCaging(const String& appRootPath, const String& pkgId)
673 bool internalInstalled = true;
674 result r = E_SUCCESS;
676 SysSecureLog(NID_IO, "[data_caging] PrepareDataCaging() was called by installer backend, appRootPath: %ls, packageId: %ls",
677 appRootPath.GetPointer(), pkgId.GetPointer());
679 if (CleanDirectories(appRootPath, pkgId) == false)
681 SysLog(NID_IO, "CleanDirectories() failed.");
685 pCwd = get_current_dir_name();
686 SysTryCatch(NID_IO, pCwd != null, r = E_SYSTEM, E_SYSTEM,
687 "[E_SYSTEM] get_current_dir_name() was failed, errno: %d (%s).", errno, strerror(errno));
689 // Check whether package is installed on internal storage or not
690 r = appRootPath.IndexOf("/opt/storage/sdcard", 0, index);
693 internalInstalled = false;
697 internalInstalled = true;
702 if (internalInstalled == true)
704 unique_ptr<char[]> pAppRootPath(_StringConverter::CopyToCharArrayN(appRootPath));
705 SysTryCatch(NID_IO, pAppRootPath != null, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] The memory is insufficient.");
707 SysTryCatch(NID_IO, chdir(pAppRootPath.get()) == 0, r = E_SYSTEM, E_SYSTEM,
708 "[E_SYSTEM] chdir() was failed (%s), path: %s", strerror(errno), pAppRootPath.get());
710 SysTryCatch(NID_IO, CreateOspInternalDirectories(appRootPath, pkgId) == true, r = E_SYSTEM, E_SYSTEM,
711 "[E_SYSTEM] fail to create OSP Internal directories");
715 String appExRootPath(appRootPath);
719 appExRootPath.Append(pkgId);
720 unique_ptr<char[]> pAppExRootPath(_StringConverter::CopyToCharArrayN(appExRootPath));
721 SysTryCatch(NID_IO, pAppExRootPath != null, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] The memory is insufficient.");
723 ret = mkdir(pAppExRootPath.get(), 0705);
724 if (ret == -1 && errno != 17) // EEXIST
726 SysLog(NID_IO, "mkdir() failed (%s), path: %s, mode: 0%o",
727 strerror(errno), pAppExRootPath.get(), 0705);
731 SysTryCatch(NID_IO, chdir(pAppExRootPath.get()) == 0, r = E_SYSTEM, E_SYSTEM,
732 "[E_SYSTEM] chdir() was failed (%s), path: %s", strerror(errno), pAppExRootPath.get());
733 SysTryCatch(NID_IO, CreateOspExternalDirectories(pkgId) == true, r = E_SYSTEM, E_SYSTEM,
734 "[E_SYSTEM] fail to create OSP External directories");
737 SysTryCatch(NID_IO, CreateSlpDirectories() == true, r = E_SYSTEM, E_SYSTEM,
738 "[E_SYSTEM] fail to create SLP directories");
739 SysTryCatch(NID_IO, CreateSymbolicLink() == true, r = E_SYSTEM, E_SYSTEM,
740 "[E_SYSTEM] Fail to create symbolic link.");
741 SysTryCatch(NID_IO, chdir(pCwd) == 0, r = E_SYSTEM, E_SYSTEM,
742 "[E_SYSTEM] chdir() was failed (%s), path: %s", strerror(errno), pCwd);
745 SysLog(NID_IO, "[data_caging] PrepareDataCaging() succeeded.");
758 SysLog(NID_IO, "[data_caging] PrepareDataCaging() failed.");
766 _FileImpl::FinalizeDataCaging(const String& appRootPath) // for 2.0 app
768 static const struct _PathInfo mountPath[] =
779 //{ "./lost+found" },
783 { "./opt/var/kdb/db" },
784 { "./opt/storage/sdcard" },
791 { "./sys/kernel/debug" },
808 { "./Storagecard/Media" },
811 { "./HomeExt/Share" },
812 { "./HomeExt/Share2" },
816 unique_ptr< char[] > pAppRootPath(_StringConverter::CopyToCharArrayN(appRootPath));
817 SysTryReturn(NID_IO, pAppRootPath != null, false, E_OUT_OF_MEMORY,
818 "[E_OUT_OF_MEMORY] The memory is insufficient.");
820 SysTryReturn(NID_IO, chdir(pAppRootPath.get()) == 0, false, E_SYSTEM,
821 "[E_SYSTEM] chdir() failed (%d, %s), path: %ls", errno, strerror(errno), appRootPath.GetPointer());
823 for (int i = 0; i < sizeof(mountPath) / sizeof(struct _PathInfo); ++i)
825 int ret = umount2(mountPath[i].destPath, MNT_DETACH);
826 SysTryLog(NID_IO, ret == 0, "umount2() errno: %d (%s)", errno, strerror(errno));
827 SysTryReturn(NID_IO, ret == 0 || errno == EINVAL || errno == ENOENT, false, E_SYSTEM,
828 "[E_SYSTEM] umount2() failed (%d, %s), path: %s", errno, strerror(errno), mountPath[i].destPath);
831 char* pkgId = strrchr(pAppRootPath.get(), '/');
832 char mountFlag[_MAX_PATH_LENGTH] = { 0, };
833 sprintf(mountFlag, "%s/%s", _INTERNAL_MOUNT_FLAG, ++pkgId);
834 int res = unlink(mountFlag);
835 if (res == -1 && errno != ENOENT)
837 SysLogException(NID_IO, E_SYSTEM, "[E_SYSTEM] Failed to remove mount flag (%s), errno: %d (%s)",
838 mountFlag, errno, strerror(errno));
842 memset(mountFlag, 0, _MAX_PATH_LENGTH);
843 sprintf(mountFlag, "%s/%s", _EXTERNAL_MOUNT_FLAG, pkgId);
844 res = unlink(mountFlag);
845 if (res == -1 && errno != ENOENT)
847 SysLogException(NID_IO, E_SYSTEM, "[E_SYSTEM] Failed to remove mount flag (%s), errno: %d (%s)",
848 mountFlag, errno, strerror(errno));
852 SysLog(NID_IO, "[data_caging] FinalizeDataCaging() succeeded, appRootPath: %ls", appRootPath.GetPointer());
857 _FileImpl::GetAvailableUid(void)
863 _FileImpl::CreateOspApplicationDirectories(const String& appRootPath, const String& pkgId) // for 2.0 app
866 struct _OspDir appSubDir[] =
868 //{ "./shared\0", 0755, false },
869 //{ "./shared/data\0", 0755, true },
870 //{ "./shared/res\0", 0755, false },
871 //{ "./shared/trusted\0", 0755, true },
872 //{ "./cache\0", 0700, true }
877 result r = E_SUCCESS;
880 SysTryReturn(NID_IO, CleanDirectories(appRootPath, pkgId) == true, false, E_SYSTEM,
881 "[E_SYSTEM] Failed to clean directories for 2.0 application compatibility.");
884 unique_ptr<char[]> pAppRootPath(_StringConverter::CopyToCharArrayN(appRootPath));
885 SysTryReturn(NID_IO, pAppRootPath != null, false, E_SYSTEM, "[E_SYSTEM] The memory is insufficient.");
887 SysTryReturn(NID_IO, chdir(pAppRootPath.get()) == 0, false, E_SYSTEM,
888 "[E_SYSTEM] chdir() failed (%d, %s), path: %s", errno, strerror(errno), pAppRootPath.get());
890 uid = GetAvailableUid();
894 for (i = 0; i < sizeof(appSubDir) / sizeof(struct _OspDir); ++i)
896 ret = mkdir(appSubDir[i].path, appSubDir[i].mode);
897 if (ret == -1 && errno != 17) // EEXIST
899 SysLog(NID_IO, "[E_SYSTEM] mkdir() failed (%d, %s), path: %s, mode: 0%o",
900 errno, strerror(errno), appSubDir[i].path, appSubDir[i].mode);
904 if (appSubDir[i].appPrivilege)
906 ret = chown(appSubDir[i].path, uid, uid);
907 SysTryCatch(NID_IO, ret == 0, , E_SYSTEM,
908 "[E_SYSTEM] chown() failed (%d, %s), path: %s, uid: %d",
909 errno, strerror(errno), appSubDir[i].path, uid);
915 SysLog(NID_IO, "_FileImpl::CreateOspApplicationDirectories() succeeded.");
929 _FileImpl::CreateOspInternalDirectories(const String& appRootPath, const String& pkgId) // for 2.0 app
934 struct _OspDir appSubDir[] =
936 // { "./data", 0700, true }, // It is created by installer.
937 { "./shared", 0755, false },
938 { "./data/Share", 0000, true }, // mount from /opt/usr/share/.osp-compat/share/{pkgId}
939 { "./data/Share2", 0000, true }, // mount from /opt/usr/share/.osp-compat/share2/{pkgId}
940 { "./Share", 0000, false }, // mount from /opt/usr/share/.osp-compat/share
941 { "./Share2", 0000, false }, // mount from /opt/usr/share/.osp-compat/share2
942 // { "./Clipboard", 0000, false },
943 // { "./NPKI", 0000, false },
944 // { "./System", 0000, false },
945 // { "./Tmp", 0000, false },
946 { "./Media", 0000, false }, // mount from /opt/usr/media
947 { "./Storagecard", 0705, false },
948 { "./Storagecard/Media", 0000, false }, // mount from /opt/storage/sdcard
951 struct _OspDir mediaDir[] =
953 { "/opt/usr/media/Images", 0777, false },
954 { "/opt/usr/media/Sounds", 0777, false },
955 { "/opt/usr/media/Videos", 0777, false },
956 //{ "/opt/usr/media/Themes", 0777, false },
957 { "/opt/usr/media/Others", 0777, false }
960 String ospCompatSharedPath = _EnvironmentImpl::GetOspCompatSharedPath();
961 String ospShareAppIdPath(L"share/");
962 String ospShare2AppIdPath(L"share2/");
963 result r = E_SUCCESS;
965 r = ospShareAppIdPath.Insert(ospCompatSharedPath, 0);
966 SysTryReturn(NID_IO, !IsFailed(r), false, E_SYSTEM,
967 "[E_SYSTEM] String::Insert() failed. (error: %s)", GetErrorMessage(r));
969 r = ospShare2AppIdPath.Insert(ospCompatSharedPath, 0);
970 SysTryReturn(NID_IO, !IsFailed(r), false, E_SYSTEM,
971 "[E_SYSTEM] String::Insert() failed. (error: %s)", GetErrorMessage(r));
973 uid = GetAvailableUid();
975 for (i = 0; i < sizeof(appSubDir) / sizeof(struct _OspDir); i++)
977 ret = mkdir(appSubDir[i].path, appSubDir[i].mode);
978 if (ret == -1 && errno != 17) // EEXIST
980 SysLog(NID_IO, "mkdir() failed (%s), path: %s, mode: 0%o",
981 strerror(errno), appSubDir[i].path, appSubDir[i].mode);
984 if (appSubDir[i].appPrivilege)
986 ret = chown(appSubDir[i].path, uid, uid);
989 SysLog(NID_IO, "chown() failed (%s), path: %s, uid: %d",
990 strerror(errno), appSubDir[i].path, uid);
996 ospShareAppIdPath.Append(pkgId);
997 unique_ptr<char[]> pOspShareAppIdPath(_StringConverter::CopyToCharArrayN(ospShareAppIdPath));
998 SysTryReturn(NID_IO, pOspShareAppIdPath != null, false, E_SYSTEM, "[E_SYSTEM] The memory is insufficient.");
999 ret = mkdir(pOspShareAppIdPath.get(), 0705);
1000 if (ret == -1 && errno != 17) // EEXIST
1002 SysLog(NID_IO, "mkdir() failed (%s), path: %s, mode: 0%o",
1003 strerror(errno), pOspShareAppIdPath.get(), 0705);
1006 ret = chown(pOspShareAppIdPath.get(), uid, uid);
1009 SysLog(NID_IO, "chown() failed (%s), path: %s, uid: %d",
1010 strerror(errno), pOspShareAppIdPath.get(), uid);
1014 ospShare2AppIdPath.Append(pkgId);
1015 unique_ptr<char[]> pOspShare2AppIdPath(_StringConverter::CopyToCharArrayN(ospShare2AppIdPath));
1016 SysTryReturn(NID_IO, pOspShare2AppIdPath != null, false, E_SYSTEM, "[E_SYSTEM] The memory is insufficient.");
1017 ret = mkdir(pOspShare2AppIdPath.get(), 0705); // TODO: change to 0770
1018 if (ret == -1 && errno != 17) // EEXIST
1020 SysLog(NID_IO, "mkdir() failed (%s), path: %s, mode: 0%o",
1021 strerror(errno), pOspShare2AppIdPath.get(), 0705);
1024 ret = chown(pOspShare2AppIdPath.get(), uid, uid);
1027 SysLog(NID_IO, "chown() failed (%s), path: %s, uid: %d",
1028 strerror(errno), pOspShare2AppIdPath.get(), uid);
1033 for (i = 0; i < sizeof(mediaDir) / sizeof(struct _OspDir); ++i)
1035 ret = mkdir(mediaDir[i].path, mediaDir[i].mode);
1036 if (ret == -1 && errno != 17) // EEXIST
1038 SysLog(NID_IO, "mkdir() failed (%s), path: %s, mode: 0%o",
1039 strerror(errno), mediaDir[i].path, mediaDir[i].mode);
1042 ret = chown(mediaDir[i].path, 5000, 5000);
1043 SysTryReturn(NID_IO, ret == 0, false, E_SYSTEM,
1044 "[E_SYSTEM] chown() failed (%d, %s), path: %s, uid: 5000, gid: 5000",
1045 errno, strerror(errno), mediaDir[i].path);
1049 // XXX: OSP compatible application's data packed in shared/data, shared/trusted directory of SDK
1050 // cannot be delivered.
1052 if (access("./shared/data", F_OK) == 0)
1054 char copyCmd[256] = { 0, };
1055 sprintf(copyCmd, "cp -rf ./shared/data/* /opt/usr/share/.osp-compat/share/%ls/", pkgId.GetPointer());
1056 ret = system(copyCmd);
1057 SysTryReturn(NID_IO, ret != -1, false, E_SYSTEM, "Failed to copy command (%s)", copyCmd);
1059 SysTryReturn(NID_IO, system("rm -rf ./shared/data") != -1, false, E_SYSTEM,
1060 "[E_SYSTEM] rmdir() failed, path: ./shared/data");
1062 char chownCmd[256] = { 0, };
1063 sprintf(chownCmd, "chown -R 5000:5000 /opt/usr/share/.osp-compat/share/%ls/", pkgId.GetPointer());
1064 SysTryReturn(NID_IO, system(chownCmd) != -1, false, E_SYSTEM, "[E_SYSTEM] chown() failed");
1067 String appSharedDataPath(appRootPath);
1068 appSharedDataPath.Append(L"/shared/data");
1070 String appSharedTrustedPath(appRootPath);
1071 appSharedTrustedPath.Append(L"/shared/trusted");
1073 SysTryReturn(NID_IO, Directory::Remove(appSharedDataPath, true) == E_SUCCESS, false, E_SYSTEM,
1074 "[E_SYSTEM] Failed to remove path (%ls)", appSharedDataPath.GetPointer());
1075 SysTryReturn(NID_IO, Directory::Remove(appSharedTrustedPath, true) == E_SUCCESS, false, E_SYSTEM,
1076 "[E_SYSTEM] Failed to remove path (%ls)", appSharedTrustedPath.GetPointer());
1078 ret = symlink(pOspShareAppIdPath.get(), "./shared/data");
1079 SysTryReturn(NID_IO, ret == 0, false, E_SYSTEM,
1080 "[E_SYSTEM] symlink() failed, errno: %d (%s), link: ./shared/data -> %s",
1081 errno, strerror(errno), pOspShareAppIdPath.get());
1082 ret = symlink(pOspShare2AppIdPath.get(), "./shared/trusted");
1083 SysTryReturn(NID_IO, ret == 0, false, E_SYSTEM,
1084 "[E_SYSTEM] symlink() failed, errno: %d (%s), link: ./shared/trusted -> %s",
1085 errno, strerror(errno), pOspShare2AppIdPath.get());
1090 // TODO: Need to test this method.
1092 _FileImpl::CreateOspExternalDirectories(const String& pkgId)
1097 struct _OspDir appSubDir[] = { // virtual path
1098 // { "./data", 0700, true },
1099 { "./System", 0000, false }, // mount from /opt/apps/com.samsung.osp/system
1100 { "./Storagecard", 0705, false },
1101 { "./Storagecard/Media", 0000, false } // mount from /opt/storage/sdcard
1104 struct _OspDir mediaDir[] = { // physical path
1105 { "/opt/usr/media/Images", 0777, false },
1106 { "/opt/usr/media/Sounds", 0777, false },
1107 { "/opt/usr/media/Videos", 0777, false },
1108 //{ "/opt/usr/media/Themes", 0777, false },
1109 { "/opt/usr/media/Others", 0777, false }
1113 for (i = 0; i < sizeof(appSubDir) / sizeof(struct _OspDir); i++)
1115 ret = mkdir(appSubDir[i].path, appSubDir[i].mode);
1116 if (ret == -1 && errno != 17) // EEXIST
1118 SysLog(NID_IO, "mkdir() failed (%s), path: %s, mode: 0%o",
1119 strerror(errno), appSubDir[i].path, appSubDir[i].mode);
1122 if (appSubDir[i].appPrivilege)
1124 ret = chown(appSubDir[i].path, uid, uid);
1127 SysLog(NID_IO, "chown() failed (%s), path: %s, uid: %d",
1128 strerror(errno), appSubDir[i].path, uid);
1135 for (i = 0; i < sizeof(mediaDir) / sizeof(struct _OspDir); i++)
1137 ret = mkdir(mediaDir[i].path, mediaDir[i].mode);
1138 if (ret == -1 && errno != 17) // EEXIST
1140 SysLog(NID_IO, "mkdir() failed (%s), path: %s, mode: 0%o",
1141 strerror(errno), mediaDir[i].path, mediaDir[i].mode);
1151 _FileImpl::CreateSlpDirectories(void)
1155 struct _OspDir slpDir[] = { // virtual path
1156 //{ "./bin", 0000, false }, // mount from /bin
1157 //{ "./boot", 0000, false },
1158 //{ "./cache", 0000, false },
1159 { "./csa", 0000, false },
1160 { "./dev", 0000, false },
1161 { "./etc", 0000, false },
1162 { "./lib", 0000, false },
1163 //{ "./lost+found", 0000, false },
1164 { "./media", 0000, false },
1165 { "./mnt", 0000, false },
1166 { "./opt", 0000, false },
1167 //{ "./packaging", 0000, false },
1168 { "./proc", 0000, false },
1169 { "./sbin", 0000, false },
1170 { "./smack", 0000, false },
1171 { "./srv", 0000, false },
1172 { "./sys", 0000, false },
1173 { "./tmp", 0000, false },
1174 { "./usr", 0000, false },
1175 { "./var", 0000, false }
1178 for (i = 0; i < sizeof(slpDir) / sizeof(struct _OspDir); i++)
1180 ret = mkdir(slpDir[i].path, slpDir[i].mode);
1181 if (ret == -1 && errno != 17) // EEXIST
1183 SysLog(NID_IO, "mkdir() failed (%s), path: %s, mode: 0%o",
1184 strerror(errno), slpDir[i].path, slpDir[i].mode);
1193 _FileImpl::CreateSymbolicLink(void)
1195 struct _LinkDir linkDirList[] = {
1196 { "/opt/home", "./home" },
1197 { "/opt/home/root", "./root" },
1198 { "/mnt/mmc", "./sdcard" }
1201 for (unsigned int i = 0; i < sizeof(linkDirList) / sizeof(struct _LinkDir); ++i)
1203 int ret = symlink(linkDirList[i].srcPath, linkDirList[i].destPath);
1204 if (ret == -1 && errno != 17) // EEXIST
1206 SysLog(NID_IO, "Failed to create symbolic link, errno: %d (%s), src path: %s, dest path: %s",
1207 strerror(errno), linkDirList[i].srcPath, linkDirList[i].destPath);
1216 _FileImpl::VerifyFilePathCompatibility(const String& filePath, bool ospCompat)
1218 if (ospCompat == true)
1220 if (filePath[0] != L'/')
1225 int length = filePath.GetLength();
1234 _FileImpl::GetInstance(File& file)
1236 return file.__pFileImpl;
1240 _FileImpl::GetInstance(const File& file)
1242 return file.__pFileImpl;
1246 _FileImpl::ConvertVirtualToPhysicalPath(const String& virtualPath, String& physicalPath) // for 2.0 app
1248 SysTryReturnResult(NID_IO, VerifyFilePathCompatibility(virtualPath, _AppInfo::IsOspCompat()) == true,
1249 E_INVALID_ARG, "[E_INVALID_ARG] %ls is not compatible.", virtualPath.GetPointer());
1251 const wchar_t* virtualPathPrefix[] =
1258 L"/Share/AppControl",
1265 L"/Storagecard/Media",
1266 //L"/Storagecard/NPKI",
1268 result r = E_SUCCESS;
1269 int count = sizeof(virtualPathPrefix)/sizeof(wchar_t*);
1272 for (i = 0; i < count; i++)
1274 SysLog(NID_IO, "[V2P] i: %d, path: %ls", i, virtualPathPrefix[i]);
1275 if (virtualPath.StartsWith(virtualPathPrefix[i], 0) == true)
1280 SysTryReturnResult(NID_IO, i != count, E_INVALID_ARG, "The path (%ls) is not matched.",
1281 virtualPath.GetPointer());
1283 int prefixLen = String(virtualPathPrefix[i]).GetLength();
1284 if (virtualPath.GetLength() > prefixLen)
1288 r = virtualPath.GetCharAt(prefixLen, ch);
1289 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] The path (%ls) is not matched.",
1290 GetErrorMessage(r), virtualPath.GetPointer());
1294 physicalPath.Clear();
1295 SysLog(NID_IO, "[E_INVALID_ARG] The path (%ls) is not matched.",
1296 virtualPath.GetPointer());
1298 return E_INVALID_ARG;
1303 virtualPath.SubString(wcslen(virtualPathPrefix[i]), subPath);
1305 Tizen::App::App* pApp = Tizen::App::App::GetInstance();
1306 SysTryReturnResult(NID_IO, pApp != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
1307 String homePath = pApp->GetAppRootPath();
1308 String ospCompatSharedPath = _EnvironmentImpl::GetOspCompatSharedPath();
1313 physicalPath = homePath + L"data/Share2";
1316 physicalPath = homePath + L"data/Share";
1319 physicalPath = homePath + L"data";
1322 physicalPath = homePath + L"res";
1325 physicalPath = ospCompatSharedPath + L"share2";
1328 physicalPath = ospCompatSharedPath + L"share/AppControl";
1331 physicalPath = ospCompatSharedPath + L"share";
1334 physicalPath = Tizen::System::Environment::GetMediaPath();
1337 physicalPath = Tizen::System::Environment::GetExternalStoragePath() + L"Media";
1340 SysLog(NID_IO, "[E_INVALID_ARG] The path (%ls) is not matched.",
1341 virtualPath.GetPointer());
1342 return E_INVALID_ARG;
1345 if (subPath.IsEmpty() == false)
1347 physicalPath.Append(subPath);
1354 _FileImpl::ConvertPhysicalToVirtualPath(const String& physicalPath, String& virtualPath) // for 2.0 app
1356 result r = E_SUCCESS;
1357 const wchar_t* homeSubDir[] = {
1363 const wchar_t* ospHomeSubDir[] = {
1365 L"/share/AppControl",
1369 int homeSubDirCount = sizeof(homeSubDir)/sizeof(wchar_t*);
1370 int ospHomeSubDirCount = sizeof(ospHomeSubDir)/sizeof(wchar_t*);
1374 SysLog(NID_IO, "[P2V] physicalPath: %ls", physicalPath.GetPointer());
1376 Tizen::App::App* pApp = Tizen::App::App::GetInstance();
1377 SysTryReturnResult(NID_IO, pApp != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
1378 String homePath = pApp->GetAppRootPath();
1379 homePath.SetLength(homePath.GetLength() - 1);
1381 String ospCompatSharedPath = _EnvironmentImpl::GetOspCompatSharedPath();
1382 ospCompatSharedPath.SetLength(ospCompatSharedPath.GetLength() - 1);
1384 String mediaPath = Tizen::System::Environment::GetMediaPath();
1385 mediaPath.SetLength(mediaPath.GetLength() - 1);
1387 String externalPath = Tizen::System::Environment::GetExternalStoragePath();
1388 externalPath.SetLength(externalPath.GetLength() - 1);
1390 if (physicalPath.StartsWith(homePath, 0) == true)
1392 physicalPath.SubString(homePath.GetLength(), subPath);
1394 for (i = 0; i < homeSubDirCount; i++)
1396 SysLog(NID_IO, "[P2V] i: %d, path: %ls", i, homeSubDir[i]);
1397 if (subPath.StartsWith(homeSubDir[i], 0) == true)
1402 SysTryReturnResult(NID_IO, i != homeSubDirCount, E_INVALID_ARG, "The path (%ls) is not matched.",
1403 physicalPath.GetPointer());
1405 baseDirLen = homePath.GetLength() + String(homeSubDir[i]).GetLength();
1410 virtualPath = L"/Home/Share2";
1413 virtualPath = L"/Home/Share";
1416 virtualPath = L"/Home";
1419 virtualPath = L"/Res";
1426 else if (physicalPath.StartsWith(ospCompatSharedPath, 0) == true)
1428 physicalPath.SubString(ospCompatSharedPath.GetLength(), subPath);
1430 for (i = 0; i < ospHomeSubDirCount; i++)
1432 SysLog(NID_IO, "[P2V] i: %d, path: %ls", i, ospHomeSubDir[i]);
1433 if (subPath.StartsWith(ospHomeSubDir[i], 0) == true)
1438 SysTryReturnResult(NID_IO, i != ospHomeSubDirCount, E_INVALID_ARG, "The path (%ls) is not matched.",
1439 physicalPath.GetPointer());
1441 baseDirLen = ospCompatSharedPath.GetLength() + String(ospHomeSubDir[i]).GetLength();
1446 virtualPath = L"/Share2";
1449 virtualPath = L"/Share/AppControl";
1452 virtualPath = L"/Share";
1458 else if (physicalPath.StartsWith(mediaPath, 0) == true)
1460 SysLog(NID_IO, "[P2V] media");
1461 virtualPath = L"/Media";
1462 baseDirLen = mediaPath.GetLength();
1464 else if (physicalPath.StartsWith(externalPath, 0) == true)
1466 SysLog(NID_IO, "[P2V] external media");
1467 virtualPath = L"/Storagecard/Media";
1468 baseDirLen = externalPath.GetLength();
1472 SysLog(NID_IO, "[E_INVALID_ARG] The path (%ls) is not matched.",
1473 physicalPath.GetPointer());
1475 return E_INVALID_ARG;
1478 if (physicalPath.GetLength() > baseDirLen)
1482 r = physicalPath.GetCharAt(baseDirLen, ch);
1483 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] The path (%ls) is not matched.",
1484 GetErrorMessage(r), physicalPath.GetPointer());
1488 virtualPath.Clear();
1489 SysLog(NID_IO, "[E_INVALID_ARG] The path (%ls) is not matched.",
1490 physicalPath.GetPointer());
1492 return E_INVALID_ARG;
1497 physicalPath.SubString(baseDirLen, subPath);
1499 if (subPath.IsEmpty() == false)
1501 virtualPath.Append(subPath);