android.system.StructStat Java Examples

The following examples show how to use android.system.StructStat. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: DexPathList.java    From AndroidComponentPlugin with Apache License 2.0 6 votes vote down vote up
/**
 * Splits the given path strings into file elements using the path
 * separator, combining the results and filtering out elements
 * that don't exist, aren't readable, or aren't either a regular
 * file or a directory (as specified). Either string may be empty
 * or {@code null}, in which case it is ignored. If both strings
 * are empty or {@code null}, or all elements get pruned out, then
 * this returns a zero-element list.
 */
private static List<File> splitPaths(String searchPath, boolean directoriesOnly) {
    List<File> result = new ArrayList<>();

    if (searchPath != null) {
        for (String path : searchPath.split(File.pathSeparator)) {
            if (directoriesOnly) {
                try {
                    StructStat sb = Libcore.os.stat(path);
                    if (!S_ISDIR(sb.st_mode)) {
                        continue;
                    }
                } catch (ErrnoException ignored) {
                    continue;
                }
            }
            result.add(new File(path));
        }
    }

    return result;
}
 
Example #2
Source File: LoadStatTask.java    From samba-documents-provider with GNU General Public License v3.0 6 votes vote down vote up
@Override
public Map<Uri, StructStat> doInBackground(Void... args) {
  Map<Uri, StructStat> stats = new HashMap<>(mMetadataMap.size());
  for (DocumentMetadata metadata : mMetadataMap.values()) {
    try {
      metadata.loadStat(mClient);
      if (isCancelled()) {
        return stats;
      }
    } catch(Exception e) {
      // Failed to load a stat for a child... Just eat this exception, the only consequence it may
      // have is constantly retrying to fetch the stat.
      Log.e(TAG, "Failed to load stat for " + metadata.getUri());
    }
  }
  return stats;
}
 
Example #3
Source File: DocumentMetadata.java    From samba-documents-provider with GNU General Public License v3.0 6 votes vote down vote up
public static DocumentMetadata fromUri(Uri uri, SmbClient client) throws IOException {
final List<String> pathSegments = uri.getPathSegments();
if (pathSegments.isEmpty()) {
  throw new UnsupportedOperationException("Can't load metadata for workgroup or server.");
}

final StructStat stat = client.stat(uri.toString());
  final DirectoryEntry entry = new DirectoryEntry(
      OsConstants.S_ISDIR(stat.st_mode) ? DirectoryEntry.DIR : DirectoryEntry.FILE,
      "",
      uri.getLastPathSegment());
  final DocumentMetadata metadata = new DocumentMetadata(uri, entry);
  metadata.mStat.set(stat);

  return metadata;
}
 
Example #4
Source File: SharedPreferencesImpl.java    From android_9.0.0_r45 with Apache License 2.0 6 votes vote down vote up
private boolean hasFileChangedUnexpectedly() {
    synchronized (mLock) {
        if (mDiskWritesInFlight > 0) {
            // If we know we caused it, it's not unexpected.
            if (DEBUG) Log.d(TAG, "disk write in flight, not unexpected.");
            return false;
        }
    }

    final StructStat stat;
    try {
        /*
         * Metadata operations don't usually count as a block guard
         * violation, but we explicitly want this one.
         */
        BlockGuard.getThreadPolicy().onReadFromDisk();
        stat = Os.stat(mFile.getPath());
    } catch (ErrnoException e) {
        return true;
    }

    synchronized (mLock) {
        return !stat.st_mtim.equals(mStatTimestamp) || mStatSize != stat.st_size;
    }
}
 
Example #5
Source File: ParcelFileDescriptor.java    From android_9.0.0_r45 with Apache License 2.0 6 votes vote down vote up
/**
 * Return the total size of the file representing this fd, as determined by
 * {@code stat()}. Returns -1 if the fd is not a file.
 */
public long getStatSize() {
    if (mWrapped != null) {
        return mWrapped.getStatSize();
    } else {
        try {
            final StructStat st = Os.fstat(mFd);
            if (S_ISREG(st.st_mode) || S_ISLNK(st.st_mode)) {
                return st.st_size;
            } else {
                return -1;
            }
        } catch (ErrnoException e) {
            Log.w(TAG, "fstat() failed: " + e);
            return -1;
        }
    }
}
 
Example #6
Source File: FileUtils.java    From android_9.0.0_r45 with Apache License 2.0 6 votes vote down vote up
/**
 * Copy the contents of one FD to another.
 * <p>
 * Attempts to use several optimization strategies to copy the data in the
 * kernel before falling back to a userspace copy as a last resort.
 *
 * @param listener to be periodically notified as the copy progresses.
 * @param signal to signal if the copy should be cancelled early.
 * @param count the number of bytes to copy.
 * @return number of bytes copied.
 */
public static long copy(@NonNull FileDescriptor in, @NonNull FileDescriptor out,
        @Nullable ProgressListener listener, @Nullable CancellationSignal signal, long count)
        throws IOException {
    if (ENABLE_COPY_OPTIMIZATIONS) {
        try {
            final StructStat st_in = Os.fstat(in);
            final StructStat st_out = Os.fstat(out);
            if (S_ISREG(st_in.st_mode) && S_ISREG(st_out.st_mode)) {
                return copyInternalSendfile(in, out, listener, signal, count);
            } else if (S_ISFIFO(st_in.st_mode) || S_ISFIFO(st_out.st_mode)) {
                return copyInternalSplice(in, out, listener, signal, count);
            }
        } catch (ErrnoException e) {
            throw e.rethrowAsIOException();
        }
    }

    // Worse case fallback to userspace
    return copyInternalUserspace(in, out, listener, signal, count);
}
 
Example #7
Source File: PackageInstallerSession.java    From android_9.0.0_r45 with Apache License 2.0 6 votes vote down vote up
/**
 * Determine if creating hard links between source and destination is
 * possible. That is, do they all live on the same underlying device.
 */
private boolean isLinkPossible(List<File> fromFiles, File toDir) {
    try {
        final StructStat toStat = Os.stat(toDir.getAbsolutePath());
        for (File fromFile : fromFiles) {
            final StructStat fromStat = Os.stat(fromFile.getAbsolutePath());
            if (fromStat.st_dev != toStat.st_dev) {
                return false;
            }
        }
    } catch (ErrnoException e) {
        Slog.w(TAG, "Failed to detect if linking possible: " + e);
        return false;
    }
    return true;
}
 
Example #8
Source File: DexPathList.java    From AndroidComponentPlugin with Apache License 2.0 6 votes vote down vote up
/**
 * Splits the given path strings into file elements using the path
 * separator, combining the results and filtering out elements
 * that don't exist, aren't readable, or aren't either a regular
 * file or a directory (as specified). Either string may be empty
 * or {@code null}, in which case it is ignored. If both strings
 * are empty or {@code null}, or all elements get pruned out, then
 * this returns a zero-element list.
 */
private static List<File> splitPaths(String searchPath, boolean directoriesOnly) {
    List<File> result = new ArrayList<>();

    if (searchPath != null) {
        for (String path : searchPath.split(File.pathSeparator)) {
            if (directoriesOnly) {
                try {
                    StructStat sb = Libcore.os.stat(path);
                    if (!S_ISDIR(sb.st_mode)) {
                        continue;
                    }
                } catch (ErrnoException ignored) {
                    continue;
                }
            }
            result.add(new File(path));
        }
    }

    return result;
}
 
Example #9
Source File: DexPathList.java    From AndroidComponentPlugin with Apache License 2.0 6 votes vote down vote up
/**
 * Helper for {@link #splitPaths}, which does the actual splitting
 * and filtering and adding to a result.
 */
private static void splitAndAdd(String searchPath, boolean directoriesOnly,
        ArrayList<File> resultList) {
    if (searchPath == null) {
        return;
    }
    for (String path : searchPath.split(":")) {
        try {
            StructStat sb = Libcore.os.stat(path);
            if (!directoriesOnly || S_ISDIR(sb.st_mode)) {
                resultList.add(new File(path));
            }
        } catch (ErrnoException ignored) {
        }
    }
}
 
