Example #1
0
  def _process_set_flags(self, account, mailbox, uid, flags):
    xattr.setxattr('%s/%s/%s/%d' % (self. root, account, mailbox, uid),
        self.xattrname, " ".join(flags))
    ret = {"response":"setflags", "account": account,
           "mailbox":mailbox, "uid": uid, "status": "OK",
	   "flags": flags} 
    return ret
Example #2
0
  def process_COLOR(self, inpath, colorname):
    try:
      attrs = xattr.xattr(inpath)
      if u'com.apple.FinderInfo' in attrs:
        finder_attrs = attrs[u'com.apple.FinderInfo']
        flags = struct.unpack(32*'B', finder_attrs)
      else:
        flags = 32 * (0,)
      colorid = colorids[colorname]
      if colorid == None:
        colorid = 0
      flag = flags[9] >> 1 & 7
      flag &= 0xF1
      flag |= (colorid << 1)
      flags = list(flags)
      flags[9] = flag
      flags = tuple(flags)

      finder_attrs = struct.pack(32*'B', *flags)
      xattr.setxattr(inpath, u'com.apple.FinderInfo', finder_attrs)
      self.send_response(200, 'OK')
      self.end_headers()
    except Exception as e:
      print traceback.format_exc()
      self.send_response(500, 'Internal Server Error')
      self.end_headers()
    def test_finder_in_use(self):
        if not AbstractOSIntegration.is_mac():
            raise SkipTest('Not relevant on Linux or Windows')
        self.engine_1.start()
        self.wait_sync(wait_for_async=True)
        self.local_client_1.make_file('/', u'File.txt',
                                      content=u'Some Content 1'.encode('utf-8'))

        # Emulate the Finder in use flag
        OSX_FINDER_INFO_ENTRY_SIZE = 32
        key = (OSX_FINDER_INFO_ENTRY_SIZE)*[0]
        key[0] = 0x62
        key[1] = 0x72
        key[2] = 0x6F
        key[3] = 0x6B
        key[4] = 0x4D
        key[5] = 0x41
        key[6] = 0x43
        key[7] = 0x53
        import xattr
        xattr.setxattr(self.local_client_1._abspath(u'/File.txt'), xattr.XATTR_FINDERINFO_NAME, bytes(bytearray(key)))

        # The file should not be synced and there have no remote id
        self.wait_sync(wait_for_async=True, fail_if_timeout=False, timeout=10)
        info = self.local_client_1.get_remote_id(u'/File.txt')
        self.assertIsNone(info)

        # Remove the Finder flag
        self.local_client_1.remove_remote_id(u'/File.txt', xattr.XATTR_FINDERINFO_NAME)

        # The sync process should now handle the file and sync it
        self.wait_sync(wait_for_async=True, fail_if_timeout=False, timeout=10)
        info = self.local_client_1.get_remote_id(u'/File.txt')
        self.assertIsNotNone(info)
Example #4
0
    def set_attr(self, attrname, value):
        """ Set the value for a raw attribute.
            Value should be a string or bytes.
            Returns the value on success.
            Possibly raises AttrError.
        """
        if isinstance(value, bytes):
            encodedvalue = value
        elif isinstance(value, str):
            encodedvalue = value.encode(self.encoding)
        else:
            valtype = type(value)
            raise TypeError(
                'Expecting str or bytes. Got: {} ({})'.format(
                    getattr(valtype, '__name__', valtype),
                    value))
        try:
            xattr.setxattr(
                self.filepath,
                attrname,
                encodedvalue,
                symlink=self.follow_symlinks)
        except EnvironmentError as ex:
            raise self.AttrError(
                'Unable to set \'{}\' for: {}\n{}'.format(
                    attrname,
                    self.filepath,
                    ex))

        return value
Example #5
0
    def execute (self):
        op = self._build_operation ('download file', {'channel': self.channel, 'file': self.filename})
        f = self._get_url_handler (op)

        # Customize the HTTP handler
        def download_f_custom_read (self_f, *args):
            tmp = self_f.read_orig (*args)
            download_f_custom_read.received += len(tmp)

            if self.callback_step:
                self.callback_step (download_f_custom_read.received)

            return tmp

        f.read_orig = f.read
        f.read = types.MethodType (download_f_custom_read, f)
        download_f_custom_read.received = 0

        # Decrypt
        out_dir = os.path.join (self.download_dir, self.channel)
        if not os.path.exists (out_dir):
            os.makedirs (out_dir, 0700)

        out_fullpath = os.path.join (out_dir, self.filename)
        self.keys.decrypt_file_to_path (f, out_fullpath)

        # Set file attributes
        xattr.setxattr (out_fullpath, 'md5_time', str(time.time()))
        xattr.setxattr (out_fullpath, 'md5', utils.md5_file(out_fullpath))
Example #6
0
        def createChildren(parent, subStructure):
            for childName, childStructure in subStructure.iteritems():

                if childName.startswith("@"):
                    continue

                childPath = os.path.join(parent, childName)
                if "@contents" in childStructure:
                    # This is a file
                    with open(childPath, "w") as child:
                        child.write(childStructure["@contents"])
                else:
                    # This is a directory
                    os.mkdir(childPath)
                    createChildren(childPath, childStructure)

                if "@xattrs" in childStructure:
                    xattrs = childStructure["@xattrs"]
                    for attr, value in xattrs.iteritems():
                        try:
                            xattr.setxattr(childPath, attr, value)
                        except IOError:
                            pass

                # Set access and modified times
                if "@timestamp" in childStructure:
                    timestamp = childStructure["@timestamp"]
                    os.utime(childPath, (timestamp, timestamp))
Example #7
0
def write_metadata(path, metadata):
    metastr = pickle.dumps(metadata, PICKLE_PROTOCOL)
    key = 0
    while metastr:
        xattr.setxattr(path, '%s%s' % (METADATA_KEY, key or ''), metastr[:254])
        metastr = metastr[254:]
        key += 1
    def set_xattr(self, filepath, key, value):
        logger.info("set_xattr - %s, %s=%s" % (filepath, key, value))

        with self._get_lock():
            ascii_path = filepath.encode('ascii', 'ignore')
            localfs_path = self._make_localfs_path(ascii_path)
            xattr.setxattr(localfs_path, key, value)
