Ticket #5520: libbe_storagekit_file-isreadable_iswritable-checkmode.patch

File libbe_storagekit_file-isreadable_iswritable-checkmode.patch, 2.8 KB (added by ver, 14 years ago)

Revision to stat only if InitCheck() results in B_OK

  • src/kits/storage/File.cpp

     
    295295/*! \brief Returns whether the file is readable.
    296296    \return
    297297    - \c true, if the BFile has been initialized properly and the file has
    298       been been opened for reading,
     298      been been opened for reading, and has read permissions.
    299299    - \c false, otherwise.
    300300*/
    301301bool
    302302BFile::IsReadable() const
    303303{
    304     return InitCheck() == B_OK
    305         && ((fMode & O_RWMASK) == O_RDONLY || (fMode & O_RWMASK) == O_RDWR);
     304    if (InitCheck() != B_OK)
     305        return false;
     306
     307    struct stat st;
     308    if (!fstat(get_fd(), &st)) {
     309        return false;
     310    }
     311
     312    unsigned int fUid = getuid();                                                                                                                                           
     313    unsigned int fGid = getgid();                                                                                                                                           
     314
     315    bool fReadable = !((fUid == st.st_uid && (S_IRUSR & st.st_mode))                                                                                                       
     316            || (fGid == st.st_gid && (S_IRGRP & st.st_mode))                                                                                                       
     317            || (S_IROTH & st.st_mode));
     318
     319    if (false==fReadable)
     320        return false;
     321
     322    return (fMode & O_RWMASK) == O_RDONLY || (fMode & O_RWMASK) == O_RDWR;
    306323}
    307324
    308325
    309326/*! \brief Returns whether the file is writable.
    310327    \return
    311328    - \c true, if the BFile has been initialized properly and the file has
    312       been opened for writing,
     329      been opened for writing, and has write permissions.
    313330    - \c false, otherwise.
    314331*/
    315332bool
    316333BFile::IsWritable() const
    317334{
    318     return InitCheck() == B_OK
    319         && ((fMode & O_RWMASK) == O_WRONLY || (fMode & O_RWMASK) == O_RDWR);
     335    if (InitCheck() != B_OK)
     336        return false;
     337
     338    struct stat st;
     339    if (!fstat(get_fd(), &st)) {
     340        return false;
     341    }
     342
     343    unsigned int fUid = getuid();                                                                                                                                           
     344    unsigned int fGid = getgid();                                                                                                                                           
     345
     346    bool fReadOnly = !((fUid == st.st_uid && (S_IWUSR & st.st_mode))                                                                                                       
     347            || (fGid == st.st_gid && (S_IWGRP & st.st_mode))                                                                                                       
     348            || (S_IWOTH & st.st_mode));
     349
     350    if (true==fReadOnly)
     351        return false;
     352
     353    return (fMode & O_RWMASK) == O_WRONLY || (fMode & O_RWMASK) == O_RDWR;
    320354}
    321355
    322356