Example #10
Source File: DexPathList.java    From AndroidComponentPlugin with Apache License 2.0 6 votes vote down vote up
/**
 * Splits the given path strings into file elements using the path
 * separator, combining the results and filtering out elements
 * that don't exist, aren't readable, or aren't either a regular
 * file or a directory (as specified). Either string may be empty
 * or {@code null}, in which case it is ignored. If both strings
 * are empty or {@code null}, or all elements get pruned out, then
 * this returns a zero-element list.
 */
@UnsupportedAppUsage
private static List<File> splitPaths(String searchPath, boolean directoriesOnly) {
    List<File> result = new ArrayList<>();

    if (searchPath != null) {
        for (String path : searchPath.split(File.pathSeparator)) {
            if (directoriesOnly) {
                try {
                    StructStat sb = Libcore.os.stat(path);
                    if (!S_ISDIR(sb.st_mode)) {
                        continue;
                    }
                } catch (ErrnoException ignored) {
                    continue;
                }
            }
            result.add(new File(path));
        }
    }

    return result;
}
 
Example #11
Source File: DexPathList.java    From AndroidComponentPlugin with Apache License 2.0 6 votes vote down vote up
/**
 * Splits the given path strings into file elements using the path
 * separator, combining the results and filtering out elements
 * that don't exist, aren't readable, or aren't either a regular
 * file or a directory (as specified). Either string may be empty
 * or {@code null}, in which case it is ignored. If both strings
 * are empty or {@code null}, or all elements get pruned out, then
 * this returns a zero-element list.
 */
private static List<File> splitPaths(String searchPath, boolean directoriesOnly) {
    List<File> result = new ArrayList<>();

    if (searchPath != null) {
        for (String path : searchPath.split(File.pathSeparator)) {
            if (directoriesOnly) {
                try {
                    StructStat sb = Libcore.os.stat(path);
                    if (!S_ISDIR(sb.st_mode)) {
                        continue;
                    }
                } catch (ErrnoException ignored) {
                    continue;
                }
            }
            result.add(new File(path));
        }
    }

    return result;
}
 
Example #12
Source File: DexPathList.java    From AndroidComponentPlugin with Apache License 2.0 6 votes vote down vote up
/**
 * Helper for {@link #splitPaths}, which does the actual splitting
 * and filtering and adding to a result.
 */
private static void splitAndAdd(String searchPath, boolean directoriesOnly,
        ArrayList<File> resultList) {
    if (searchPath == null) {
        return;
    }
    for (String path : searchPath.split(":")) {
        try {
            StructStat sb = Libcore.os.stat(path);
            if (!directoriesOnly || S_ISDIR(sb.st_mode)) {
                resultList.add(new File(path));
            }
        } catch (ErrnoException ignored) {
        }
    }
}
 
Example #13
Source File: DexPathList.java    From AndroidComponentPlugin with Apache License 2.0 6 votes vote down vote up
/**
 * Splits the given path strings into file elements using the path
 * separator, combining the results and filtering out elements
 * that don't exist, aren't readable, or aren't either a regular
 * file or a directory (as specified). Either string may be empty
 * or {@code null}, in which case it is ignored. If both strings
 * are empty or {@code null}, or all elements get pruned out, then
 * this returns a zero-element list.
 */
private static List<File> splitPaths(String searchPath, boolean directoriesOnly) {
    List<File> result = new ArrayList<>();

    if (searchPath != null) {
        for (String path : searchPath.split(File.pathSeparator)) {
            if (directoriesOnly) {
                try {
                    StructStat sb = Libcore.os.stat(path);
                    if (!S_ISDIR(sb.st_mode)) {
                        continue;
                    }
                } catch (ErrnoException ignored) {
                    continue;
                }
            }
            result.add(new File(path));
        }
    }

    return result;
}
 
