summaryrefslogtreecommitdiffstats
path: root/net/disk_cache/entry_impl.cc
blob: 2e7b8c46e911e79f9b94086228c7e33ff181d956 (plain)
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "net/disk_cache/entry_impl.h"

#include "base/histogram.h"
#include "base/message_loop.h"
#include "base/string_util.h"
#include "net/base/net_errors.h"
#include "net/disk_cache/backend_impl.h"
#include "net/disk_cache/cache_util.h"

using base::Time;
using base::TimeDelta;

namespace {

// This class implements FileIOCallback to buffer the callback from a file IO
// operation from the actual net class.
class SyncCallback: public disk_cache::FileIOCallback {
 public:
  SyncCallback(disk_cache::EntryImpl* entry,
               net::CompletionCallback* callback )
      : entry_(entry), callback_(callback) {
    entry->AddRef();
    entry->IncrementIoCount();
  }
  ~SyncCallback() {}

  virtual void OnFileIOComplete(int bytes_copied);
  void Discard();
 private:
  disk_cache::EntryImpl* entry_;
  net::CompletionCallback* callback_;

  DISALLOW_EVIL_CONSTRUCTORS(SyncCallback);
};

void SyncCallback::OnFileIOComplete(int bytes_copied) {
  entry_->DecrementIoCount();
  entry_->Release();
  if (callback_)
    callback_->Run(bytes_copied);
  delete this;
}

void SyncCallback::Discard() {
  callback_ = NULL;
  OnFileIOComplete(0);
}

// Clears buffer before offset and after valid_len, knowing that the size of
// buffer is kMaxBlockSize.
void ClearInvalidData(char* buffer, int offset, int valid_len) {
  DCHECK(offset >= 0);
  DCHECK(valid_len >= 0);
  DCHECK(disk_cache::kMaxBlockSize >= offset + valid_len);
  if (offset)
    memset(buffer, 0, offset);
  int end = disk_cache::kMaxBlockSize - offset - valid_len;
  if (end)
    memset(buffer + offset + valid_len, 0, end);
}

}  // namespace