Example #9
0
def write_metadata(fd, metadata, xattr_size=65536, md_key=None):
    """
    Helper function to write pickled metadata for an object file.

    :param fd: file descriptor or filename to write the metadata
    :param md_key: metadata key to be write to object file
    :param metadata: metadata to write
    """
    meta_key = SWIFT_METADATA_KEY

    metastr = pickle.dumps(metadata, PICKLE_PROTOCOL)
    key = 0
    while metastr:
        try:
            xattr.setxattr(fd, '%s%s' % (meta_key, key or ''),
                           metastr[:xattr_size])
            metastr = metastr[xattr_size:]
            key += 1
        except IOError as e:
            for err in 'ENOTSUP', 'EOPNOTSUPP':
                if hasattr(errno, err) and e.errno == getattr(errno, err):
                    msg = "Filesystem at %s does not support xattr" % \
                          _get_filename(fd)
                    logging.exception(msg)
                    raise DiskFileXattrNotSupported(e)
            if e.errno in (errno.ENOSPC, errno.EDQUOT):
                msg = "No space left on device for %s" % _get_filename(fd)
                logging.exception(msg)
                raise DiskFileNoSpace()
            raise
Example #10
0
 def test_xattr_bad_content_container(self):
     self.init_content()
     xattr.setxattr(
         self.chunk_path, 'user.grid.content.container',
         self.bad_container_id)
     self.assertRaises(exc.OrphanChunk, self.auditor.chunk_audit,
                       self.chunk_path)
Example #11
0
def download(file_path):
  '''Download the file by name of "file_path"'''
  if (user.is_user_signed_in()):
    client = c.get_client()
    filename = os.path.basename(file_path)
    file_path = sanitize_path(file_path)
    if (os.path.isfile(filename) or os.path.isdir(filename)):
      logger.die('File or dir with name "' + filename + '" in current directory')
    else:
      existance = check_existance(file_path, client)
      if (existance[0]):
        try:
          client.download_file("mf:" + file_path , '.')
          file_info = existance[1]
          xattr.setxattr(filename, 'hash', binascii.a2b_qp(file_info['hash']))
          logger.log('File "' + filename + '" downloaded successfully.')
        except NotAFolderError:
          logger.die('Path "' + remote_path + '" not found on MediaFire')
        except ResourceNotFoundError:
          logger.die('Path "' + remote_path + '" not found on MediaFire.')
        except requests.exceptions.RequestException:
          logger.die('Network error, please check network status and try again')

      else:
        logger.log('File path and name "' + file_path + '" does not exist.')
  else:
    user.get_auth()
Example #12
0
def write_metadata(path_or_fd, metadata):
    """
    Helper function to write pickled metadata for a File/Directory.

    :param path_or_fd: File/Directory path or fd to write the metadata
    :param metadata: dictionary of metadata write
    """
    assert isinstance(metadata, dict)
    metastr = pickle.dumps(metadata, PICKLE_PROTOCOL)
    key = 0
    while metastr:
        try:
            xattr.setxattr(path_or_fd,
                           '%s%s' % (METADATA_KEY, key or ''),
                           metastr[:MAX_XATTR_SIZE])
        except IOError as err:
            if err.errno in (errno.ENOSPC, errno.EDQUOT):
                if isinstance(path_or_fd, int):
                    filename = get_filename_from_fd(path_or_fd)
                    do_log_rl("write_metadata(%d, metadata) failed: %s : %s",
                              path_or_fd, err, filename)
                else:
                    do_log_rl("write_metadata(%s, metadata) failed: %s",
                              path_or_fd, err)
                raise DiskFileNoSpace()
            else:
                raise GlusterFileSystemIOError(
                    err.errno,
                    'xattr.setxattr("%s", %s, metastr)' % (path_or_fd, key))
        metastr = metastr[MAX_XATTR_SIZE:]
        key += 1
Example #13
0
 def is_partition_supported(self, folder):
     if folder is None:
         return False
     result = False
     to_delete = not os.path.exists(folder)
     try:
         if to_delete:
             os.mkdir(folder)
         if not os.access(folder, os.W_OK):
             import stat
             os.chmod(folder, stat.S_IXUSR | stat.S_IRGRP | stat.S_IXGRP |
                             stat.S_IRUSR | stat.S_IWGRP | stat.S_IWUSR)
         import xattr
         attr = "drive-test"
         xattr.setxattr(folder, attr, attr)
         if xattr.getxattr(folder, attr) == attr:
             result = True
         xattr.removexattr(folder, attr)
     finally:
         try:
             if to_delete:
                 os.rmdir(folder)
         except:
             pass
     return result
Example #14
0
def set_xattr(path, key, value):
    """Set the value of a specified xattr.

    If xattrs aren't supported by the file-system, we skip setting the value.
    """
    namespaced_key = _make_namespaced_xattr_key(key)
    xattr.setxattr(path, namespaced_key, str(value))
Example #15
0
    def upgradeCalendarHome(homePath, directory):

        errorOccurred = False

        log.debug("Upgrading calendar home: %s" % (homePath,))

        try:
            for cal in os.listdir(homePath):
                calPath = os.path.join(homePath, cal)
                if not os.path.isdir(calPath):
                    # Skip non-directories; these might have been uploaded by a
                    # random DAV client, they can't be calendar collections.
                    continue
                if cal == 'notifications':
                    # Delete the old, now obsolete, notifications directory.
                    rmdir(calPath)
                    continue
                log.debug("Upgrading calendar: %s" % (calPath,))
                if not upgradeCalendarCollection(calPath, directory):
                    errorOccurred = True

                # Change the calendar-free-busy-set xattrs of the inbox to the
                # __uids__/<guid> form
                if cal == "inbox":
                    for attr, value in xattr.xattr(calPath).iteritems():
                        if attr == "WebDAV:{urn:ietf:params:xml:ns:caldav}calendar-free-busy-set":
                            value = updateFreeBusySet(value, directory)
                            if value is not None:
                                # Need to write the xattr back to disk
                                xattr.setxattr(calPath, attr, value)

        except Exception, e:
            log.error("Failed to upgrade calendar home %s: %s" % (homePath, e))
            raise