Example #14
Source File: DexPathList.java    From AndroidComponentPlugin with Apache License 2.0 6 votes vote down vote up
/**
 * Splits the given path strings into file elements using the path
 * separator, combining the results and filtering out elements
 * that don't exist, aren't readable, or aren't either a regular
 * file or a directory (as specified). Either string may be empty
 * or {@code null}, in which case it is ignored. If both strings
 * are empty or {@code null}, or all elements get pruned out, then
 * this returns a zero-element list.
 */
private static List<File> splitPaths(String searchPath, boolean directoriesOnly) {
    List<File> result = new ArrayList<>();

    if (searchPath != null) {
        for (String path : searchPath.split(File.pathSeparator)) {
            if (directoriesOnly) {
                try {
                    StructStat sb = Libcore.os.stat(path);
                    if (!S_ISDIR(sb.st_mode)) {
                        continue;
                    }
                } catch (ErrnoException ignored) {
                    continue;
                }
            }
            result.add(new File(path));
        }
    }

    return result;
}
 
Example #15
Source File: DexPathList.java    From AndroidComponentPlugin with Apache License 2.0 6 votes vote down vote up
/**
 * Splits the given path strings into file elements using the path
 * separator, combining the results and filtering out elements
 * that don't exist, aren't readable, or aren't either a regular
 * file or a directory (as specified). Either string may be empty
 * or {@code null}, in which case it is ignored. If both strings
 * are empty or {@code null}, or all elements get pruned out, then
 * this returns a zero-element list.
 */
private static List<File> splitPaths(String searchPath, boolean directoriesOnly) {
    List<File> result = new ArrayList<>();

    if (searchPath != null) {
        for (String path : searchPath.split(File.pathSeparator)) {
            if (directoriesOnly) {
                try {
                    StructStat sb = Libcore.os.stat(path);
                    if (!S_ISDIR(sb.st_mode)) {
                        continue;
                    }
                } catch (ErrnoException ignored) {
                    continue;
                }
            }
            result.add(new File(path));
        }
    }

    return result;
}
 
Example #16
Source File: DexPathList.java    From AndroidComponentPlugin with Apache License 2.0 5 votes vote down vote up
/**
 * Splits the given path strings into file elements using the path
 * separator, combining the results and filtering out elements
 * that don't exist, aren't readable, or aren't either a regular
 * file or a directory (as specified). Either string may be empty
 * or {@code null}, in which case it is ignored. If both strings
 * are empty or {@code null}, or all elements get pruned out, then
 * this returns a zero-element list.
 */
private static List<File> splitPaths(String searchPath, boolean directoriesOnly) {
    List<File> result = new ArrayList<>();

    if (searchPath != null) {
        for (String path : searchPath.split(File.pathSeparator)) {
            if (directoriesOnly) {
                try {
                    StructStat sb = Libcore.os.stat(path);
                    if (!S_ISDIR(sb.st_mode)) {
                        continue;
                    }
                } catch (ErrnoException ignored) {
                    continue;
                }
            }
            result.add(new File(path));
        }
    }

    return result;
}
 
Example #17
Source File: SafeContentResolverApi21.java    From SafeContentResolver with Apache License 2.0 5 votes vote down vote up
@Override
protected int getFileUidOrThrow(@NotNull FileDescriptor fileDescriptor) throws FileNotFoundException {
    try {
        StructStat st = Os.fstat(fileDescriptor);
        return st.st_uid;
    } catch (android.system.ErrnoException e) {
        throw new FileNotFoundException(e.getMessage());
    }
}
 
Example #18
Source File: CleanCacheService21.java    From fdroidclient with GNU General Public License v3.0 5 votes vote down vote up
static void deleteIfOld(File file, long olderThan) {
    if (file == null || !file.exists()) {
        return;
    }
    try {
        StructStat stat = Os.lstat(file.getAbsolutePath());
        if ((stat.st_atime * 1000L) < olderThan) {
            file.delete();
        }
    } catch (ErrnoException e) {
        e.printStackTrace();
    }
}
 
Example #19
Source File: SambaProxyFileCallback.java    From samba-documents-provider with GNU General Public License v3.0 5 votes vote down vote up
@Override
public long onGetSize() throws ErrnoException {
  StructStat stat;
  try {
    stat = mFile.fstat();
    return stat.st_size;
  } catch (IOException e) {
    throwErrnoException(e);
  }

  return 0;
}
 
Example #20
Source File: SambaFileClient.java    From samba-documents-provider with GNU General Public License v3.0 5 votes vote down vote up
@Override
public StructStat fstat() throws IOException {
  try (final MessageValues<StructStat> messageValues = MessageValues.obtain()) {
    final Message msg = mHandler.obtainMessage(FSTAT, messageValues);
    enqueue(msg);
    return messageValues.getObj();
  }
}
 
Example #21
Source File: NativeSambaFacade.java    From samba-documents-provider with GNU General Public License v3.0 5 votes vote down vote up
@Override
public StructStat stat(String uri) throws IOException {
  try {
    checkNativeHandler();
    return stat(mNativeHandler, uri);
  } catch (ErrnoException e) {
    throw new IOException("Failed to get stat of " + uri, e);
  }
}
 
Example #22
Source File: SambaFile.java    From samba-documents-provider with GNU General Public License v3.0 5 votes vote down vote up
@Override
public StructStat fstat() throws IOException {
  try {
    return fstat(mNativeHandler, mNativeFd);
  } catch (ErrnoException e) {
    throw new IOException("Failed to get stat of " + mNativeFd, e);
  }
}
 
Example #23
Source File: SambaFacadeClient.java    From samba-documents-provider with GNU General Public License v3.0 5 votes vote down vote up
@Override
public StructStat stat(String uri) throws IOException {
  try (final MessageValues<StructStat> messageValues = MessageValues.obtain()) {
    final Message msg = obtainMessage(STAT, messageValues, uri);
    enqueue(msg);
    return messageValues.getObj();
  }
}
 
Example #24
Source File: FileUtils.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
public static void copyPermissions(File from, File to) throws IOException {
    try {
        final StructStat stat = Os.stat(from.getAbsolutePath());
        Os.chmod(to.getAbsolutePath(), stat.st_mode);
        Os.chown(to.getAbsolutePath(), stat.st_uid, stat.st_gid);
    } catch (ErrnoException e) {
        throw e.rethrowAsIOException();
    }
}
 
Example #25
Source File: LoadStatTask.java    From samba-documents-provider with GNU General Public License v3.0 4 votes vote down vote up
@Override
public void onPostExecute(Map<Uri, StructStat> stats) {
  mCallback.onTaskFinished(OnTaskFinishedCallback.SUCCEEDED, mMetadataMap, null);
}
 
Example #26
Source File: LoadStatTask.java    From samba-documents-provider with GNU General Public License v3.0 4 votes vote down vote up
@Override
public void onCancelled(Map<Uri, StructStat> stats) {
  mCallback.onTaskFinished(OnTaskFinishedCallback.CANCELLED, mMetadataMap, null);
}
 
Example #27
Source File: DocumentMetadata.java    From samba-documents-provider with GNU General Public License v3.0 4 votes vote down vote up
public Long getLastModified() {
  final StructStat stat = mStat.get();
  return (stat == null) ? null : TimeUnit.MILLISECONDS.convert(stat.st_mtime, TimeUnit.SECONDS);
}
 
Example #28
Source File: DocumentMetadata.java    From samba-documents-provider with GNU General Public License v3.0 4 votes vote down vote up
public Long getSize() {
  final StructStat stat = mStat.get();
  return (stat == null) ? null : stat.st_size;
}
 
Example #29
Source File: SmbClient.java    From samba-documents-provider with GNU General Public License v3.0 votes vote down vote up
StructStat stat(String uri) throws IOException; 
Example #30
Source File: SmbFile.java    From samba-documents-provider with GNU General Public License v3.0 votes vote down vote up
StructStat fstat() throws IOException;