namespace disk_cache {

EntryImpl::EntryImpl(BackendImpl* backend, Addr address)
    : entry_(NULL, Addr(0)), node_(NULL, Addr(0)) {
  entry_.LazyInit(backend->File(address), address);
  doomed_ = false;
  backend_ = backend;
  unreported_size_[0] = unreported_size_[1] = 0;
}

// When an entry is deleted from the cache, we clean up all the data associated
// with it for two reasons: to simplify the reuse of the block (we know that any
// unused block is filled with zeros), and to simplify the handling of write /
// read partial information from an entry (don't have to worry about returning
// data related to a previous cache entry because the range was not fully
// written before).
EntryImpl::~EntryImpl() {
  if (doomed_) {
    UMA_HISTOGRAM_COUNTS(L"DiskCache.DeleteHeader", GetDataSize(0));
    UMA_HISTOGRAM_COUNTS(L"DiskCache.DeleteData", GetDataSize(1));
    for (int index = 0; index < kKeyFileIndex; index++) {
      Addr address(entry_.Data()->data_addr[index]);
      if (address.is_initialized()) {
        DeleteData(address, index);
        backend_->ModifyStorageSize(entry_.Data()->data_size[index] -
                                        unreported_size_[index], 0);
      }
    }
    Addr address(entry_.Data()->long_key);
    DeleteData(address, kKeyFileIndex);
    backend_->ModifyStorageSize(entry_.Data()->key_len, 0);

    memset(node_.buffer(), 0, node_.size());
    memset(entry_.buffer(), 0, entry_.size());
    node_.Store();
    entry_.Store();

    backend_->DeleteBlock(node_.address(), false);
    backend_->DeleteBlock(entry_.address(), false);
  } else {
    bool ret = true;
    for (int index = 0; index < kKeyFileIndex; index++) {
      if (user_buffers_[index].get()) {
        if (!(ret = Flush(index, entry_.Data()->data_size[index], false)))
          LOG(ERROR) << "Failed to save user data";
      } else if (unreported_size_[index]) {
        backend_->ModifyStorageSize(
            entry_.Data()->data_size[index] - unreported_size_[index],
            entry_.Data()->data_size[index]);
      }
    }
    if (node_.HasData() && this == node_.Data()->pointer) {
      // We have to do this after Flush because we may trigger a cache trim from
      // there, and technically this entry should be "in use".
      node_.Data()->pointer = NULL;
      node_.set_modified();
    }

    if (!ret) {
      // There was a failure writing the actual data. Mark the entry as dirty.
      int current_id = backend_->GetCurrentEntryId();
      node_.Data()->dirty = current_id == 1 ? -1 : current_id - 1;
      node_.Store();
    } else if (node_.HasData() && node_.Data()->dirty) {
      node_.Data()->dirty = 0;
      node_.Store();
    }
  }

  backend_->CacheEntryDestroyed();
}

void EntryImpl::Doom() {
  if (doomed_)
    return;

  SetPointerForInvalidEntry(backend_->GetCurrentEntryId());
  backend_->InternalDoomEntry(this);
}

void EntryImpl::Close() {
  Release();
}

std::string EntryImpl::GetKey() const {
  CacheEntryBlock* entry = const_cast<CacheEntryBlock*>(&entry_);
  if (entry->Data()->key_len > kMaxInternalKeyLength) {
    Addr address(entry->Data()->long_key);
    DCHECK(address.is_initialized());
    File* file = const_cast<EntryImpl*>(this)->GetBackingFile(address,
                                                              kKeyFileIndex);

    size_t offset = 0;
    if (address.is_block_file())
      offset = address.start_block() * address.BlockSize() + kBlockHeaderSize;

    std::string key;
    if (!file || !file->Read(WriteInto(&key, entry->Data()->key_len + 1),
                             entry->Data()->key_len + 1, offset))
      key.clear();
    return key;
  } else {
    return std::string(entry->Data()->key);
  }
}

Time EntryImpl::GetLastUsed() const {
  CacheRankingsBlock* node = const_cast<CacheRankingsBlock*>(&node_);
  return Time::FromInternalValue(node->Data()->last_used);
}

Time EntryImpl::GetLastModified() const {
  CacheRankingsBlock* node = const_cast<CacheRankingsBlock*>(&node_);
  return Time::FromInternalValue(node->Data()->last_modified);
}

int32 EntryImpl::GetDataSize(int index) const {
  if (index < 0 || index > 1)
    return 0;

  CacheEntryBlock* entry = const_cast<CacheEntryBlock*>(&entry_);
  return entry->Data()->data_size[index];
}

int EntryImpl::ReadData(int index, int offset, char* buf, int buf_len,
                        net::CompletionCallback* completion_callback) {
  DCHECK(node_.Data()->dirty);
  if (index < 0 || index > 1)
    return net::ERR_INVALID_ARGUMENT;

  int entry_size = entry_.Data()->data_size[index];
  if (offset >= entry_size || offset < 0 || !buf_len)
    return 0;

  if (buf_len < 0)
    return net::ERR_INVALID_ARGUMENT;

  Time start = Time::Now();
  static Histogram stats(L"DiskCache.ReadTime", TimeDelta::FromMilliseconds(1),
                         TimeDelta::FromSeconds(10), 50);
  stats.SetFlags(kUmaTargetedHistogramFlag);

  if (offset + buf_len > entry_size)
    buf_len = entry_size - offset;

  UpdateRank(false);

  backend_->OnEvent(Stats::READ_DATA);

  if (user_buffers_[index].get()) {
    // Complete the operation locally.
    DCHECK(kMaxBlockSize >= offset + buf_len);
    memcpy(buf , user_buffers_[index].get() + offset, buf_len);
    stats.AddTime(Time::Now() - start);
    return buf_len;
  }

  Addr address(entry_.Data()->data_addr[index]);
  DCHECK(address.is_initialized());
  if (!address.is_initialized())
    return net::ERR_FAILED;

  File* file = GetBackingFile(address, index);
  if (!file)
    return net::ERR_FAILED;

  size_t file_offset = offset;
  if (address.is_block_file())
    file_offset += address.start_block() * address.BlockSize() +
                   kBlockHeaderSize;

  SyncCallback* io_callback = NULL;
  if (completion_callback)
    io_callback = new SyncCallback(this, completion_callback);

  bool completed;
  if (!file->Read(buf, buf_len, file_offset, io_callback, &completed)) {
    if (io_callback)
      io_callback->Discard();
    return net::ERR_FAILED;
  }

  if (io_callback && completed)
    io_callback->Discard();

  stats.AddTime(Time::Now() - start);
  return (completed || !completion_callback) ? buf_len : net::ERR_IO_PENDING;
}

int EntryImpl::WriteData(int index, int offset, const char* buf, int buf_len,
                         net::CompletionCallback* completion_callback,
                         bool truncate) {
  DCHECK(node_.Data()->dirty);
  if (index < 0 || index > 1)
    return net::ERR_INVALID_ARGUMENT;

  if (offset < 0 || buf_len < 0)
    return net::ERR_INVALID_ARGUMENT;

  int max_file_size = backend_->MaxFileSize();

  // offset of buf_len could be negative numbers.
  if (offset > max_file_size || buf_len > max_file_size ||
      offset + buf_len > max_file_size) {
    int size = offset + buf_len;
    if (size <= max_file_size)
      size = kint32max;
    backend_->TooMuchStorageRequested(size);
    return net::ERR_FAILED;
  }

  Time start = Time::Now();
  static Histogram stats(L"DiskCache.WriteTime", TimeDelta::FromMilliseconds(1),
                         TimeDelta::FromSeconds(10), 50);
  stats.SetFlags(kUmaTargetedHistogramFlag);

  // Read the size at this point (it may change inside prepare).
  int entry_size = entry_.Data()->data_size[index];
  if (!PrepareTarget(index, offset, buf_len, truncate))
    return net::ERR_FAILED;

  if (entry_size < offset + buf_len) {
    unreported_size_[index] += offset + buf_len - entry_size;
    entry_.Data()->data_size[index] = offset + buf_len;
    entry_.set_modified();
    if (!buf_len)
      truncate = true;  // Force file extension.
  } else if (truncate) {
      // If the size was modified inside PrepareTarget, we should not do
      // anything here.
      if ((entry_size > offset + buf_len) &&
          (entry_size == entry_.Data()->data_size[index])) {
        unreported_size_[index] += offset + buf_len - entry_size;
        entry_.Data()->data_size[index] = offset + buf_len;
        entry_.set_modified();
      } else {
        // Nothing to truncate.
        truncate = false;
      }
  }

  UpdateRank(true);

  backend_->OnEvent(Stats::WRITE_DATA);

  if (user_buffers_[index].get()) {
    // Complete the operation locally.
    DCHECK(kMaxBlockSize >= offset + buf_len);
    memcpy(user_buffers_[index].get() + offset, buf, buf_len);
    stats.AddTime(Time::Now() - start);
    return buf_len;
  }

  Addr address(entry_.Data()->data_addr[index]);
  File* file = GetBackingFile(address, index);
  if (!file)
    return net::ERR_FAILED;

  size_t file_offset = offset;
  if (address.is_block_file()) {
    file_offset += address.start_block() * address.BlockSize() +
                   kBlockHeaderSize;
  } else if (truncate) {
    if (!file->SetLength(offset + buf_len))
      return net::ERR_FAILED;
  }

  if (!buf_len)
    return 0;

  SyncCallback* io_callback = NULL;
  if (completion_callback)
    io_callback = new SyncCallback(this, completion_callback);

  bool completed;
  if (!file->Write(buf, buf_len, file_offset, io_callback, &completed)) {
    if (io_callback)
      io_callback->Discard();
    return net::ERR_FAILED;
  }

  if (io_callback && completed)
    io_callback->Discard();

  stats.AddTime(Time::Now() - start);
  return (completed || !completion_callback) ? buf_len : net::ERR_IO_PENDING;
}

uint32 EntryImpl::GetHash() {
  return entry_.Data()->hash;
}

bool EntryImpl::CreateEntry(Addr node_address, const std::string& key,
                            uint32 hash) {
  Trace("Create entry In");
  EntryStore* entry_store = entry_.Data();
  RankingsNode* node = node_.Data();
  memset(entry_store, 0, sizeof(EntryStore) * entry_.address().num_blocks());
  memset(node, 0, sizeof(RankingsNode));
  if (!node_.LazyInit(backend_->File(node_address), node_address))
    return false;

  entry_store->rankings_node = node_address.value();
  node->contents = entry_.address().value();
  node->pointer = this;

  entry_store->hash = hash;
  entry_store->key_len = static_cast<int32>(key.size());
  if (entry_store->key_len > kMaxInternalKeyLength) {
    Addr address(0);
    if (!CreateBlock(entry_store->key_len + 1, &address))
      return false;

    entry_store->long_key = address.value();
    File* file = GetBackingFile(address, kKeyFileIndex);

    size_t offset = 0;
    if (address.is_block_file())
      offset = address.start_block() * address.BlockSize() + kBlockHeaderSize;

    if (!file || !file->Write(key.data(), key.size(), offset)) {
      DeleteData(address, kKeyFileIndex);
      return false;
    }

    if (address.is_separate_file())
      file->SetLength(key.size() + 1);
  } else {
    memcpy(entry_store->key, key.data(), key.size());
    entry_store->key[key.size()] = '\0';
  }
  backend_->ModifyStorageSize(0, static_cast<int32>(key.size()));
  node->dirty = backend_->GetCurrentEntryId();
  Log("Create Entry ");
  return true;
}

bool EntryImpl::IsSameEntry(const std::string& key, uint32 hash) {
  if (entry_.Data()->hash != hash ||
      static_cast<size_t>(entry_.Data()->key_len) != key.size())
    return false;

  std::string my_key = GetKey();
  return key.compare(my_key) ? false : true;
}

void EntryImpl::InternalDoom() {
  DCHECK(node_.HasData());
  if (!node_.Data()->dirty) {
    node_.Data()->dirty = backend_->GetCurrentEntryId();
    node_.Store();
  }
  doomed_ = true;
}

CacheAddr EntryImpl::GetNextAddress() {
  return entry_.Data()->next;
}

void EntryImpl::SetNextAddress(Addr address) {
  entry_.Data()->next = address.value();
  bool success = entry_.Store();
  DCHECK(success);
}

bool EntryImpl::LoadNodeAddress() {
  Addr address(entry_.Data()->rankings_node);
  if (!node_.LazyInit(backend_->File(address), address))
    return false;
  return node_.Load();
}

EntryImpl* EntryImpl::Update(EntryImpl* entry) {
  DCHECK(entry->rankings()->HasData());

  RankingsNode* rankings = entry->rankings()->Data();
  if (rankings->pointer) {
    // Already in memory. Prevent clearing the dirty flag on the destructor.
    rankings->dirty = 0;
    EntryImpl* real_node = reinterpret_cast<EntryImpl*>(rankings->pointer);
    real_node->AddRef();
    entry->Release();
    return real_node;
  } else {
    rankings->dirty = entry->backend_->GetCurrentEntryId();
    rankings->pointer = entry;
    if (!entry->rankings()->Store()) {
      entry->Release();
      return NULL;
    }
    return entry;
  }
}

bool EntryImpl::IsDirty(int32 current_id) {
  DCHECK(node_.HasData());
  return node_.Data()->dirty && current_id != node_.Data()->dirty;
}

void EntryImpl::ClearDirtyFlag() {
  node_.Data()->dirty = 0;
}

void EntryImpl::SetPointerForInvalidEntry(int32 new_id) {
  node_.Data()->dirty = new_id;
  node_.Data()->pointer = this;
  node_.Store();
}

bool EntryImpl::SanityCheck() {
  if (!entry_.Data()->rankings_node || !entry_.Data()->key_len)
    return false;

  Addr rankings_addr(entry_.Data()->rankings_node);
  if (!rankings_addr.is_initialized() || rankings_addr.is_separate_file() ||
      rankings_addr.file_type() != RANKINGS)
    return false;

  Addr next_addr(entry_.Data()->next);
  if (next_addr.is_initialized() &&
      (next_addr.is_separate_file() || next_addr.file_type() != BLOCK_256))
    return false;

  return true;
}

void EntryImpl::IncrementIoCount() {
  backend_->IncrementIoCount();
}

void EntryImpl::DecrementIoCount() {
  backend_->DecrementIoCount();
}

void EntryImpl::SetTimes(base::Time last_used, base::Time last_modified) {
  node_.Data()->last_used = last_used.ToInternalValue();
  node_.Data()->last_modified = last_modified.ToInternalValue();
  node_.set_modified();
}

bool EntryImpl::CreateDataBlock(int index, int size) {
  Addr address(entry_.Data()->data_addr[index]);
  DCHECK(0 == index || 1 == index);

  if (!CreateBlock(size, &address))
    return false;

  entry_.Data()->data_addr[index] = address.value();
  entry_.Store();
  return true;
}

bool EntryImpl::CreateBlock(int size, Addr* address) {
  DCHECK(!address->is_initialized());

  FileType file_type = Addr::RequiredFileType(size);
  if (EXTERNAL == file_type) {
    if (size > backend_->MaxFileSize())
      return false;
    if (!backend_->CreateExternalFile(address))
      return false;
  } else {
    int num_blocks = (size + Addr::BlockSizeForFileType(file_type) - 1) /
                     Addr::BlockSizeForFileType(file_type);

    if (!backend_->CreateBlock(file_type, num_blocks, address))
      return false;
  }
  return true;
}

void EntryImpl::DeleteData(Addr address, int index) {
  if (!address.is_initialized())
    return;
  if (address.is_separate_file()) {
    if (files_[index])
      files_[index] = NULL;  // Releases the object.

    if (!DeleteCacheFile(backend_->GetFileName(address))) {
      UMA_HISTOGRAM_COUNTS(L"DiskCache.DeleteFailed", 1);
      LOG(ERROR) << "Failed to delete " << backend_->GetFileName(address) <<
                    " from the cache.";
    }
  } else {
    backend_->DeleteBlock(address, true);
  }
}

void EntryImpl::UpdateRank(bool modified) {
  if (!doomed_) {
    // Everything is handled by the backend.
    backend_->UpdateRank(&node_, true);
    return;
  }

  Time current = Time::Now();
  node_.Data()->last_used = current.ToInternalValue();

  if (modified)
    node_.Data()->last_modified = current.ToInternalValue();
}

File* EntryImpl::GetBackingFile(Addr address, int index) {
  File* file;
  if (address.is_separate_file())
    file = GetExternalFile(address, index);
  else
    file = backend_->File(address);
  return file;
}

File* EntryImpl::GetExternalFile(Addr address, int index) {
  DCHECK(index >= 0 && index <= 2);
  if (!files_[index].get()) {
    // For a key file, use mixed mode IO.
    scoped_refptr<File> file(new File(2 == index));
    if (file->Init(backend_->GetFileName(address)))
      files_[index].swap(file);
  }
  return files_[index].get();
}

bool EntryImpl::PrepareTarget(int index, int offset, int buf_len,
                              bool truncate) {
  Addr address(entry_.Data()->data_addr[index]);
  if (address.is_initialized() || user_buffers_[index].get())
    return GrowUserBuffer(index, offset, buf_len, truncate);

  if (offset + buf_len > kMaxBlockSize)
    return CreateDataBlock(index, offset + buf_len);

  user_buffers_[index].reset(new char[kMaxBlockSize]);

  // Overwrite the parts of the buffer that are not going to be written
  // by the current operation (and yes, let's assume that nothing is going
  // to fail, and we'll actually write over the part that we are not cleaning
  // here). The point is to avoid writing random stuff to disk later on.
  ClearInvalidData(user_buffers_[index].get(), offset, buf_len);

  return true;
}

// We get to this function with some data already stored. If there is a
// truncation that results on data stored internally, we'll explicitly
// handle the case here.
bool EntryImpl::GrowUserBuffer(int index, int offset, int buf_len,
                               bool truncate) {
  Addr address(entry_.Data()->data_addr[index]);

  if (offset + buf_len > kMaxBlockSize) {
    // The data has to be stored externally.
    if (address.is_initialized()) {
      if (address.is_separate_file())
        return true;
      if (!MoveToLocalBuffer(index))
        return false;
    }
    return Flush(index, offset + buf_len, true);
  }

  if (!address.is_initialized()) {
    DCHECK(user_buffers_[index].get());
    if (truncate)
      ClearInvalidData(user_buffers_[index].get(), 0, offset + buf_len);
    return true;
  }
  if (address.is_separate_file()) {
    if (!truncate)
      return true;
    return ImportSeparateFile(index, offset, buf_len);
  }

  // At this point we are dealing with data stored on disk, inside a block file.
  if (offset + buf_len <= address.BlockSize() * address.num_blocks())
    return true;

  // ... and the allocated block has to change.
  if (!MoveToLocalBuffer(index))
    return false;

  int clear_start = entry_.Data()->data_size[index];
  if (truncate)
    clear_start = std::min(clear_start, offset + buf_len);
  else if (offset < clear_start)
    clear_start = std::max(offset + buf_len, clear_start);

  // Clear the end of the buffer.
  ClearInvalidData(user_buffers_[index].get(), 0, clear_start);
  return true;
}

bool EntryImpl::MoveToLocalBuffer(int index) {
  Addr address(entry_.Data()->data_addr[index]);
  DCHECK(!user_buffers_[index].get());
  DCHECK(address.is_initialized());
  scoped_array<char> buffer(new char[kMaxBlockSize]);

  File* file = GetBackingFile(address, index);
  size_t len = entry_.Data()->data_size[index];
  size_t offset = 0;

  if (address.is_block_file())
    offset = address.start_block() * address.BlockSize() + kBlockHeaderSize;

  if (!file || !file->Read(buffer.get(), len, offset, NULL, NULL))
    return false;

  DeleteData(address, index);
  entry_.Data()->data_addr[index] = 0;
  entry_.Store();

  // If we lose this entry we'll see it as zero sized.
  backend_->ModifyStorageSize(static_cast<int>(len) - unreported_size_[index],
                              0);
  unreported_size_[index] = static_cast<int>(len);

  user_buffers_[index].swap(buffer);
  return true;
}

bool EntryImpl::ImportSeparateFile(int index, int offset, int buf_len) {
  if (entry_.Data()->data_size[index] > offset + buf_len) {
    unreported_size_[index] += offset + buf_len -
                               entry_.Data()->data_size[index];
    entry_.Data()->data_size[index] = offset + buf_len;
  }

  if (!MoveToLocalBuffer(index))
    return false;

  // Clear the end of the buffer.
  ClearInvalidData(user_buffers_[index].get(), 0, offset + buf_len);
  return true;
}

// The common scenario is that this is called from the destructor of the entry,
// to write to disk what we have buffered. We don't want to hold the destructor
// until the actual IO finishes, so we'll send an asynchronous write that will
// free up the memory containing the data. To be consistent, this method always
// returns with the buffer freed up (on success).
bool EntryImpl::Flush(int index, int size, bool async) {
  Addr address(entry_.Data()->data_addr[index]);
  DCHECK(user_buffers_[index].get());
  DCHECK(!address.is_initialized());

  if (!size)
    return true;

  if (!CreateDataBlock(index, size))
    return false;

  address.set_value(entry_.Data()->data_addr[index]);

  File* file = GetBackingFile(address, index);
  size_t len = entry_.Data()->data_size[index];
  size_t offset = 0;
  if (address.is_block_file())
    offset = address.start_block() * address.BlockSize() + kBlockHeaderSize;

  // We just told the backend to store len bytes for real.
  DCHECK(len == static_cast<size_t>(unreported_size_[index]));
  backend_->ModifyStorageSize(0, static_cast<int>(len));
  unreported_size_[index] = 0;

  if (!file)
    return false;

  // TODO(rvargas): figure out if it's worth to re-enable posting operations.
  // Right now it is only used from GrowUserBuffer, not the destructor, and
  // it is not accounted for from the point of view of the total number of
  // pending operations of the cache. It is also racing with the actual write
  // on the GrowUserBuffer path because there is no code to exclude the range
  // that is going to be written.
  async = false;
  if (async) {
    if (!file->PostWrite(user_buffers_[index].get(), len, offset))
      return false;
  } else {
    if (!file->Write(user_buffers_[index].get(), len, offset, NULL, NULL))
      return false;
    user_buffers_[index].reset(NULL);
  }

  // The buffer is deleted from the PostWrite operation.
  user_buffers_[index].release();

  return true;
}

void EntryImpl::Log(const char* msg) {
  void* pointer = NULL;
  int dirty = 0;
  if (node_.HasData()) {
    pointer = node_.Data()->pointer;
    dirty = node_.Data()->dirty;
  }

  Trace("%s 0x%p 0x%x 0x%x", msg, reinterpret_cast<void*>(this),
        entry_.address().value(), node_.address().value());

  Trace("  data: 0x%x 0x%x 0x%x", entry_.Data()->data_addr[0],
        entry_.Data()->data_addr[1], entry_.Data()->long_key);

  Trace("  doomed: %d 0x%p 0x%x", doomed_, pointer, dirty);
}

}  // namespace disk_cache