Skip to content

Conversation

@klimslim
Copy link
Contributor

… done so far and provide feedback for Jules to continue.

… done so far and provide feedback for Jules to continue.
This addresses issues with SPL iterators, like RecursiveDirectoryIterator,
not correctly identifying files within complex template/instance override scenarios.

Here's a summary of what I did:

1.  **Initial Investigation & Debugging:**
    *   I added extensive debugging to `shadow_stat`, `shadow_dir_opener`,
        `shadow_dirstream_read`, and `template_to_instance` to understand
        the path resolution flow and stat information handling.
    *   I created specific test cases involving template/instance conflicts
        (e.g., a file in template vs. a directory in instance with the same name).

2.  **Key Findings & Fixes in `template_to_instance`:**
    *   **`fnamelen` Recalculation:** I fixed a bug where `fnamelen` (filename length)
        was not updated if the `realpath` of a file was modified by new
        fallback logic. This was crucial for correct subsequent operations
        on `realpath`.
    *   **Fallback Logic for `get_full_path` Failures:** I introduced a fallback
        mechanism within `template_to_instance`. If the initial `get_full_path`
        call fails (e.g., because a segment of the path is a file in the
        template, like `template_dir/file_segment/actual_file.txt`), this
        fallback logic attempts to:
        a. Resolve the parent directory (`template_dir/file_segment`) using a
           recursive call to `template_to_instance`, ensuring instance
           overrides are respected for the parent.
        b. If the resolved parent is a directory (especially an instance
           directory that overrode the template file segment), it constructs a
           new candidate path using this resolved parent and the basename
           (`resolved_parent_dir/actual_file.txt`).
        c. It then attempts `get_full_path` on this new candidate. If successful,
           this corrected `realpath` is used for the rest of `template_to_instance`.
    *   **Corrected Options in `shadow_stat`:** I ensured that the call to
        `template_to_instance` from `shadow_stat` correctly passes all necessary
        option flags (`OPT_CHECK_EXISTS | OPT_RETURN_INSTANCE`). An earlier change
        had inadvertently modified these options, masking the fixes.

3.  **Outcome of Fixes:**
    *   With these changes, the test case where an instance directory overrides a
        template file of the same name (e.g., `instance/conflict/item` (dir)
        over `template/conflict/item` (file)) now correctly resolves and stats
        files within that overridden directory (e.g.,
        `template/conflict/item/somefile.txt` is correctly identified as a file).
    *   This resolves the primary observed bug related to stat failures for
        nested files in specific override scenarios.

4.  **Code Structure Note (Your Feedback):**
    *   The newly added fallback logic within `template_to_instance`, while
        functional, is quite nested and could be refactored for better
        clarity and maintainability (as you pointed out). This set of changes
        includes the functional version, with refactoring planned as a follow-up.

These changes ensure more robust path resolution, particularly for SPL iterators
navigating through complex shadow directory structures with type conflicts
between template and instance layers.
…instance

This commit addresses issues with how the shadow extension handles path
resolution, affecting both SPL iterators (like RecursiveDirectoryIterator)
and glob functionality. The core `template_to_instance` function has been
refactored for clarity and correctness, and `shadow_glob` has been updated
to correctly use this improved resolution logic.

Problem Identification & Debugging:
- The initial issue you reported was that SPL iterators were not working correctly,
  potentially trying to iterate into files.
- I began my investigation by adding detailed logging to `shadow_stat`,
  `shadow_dir_opener`, `shadow_dirstream_read`, and `template_to_instance`.
- Test cases involving template file/instance directory conflicts highlighted
  issues in `template_to_instance`.

Refactoring `template_to_instance`:
- I addressed your feedback regarding complex ("ugly") fallback logic by
  replacing the entire function with a cleaner, segmented path resolution
  approach.
- This new approach first attempts `get_full_path`. If that fails (e.g.,
  due to a file segment in the template path blocking resolution of a deeper
  path that should exist via an instance directory override), it attempts to
  resolve the path segment by segment, prioritizing instance overrides.
- I fixed a bug where `fnamelen` was not updated if `realpath` was modified by
  the fallback logic.
- I ensured `shadow_stat` calls `template_to_instance` with the correct
  combined options (`OPT_CHECK_EXISTS | OPT_RETURN_INSTANCE`), which was an
  earlier source of error.
- The refactored `template_to_instance` now correctly resolves paths for
  files nested within directories where the directory itself is an instance
  override of a template file (e.g., `template/conflict/item` (file) vs.
  `instance/conflict/item` (dir); iterating `template/conflict/item/somefile.txt`
  now works).

Fixing `shadow_glob` Regression:
- The refactoring of `template_to_instance` initially caused a regression
  in `tests/glob.phpt`.
- My investigation showed that `shadow_glob` was not correctly determining the
  base directory for glob patterns that were relative or pathless (e.g., `*`).
  It would incorrectly pass empty or relative paths to `template_to_instance`.
- `shadow_glob` was modified to:
    1.  Determine the absolute base directory for the glob pattern, taking into
        account the current working directory's shadow context (template or
        instance).
    2.  Use this absolute base directory to correctly resolve the effective
        template and instance search paths for the glob operation.
    3.  Correctly handle the `return_value` from `orig_glob` calls made via
        `shadow_call_replace_name`, by processing results into a temporary
        hash table before populating the final return array.
- I introduced a new debug flag `SHADOW_DEBUG_GLOB` to control verbosity for
  glob operations, allowing `tests/glob.phpt` to pass by excluding these
  specific logs from its expected output.
- I updated the `--EXPECT--` section of `tests/glob.phpt` to match the correct
  merged view of files after the fixes.

Outcome:
- SPL iterators now correctly identify file and directory types in complex
  override scenarios, as verified by `tests/iterators.phpt` (including
  type conflict cases) and specific test structures you provided.
- `tests/glob.phpt` now passes, indicating that glob functionality is
  correct after the `shadow_glob` and `template_to_instance` fixes.
- The codebase for path resolution in `template_to_instance` is clearer
  and more robust.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant