File: protection.cc

package info (click to toggle)
bobcat 6.02.02-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 13,960 kB
  • sloc: cpp: 18,954; fortran: 5,617; makefile: 2,787; sh: 659; perl: 401; ansic: 26
file content (36 lines) | stat: -rw-r--r-- 1,331 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include "exception.ih"

size_t Exception::protection(std::string const &name, size_t protect,
                           Protection type)
{
    if (protect > 0777)
        throw Exception{} << "Protection for `" << name << "' "
                             "may not exceed 0777 (requested: 0" << oct <<
                                protect << dec << ')';

    struct stat statbuf;

                            // no: create it with the requested protection
    if (stat(name.c_str(), &statbuf) != 0)                          // bits
    {
        if (errno != ENOENT)    // stat error, but not non-existing file
            throw Exception{} << "Cannot obtain details about `" << name <<
                                                                        '\'';
        int fd = ::open(name.c_str(), O_CREAT, protect);

        if (fd < 0)
            throw Exception{} << "Can't create 0" << oct << protect <<
                                 dec << ' ' << name;
        close(fd);

        return protect;
    }

    size_t mode = statbuf.st_mode & 07777;  // get the actual mode

    if (type == EQUAL && mode != protect)
        throw Exception{} << "Protection of `" << name << "' (0" << oct <<
                            mode << ") differs from required 0" <<
                            protect;
    return mode;
}