xref: /aosp_15_r20/external/cronet/base/apple/backup_util.mm (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1// Copyright 2021 The Chromium Authors
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "base/apple/backup_util.h"
6
7#import <Foundation/Foundation.h>
8
9#include "base/apple/foundation_util.h"
10#include "base/files/file_path.h"
11#include "base/logging.h"
12#include "base/strings/sys_string_conversions.h"
13#include "base/threading/scoped_blocking_call.h"
14
15namespace base::apple {
16
17bool GetBackupExclusion(const FilePath& file_path) {
18  base::ScopedBlockingCall scoped_blocking_call(FROM_HERE,
19                                                base::BlockingType::MAY_BLOCK);
20
21  NSURL* file_url = apple::FilePathToNSURL(file_path);
22  DCHECK([file_url checkPromisedItemIsReachableAndReturnError:nil]);
23
24  NSError* error = nil;
25  NSNumber* value = nil;
26  BOOL success = [file_url getResourceValue:&value
27                                     forKey:NSURLIsExcludedFromBackupKey
28                                      error:&error];
29  if (!success) {
30    LOG(ERROR) << base::SysNSStringToUTF8(error.description);
31    return false;
32  }
33
34  return value && value.boolValue;
35}
36
37namespace {
38
39bool SetBackupState(const FilePath& file_path, bool excluded) {
40  base::ScopedBlockingCall scoped_blocking_call(FROM_HERE,
41                                                base::BlockingType::MAY_BLOCK);
42
43  NSURL* file_url = apple::FilePathToNSURL(file_path);
44  DCHECK([file_url checkPromisedItemIsReachableAndReturnError:nil]);
45
46  NSError* error = nil;
47  BOOL success = [file_url setResourceValue:@(excluded)
48                                     forKey:NSURLIsExcludedFromBackupKey
49                                      error:&error];
50  LOG_IF(WARNING, !success) << base::SysNSStringToUTF8(error.description);
51  return success;
52}
53
54}  // namespace
55
56bool SetBackupExclusion(const FilePath& file_path) {
57  return SetBackupState(file_path, true);
58}
59
60bool ClearBackupExclusion(const FilePath& file_path) {
61  return SetBackupState(file_path, false);
62}
63
64}  // namespace base::apple
65