Example #16
0
 def _checkDeprecated(self, item, symlink=False):
     """check deprecated list, set, get operations against an item"""
     self.assertEqual(self._ignore(xattr.listxattr(item, symlink)),
                      [])
     self.assertRaises(EnvironmentError, xattr.setxattr, item,
                       self.USER_ATTR, self.USER_VAL,
                       XATTR_REPLACE)
     try:
         xattr.setxattr(item, self.USER_ATTR, self.USER_VAL, 0, symlink)
     except IOError:
         err = sys.exc_info()[1]
         if err.errno == errno.EPERM and symlink:
             # symlinks may fail, in which case we abort the rest
             # of the test for this case
             return
         raise
     self.assertRaises(EnvironmentError, xattr.setxattr, item,
                       self.USER_ATTR, self.USER_VAL, XATTR_CREATE)
     self.assertEqual(self._ignore(xattr.listxattr(item, symlink)),
                      [self.USER_ATTR])
     self.assertEqual(xattr.getxattr(item, self.USER_ATTR, symlink),
                      self.USER_VAL)
     self.assertEqual(self._ignore_tuples(xattr.get_all(item,
                                                        nofollow=symlink)),
                      [(self.USER_ATTR, self.USER_VAL)])
     xattr.removexattr(item, self.USER_ATTR)
     self.assertEqual(self._ignore(xattr.listxattr(item, symlink)),
                      [])
     self.assertEqual(self._ignore_tuples(xattr.get_all(item,
                                                        nofollow=symlink)),
                      [])
     self.assertRaises(EnvironmentError, xattr.removexattr,
                       item, self.USER_ATTR)
Example #17
0
def _test_xattr_on_file_with_contents(filename, file_contents, xattrs=[], xar_create_flags=[], xar_extract_flags=[]):
	try:
		# Write file out
		with open(filename, "w") as f:
			f.write(file_contents)
			for (key, value) in xattrs:
				xattr.setxattr(f, key, value)
		
		# Store it into a xarchive
		archive_name = "{f}.xar".format(f=filename)
		with util.archive_created(archive_name, filename, *xar_create_flags) as path:
			# Extract xarchive
			with util.directory_created("extracted") as directory:
				# Validate resulting xattrs
				subprocess.check_call(["xar", "-x", "-C", directory, "-f", path] + xar_extract_flags)
				for (key, value) in xattrs:
					try:
						assert xattr.getxattr(os.path.join(directory, filename), key) == value, "extended attribute \"{n}\" has incorrect contents after extraction".format(n=key)
					except KeyError:
						raise MissingExtendedAttributeError("extended attribute \"{n}\" missing after extraction".format(n=key))
				
				# Validate file contents
				with open(os.path.join(directory, filename), "r") as f:
					if f.read() != file_contents:
						raise MissingExtendedAttributeError("archived file \"{f}\" has has incorrect contents after extraction".format(f=filename))
	finally:
		os.unlink(filename)
Example #18
0
 def copy_xattr(self, src, dest):
     attrs = xattr.listxattr(src)
     for k in attrs:
         try:
             val = xattr.getxattr(src, k)
             xattr.setxattr(dest, k, val)
         except IOError as e:
             log.warn(e, extra=self.d)
Example #19
0
 def setUp(self):
     if not os.path.ismount(mount_point):
         raise "%s is not a mount point" % mount_point
     os.system('rm -rf %s/*' % mount_point)
     self.folder_path = os.path.join(mount_point, folder)
     os.mkdir(self.folder_path)
     xattr.setxattr(self.folder_path, 'user.quota', str(quota))
     os.system('sync')
Example #20
0
 def _message_at_path(self, path, load_content=True):
     try:
         content = None
         if load_content:
             f = open(path, "rb")
             content = f.read()
             f.close()
         
         mtime = os.path.getmtime(path)
         
         directory, filename = os.path.split(path)
         directory, subdir = os.path.split(directory)
         
         msgid = None
         info = None
         parts = filename.split(":")
         if len(parts) > 0:
             msgid = parts[0]
         if len(parts) > 1:
             info = parts[1]
         
         msg = Message(content=content, msgid=msgid, info=info, subdir=subdir, mtime=mtime)
         if not msg.msg_md5 and self._use_xattrs and load_content:
             try:
                 xattrs = xattr.listxattr(path)
                 # logging.debug(xattrs)
                 if XATTR_MD5SUM in xattrs:
                     msg.msg_md5 = xattr.getxattr(path, XATTR_MD5SUM)
                     # logging.debug("Read md5: %s", msg.msg_md5)
                 else:
                     c = msg.content_hash
                     if c:
                         # logging.debug("Setting shasum xattr: %r", c)
                         xattr.setxattr(path, XATTR_MD5SUM, c)
                     else:
                         logging.warning("Could not generate content hash of %s", msgid)
                 
                 if XATTR_DATE in xattrs:
                     msg._date = xattr.getxattr(path, XATTR_DATE).decode("utf8")
                     msg._date = datetime.datetime.fromtimestamp(float(msg._date))
                     # logging.debug("Read date: %s", msg._date)
                 else:
                     d = str(msg.date.timestamp()).encode("utf8")
                     if d:
                         # logging.debug("Setting date xattr: %r", d)
                         xattr.setxattr(path, XATTR_DATE, d)
                     else:
                         logging.warning("Could not determine message date of %s", msgid)
             
             except IOError:
                 # read-only FS, unsupported on FS, etc.
                 self._use_xattrs = False
                 log.debug("host filesystem for %s does not support xattrs; disabling" % self.name)
         
         return msg
         
     except OSError:
         raise KeyError
Example #21
0
 def set_xattr(self, name, value):
     """
     Sets an extended attribute with the specified name and value on this
     file.
     
     The same compatibility warnings present on list_xattrs apply here.
     """
     import xattr
     xattr.setxattr(self.path, name, value)
    def test_download_packages_with_resume_02(self):
        # If download that should be resumed fails,
        # the original file should not be modified or deleted
        h = librepo.Handle()

        url = "%s%s" % (self.MOCKURL, config.REPO_YUM_01_PATH)
        h.urls = [url]
        h.repotype = librepo.LR_YUMREPO

        fn = os.path.join(self.tmpdir, "package.rpm")

        # Download first 10 bytes of the package
        pkgs = []
        pkgs.append(librepo.PackageTarget(config.PACKAGE_01_01,
                                          handle=h,
                                          dest=fn,
                                          resume=False,
                                          byterangeend=9))

        librepo.download_packages(pkgs)
        pkg = pkgs[0]
        self.assertTrue(pkg.err is None)
        self.assertTrue(os.path.isfile(pkg.local_path))
        self.assertEqual(os.path.getsize(pkg.local_path), 10)
        fchksum = hashlib.md5(open(pkg.local_path, "rb").read()).hexdigest()

        # Mark the file as it was downloaded by Librepo
        # Otherwise librepo refuse to resume
        try:
            xattr.setxattr(pkg.local_path,
                           "user.Librepo.DownloadInProgress".encode("utf-8"),
                           "".encode("utf-8"))
        except IOError as err:
            if err.errno == 95:
                self.skipTest('extended attributes are not supported')
            raise

        # Now try to resume from bad URL
        pkgs = []
        pkgs.append(librepo.PackageTarget("bad_path.rpm",
                                          handle=h,
                                          dest=fn,
                                          resume=True,
                                          checksum_type=librepo.SHA256,
                                          checksum=config.PACKAGE_01_01_SHA256))

        # Download should fail (path is bad, the file doesn't exist)
        self.assertRaises(librepo.LibrepoException, librepo.download_packages,
                pkgs, failfast=True)

        # The package should exists (should not be removed or changed)
        pkg = pkgs[0]
        self.assertTrue(pkg.err)
        self.assertTrue(os.path.isfile(pkg.local_path))
        self.assertEqual(os.path.getsize(pkg.local_path), 10)
        fchksum_new = hashlib.md5(open(pkg.local_path, "rb").read()).hexdigest()
        self.assertEqual(fchksum, fchksum_new)
Example #23
0
	def get_sha1(self):
		name = self.get_real_name()
		if "user.sha1" in xattr.list(name):
			val = xattr.get(name, "user.sha1")[:-1]
		else:
			with open(name) as f:
				val = sha1(f.read()).hexdigest()
			xattr.setxattr(name, "user.sha1", "%s\x00" % val)
		return val
Example #24
0
def setMacCreatorAndType(path, fileCreator, fileType):
	if xattr is not None:
		from fontTools.misc.textTools import pad
		if not all(len(s) == 4 for s in (fileCreator, fileType)):
			raise TypeError('arg must be string of 4 chars')
		finderInfo = pad(bytesjoin([fileType, fileCreator]), 32)
		xattr.setxattr(path, 'com.apple.FinderInfo', finderInfo)
	if MacOS is not None:
		MacOS.SetCreatorAndType(path, fileCreator, fileType)
Example #25
0
def writeCachedChecksum(file_path, fhash=None):
    """Write the sha256 checksum of a file to an xattr so we do not need to
       calculate it again. Optionally pass the recently calculated hash value.
    """
    if not fhash:
        fhash = munkihash.getsha256hash(file_path)
    if len(fhash) == 64:
        xattr.setxattr(file_path, XATTR_SHA, fhash)
        return fhash
    return None
Example #26
0
def comment_set(path, comment=None):
    assert_exists(path)
    if comment is None:
        xattr.removexattr(path, kMDItemFinderComment)
        return
    old = comment_get(path)
    if comment != old:
        if comment is not None and hasattr(comment, "encode"):
            comment = comment.encode("utf-8")  # str/bytes required
        xattr.setxattr(path, kMDItemFinderComment, comment)
Example #27
0
def paste_file(id, contents, language=None, original_id=None):
	target_file = file_from_id(id)
	contents = contents.replace('\r\n', '\n')
	contents = contents.rstrip('\n') + '\n'
	with open(target_file, 'wb') as f:
		f.write(contents.encode('utf-8'))

	xattr.setxattr(target_file, 'user.upaste.language', language.encode('utf-8'))
	if original_id:
		xattr.setxattr(target_file, 'user.upaste.original_id', original_id.encode('utf-8'))
Example #28
0
def localCopyOfImageInFolder(imageURLString, imagePageString, targetFolder):
	username = None
	if imageURLString != None:
		fileName = NSString.stringWithString_(imageURLString).lastPathComponent()
		filePath = targetFolder + "/" + fileName

		if NSFileManager.defaultManager().fileExistsAtPath_(filePath) == False:
			imageURL = NSURL.URLWithString_(NSString.stringWithString_(NSString.stringWithString_(imageURLString).stringByAddingPercentEscapesUsingEncoding_(NSUTF8StringEncoding)))
			request = NSURLRequest.requestWithURL_(imageURL)
			data, response, error = NSURLConnection.sendSynchronousRequest_returningResponse_error_(request, None, None)
			if data != None:
				if data.writeToFile_atomically_(filePath, True):
					print "	Image file cached successfully at " + filePath
				else:
					print "	* Failed to write image file at " + filePath
			else:
				print "	* Could not load " + imageURLString + " (" + error.description() + ")"

			# get image creator's name
			infoURL = NSURL.URLWithString_(NSString.stringWithString_(imagePageString).stringByAddingPercentEscapesUsingEncoding_(NSUTF8StringEncoding))
			(infoPageString, error) = NSString.stringWithContentsOfURL_encoding_error_(infoURL, NSUTF8StringEncoding, None)
			if infoPageString != None:
				(infoXHTML, error) = NSXMLDocument.alloc().initWithXMLString_options_error_(infoPageString, NSXMLDocumentTidyXML, None)
				if infoXHTML != None:
					nodes, error = infoXHTML.nodesForXPath_error_("""//tr[contains(.,"current")]/td/a[contains(@class,'mw-userlink')]""", None)
					if len(nodes) > 0:
						wikipediaPath = nodes[0]
						username = wikipediaPath.stringValue().split(":")[-1]
						xattr.setxattr(filePath, "net.earthlingsoft.imageauthor", username)
						print "	Image author »" + username + "« stored."
					else:
						nodes, error = infoXHTML.nodesForXPath_error_("""//tr[contains(.,"aktuell")]/td/a[contains(@class,'mw-userlink')]""", None)
						if len(nodes) > 0:
							wikipediaPath = nodes[0]
							username = wikipediaPath.stringValue().split(":")[-1]
							xattr.setxattr(filePath, "net.earthlingsoft.imageauthor", username)
							print "	Image author »" + username + "« stored."
						else:
							print "		* Could not find author information in info page at " + imagePageString
				else:
					print "		* Could not parse XML of info page at " + imagePageString
			else:
				print "		* Could not download image information at " + imagePageString

		else:
			xattrs = xattr.listxattr(filePath)
			if "net.earthlingsoft.imageauthor" in xattrs:
				username = xattr.getxattr(filePath, "net.earthlingsoft.imageauthor")
			print "	Cached image file available"
		
		newURL = myURL + targetFolder + "/" + fileName
	else:
		newURL = None
		
	return [newURL, username]
