uos.rst 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. :mod:`uos` -- basic "operating system" services
  2. ===============================================
  3. .. module:: uos
  4. :synopsis: basic "operating system" services
  5. |see_cpython_module| :mod:`python:os`.
  6. The ``uos`` module contains functions for filesystem access and mounting,
  7. terminal redirection and duplication, and the ``uname`` and ``urandom``
  8. functions.
  9. General functions
  10. -----------------
  11. .. function:: uname()
  12. Return a tuple (possibly a named tuple) containing information about the
  13. underlying machine and/or its operating system. The tuple has five fields
  14. in the following order, each of them being a string:
  15. * ``sysname`` -- the name of the underlying system
  16. * ``nodename`` -- the network name (can be the same as ``sysname``)
  17. * ``release`` -- the version of the underlying system
  18. * ``version`` -- the MicroPython version and build date
  19. * ``machine`` -- an identifier for the underlying hardware (eg board, CPU)
  20. .. function:: urandom(n)
  21. Return a bytes object with *n* random bytes. Whenever possible, it is
  22. generated by the hardware random number generator.
  23. Filesystem access
  24. -----------------
  25. .. function:: chdir(path)
  26. Change current directory.
  27. .. function:: getcwd()
  28. Get the current directory.
  29. .. function:: ilistdir([dir])
  30. This function returns an iterator which then yields tuples corresponding to
  31. the entries in the directory that it is listing. With no argument it lists the
  32. current directory, otherwise it lists the directory given by *dir*.
  33. The tuples have the form *(name, type, inode[, size])*:
  34. - *name* is a string (or bytes if *dir* is a bytes object) and is the name of
  35. the entry;
  36. - *type* is an integer that specifies the type of the entry, with 0x4000 for
  37. directories and 0x8000 for regular files;
  38. - *inode* is an integer corresponding to the inode of the file, and may be 0
  39. for filesystems that don't have such a notion.
  40. - Some platforms may return a 4-tuple that includes the entry's *size*. For
  41. file entries, *size* is an integer representing the size of the file
  42. or -1 if unknown. Its meaning is currently undefined for directory
  43. entries.
  44. .. function:: listdir([dir])
  45. With no argument, list the current directory. Otherwise list the given directory.
  46. .. function:: mkdir(path)
  47. Create a new directory.
  48. .. function:: remove(path)
  49. Remove a file.
  50. .. function:: rmdir(path)
  51. Remove a directory.
  52. .. function:: rename(old_path, new_path)
  53. Rename a file.
  54. .. function:: stat(path)
  55. Get the status of a file or directory.
  56. .. function:: statvfs(path)
  57. Get the status of a fileystem.
  58. Returns a tuple with the filesystem information in the following order:
  59. * ``f_bsize`` -- file system block size
  60. * ``f_frsize`` -- fragment size
  61. * ``f_blocks`` -- size of fs in f_frsize units
  62. * ``f_bfree`` -- number of free blocks
  63. * ``f_bavail`` -- number of free blocks for unpriviliged users
  64. * ``f_files`` -- number of inodes
  65. * ``f_ffree`` -- number of free inodes
  66. * ``f_favail`` -- number of free inodes for unpriviliged users
  67. * ``f_flag`` -- mount flags
  68. * ``f_namemax`` -- maximum filename length
  69. Parameters related to inodes: ``f_files``, ``f_ffree``, ``f_avail``
  70. and the ``f_flags`` parameter may return ``0`` as they can be unavailable
  71. in a port-specific implementation.
  72. .. function:: sync()
  73. Sync all filesystems.
  74. Terminal redirection and duplication
  75. ------------------------------------
  76. .. function:: dupterm(stream_object, index=0)
  77. Duplicate or switch the MicroPython terminal (the REPL) on the given `stream`-like
  78. object. The *stream_object* argument must implement the ``readinto()`` and
  79. ``write()`` methods. The stream should be in non-blocking mode and
  80. ``readinto()`` should return ``None`` if there is no data available for reading.
  81. After calling this function all terminal output is repeated on this stream,
  82. and any input that is available on the stream is passed on to the terminal input.
  83. The *index* parameter should be a non-negative integer and specifies which
  84. duplication slot is set. A given port may implement more than one slot (slot 0
  85. will always be available) and in that case terminal input and output is
  86. duplicated on all the slots that are set.
  87. If ``None`` is passed as the *stream_object* then duplication is cancelled on
  88. the slot given by *index*.
  89. The function returns the previous stream-like object in the given slot.
  90. Filesystem mounting
  91. -------------------
  92. Some ports provide a Virtual Filesystem (VFS) and the ability to mount multiple
  93. "real" filesystems within this VFS. Filesystem objects can be mounted at either
  94. the root of the VFS, or at a subdirectory that lives in the root. This allows
  95. dynamic and flexible configuration of the filesystem that is seen by Python
  96. programs. Ports that have this functionality provide the :func:`mount` and
  97. :func:`umount` functions, and possibly various filesystem implementations
  98. represented by VFS classes.
  99. .. function:: mount(fsobj, mount_point, \*, readonly)
  100. Mount the filesystem object *fsobj* at the location in the VFS given by the
  101. *mount_point* string. *fsobj* can be a a VFS object that has a ``mount()``
  102. method, or a block device. If it's a block device then the filesystem type
  103. is automatically detected (an exception is raised if no filesystem was
  104. recognised). *mount_point* may be ``'/'`` to mount *fsobj* at the root,
  105. or ``'/<name>'`` to mount it at a subdirectory under the root.
  106. If *readonly* is ``True`` then the filesystem is mounted read-only.
  107. During the mount process the method ``mount()`` is called on the filesystem
  108. object.
  109. Will raise ``OSError(EPERM)`` if *mount_point* is already mounted.
  110. .. function:: umount(mount_point)
  111. Unmount a filesystem. *mount_point* can be a string naming the mount location,
  112. or a previously-mounted filesystem object. During the unmount process the
  113. method ``umount()`` is called on the filesystem object.
  114. Will raise ``OSError(EINVAL)`` if *mount_point* is not found.
  115. .. class:: VfsFat(block_dev)
  116. Create a filesystem object that uses the FAT filesystem format. Storage of
  117. the FAT filesystem is provided by *block_dev*.
  118. Objects created by this constructor can be mounted using :func:`mount`.
  119. .. staticmethod:: mkfs(block_dev)
  120. Build a FAT filesystem on *block_dev*.
  121. Block devices
  122. -------------
  123. A block device is an object which implements the block protocol, which is a set
  124. of methods described below by the :class:`AbstractBlockDev` class. A concrete
  125. implementation of this class will usually allow access to the memory-like
  126. functionality a piece of hardware (like flash memory). A block device can be
  127. used by a particular filesystem driver to store the data for its filesystem.
  128. .. class:: AbstractBlockDev(...)
  129. Construct a block device object. The parameters to the constructor are
  130. dependent on the specific block device.
  131. .. method:: readblocks(block_num, buf)
  132. Starting at the block given by the index *block_num*, read blocks from
  133. the device into *buf* (an array of bytes).
  134. The number of blocks to read is given by the length of *buf*,
  135. which will be a multiple of the block size.
  136. .. method:: writeblocks(block_num, buf)
  137. Starting at the block given by the index *block_num*, write blocks from
  138. *buf* (an array of bytes) to the device.
  139. The number of blocks to write is given by the length of *buf*,
  140. which will be a multiple of the block size.
  141. .. method:: ioctl(op, arg)
  142. Control the block device and query its parameters. The operation to
  143. perform is given by *op* which is one of the following integers:
  144. - 1 -- initialise the device (*arg* is unused)
  145. - 2 -- shutdown the device (*arg* is unused)
  146. - 3 -- sync the device (*arg* is unused)
  147. - 4 -- get a count of the number of blocks, should return an integer
  148. (*arg* is unused)
  149. - 5 -- get the number of bytes in a block, should return an integer,
  150. or ``None`` in which case the default value of 512 is used
  151. (*arg* is unused)
  152. By way of example, the following class will implement a block device that stores
  153. its data in RAM using a ``bytearray``::
  154. class RAMBlockDev:
  155. def __init__(self, block_size, num_blocks):
  156. self.block_size = block_size
  157. self.data = bytearray(block_size * num_blocks)
  158. def readblocks(self, block_num, buf):
  159. for i in range(len(buf)):
  160. buf[i] = self.data[block_num * self.block_size + i]
  161. def writeblocks(self, block_num, buf):
  162. for i in range(len(buf)):
  163. self.data[block_num * self.block_size + i] = buf[i]
  164. def ioctl(self, op, arg):
  165. if op == 4: # get number of blocks
  166. return len(self.data) // self.block_size
  167. if op == 5: # get block size
  168. return self.block_size
  169. It can be used as follows::
  170. import uos
  171. bdev = RAMBlockDev(512, 50)
  172. uos.VfsFat.mkfs(bdev)
  173. vfs = uos.VfsFat(bdev)
  174. uos.mount(vfs, '/ramdisk')