Example #1
0
sqInt	ioFilenamefromStringofLengthresolveAliasesRetry(char* dst, char* src, sqInt num, sqInt resolveAlias, Boolean retry) {
 int		bytes;
 FSRef targetFSRef;
 OSStatus err; 
 
 if (retry)
	bytes = sq2uxPath(src, num, dst, DOCUMENT_NAME_SIZE, 1);
 else {
	memcpy(dst,src,num);
	dst[num] = 0x00;
	bytes = num;
 }
  		
 err = getFSRef(dst,&targetFSRef,kCFStringEncodingUTF8); 
 if (retry) {
	if (err) {
		char possiblePath[DOCUMENT_NAME_SIZE+1];
		
		bytes = wanderDownPath(dst,bytes,possiblePath,resolveAlias);
		if (bytes) 
			strcpy(dst,possiblePath);
		return bytes;
	}
 }

  if (err == 0 && resolveAlias) {
		Boolean	targetIsFolder,wasAliased;
		err = FSResolveAliasFileWithMountFlags(&targetFSRef,true,&targetIsFolder,&wasAliased,kResolveAliasFileNoUI);
		if (err || wasAliased == false)
			return bytes; 
		PathToFileViaFSRef(dst, DOCUMENT_NAME_SIZE, &targetFSRef,kCFStringEncodingUTF8);
		bytes = strlen(dst);
	 }
  
 return bytes;
}
Example #2
0
void TranslateAliasShortcut(CStdString &path)
{
#if defined(__APPLE__) && !defined(__arm__)
  FSRef fileRef;
  Boolean targetIsFolder, wasAliased;

  if (noErr == FSPathMakeRefWithOptions((UInt8*)path.c_str(), kFSPathMakeRefDefaultOptions, &fileRef, NULL))
  {
    if (noErr == FSResolveAliasFileWithMountFlags(&fileRef, TRUE, &targetIsFolder, &wasAliased, kResolveAliasFileNoUI))
    {
      if (wasAliased)
      {
        char real_path[PATH_MAX];
        if (noErr == FSRefMakePath(&fileRef, (UInt8*)real_path, PATH_MAX))
        {
          path = real_path;
        }
      }
    }
  }
#elif defined(_LINUX)
  // Linux does not use alias or shortcut methods

#elif defined(WIN32)
/* Needs testing under Windows platform so ignore shortcuts for now
  CComPtr<IShellLink> ipShellLink;

  // Get a pointer to the IShellLink interface
  if (NOERROR == CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink, (void**)&ipShellLink))
    WCHAR wszTemp[MAX_PATH];

    // Get a pointer to the IPersistFile interface
    CComQIPtr<IPersistFile> ipPersistFile(ipShellLink);

    // IPersistFile is using LPCOLESTR so make sure that the string is Unicode
#if !defined _UNICODE
    MultiByteToWideChar(CP_ACP, 0, lpszShortcutPath, -1, wszTemp, MAX_PATH);
#else
    wcsncpy(wszTemp, lpszShortcutPath, MAX_PATH);
#endif

    // Open the shortcut file and initialize it from its contents
    if (NOERROR == ipPersistFile->Load(wszTemp, STGM_READ))
    {
      // Try to find the target of a shortcut even if it has been moved or renamed
      if (NOERROR == ipShellLink->Resolve(NULL, SLR_UPDATE))
      {
        WIN32_FIND_DATA wfd;
        TCHAR real_path[PATH_MAX];
        // Get the path to the shortcut target
        if (NOERROR == ipShellLink->GetPath(real_path, MAX_PATH, &wfd, SLGP_RAWPATH))
        {
          // Get the description of the target
          TCHAR szDesc[MAX_PATH];
          if (NOERROR == ipShellLink->GetDescription(szDesc, MAX_PATH))
          {
            path = real_path;
          }
        }
      }
    }
  }
*/
#endif
}
Example #3
0
/*
 * Add support for resolving Mac native alias files (not the same as unix alias files)
 * (what are "unix alias files"?)
 * returns 0 on success.
 */