Example #29
0
 def _create_ondisk_file(self, df, data, timestamp, metadata=None,
                         ext='.data'):
     mkdirs(df._datadir)
     if timestamp is None:
         timestamp = time()
         timestamp = normalize_timestamp(timestamp)
     data_file = os.path.join(df._datadir, timestamp + ext)
     with open(data_file, 'wb') as f:
         f.write(data)
         xattr.setxattr(f.fileno(), diskfile.METADATA_KEY,
                        pickle.dumps(metadata, diskfile.PICKLE_PROTOCOL))
Example #30
0
        def test_dir_attr(self):

        	''' set attrs, get attrs and remove attrs for a dir '''

                os.mkdir(TESTDIR, 0755)
		try:
			xattr.setxattr(TESTDIR, DIR_ATTR, DIR_ATTR_VAL)
		except IOError, e:
			print e
                	os.rmdir(TESTDIR)
			raise e
Example #31
0
 def on_delete(self, req, resp, cn, tag):
     path, buf, cert, signed, expires = self.authority.get_signed(cn)
     tags = set(getxattr(path, "user.xdg.tags").decode("utf-8").split(","))
     tags.remove(tag)
     if not tags:
         removexattr(path, "user.xdg.tags")
     else:
         setxattr(path, "user.xdg.tags", ",".join(tags))
     logger.info("Tag %s removed for %s by %s" %
                 (tag, cn, req.context.get("user")))
     push.publish("tag-update", cn)
Example #32
0
 def whiteout_opaque_directory(self, top, lower_relpath):
     upper_opaque_dir = os.path.join(top, lower_relpath)
     if self.spec == WhiteoutSpec.OCI:
         os.makedirs(upper_opaque_dir, exist_ok=True)
         f = os.open(os.path.join(upper_opaque_dir, ".wh..wh..opq"),
                     os.O_CREAT)
         os.close(f)
     elif self.spec == WhiteoutSpec.OVERLAY:
         os.makedirs(upper_opaque_dir, exist_ok=True)
         xattr.setxattr(upper_opaque_dir, self.opaque_dir_key,
                        self.opaque_dir_value)
Example #33
0
def set_xattr(path, key, value):
    """Set the value of a specified xattr.

    If xattrs aren't supported by the file-system, we skip setting the value.
    """
    namespaced_key = _make_namespaced_xattr_key(key)
    if not isinstance(value, six.binary_type):
        value = str(value)
        if six.PY3:
            value = value.encode('utf-8')
    xattr.setxattr(path, namespaced_key, value)
Example #34
0
def set_xattr(path, key, value):
    """Set the value of a specified xattr.

    If xattrs aren't supported by the file-system, we skip setting the value.
    """
    namespaced_key = _make_namespaced_xattr_key(key)
    if not isinstance(value, six.binary_type):
        value = str(value)
        if six.PY3:
            value = value.encode('utf-8')
    xattr.setxattr(path, namespaced_key, value)
Example #35
0
def test_many_ops_deprecated(subject):
    """test many ops (deprecated functions)"""
    item = subject[0]
    xattr.setxattr(item, USER_ATTR, USER_VAL)
    VL = [USER_ATTR]
    for i in range(MANYOPS_COUNT):
        lists_equal(xattr.listxattr(item), VL)
    for i in range(MANYOPS_COUNT):
        assert xattr.getxattr(item, USER_ATTR) == USER_VAL
    for i in range(MANYOPS_COUNT):
        tuples_equal(xattr.get_all(item),
                     [(USER_ATTR, USER_VAL)])
Example #36
0
        def test_file_attr(self):

        	''' set attrs, get attrs and remove attrs for a file '''

                f = open(TESTFILE, 'w')
                f.close()
		try:
                	xattr.setxattr(TESTFILE, FILE_ATTR, FILE_ATTR_VAL)
		except IOError, e:
			print e
                	safe_rm(TESTFILE)
			raise e
Example #37
0
def get_id(path):
    import xattr
    try:
        return xattr.getxattr(path, XATTR_KEY)
    except OSError:
        pass
    try:
        id = generate_id()
        xattr.setxattr(path, XATTR_KEY, id.encode('utf-8'))
        return id
    except:
        pass
Example #38
0
  def put(self, key, stream):
    with tempfile.NamedTemporaryFile(dir=self.tmpdir, delete=False) as f:
      shutil.copyfileobj(stream, f)

      # save the real name in xattr in case we rebuild cache
      xattr.setxattr(f.name, 'user.key', key.encode('utf-8'))

    # Note, a crash here will leave a tmp file around.
    # This is okay and will be deleted on volume server restart

    # TODO: check hash
    os.rename(f.name, self.k2p(key, True))
