Exemple #1
0
    def open(self, path, flags, mode=0777):
        """
        Similar to Python's os.open()

        As of today, the only way to consume the raw glfd returned is by
        passing it to File class.

        :param path: Path of file to be opened
        :param flags: Integer which flags must include one of the following
                      access modes: os.O_RDONLY, os.O_WRONLY, or os.O_RDWR.
        :param mode: specifies the permissions to use in case a new
                     file is created.
        :returns: the raw glfd
        """
        if not isinstance(flags, int):
            raise TypeError("flags must evaluate to an integer")

        if (os.O_CREAT & flags) == os.O_CREAT:
            fd = api.glfs_creat(self.fs, path, flags, mode)
        else:
            fd = api.glfs_open(self.fs, path, flags)
        if not fd:
            err = ctypes.get_errno()
            raise OSError(err, os.strerror(err))

        return fd
Exemple #2
0
    def fopen(self, path, mode='r'):
        """
        Similar to Python's built-in File object returned by Python's open()

        Unlike Python's open(), fopen() provided here is only for convenience
        and it does NOT invoke glibc's fopen and does NOT do any kind of
        I/O bufferring as of today.

        :param path: Path of file to be opened
        :param mode: Mode to open the file with. This is a string.
        :returns: an instance of File class
        """
        if not isinstance(mode, basestring):
            raise TypeError("Mode must be a string")
        try:
            flags = python_mode_to_os_flags[mode]
        except KeyError:
            raise ValueError("Invalid mode")
        else:
            if (os.O_CREAT & flags) == os.O_CREAT:
                fd = api.glfs_creat(self.fs, path, flags, 0666)
            else:
                fd = api.glfs_open(self.fs, path, flags)
            if not fd:
                err = ctypes.get_errno()
                raise OSError(err, os.strerror(err))
            return File(fd, path=path, mode=mode)
Exemple #3
0
    def open(self, path, flags, mode=0777):
        """
        Similar to Python's os.open()

        As of today, the only way to consume the raw glfd returned is by
        passing it to File class.

        :param path: Path of file to be opened
        :param flags: Integer which flags must include one of the following
                      access modes: os.O_RDONLY, os.O_WRONLY, or os.O_RDWR.
        :param mode: specifies the permissions to use in case a new
                     file is created.
        :returns: the raw glfd
        """
        if not isinstance(flags, int):
            raise TypeError("flags must evaluate to an integer")

        if (os.O_CREAT & flags) == os.O_CREAT:
            fd = api.glfs_creat(self.fs, path, flags, mode)
        else:
            fd = api.glfs_open(self.fs, path, flags)
        if not fd:
            err = ctypes.get_errno()
            raise OSError(err, os.strerror(err))

        return fd
    def fopen(self, path, mode='r'):
        """
        Similar to Python's built-in File object returned by Python's open()

        Unlike Python's open(), fopen() provided here is only for convenience
        and it does NOT invoke glibc's fopen and does NOT do any kind of
        I/O bufferring as of today.

        The most commonly-used values of mode are 'r' for reading, 'w' for
        writing (truncating the file if it already exists), and 'a' for
        appending. If mode is omitted, it defaults to 'r'.

        Modes 'r+', 'w+' and 'a+' open the file for updating (reading and
        writing); note that 'w+' truncates the file.

        Append 'b' to the mode to open the file in binary mode but this has
        no effect as of today.

        :param path: Path of file to be opened
        :param mode: Mode to open the file with. This is a string.
        :returns: an instance of File class
        :raises: OSError on failure to create/open file.
                 TypeError and ValueError if mode is invalid.
        """
        if not isinstance(mode, basestring):
            raise TypeError("Mode must be a string")
        try:
            flags = python_mode_to_os_flags[mode]
        except KeyError:
            raise ValueError("Invalid mode")
        else:
            if (os.O_CREAT & flags) == os.O_CREAT:
                fd = api.glfs_creat(self.fs, path, flags, 0666)
            else:
                fd = api.glfs_open(self.fs, path, flags)
            if not fd:
                err = ctypes.get_errno()
                raise OSError(err, os.strerror(err))
            return File(fd, path=path, mode=mode)