BlockGuardOs.java revision 996bf79565ac88402920bd826d6f85952c83be20
152724d3ebd4ccaaa4b9f5576e329d4272cde8ea9Elliott Hughes/*
252724d3ebd4ccaaa4b9f5576e329d4272cde8ea9Elliott Hughes * Copyright (C) 2011 The Android Open Source Project
352724d3ebd4ccaaa4b9f5576e329d4272cde8ea9Elliott Hughes *
452724d3ebd4ccaaa4b9f5576e329d4272cde8ea9Elliott Hughes * Licensed under the Apache License, Version 2.0 (the "License");
552724d3ebd4ccaaa4b9f5576e329d4272cde8ea9Elliott Hughes * you may not use this file except in compliance with the License.
652724d3ebd4ccaaa4b9f5576e329d4272cde8ea9Elliott Hughes * You may obtain a copy of the License at
752724d3ebd4ccaaa4b9f5576e329d4272cde8ea9Elliott Hughes *
852724d3ebd4ccaaa4b9f5576e329d4272cde8ea9Elliott Hughes *      http://www.apache.org/licenses/LICENSE-2.0
952724d3ebd4ccaaa4b9f5576e329d4272cde8ea9Elliott Hughes *
1052724d3ebd4ccaaa4b9f5576e329d4272cde8ea9Elliott Hughes * Unless required by applicable law or agreed to in writing, software
1152724d3ebd4ccaaa4b9f5576e329d4272cde8ea9Elliott Hughes * distributed under the License is distributed on an "AS IS" BASIS,
1252724d3ebd4ccaaa4b9f5576e329d4272cde8ea9Elliott Hughes * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1352724d3ebd4ccaaa4b9f5576e329d4272cde8ea9Elliott Hughes * See the License for the specific language governing permissions and
1452724d3ebd4ccaaa4b9f5576e329d4272cde8ea9Elliott Hughes * limitations under the License.
1552724d3ebd4ccaaa4b9f5576e329d4272cde8ea9Elliott Hughes */
1652724d3ebd4ccaaa4b9f5576e329d4272cde8ea9Elliott Hughes
1752724d3ebd4ccaaa4b9f5576e329d4272cde8ea9Elliott Hughespackage libcore.io;
1852724d3ebd4ccaaa4b9f5576e329d4272cde8ea9Elliott Hughes
1952724d3ebd4ccaaa4b9f5576e329d4272cde8ea9Elliott Hughesimport dalvik.system.BlockGuard;
2052724d3ebd4ccaaa4b9f5576e329d4272cde8ea9Elliott Hughesimport java.io.FileDescriptor;
21996bf79565ac88402920bd826d6f85952c83be20Elliott Hughesimport java.net.InetAddress;
2226c7025a7a919044771fb89031161bd26fe03032Elliott Hughesimport java.nio.ByteBuffer;
230ac77ac8e915bff1a863e371f9b363033f9cf759Elliott Hughesimport static libcore.io.OsConstants.*;
2452724d3ebd4ccaaa4b9f5576e329d4272cde8ea9Elliott Hughes
2552724d3ebd4ccaaa4b9f5576e329d4272cde8ea9Elliott Hughes/**
2652724d3ebd4ccaaa4b9f5576e329d4272cde8ea9Elliott Hughes * Informs BlockGuard of any activity it should be aware of.
2752724d3ebd4ccaaa4b9f5576e329d4272cde8ea9Elliott Hughes */
2852724d3ebd4ccaaa4b9f5576e329d4272cde8ea9Elliott Hughespublic class BlockGuardOs extends ForwardingOs {
2952724d3ebd4ccaaa4b9f5576e329d4272cde8ea9Elliott Hughes    public BlockGuardOs(Os os) {
3052724d3ebd4ccaaa4b9f5576e329d4272cde8ea9Elliott Hughes        super(os);
3152724d3ebd4ccaaa4b9f5576e329d4272cde8ea9Elliott Hughes    }
3252724d3ebd4ccaaa4b9f5576e329d4272cde8ea9Elliott Hughes
33996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes    public void connect(FileDescriptor fd, InetAddress address, int port) throws ErrnoException {
34996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes        BlockGuard.getThreadPolicy().onNetwork();
35996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes        os.connect(fd, address, port);
36996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes    }
37996bf79565ac88402920bd826d6f85952c83be20Elliott Hughes
3852724d3ebd4ccaaa4b9f5576e329d4272cde8ea9Elliott Hughes    public void fdatasync(FileDescriptor fd) throws ErrnoException {
3952724d3ebd4ccaaa4b9f5576e329d4272cde8ea9Elliott Hughes        BlockGuard.getThreadPolicy().onWriteToDisk();
4052724d3ebd4ccaaa4b9f5576e329d4272cde8ea9Elliott Hughes        os.fdatasync(fd);
4152724d3ebd4ccaaa4b9f5576e329d4272cde8ea9Elliott Hughes    }
4252724d3ebd4ccaaa4b9f5576e329d4272cde8ea9Elliott Hughes
4352724d3ebd4ccaaa4b9f5576e329d4272cde8ea9Elliott Hughes    public void fsync(FileDescriptor fd) throws ErrnoException {
4452724d3ebd4ccaaa4b9f5576e329d4272cde8ea9Elliott Hughes        BlockGuard.getThreadPolicy().onWriteToDisk();
4552724d3ebd4ccaaa4b9f5576e329d4272cde8ea9Elliott Hughes        os.fsync(fd);
4652724d3ebd4ccaaa4b9f5576e329d4272cde8ea9Elliott Hughes    }
47f5333fd2094bdac4d6506177b1964b79afa64d77Elliott Hughes
48f5333fd2094bdac4d6506177b1964b79afa64d77Elliott Hughes    public void ftruncate(FileDescriptor fd, long length) throws ErrnoException {
49f5333fd2094bdac4d6506177b1964b79afa64d77Elliott Hughes        BlockGuard.getThreadPolicy().onWriteToDisk();
50f5333fd2094bdac4d6506177b1964b79afa64d77Elliott Hughes        os.ftruncate(fd, length);
51f5333fd2094bdac4d6506177b1964b79afa64d77Elliott Hughes    }
520ac77ac8e915bff1a863e371f9b363033f9cf759Elliott Hughes
530ac77ac8e915bff1a863e371f9b363033f9cf759Elliott Hughes    public FileDescriptor open(String path, int flags, int mode) throws ErrnoException {
540ac77ac8e915bff1a863e371f9b363033f9cf759Elliott Hughes        BlockGuard.getThreadPolicy().onReadFromDisk();
550ac77ac8e915bff1a863e371f9b363033f9cf759Elliott Hughes        if ((mode & O_ACCMODE) != O_RDONLY) {
560ac77ac8e915bff1a863e371f9b363033f9cf759Elliott Hughes            BlockGuard.getThreadPolicy().onWriteToDisk();
570ac77ac8e915bff1a863e371f9b363033f9cf759Elliott Hughes        }
580ac77ac8e915bff1a863e371f9b363033f9cf759Elliott Hughes        return os.open(path, flags, mode);
590ac77ac8e915bff1a863e371f9b363033f9cf759Elliott Hughes    }
6026c7025a7a919044771fb89031161bd26fe03032Elliott Hughes
6126c7025a7a919044771fb89031161bd26fe03032Elliott Hughes    public int read(FileDescriptor fd, ByteBuffer buffer) throws ErrnoException {
6226c7025a7a919044771fb89031161bd26fe03032Elliott Hughes        BlockGuard.getThreadPolicy().onReadFromDisk();
6326c7025a7a919044771fb89031161bd26fe03032Elliott Hughes        return os.read(fd, buffer);
6426c7025a7a919044771fb89031161bd26fe03032Elliott Hughes    }
6526c7025a7a919044771fb89031161bd26fe03032Elliott Hughes
6626c7025a7a919044771fb89031161bd26fe03032Elliott Hughes    public int read(FileDescriptor fd, byte[] bytes, int byteOffset, int byteCount) throws ErrnoException {
6726c7025a7a919044771fb89031161bd26fe03032Elliott Hughes        BlockGuard.getThreadPolicy().onReadFromDisk();
6826c7025a7a919044771fb89031161bd26fe03032Elliott Hughes        return os.read(fd, bytes, byteOffset, byteCount);
6926c7025a7a919044771fb89031161bd26fe03032Elliott Hughes    }
7078c7cc547101002b9f9043cf3845970719d1bda8Elliott Hughes
71bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes    public int readv(FileDescriptor fd, Object[] buffers, int[] offsets, int[] byteCounts) throws ErrnoException {
72bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes        BlockGuard.getThreadPolicy().onReadFromDisk();
73bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes        return os.readv(fd, buffers, offsets, byteCounts);
74bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes    }
75bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes
7678c7cc547101002b9f9043cf3845970719d1bda8Elliott Hughes    public int write(FileDescriptor fd, ByteBuffer buffer) throws ErrnoException {
7778c7cc547101002b9f9043cf3845970719d1bda8Elliott Hughes        BlockGuard.getThreadPolicy().onWriteToDisk();
7878c7cc547101002b9f9043cf3845970719d1bda8Elliott Hughes        return os.write(fd, buffer);
7978c7cc547101002b9f9043cf3845970719d1bda8Elliott Hughes    }
8078c7cc547101002b9f9043cf3845970719d1bda8Elliott Hughes
8178c7cc547101002b9f9043cf3845970719d1bda8Elliott Hughes    public int write(FileDescriptor fd, byte[] bytes, int byteOffset, int byteCount) throws ErrnoException {
8278c7cc547101002b9f9043cf3845970719d1bda8Elliott Hughes        BlockGuard.getThreadPolicy().onWriteToDisk();
8378c7cc547101002b9f9043cf3845970719d1bda8Elliott Hughes        return os.write(fd, bytes, byteOffset, byteCount);
8478c7cc547101002b9f9043cf3845970719d1bda8Elliott Hughes    }
85bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes
86bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes    public int writev(FileDescriptor fd, Object[] buffers, int[] offsets, int[] byteCounts) throws ErrnoException {
87bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes        BlockGuard.getThreadPolicy().onWriteToDisk();
88bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes        return os.writev(fd, buffers, offsets, byteCounts);
89bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes    }
9052724d3ebd4ccaaa4b9f5576e329d4272cde8ea9Elliott Hughes}
91