Example #39
0
 def testManyOpsDeprecated(self):
     """test many ops (deprecated functions)"""
     fh, fname = self._getfile()
     xattr.setxattr(fh, self.USER_ATTR, self.USER_VAL)
     VL = [self.USER_ATTR]
     for i in range(self.MANYOPS_COUNT):
         self.checkList(xattr.listxattr(fh), VL)
     for i in range(self.MANYOPS_COUNT):
         self.assertEqual(xattr.getxattr(fh, self.USER_ATTR), self.USER_VAL)
     for i in range(self.MANYOPS_COUNT):
         self.checkTuples(xattr.get_all(fh),
                          [(self.USER_ATTR, self.USER_VAL)])
Example #40
0
 def testBinaryPayloadDeprecated(self):
     """test binary values (deprecated functions)"""
     fh, fname = self._getfile()
     os.close(fh)
     BINVAL = "abc" + '\0' + "def"
     if PY3K:
         BINVAL = BINVAL.encode()
     xattr.setxattr(fname, self.USER_ATTR, BINVAL)
     self.checkList(xattr.listxattr(fname), [self.USER_ATTR])
     self.assertEqual(xattr.getxattr(fname, self.USER_ATTR), BINVAL)
     self.checkTuples(xattr.get_all(fname), [(self.USER_ATTR, BINVAL)])
     xattr.removexattr(fname, self.USER_ATTR)
Example #41
0
 def testSymlinkOpsDeprecated(self):
     """test symlink operations (deprecated functions)"""
     _, sname = self._getsymlink()
     self.assertRaises(EnvironmentError, xattr.listxattr, sname)
     self._checkDeprecated(sname, symlink=True)
     target, sname = self._getsymlink(dangling=False)
     xattr.setxattr(target, self.USER_ATTR, self.USER_VAL)
     self.checkList(xattr.listxattr(target), [self.USER_ATTR])
     self.checkList(xattr.listxattr(sname, True), [])
     self.assertRaises(EnvironmentError, xattr.removexattr, sname,
                       self.USER_ATTR, True)
     xattr.removexattr(sname, self.USER_ATTR, False)
Example #42
0
 def testMixedAccessDeprecated(self):
     """test mixed access to file (deprecated functions)"""
     fh, fname = self._getfile()
     fo = os.fdopen(fh)
     self.checkList(xattr.listxattr(fname), [])
     xattr.setxattr(fname, self.USER_ATTR, self.USER_VAL)
     self.checkList(xattr.listxattr(fh), [self.USER_ATTR])
     self.assertEqual(xattr.getxattr(fo, self.USER_ATTR), self.USER_VAL)
     self.checkTuples(xattr.get_all(fo), [(self.USER_ATTR, self.USER_VAL)])
     self.checkTuples(xattr.get_all(fname),
                      [(self.USER_ATTR, self.USER_VAL)])
     fo.close()
Example #43
0
def paste_file(id, contents, language=None, original_id=None):
    target_file = file_from_id(id)
    contents = contents.replace('\r\n', '\n')
    contents = contents.rstrip('\n') + '\n'
    with open(target_file, 'wb') as f:
        f.write(contents.encode('utf-8'))

    xattr.setxattr(target_file, 'user.upaste.language',
                   language.encode('utf-8'))
    if original_id:
        xattr.setxattr(target_file, 'user.upaste.original_id',
                       original_id.encode('utf-8'))
Example #44
0
def change_file(path, trusted):
    """Change the trust state of a file"""

    # Save the original permissions of the file
    orig_perms = os.stat(path).st_mode

    # See if the file is readable
    try:
        with open(path):
            pass

    except IOError:
        # Try to unlock file to get read access
        safe_chmod(path, 0o644, 'Could not unlock {} for reading'.format(path))
    if trusted:
        # Set file to trusted
        # AKA remove our xattr
        file_xattrs = xattr.get_all(path)
        untrusted_attribute = (b'user.qubes.untrusted', b'true')

        # Check if the xattr exists first
        if untrusted_attribute in file_xattrs:
            try:
                xattr.removexattr(path, 'user.qubes.untrusted')
            except:
                # Unable to remove our xattr, return original permissions
                error(
                    'Unable to remove untrusted attribute on {}'.format(path))
                safe_chmod(path, orig_perms,
                           'Unable to set original perms. on {}'.format(path))

        # Remove visual attributes
        set_visual_attributes(path, False)

    else:
        # Set file to untrusted
        # AKA add our xattr and lock
        try:
            xattr.setxattr(path, 'user.qubes.untrusted', 'true')
            safe_chmod(
                path, 0o0,
                'Unable to set untrusted permissions on: {}'.format(path))
        except:
            # Unable to remove our xattr, return original permissions
            safe_chmod(
                path, orig_perms,
                'Unable to return perms after setting as untrusted: {}'.format(
                    path))
            sys.exit(65)

        # Add visual attributes
        set_visual_attributes(path, True)
Example #45
0
 def write_cache(self):
     if not xattr:
         return
     try:
         self.clear_cache()
         xattr.setxattr(self.name, 'user.pyanidb.mtime',
                        bytes(str(int(self.mtime)), 'utf-8'))
         for n in ('ed2k', 'md5', 'sha1', 'crc32'):
             if hasattr(self, n):
                 xattr.setxattr(self.name, 'user.pyanidb.' + n,
                                bytes(getattr(self, n), 'utf-8'))
     except IOError as e:
         pass
Example #46
0
def write_metadata(fd, metadata):
    """
    Helper function to write pickled metadata for an object file.

    :param fd: file descriptor to write the metadata
    :param metadata: metadata to write
    """
    metastr = pickle.dumps(metadata, PICKLE_PROTOCOL)
    key = 0
    while metastr:
        setxattr(fd, '%s%s' % (METADATA_KEY, key or ''), metastr[:254])
        metastr = metastr[254:]
        key += 1
Example #47
0
def create_chat(path, source=None):
  """Creates a chat file with appropriate (extended) attributes.

  Args:
      path: Path object to the chat file.
      source: Path to source file to copy.
  """
  if source:
    shutil.copy(source, path)

  path.touch(**FILE_ARGS)
  xattr.setxattr(path, XATTR_QTINE_KEY, get_qtine_val(path))
  xattr.setxattr(path, XATTR_FINDER_KEY, XATTR_FINDER_VAL)