int macxp_resolveAlias(char *path, int buflen)
{
#if HAVE_FEATURE_MACOSX_SANDBOX
    /* Avoid unnecessary messages in the system.log:
     *
     * soffice(57342) deny file-read-data /Users/tml/Documents/b.odt/..namedfork/rsrc
     * etc.
     *
     * Just don't bother with resolving aliases. I doubt its usefulness anyway.
     */
    (void) path;
    (void) buflen;
    return 0;
#else
#if MAC_OS_X_VERSION_MAX_ALLOWED < 1060
    FSRef aFSRef;
    OSStatus nErr;
    Boolean bFolder;
    Boolean bAliased;
#else
    CFStringRef cfpath;
    CFURLRef cfurl;
    CFErrorRef cferror;
    CFDataRef cfbookmark;
#endif

    char *unprocessedPath = path;

    if ( *unprocessedPath == '/' )
        unprocessedPath++;

    int nRet = 0;
    while ( !nRet && unprocessedPath && *unprocessedPath )
    {
        unprocessedPath = strchr( unprocessedPath, '/' );
        if ( unprocessedPath )
            *unprocessedPath = '\0';

#if MAC_OS_X_VERSION_MAX_ALLOWED < 1060
        nErr = noErr;
        bFolder = FALSE;
        bAliased = FALSE;

        if ( FSPathMakeRef( (const UInt8 *)path, &aFSRef, 0 ) == noErr )
        {
            nErr = FSResolveAliasFileWithMountFlags( &aFSRef, TRUE, &bFolder, &bAliased, kResolveAliasFileNoUI );
            if ( nErr == nsvErr )
            {
                errno = ENOENT;
                nRet = -1;
            }
            else if ( nErr == noErr && bAliased )
            {
                char tmpPath[ PATH_MAX ];
                if ( FSRefMakePath( &aFSRef, (UInt8 *)tmpPath, PATH_MAX ) == noErr )
                {
                    int nLen = strlen( tmpPath ) + ( unprocessedPath ? strlen( unprocessedPath + 1 ) + 1 : 0 );
                    if ( nLen < buflen && nLen < PATH_MAX )
                    {
                        if ( unprocessedPath )
                        {
                            int nTmpPathLen = strlen( tmpPath );
                            strcat( tmpPath, "/" );
                            strcat( tmpPath, unprocessedPath + 1 );
                            strcpy( path, tmpPath);
                            unprocessedPath = path + nTmpPathLen;
                        }
                        else if ( !unprocessedPath )
                        {
                            strcpy( path, tmpPath);
                        }
                    }
                    else
                    {
                        errno = ENAMETOOLONG;
                        nRet = -1;
                    }
                }
            }
        }
#else
        cfpath = CFStringCreateWithCString( NULL, path, kCFStringEncodingUTF8 );
        cfurl = CFURLCreateWithFileSystemPath( NULL, cfpath, kCFURLPOSIXPathStyle, false );
        CFRelease( cfpath );
        cferror = NULL;
        cfbookmark = CFURLCreateBookmarkDataFromFile( NULL, cfurl, &cferror );
        CFRelease( cfurl );

        if ( cfbookmark == NULL )
        {
            if(cferror)
            {
                CFRelease( cferror );
            }
        }
        else
        {
            Boolean isStale;
            cfurl = CFURLCreateByResolvingBookmarkData( NULL, cfbookmark, kCFBookmarkResolutionWithoutUIMask,
                    NULL, NULL, &isStale, &cferror );
            CFRelease( cfbookmark );
            if ( cfurl == NULL )
            {
                CFRelease( cferror );
            }
            else
            {
                cfpath = CFURLCopyFileSystemPath( cfurl, kCFURLPOSIXPathStyle );
                CFRelease( cfurl );
                if ( cfpath != NULL )
                {
                    char tmpPath[ PATH_MAX ];
                    if ( CFStringGetCString( cfpath, tmpPath, PATH_MAX, kCFStringEncodingUTF8 ) )
                    {
                        int nLen = strlen( tmpPath ) + ( unprocessedPath ? strlen( unprocessedPath + 1 ) + 1 : 0 );
                        if ( nLen < buflen && nLen < PATH_MAX )
                        {
                            if ( unprocessedPath )
                            {
                                int nTmpPathLen = strlen( tmpPath );
                                strcat( tmpPath, "/" );
                                strcat( tmpPath, unprocessedPath + 1 );
                                strcpy( path, tmpPath);
                                unprocessedPath = path + nTmpPathLen;
                            }
                            else if ( !unprocessedPath )
                            {
                                strcpy( path, tmpPath );
                            }
                        }
                        else
                        {
                            errno = ENAMETOOLONG;
                            nRet = -1;
                        }
                    }
                    CFRelease( cfpath );
                }
            }
        }
#endif

        if ( unprocessedPath )
            *unprocessedPath++ = '/';
    }

    return nRet;
#endif
}
Example #4
0
sqInt dir_Lookup(char *pathString, sqInt pathStringLength, sqInt index,
/* outputs: */  char *name, sqInt *nameLength, sqInt *creationDate, sqInt *modificationDate,
		sqInt *isDirectory, squeakFileOffsetType *sizeIfFile)
{
  /* Lookup the index-th entry of the directory with the given path, starting
     at the root of the file system. Set the name, name length, creation date,
     creation time, directory flag, and file size (if the entry is a file).
     Return:	0 	if a entry is found at the given index
     		1	if the directory has fewer than index entries
		2	if the given path has bad syntax or does not reach a directory
  */

  int i;
  int nameLen= 0;
  struct dirent *dirEntry= 0;
  char unixPath[DOCUMENT_NAME_SIZE+1];
  struct stat statBuf;

  /* default return values */
  *name             = 0;
  *nameLength       = 0;
  *creationDate     = 0;
  *modificationDate = 0;
  *isDirectory      = false;
  *sizeIfFile       = 0;

  if ((pathStringLength == 0))
    strcpy(unixPath, ".");
  else  {
	if (!ioFilenamefromStringofLengthresolveAliasesRetry(unixPath, pathString,pathStringLength, true, true))
		return BAD_PATH;
	}

  /* get file or directory info */
  if (!maybeOpenDir(unixPath))
    return BAD_PATH;

  if (++lastIndex == index)
    index= 1;		/* fake that the dir is rewound and we want the first entry */
  else
    {
      rewinddir(openDir);	/* really rewind it, and read to the index */
      lastIndex= index;
    }

  for (i= 0; i < index; i++)
    {
    nextEntry:
      do
	{ 
	  errno= 0; 
	  dirEntry= readdir(openDir);
	}
      while ((dirEntry == 0) && (errno == EINTR));

      if (!dirEntry)
	return NO_MORE_ENTRIES;
      
      nameLen= NAMLEN(dirEntry);

      /* ignore '.' and '..' (these are not *guaranteed* to be first) */
      if (nameLen < 3 && dirEntry->d_name[0] == '.')
	if (nameLen == 1 || dirEntry->d_name[1] == '.')
	  goto nextEntry;
    }

  *nameLength= ux2sqPath(dirEntry->d_name, nameLen, name, 256, 0);

  {
    char terminatedName[DOCUMENT_NAME_SIZE+1];
    strncpy(terminatedName, dirEntry->d_name, nameLen);
    terminatedName[nameLen]= '\0';
    strcat(unixPath, "/");
    strcat(unixPath, terminatedName);
    if (stat(unixPath, &statBuf) && lstat(unixPath, &statBuf))
      {
	/* We can't stat the entry, but failing here would invalidate
	   the whole directory --bertf */
	return ENTRY_FOUND;
      }
  }

  /* last change time */
  *creationDate= convertToSqueakTime(statBuf.st_ctime);
  /* modification time */
  *modificationDate= convertToSqueakTime(statBuf.st_mtime);
	{
		FSRef targetFSRef;
		Boolean	targetIsFolder,wasAliased;
		OSErr err;
		
		err = getFSRef(unixPath,&targetFSRef,kCFStringEncodingUTF8);
		if (!err) {
			FSResolveAliasFileWithMountFlags(&targetFSRef,true,&targetIsFolder,&wasAliased,kResolveAliasFileNoUI);
			if (wasAliased && targetIsFolder) {
				*isDirectory= true;
				return ENTRY_FOUND;
			}
		}
	}
  if (S_ISDIR(statBuf.st_mode))
    *isDirectory= true;
  else
    *sizeIfFile= statBuf.st_size;

  return ENTRY_FOUND;
}