- for (int i = 0; i < fSuffixes.count(); i++) {
- dir = SkOSPath::Join(dir.c_str(), fSuffixes[i].c_str());
- this->makeDirOrFail(dir);
+
+ SkString path;
+ if (FLAGS_nameByHash) {
+ // Flat directory of hash-named files.
+ path = SkOSPath::Join(dir.c_str(), md5.c_str());
+ path.append(fExtension);
+ // We're content-addressed, so it's possible two threads race to write
+ // this file. We let the first one win. This also means we won't
+ // overwrite identical files from previous runs.
+ if (sk_exists(path.c_str())) {
+ return;
+ }
+ } else {
+ // Nested by mode, config, etc.
+ for (int i = 0; i < fSuffixes.count(); i++) {
+ dir = SkOSPath::Join(dir.c_str(), fSuffixes[i].c_str());
+ this->makeDirOrFail(dir);
+ }
+ path = SkOSPath::Join(dir.c_str(), fBaseName.c_str());
+ path.append(fExtension);
+ // The path is unique, so two threads can't both write to the same file.
+ // If already present we overwrite here, since the content may have changed.