Example #48
0
    def set_eas(self, rp, write):
        """Set extended attributes from rp. Tests writing if write is true."""
        assert Globals.local_connection is rp.conn
        assert rp.lstat()
        if Globals.eas_active == 0:
            log.Log(
                "Extended attributes test skipped. rdiff-backup run "
                "with --no-eas option.", 4)
            self.eas = 0
            return
        try:
            import xattr
        except ImportError:
            log.Log(
                "Unable to import module xattr.\nExtended attributes not "
                "supported on filesystem at %s" % (rp.path, ), 4)
            self.eas = 0
            return

        try:
            ver = xattr.__version__
        except AttributeError:
            ver = 'unknown'
        if ver < '0.2.2' or ver == 'unknown':
            log.Log(
                "Warning: Your version of pyxattr (%s) has broken support "
                "for extended\nattributes on symlinks. If you choose not "
                "to upgrade to a more recent version,\nyou may see many "
                "warning messages from listattr().\n" % (ver, ), 3)

        try:
            xattr.listxattr(rp.path)
            if write:
                xattr.setxattr(rp.path, "user.test", "test val")
                assert xattr.getxattr(rp.path, "user.test") == "test val"
        except IOError:
            log.Log(
                "Extended attributes not supported by "
                "filesystem at %s" % (rp.path, ), 4)
            self.eas = 0
        except AssertionError:
            log.Log(
                "Extended attributes support is broken on filesystem at "
                "%s.\nPlease upgrade the filesystem driver, contact the "
                "developers,\nor use the --no-eas option to disable "
                "extended attributes\nsupport and suppress this message." %
                (rp.path, ), 1)
            self.eas = 0
        else:
            self.eas = 1
Example #49
0
def _run_check_and_copy_results(catcodename, filecode, task, onedata_path):

    start_date = _xsd_dateTime()

    try:
        _run_Popen(task)
        metadatalist = get_dataset_metadata(catcodename, filecode, start_date,
                                            _xsd_dateTime())
        for md in metadatalist:
            id = json.loads(md)['@id']
            shutil.move('.' + id, onedata_path + id)
            xattr.setxattr(onedata_path + id, 'onedata_json', md)
    except Exception as inst:
        raise inst
Example #50
0
def setxattr_files(files, randname, dir_path):
    char = string.uppercase + string.digits
    if not randname:
        for k in range(files):
            v = ''.join(random.choice(char) for i in range(10))
            n = "user." + v
            xattr.setxattr(dir_path + "/" + "file" + str(k), n, v)
    else:
        dirs = os.listdir(dir_path + "/")
        for fil in dirs:
            v = ''.join(random.choice(char) for i in range(10))
            n = "user." + v
            xattr.setxattr(dir_path + "/" + fil, n, v)
    return
Example #51
0
    def set_path_remote_id(path: Path,
                           remote_id: Union[bytes, str],
                           name: str = "ndrive") -> None:
        if not isinstance(remote_id, bytes):
            remote_id = unicodedata.normalize("NFC", remote_id).encode("utf-8")

        locker = unlock_path(path, False)
        if WINDOWS:
            path_alt = f"{path}:{name}"
            try:
                stat_ = path.stat()
                with open(path_alt, "wb") as f:
                    f.write(remote_id)

                    # Force write of file to disk
                    f.flush()
                    os.fsync(f.fileno())

                # Avoid time modified change
                os.utime(path, (stat_.st_atime, stat_.st_mtime))
            except FileNotFoundError:
                pass
            except OSError as e:
                # Should not happen
                if e.errno != errno.EACCES:
                    raise e
                unset_path_readonly(path)
                with open(path_alt, "wb") as f:
                    f.write(remote_id)

                    # Force write of file to disk
                    f.flush()
                    os.fsync(f.fileno())

                set_path_readonly(path)
            finally:
                lock_path(path, locker)
            return

        if LINUX:
            name = f"user.{name}"

        try:
            stat_ = path.stat()
            xattr.setxattr(str(path), name, remote_id)
            os.utime(path, (stat_.st_atime, stat_.st_mtime))
        except FileNotFoundError:
            pass
        finally:
            lock_path(path, locker)
Example #52
0
def do_setxattr(path, key, value):
    fd = None
    if not os.path.isdir(path):
        fd = do_open(path, 'rb')
    else:
        fd = path
    if fd or os.path.isdir(path):
        try:
            setxattr(fd, key, value)
        except Exception, err:
            logging.exception("setxattr failed on %s key %s err: %s", path,
                              key, str(err))
            raise
        finally:
Example #53
0
def xattr_set(xfile, xsetter):
    """
    Set extended file attributes
    Accepts a dict with attrib names that are not prefixed with the 'user.' namespace
    """
    for k, v in xsetter.iteritems():
        try:
            xattr.setxattr(xfile, 'user.'+str(k), str(v))
        except Exception as e:
            logthis("Failed to set extended file attributes for", suffix=xfile, loglevel=LL.WARNING)
            logthis("xattr:", suffix=e, loglevel=LL.WARNING)
            return False

    return True
Example #54
0
 def on_post(self, req, resp, cn):
     path, buf, cert = authority.get_signed(cn)
     key, value = req.get_param("key", required=True), req.get_param("value", required=True)
     try:
         tags = set(getxattr(path, "user.xdg.tags").decode("utf-8").split(","))
     except IOError:
         tags = set()
     if key == "other":
         tags.add(value)
     else:
         tags.add("%s=%s" % (key,value))
     setxattr(path, "user.xdg.tags", ",".join(tags).encode("utf-8"))
     logger.debug(u"Tag %s=%s set for %s" % (key, value, cn))
     push.publish("tag-update", cn)
Example #55
0
File: gurl.py Project: zippyy/munki
 def storeHeaders_(self, headers):
     '''Store dictionary data as an xattr for self.destination_path'''
     plistData, error = (NSPropertyListSerialization.
                         dataFromPropertyList_format_errorDescription_(
                             headers, NSPropertyListXMLFormat_v1_0, None))
     if error:
         string = ''
     else:
         string = str(plistData)
     try:
         xattr.setxattr(self.destination_path, self.GURL_XATTR, string)
     except IOError, err:
         self.log('Could not store metadata to %s: %s' %
                  (self.destination_path, err))
Example #56
0
    def set_remote_id(self,
                      ref: str,
                      remote_id: bytes,
                      name: str = "ndrive") -> None:
        path = self.abspath(ref)

        if not isinstance(remote_id, bytes):
            remote_id = unicodedata.normalize("NFC", remote_id).encode("utf-8")

        log.trace("Setting xattr %r with value %r on %r", name, remote_id,
                  path)
        locker = unlock_path(path, False)
        if WINDOWS:
            path_alt = path + ":" + name
            try:
                if not os.path.exists(path):
                    raise NotFound()

                stat_ = os.stat(path)
                with open(path_alt, "wb") as f:
                    f.write(remote_id)

                # Avoid time modified change
                os.utime(path, (stat_.st_atime, stat_.st_mtime))
            except FileNotFoundError:
                pass
            except OSError as e:
                # Should not happen
                if e.errno != errno.EACCES:
                    raise e
                unset_path_readonly(path)
                with open(path_alt, "wb") as f:
                    f.write(remote_id)
                set_path_readonly(path)
            finally:
                lock_path(path, locker)
            return

        if LINUX:
            name = "user." + name

        try:
            stat_ = os.stat(path)
            xattr.setxattr(path, name, remote_id)
            os.utime(path, (stat_.st_atime, stat_.st_mtime))
        except FileNotFoundError:
            pass
        finally:
            lock_path(path, locker)
    def test_download_packages_with_resume_02(self):
        # If download that should be resumed fails,
        # the original file should not be modified or deleted
        h = librepo.Handle()

        url = "%s%s" % (self.MOCKURL, config.REPO_YUM_01_PATH)
        h.urls = [url]
        h.repotype = librepo.LR_YUMREPO
        h.allowedmirrorfailures = 1

        fn = os.path.join(self.tmpdir, "package.rpm")

        # Make fake unfinished download
        CONTENT = "0123456789"
        open(fn, "w").write(CONTENT)

        # Mark the file as it was downloaded by Librepo
        # Otherwise librepo refuse to resume
        try:
            xattr.setxattr(fn,
                           "user.Librepo.DownloadInProgress".encode("utf-8"),
                           "".encode("utf-8"))
        except IOError as err:
            if err.errno == 95:
                self.skipTest('extended attributes are not supported')
            raise

        # Now try to resume from bad URL
        pkgs = []
        pkgs.append(
            librepo.PackageTarget("bad_path.rpm",
                                  handle=h,
                                  dest=fn,
                                  resume=True,
                                  checksum_type=librepo.SHA256,
                                  checksum=config.PACKAGE_01_01_SHA256))

        # Download should fail (path is bad, the file doesn't exist)
        self.assertRaises(librepo.LibrepoException,
                          librepo.download_packages,
                          pkgs,
                          failfast=True)

        # The package should exists (should not be removed or changed)
        pkg = pkgs[0]
        self.assertTrue(pkg.err)
        self.assertTrue(os.path.isfile(pkg.local_path))
        self.assertEqual(
            open(pkg.local_path, "rb").read(), CONTENT.encode('utf-8'))
Example #58
0
def main():
    if len(sys.argv) <= 1:
        eprint(f'usage: {os.path.basename(sys.argv[0])} file ...\n'
               f'       move file(s) to Trash')
        sys.exit(64)  # what rm does with no args

    exitcode = 0
    for arg in sys.argv[1:]:
        path = Path(arg)
        trash_path = Path.home() / ".Trash" / path.name
        trash_file = None

        while True:
            try:
                if path.is_dir() and not path.is_symlink():
                    trash_path.mkdir()
                else:
                    trash_file = trash_path.open("x")
            except FileExistsError:
                # almost same format as macOS uses, except this has zero-padding on the hour
                trash_path = trash_path.with_name(trash_path.name +
                                                  strftime(" %I.%M.%S %p"))
            else:
                break

        try:
            path.replace(trash_path)
        except FileNotFoundError:
            eprint(f"{path}: No such file or directory")
            if trash_file:
                trash_file.close()
                trash_path.unlink()
            else:
                trash_path.rmdir()
            exitcode = 1
            continue

        if trash_file:
            trash_file.close()

        # Resolve symlinks in the directory but not the file itself
        orig_path = path.parent.resolve() / path.name
        setxattr(trash_path,
                 ORIG_PATH_XATTR,
                 os.fsencode(orig_path),
                 symlink=True)
        # subprocess.check_call(["xattr", "-w", "-s", "trash101_orig_path", orig_path, trash_path])

    sys.exit(exitcode)
Example #59
0
    def set_path_remote_id(path: Path,
                           remote_id: Union[bytes, str],
                           name: str = "ndrive") -> None:
        if not isinstance(remote_id, bytes):
            remote_id = unicodedata.normalize("NFC", remote_id).encode("utf-8")

        locker = unlock_path(path, False)
        try:
            stat_ = path.stat()
            xattr.setxattr(str(path), name, remote_id)
            os.utime(path, (stat_.st_atime, stat_.st_mtime))
        except FileNotFoundError:
            pass
        finally:
            lock_path(path, locker)
Example #60
0
 def whiteout_one_dir(self, top, lower_relpath):
     whiteout_dir_parent, whiteout_dir = Whiteout.mirror_fs_structure(
         top, lower_relpath)
     if self.spec == WhiteoutSpec.OCI:
         os.makedirs(
             os.path.join(top, whiteout_dir_parent, f".wh.{whiteout_dir}"))
     elif self.spec == WhiteoutSpec.OVERLAY:
         d = os.path.join(top, whiteout_dir_parent, whiteout_dir)
         os.mknod(
             d,
             0o644 | stat.S_IFCHR,
             0,
         )
         # Whitout a direcotoy does not need such xattr pair, but it's a naughty monkey
         xattr.setxattr(d, self.opaque_dir_key, self.opaque_dir_value)
         xattr.setxattr(d, "trusted.nydus.opaque", "y".encode())